|
| 1 | +import { Get, JsonController, Param, QueryParams } from 'routing-controllers'; |
| 2 | +import { ResponseSchema } from 'routing-controllers-openapi'; |
| 3 | +import { FindOptionsWhere } from 'typeorm'; |
| 4 | + |
| 5 | +import { |
| 6 | + ActivityLog, |
| 7 | + ActivityLogFilter, |
| 8 | + ActivityLogListChunk, |
| 9 | + BaseFilter, |
| 10 | + dataSource, |
| 11 | + LogableTable, |
| 12 | + Operation, |
| 13 | + User, |
| 14 | + UserRank, |
| 15 | + UserRankListChunk |
| 16 | +} from '../model'; |
| 17 | + |
| 18 | +const store = dataSource.getRepository(ActivityLog), |
| 19 | + userStore = dataSource.getRepository(User), |
| 20 | + userRankStore = dataSource.getRepository(UserRank); |
| 21 | + |
| 22 | +@JsonController('/activity-log') |
| 23 | +export class ActivityLogController { |
| 24 | + static logCreate( |
| 25 | + createdBy: User, |
| 26 | + tableName: ActivityLog['tableName'], |
| 27 | + recordId: number |
| 28 | + ) { |
| 29 | + const operation = Operation.Create; |
| 30 | + |
| 31 | + return store.save({ createdBy, operation, tableName, recordId }); |
| 32 | + } |
| 33 | + |
| 34 | + static logUpdate( |
| 35 | + createdBy: User, |
| 36 | + tableName: ActivityLog['tableName'], |
| 37 | + recordId: number |
| 38 | + ) { |
| 39 | + const operation = Operation.Update; |
| 40 | + |
| 41 | + return store.save({ createdBy, operation, tableName, recordId }); |
| 42 | + } |
| 43 | + |
| 44 | + static logDelete( |
| 45 | + createdBy: User, |
| 46 | + tableName: ActivityLog['tableName'], |
| 47 | + recordId: number |
| 48 | + ) { |
| 49 | + const operation = Operation.Delete; |
| 50 | + |
| 51 | + return store.save({ createdBy, operation, tableName, recordId }); |
| 52 | + } |
| 53 | + |
| 54 | + @Get('/user-rank') |
| 55 | + @ResponseSchema(UserRankListChunk) |
| 56 | + async getUserRankList(@QueryParams() { pageSize, pageIndex }: BaseFilter) { |
| 57 | + const skip = pageSize * (pageIndex - 1); |
| 58 | + |
| 59 | + const [list, count] = await userRankStore.findAndCount({ |
| 60 | + order: { score: 'DESC' }, |
| 61 | + skip, |
| 62 | + take: pageSize |
| 63 | + }); |
| 64 | + for (let i = 0, item: UserRank; (item = list[i]); i++) { |
| 65 | + item.rank = skip + i + 1; |
| 66 | + item.user = await userStore.findOneBy({ id: item.userId }); |
| 67 | + } |
| 68 | + return { list, count }; |
| 69 | + } |
| 70 | + |
| 71 | + @Get('/user/:id') |
| 72 | + @ResponseSchema(ActivityLogListChunk) |
| 73 | + getUserList( |
| 74 | + @Param('id') id: number, |
| 75 | + @QueryParams() { operation, pageSize, pageIndex }: ActivityLogFilter |
| 76 | + ) { |
| 77 | + return this.queryList( |
| 78 | + { operation, createdBy: { id } }, |
| 79 | + { pageSize, pageIndex } |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + @Get('/:table/:id') |
| 84 | + @ResponseSchema(ActivityLogListChunk) |
| 85 | + getList( |
| 86 | + @Param('table') tableName: keyof typeof LogableTable, |
| 87 | + @Param('id') recordId: number, |
| 88 | + @QueryParams() { operation, pageSize, pageIndex }: ActivityLogFilter |
| 89 | + ) { |
| 90 | + return this.queryList( |
| 91 | + { operation, tableName, recordId }, |
| 92 | + { pageSize, pageIndex } |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + async queryList( |
| 97 | + where: FindOptionsWhere<ActivityLog>, |
| 98 | + { pageSize, pageIndex }: BaseFilter |
| 99 | + ) { |
| 100 | + const [list, count] = await store.findAndCount({ |
| 101 | + where, |
| 102 | + relations: ['createdBy'], |
| 103 | + skip: pageSize * (pageIndex - 1), |
| 104 | + take: pageSize |
| 105 | + }); |
| 106 | + |
| 107 | + for (const activity of list) |
| 108 | + activity.record = await dataSource |
| 109 | + .getRepository<ActivityLog['record']>(activity.tableName) |
| 110 | + .findOneBy({ id: activity.recordId }); |
| 111 | + |
| 112 | + return { list, count }; |
| 113 | + } |
| 114 | +} |
0 commit comments