-
Notifications
You must be signed in to change notification settings - Fork 741
/
Copy pathactivity.repo.ts
153 lines (139 loc) · 3.78 KB
/
activity.repo.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { DbStore, RepositoryBase } from '@crowd/database'
import { Logger } from '@crowd/logging'
import { IDbActivitySyncData } from './activity.data'
import { IndexedEntityType } from './indexing.data'
export class ActivityRepository extends RepositoryBase<ActivityRepository> {
constructor(dbStore: DbStore, parentLog: Logger) {
super(dbStore, parentLog)
}
public async getActivityData(activityIds: string[]): Promise<IDbActivitySyncData[]> {
const results = await this.db().any(
`
select id,
"tenantId",
"segmentId",
type,
timestamp,
platform,
"isContribution",
score,
"sourceId",
"sourceParentId",
channel,
body,
title,
url,
(sentiment -> 'sentiment')::int as sentiment,
"importHash",
"memberId",
"conversationId",
"parentId",
username,
"objectMemberId",
"objectMemberUsername",
"organizationId"
from activities where id in ($(activityIds:csv)) and "deletedAt" is null
`,
{
activityIds,
},
)
return results
}
public async checkActivitiesExist(tenantId: string, activityIds: string[]): Promise<string[]> {
const results = await this.db().any(
`
select id from activities where "tenantId" = $(tenantId) and id in ($(activityIds:csv)) and "deletedAt" is null
`,
{
tenantId,
activityIds,
},
)
return results.map((r) => r.id)
}
public async getTenantActivitiesForSync(tenantId: string, perPage: number): Promise<string[]> {
const results = await this.db().any(
`
select id from activities a
left join indexed_entities ie on
a.id = ie.entity_id and ie.type = $(type)
where a."tenantId" = $(tenantId) and a."deletedAt" is null and ie.entity_id is null
limit ${perPage}
`,
{
tenantId,
type: IndexedEntityType.ACTIVITY,
},
)
return results.map((r) => r.id)
}
public async getOrganizationActivitiesForSync(
organizationId: string,
perPage: number,
lastId?: string,
): Promise<string[]> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let results: any[]
if (lastId) {
results = await this.db().any(
`
select id from activities
where "organizationId" = $(organizationId) and "deletedAt" is null and id > $(lastId)
order by id
limit ${perPage};
`,
{
organizationId,
lastId,
},
)
} else {
results = await this.db().any(
`
select id from activities
where "organizationId" = $(organizationId) and "deletedAt" is null
order by id
limit ${perPage};
`,
{
organizationId,
},
)
}
return results.map((r) => r.id)
}
public async getRemainingTenantActivitiesForSync(
tenantId: string,
page: number,
perPage: number,
cutoffDate: string,
): Promise<string[]> {
const results = await this.db().any(
`
select id from activities
where "tenantId" = $(tenantId) and "deletedAt" is null
and (
"searchSyncedAt" is null or
"searchSyncedAt" < $(cutoffDate)
)
limit ${perPage} offset ${(page - 1) * perPage};
`,
{
tenantId,
cutoffDate,
},
)
return results.map((r) => r.id)
}
public async getTenantIds(): Promise<string[]> {
const results = await this.db().any(
`select "tenantId"
from activities
where "deletedAt" is null
group by "tenantId"
order by count(id) asc`,
)
return results.map((r) => r.tenantId)
}
}