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