Generate @rondo.dev/common entities

This commit is contained in:
Jerko Steiner 2019-08-28 08:08:00 +07:00
parent 3b6011f88d
commit cd5ff9b5da
8 changed files with 114 additions and 25 deletions

View File

@ -0,0 +1,5 @@
export interface IContext {
user?: {
id: number
}
}

View File

@ -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
}

View File

@ -1,3 +1,5 @@
export * from './entities'
export * from './IContext'
export * from './IAPIDef' export * from './IAPIDef'
export * from './ICredentials' export * from './ICredentials'
export * from './ILogger' export * from './ILogger'
@ -19,3 +21,6 @@ export * from './without'
import * as team from './team' import * as team from './team'
export {team} export {team}
import * as entities from './entities'
export {entities}

View File

@ -1,4 +1,5 @@
import {ITeam} from './ITeam' // import {ITeam} from './ITeam'
import {Team} from './entities'
import {IUserInTeam} from './IUserInTeam' import {IUserInTeam} from './IUserInTeam'
export interface ITeamAddUserParams { export interface ITeamAddUserParams {
@ -25,19 +26,19 @@ export interface IContext {
} }
export interface ITeamService { export interface ITeamService {
create(params: ITeamCreateParams): Promise<ITeam> create(params: ITeamCreateParams): Promise<Team>
remove(params: ITeamRemoveParams): Promise<{id: number}> remove(params: ITeamRemoveParams): Promise<{id: number}>
update(params: ITeamUpdateParams): Promise<ITeam> update(params: ITeamUpdateParams): Promise<Team>
addUser(params: ITeamAddUserParams): Promise<IUserInTeam> addUser(params: ITeamAddUserParams): Promise<IUserInTeam>
removeUser(params: ITeamAddUserParams): Promise<ITeamAddUserParams> removeUser(params: ITeamAddUserParams): Promise<ITeamAddUserParams>
findOne(id: number): Promise<ITeam | undefined> findOne(id: number): Promise<Team | undefined>
find(): Promise<ITeam[]> find(): Promise<Team[]>
findUsers(teamId: number): Promise<IUserInTeam[]> findUsers(teamId: number): Promise<IUserInTeam[]>

View File

@ -3,7 +3,7 @@ import 'reflect-metadata'
export const ensureKey = Symbol('ensure') export const ensureKey = Symbol('ensure')
export const ensureClassKey = Symbol('ensureClass') export const ensureClassKey = Symbol('ensureClass')
export type Validate<Context> = (context: Context) => boolean export type Validate<Context> = (context: Context) => boolean | Promise<boolean>
export function ensure<Context>( export function ensure<Context>(
validate: Validate<Context>, validate: Validate<Context>,

View File

@ -71,15 +71,17 @@ export function createSuccessResponse<T>(id: number | string, result: T)
} }
} }
function validateServiceContext<T, M extends FunctionPropertyNames<T>, Context>( async function validateServiceContext<
T, M extends FunctionPropertyNames<T>, Context
>(
id: string | number | null, id: string | number | null,
service: T, service: T,
method: M, method: M,
context: Context, context: Context,
) { ) {
function doValidate(validate: Validate<Context>) { async function doValidate(validate: Validate<Context>) {
const success = validate(context) const success = await validate(context)
if (!success) { if (!success) {
throw createError(ERROR_INVALID_REQUEST, { throw createError(ERROR_INVALID_REQUEST, {
id, id,
@ -89,11 +91,13 @@ function validateServiceContext<T, M extends FunctionPropertyNames<T>, Context>(
} }
} }
getValidatorsForInstance<Context>(service) for (const validate of getValidatorsForInstance<Context>(service)) {
.forEach(doValidate) await doValidate(validate)
}
getValidatorsForMethod<Context>(service, method) for (const validate of getValidatorsForMethod<Context>(service, method)) {
.forEach(doValidate) await doValidate(validate)
}
} }
export const createRpcService = <T, M extends FunctionPropertyNames<T>>( export const createRpcService = <T, M extends FunctionPropertyNames<T>>(
@ -133,7 +137,7 @@ export const createRpcService = <T, M extends FunctionPropertyNames<T>>(
}) })
} }
validateServiceContext(id, service, method, context) await validateServiceContext(id, service, method, context)
let retValue = (rpcService[method] as any)(...params, context) let retValue = (rpcService[method] as any)(...params, context)

View File

@ -353,7 +353,8 @@ export function intergen(...argv: string[]): string {
const name = nameMappings.get(classDef.type)! const name = nameMappings.get(classDef.type)!
const start = `export interface ${name} {` const start = `export interface ${name} {`
const properties = classDef.properties.map(p => { 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') .join('\n')
const end = '}' const end = '}'
@ -368,7 +369,8 @@ export function intergen(...argv: string[]): string {
module: ts.ModuleKind.CommonJS, 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') const value = header + interfaces.join('\n\n')
if (args.output === '-') { if (args.output === '-') {
info(value) info(value)

View File

@ -5,13 +5,14 @@ import {UserTeam} from '../entities/UserTeam'
import {IUserPermissions} from '../user/IUserPermissions' import {IUserPermissions} from '../user/IUserPermissions'
import { import {
trim, trim,
IContext,
entities as e,
team as t, team as t,
IUserInTeam, IUserInTeam,
} from '@rondo.dev/common' } from '@rondo.dev/common'
import {Contextual} from '@rondo.dev/jsonrpc' import {Contextual} from '@rondo.dev/jsonrpc'
type IContext = t.IContext // TODO ensureLoggedIn
export class TeamService2 implements Contextual<t.ITeamService, IContext> { export class TeamService2 implements Contextual<t.ITeamService, IContext> {
constructor( constructor(
protected readonly db: IDatabase, protected readonly db: IDatabase,
@ -19,7 +20,7 @@ export class TeamService2 implements Contextual<t.ITeamService, IContext> {
) {} ) {}
async create(params: t.ITeamCreateParams, context: IContext) { async create(params: t.ITeamCreateParams, context: IContext) {
const {userId} = context const userId = context.user!.id
const name = trim(params.name) const name = trim(params.name)
new Validator({name, userId}) new Validator({name, userId})
@ -43,11 +44,11 @@ export class TeamService2 implements Contextual<t.ITeamService, IContext> {
} }
async remove({id}: t.ITeamRemoveParams, context: IContext) { async remove({id}: t.ITeamRemoveParams, context: IContext) {
const {userId} = context const userId = context.user!.id
await this.permissions.belongsToTeam({ await this.permissions.belongsToTeam({
teamId: id, teamId: id,
userId: context.userId, userId,
}) })
await this.db.getRepository(UserTeam) await this.db.getRepository(UserTeam)
@ -60,9 +61,11 @@ export class TeamService2 implements Contextual<t.ITeamService, IContext> {
} }
async update({id, name}: t.ITeamUpdateParams, context: IContext) { async update({id, name}: t.ITeamUpdateParams, context: IContext) {
const userId = context.user!.id
await this.permissions.belongsToTeam({ await this.permissions.belongsToTeam({
teamId: id, teamId: id,
userId: context.userId, userId,
}) })
await this.db.getRepository(Team) await this.db.getRepository(Team)
@ -96,7 +99,7 @@ export class TeamService2 implements Contextual<t.ITeamService, IContext> {
await this.permissions.belongsToTeam({ await this.permissions.belongsToTeam({
teamId: params.teamId, teamId: params.teamId,
userId: context.userId, userId: context.user!.id,
}) })
// TODO check if this is the last admin team member // TODO check if this is the last admin team member
@ -111,7 +114,8 @@ export class TeamService2 implements Contextual<t.ITeamService, IContext> {
} }
async find(context: IContext) { async find(context: IContext) {
const {userId} = context const userId = context.user!.id
return this.db.getRepository(Team) return this.db.getRepository(Team)
.createQueryBuilder('team') .createQueryBuilder('team')
.select('team') .select('team')
@ -121,9 +125,11 @@ export class TeamService2 implements Contextual<t.ITeamService, IContext> {
} }
async findUsers(teamId: number, context: IContext) { async findUsers(teamId: number, context: IContext) {
const userId = context.user!.id
await this.permissions.belongsToTeam({ await this.permissions.belongsToTeam({
teamId, teamId,
userId: context.userId, userId,
}) })
const userTeams = await this.createFindUserInTeamQuery() const userTeams = await this.createFindUserInTeamQuery()