-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathActivityLog.ts
More file actions
80 lines (71 loc) · 2.45 KB
/
ActivityLog.ts
File metadata and controls
80 lines (71 loc) · 2.45 KB
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
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,
User,
UserRank,
UserRankListChunk
} from '../model';
const store = dataSource.getRepository(ActivityLog),
userStore = dataSource.getRepository(User),
userRankStore = dataSource.getRepository(UserRank);
@JsonController('/activity-log')
export class ActivityLogController {
@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 && { 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 && { 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 };
}
}