-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathOrganizationHistoryDatasource.ts
More file actions
83 lines (77 loc) · 2.13 KB
/
OrganizationHistoryDatasource.ts
File metadata and controls
83 lines (77 loc) · 2.13 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
81
82
83
import { MongoDataSource } from 'apollo-datasource-mongodb'
import { MUUID } from 'uuid-mongodb'
import { OrganizationChangeLogType } from '../db/ChangeLogType.js'
import { getChangeLogModel } from '../db/index.js'
export class OrganizationHistoryDataSource extends MongoDataSource<OrganizationChangeLogType> {
changelogModel = getChangeLogModel()
async getChangeSetsByOrgId (orgId?: MUUID, limit: number = 50, offset: number = 0): Promise<OrganizationChangeLogType[]> {
let rs
if (orgId == null) {
// No orgId specified: return all changes
const filter: any = {
$match: {
'changes.kind': 'organizations'
}
}
rs = await this.changelogModel.aggregate([
filter,
{
$sort: {
createdAt: -1
}
},
{
$skip: offset
},
{
$limit: limit
}
])
return rs as OrganizationChangeLogType[]
} else {
const filter = {
$match: {
changes: {
$elemMatch:
{ 'fullDocument.orgId': orgId, kind: 'organizations' }
}
}
}
const rs2 = await this.changelogModel
.aggregate([
filter,
// https://github.com/Automattic/mongoose/issues/12415
// {
// $set: {
// changes: {
// $sortArray: {
// input: '$changes',
// sortBy: { 'fullDocument._change.seq': -1 }
// }
// }
// }
// },
{
$sort: {
createdAt: -1
}
},
{
$skip: offset
},
{
$limit: limit
}
])
return rs2
}
}
static instance: OrganizationHistoryDataSource
static getInstance (): OrganizationHistoryDataSource {
if (OrganizationHistoryDataSource.instance == null) {
// @ts-expect-error
OrganizationHistoryDataSource.instance = new OrganizationHistoryDataSource({ modelOrCollection: getChangeLogModel() })
}
return OrganizationHistoryDataSource.instance
}
}