Generate @rondo.dev/common entities
This commit is contained in:
parent
3b6011f88d
commit
cd5ff9b5da
5
packages/common/src/IContext.ts
Normal file
5
packages/common/src/IContext.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export interface IContext {
|
||||
user?: {
|
||||
id: number
|
||||
}
|
||||
}
|
||||
66
packages/common/src/entities.ts
Normal file
66
packages/common/src/entities.ts
Normal 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
|
||||
}
|
||||
@ -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}
|
||||
|
||||
@ -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<ITeam>
|
||||
create(params: ITeamCreateParams): Promise<Team>
|
||||
|
||||
remove(params: ITeamRemoveParams): Promise<{id: number}>
|
||||
|
||||
update(params: ITeamUpdateParams): Promise<ITeam>
|
||||
update(params: ITeamUpdateParams): Promise<Team>
|
||||
|
||||
addUser(params: ITeamAddUserParams): Promise<IUserInTeam>
|
||||
|
||||
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[]>
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ import 'reflect-metadata'
|
||||
export const ensureKey = Symbol('ensure')
|
||||
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>(
|
||||
validate: Validate<Context>,
|
||||
|
||||
@ -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,
|
||||
service: T,
|
||||
method: M,
|
||||
context: Context,
|
||||
) {
|
||||
|
||||
function doValidate(validate: Validate<Context>) {
|
||||
const success = validate(context)
|
||||
async function doValidate(validate: Validate<Context>) {
|
||||
const success = await validate(context)
|
||||
if (!success) {
|
||||
throw createError(ERROR_INVALID_REQUEST, {
|
||||
id,
|
||||
@ -89,11 +91,13 @@ function validateServiceContext<T, M extends FunctionPropertyNames<T>, Context>(
|
||||
}
|
||||
}
|
||||
|
||||
getValidatorsForInstance<Context>(service)
|
||||
.forEach(doValidate)
|
||||
for (const validate of getValidatorsForInstance<Context>(service)) {
|
||||
await doValidate(validate)
|
||||
}
|
||||
|
||||
getValidatorsForMethod<Context>(service, method)
|
||||
.forEach(doValidate)
|
||||
for (const validate of getValidatorsForMethod<Context>(service, method)) {
|
||||
await doValidate(validate)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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<t.ITeamService, IContext> {
|
||||
constructor(
|
||||
protected readonly db: IDatabase,
|
||||
@ -19,7 +20,7 @@ export class TeamService2 implements Contextual<t.ITeamService, IContext> {
|
||||
) {}
|
||||
|
||||
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<t.ITeamService, IContext> {
|
||||
}
|
||||
|
||||
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<t.ITeamService, IContext> {
|
||||
}
|
||||
|
||||
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<t.ITeamService, IContext> {
|
||||
|
||||
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<t.ITeamService, IContext> {
|
||||
}
|
||||
|
||||
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<t.ITeamService, IContext> {
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user