diff --git a/packages/common/src/IContext.ts b/packages/common/src/IContext.ts new file mode 100644 index 0000000..dd841c5 --- /dev/null +++ b/packages/common/src/IContext.ts @@ -0,0 +1,5 @@ +export interface IContext { + user?: { + id: number + } +} diff --git a/packages/common/src/entities.ts b/packages/common/src/entities.ts new file mode 100644 index 0000000..d4ba3c8 --- /dev/null +++ b/packages/common/src/entities.ts @@ -0,0 +1,66 @@ +/* This file was generated by rondo intergen script */ +/* tslint:disable */ + +export interface BaseEntity { + id: number + createDate: string + updateDate: string +} + +export interface Role { + name: string + id: number + createDate: string + updateDate: string +} + +export interface Team { + name: string + userId: number + user?: User + userTeams: UserTeam[] + id: number + createDate: string + updateDate: string +} + +export interface User { + firstName: string + lastName: string + emails: UserEmail[] + password?: string + sessions: Session[] + userTeams: UserTeam[] + id: number + createDate: string + updateDate: string +} + +export interface UserEmail { + email: string + user?: User + userId?: number + id: number + createDate: string + updateDate: string +} + +export interface Session { + id: string + expiredAt: number + user?: User + userId: number + json: string +} + +export interface UserTeam { + user: User + userId: number + team?: Team + teamId: number + role?: Role + roleId: number + id: number + createDate: string + updateDate: string +} \ No newline at end of file diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index f42456a..588c2c0 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -1,3 +1,5 @@ +export * from './entities' +export * from './IContext' export * from './IAPIDef' export * from './ICredentials' export * from './ILogger' @@ -19,3 +21,6 @@ export * from './without' import * as team from './team' export {team} + +import * as entities from './entities' +export {entities} diff --git a/packages/common/src/team.ts b/packages/common/src/team.ts index f01be33..daebe59 100644 --- a/packages/common/src/team.ts +++ b/packages/common/src/team.ts @@ -1,4 +1,5 @@ -import {ITeam} from './ITeam' +// import {ITeam} from './ITeam' +import {Team} from './entities' import {IUserInTeam} from './IUserInTeam' export interface ITeamAddUserParams { @@ -25,19 +26,19 @@ export interface IContext { } export interface ITeamService { - create(params: ITeamCreateParams): Promise + create(params: ITeamCreateParams): Promise remove(params: ITeamRemoveParams): Promise<{id: number}> - update(params: ITeamUpdateParams): Promise + update(params: ITeamUpdateParams): Promise addUser(params: ITeamAddUserParams): Promise removeUser(params: ITeamAddUserParams): Promise - findOne(id: number): Promise + findOne(id: number): Promise - find(): Promise + find(): Promise findUsers(teamId: number): Promise diff --git a/packages/jsonrpc/src/ensure.ts b/packages/jsonrpc/src/ensure.ts index 177fec2..7a9e156 100644 --- a/packages/jsonrpc/src/ensure.ts +++ b/packages/jsonrpc/src/ensure.ts @@ -3,7 +3,7 @@ import 'reflect-metadata' export const ensureKey = Symbol('ensure') export const ensureClassKey = Symbol('ensureClass') -export type Validate = (context: Context) => boolean +export type Validate = (context: Context) => boolean | Promise export function ensure( validate: Validate, diff --git a/packages/jsonrpc/src/jsonrpc.ts b/packages/jsonrpc/src/jsonrpc.ts index fb54326..cbb0d63 100644 --- a/packages/jsonrpc/src/jsonrpc.ts +++ b/packages/jsonrpc/src/jsonrpc.ts @@ -71,15 +71,17 @@ export function createSuccessResponse(id: number | string, result: T) } } -function validateServiceContext, Context>( +async function validateServiceContext< + T, M extends FunctionPropertyNames, Context +>( id: string | number | null, service: T, method: M, context: Context, ) { - function doValidate(validate: Validate) { - const success = validate(context) + async function doValidate(validate: Validate) { + const success = await validate(context) if (!success) { throw createError(ERROR_INVALID_REQUEST, { id, @@ -89,11 +91,13 @@ function validateServiceContext, Context>( } } - getValidatorsForInstance(service) - .forEach(doValidate) + for (const validate of getValidatorsForInstance(service)) { + await doValidate(validate) + } - getValidatorsForMethod(service, method) - .forEach(doValidate) + for (const validate of getValidatorsForMethod(service, method)) { + await doValidate(validate) + } } export const createRpcService = >( @@ -133,7 +137,7 @@ export const createRpcService = >( }) } - validateServiceContext(id, service, method, context) + await validateServiceContext(id, service, method, context) let retValue = (rpcService[method] as any)(...params, context) diff --git a/packages/scripts/src/scripts/intergen.ts b/packages/scripts/src/scripts/intergen.ts index 35c06a5..f405615 100644 --- a/packages/scripts/src/scripts/intergen.ts +++ b/packages/scripts/src/scripts/intergen.ts @@ -353,7 +353,8 @@ export function intergen(...argv: string[]): string { const name = nameMappings.get(classDef.type)! const start = `export interface ${name} {` const properties = classDef.properties.map(p => { - return ` ${p.name}: ${nameMappings.get(p.type) || p.typeString}` + const q = p.optional ? '?' : '' + return ` ${p.name}${q}: ${nameMappings.get(p.type) || p.typeString}` }) .join('\n') const end = '}' @@ -368,7 +369,8 @@ export function intergen(...argv: string[]): string { module: ts.ModuleKind.CommonJS, }) - const header = '// This file was generated by rondo intergen script\n\n' + const header = '/* This file was generated by rondo intergen script */\n' + + '/* tslint:disable */\n\n' const value = header + interfaces.join('\n\n') if (args.output === '-') { info(value) diff --git a/packages/server/src/services/TeamService2.ts b/packages/server/src/services/TeamService2.ts index 08b325e..e658cf3 100644 --- a/packages/server/src/services/TeamService2.ts +++ b/packages/server/src/services/TeamService2.ts @@ -5,13 +5,14 @@ import {UserTeam} from '../entities/UserTeam' import {IUserPermissions} from '../user/IUserPermissions' import { trim, + IContext, + entities as e, team as t, IUserInTeam, } from '@rondo.dev/common' import {Contextual} from '@rondo.dev/jsonrpc' -type IContext = t.IContext - +// TODO ensureLoggedIn export class TeamService2 implements Contextual { constructor( protected readonly db: IDatabase, @@ -19,7 +20,7 @@ export class TeamService2 implements Contextual { ) {} async create(params: t.ITeamCreateParams, context: IContext) { - const {userId} = context + const userId = context.user!.id const name = trim(params.name) new Validator({name, userId}) @@ -43,11 +44,11 @@ export class TeamService2 implements Contextual { } async remove({id}: t.ITeamRemoveParams, context: IContext) { - const {userId} = context + const userId = context.user!.id await this.permissions.belongsToTeam({ teamId: id, - userId: context.userId, + userId, }) await this.db.getRepository(UserTeam) @@ -60,9 +61,11 @@ export class TeamService2 implements Contextual { } async update({id, name}: t.ITeamUpdateParams, context: IContext) { + const userId = context.user!.id + await this.permissions.belongsToTeam({ teamId: id, - userId: context.userId, + userId, }) await this.db.getRepository(Team) @@ -96,7 +99,7 @@ export class TeamService2 implements Contextual { await this.permissions.belongsToTeam({ teamId: params.teamId, - userId: context.userId, + userId: context.user!.id, }) // TODO check if this is the last admin team member @@ -111,7 +114,8 @@ export class TeamService2 implements Contextual { } async find(context: IContext) { - const {userId} = context + const userId = context.user!.id + return this.db.getRepository(Team) .createQueryBuilder('team') .select('team') @@ -121,9 +125,11 @@ export class TeamService2 implements Contextual { } async findUsers(teamId: number, context: IContext) { + const userId = context.user!.id + await this.permissions.belongsToTeam({ teamId, - userId: context.userId, + userId, }) const userTeams = await this.createFindUserInTeamQuery()