-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathactivityService.ts
More file actions
190 lines (155 loc) · 5.18 KB
/
Copy pathactivityService.ts
File metadata and controls
190 lines (155 loc) · 5.18 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { queryActivities } from '@crowd/data-access-layer'
import {
getSegmentSubprojectIds,
getSegmentSubprojects,
} from '@crowd/data-access-layer/src/segments'
import { LoggerBase } from '@crowd/logging'
import { IMemberIdentity, IntegrationResultType } from '@crowd/types'
import SequelizeRepository from '@/database/repositories/sequelizeRepository'
import { getDataSinkWorkerEmitter } from '@/serverless/utils/queueService'
import ActivityRepository from '../database/repositories/activityRepository'
import {
UsernameIdentities,
mapUsernameToIdentities,
} from '../database/repositories/types/memberTypes'
import { IServiceOptions } from './IServiceOptions'
import SegmentService from './segmentService'
export default class ActivityService extends LoggerBase {
options: IServiceOptions
constructor(options: IServiceOptions) {
super(options.log)
this.options = options
}
async createWithMember(data) {
const logger = this.options.log
const dataSinkWorkerEmitter = await getDataSinkWorkerEmitter()
try {
data.member.username = mapUsernameToIdentities(data.member.username, data.platform)
if (!data.username) {
data.username = data.member.username[data.platform][0].value
}
logger.trace(
{ type: data.type, platform: data.platform, username: data.username },
'Processing activity with member!',
)
data.member.identities = ActivityService.processMemberIdentities(data.member, data.platform)
// prepare objectMember for dataSinkWorker
if (data.objectMember) {
data.objectMember.username = mapUsernameToIdentities(
data.objectMember.username,
data.platform,
)
if (!data.objectMember.username[data.platform]) {
throw new Error(`objectMember username for ${data.platform} is missing!`)
}
data.objectMemberUsername = data.objectMember.username[data.platform][0].value
data.objectMember.identities = ActivityService.processMemberIdentities(
data.objectMember,
data.platform,
)
}
if (data.member.organizations) {
data.member.organizations.forEach((org) => {
org.identities = [
{
name: org.name || org.website,
platform: data.platform,
},
]
})
}
const resultId = await ActivityRepository.createResults(
{
type: IntegrationResultType.ACTIVITY,
data,
},
this.options,
)
logger.trace(
{ type: data.type, platform: data.platform, username: data.username, processedData: data },
'Sending activity with member to data-sink-worker!',
)
await dataSinkWorkerEmitter.triggerResultProcessing(resultId, resultId, true)
} catch (error) {
this.log.error(error, 'Error during activity create with member!')
throw error
}
}
async findActivityTypes() {
const qx = SequelizeRepository.getQueryExecutor(this.options)
const currentSegments = SequelizeRepository.getSegmentIds(this.options)
const subprojects = await getSegmentSubprojects(qx, currentSegments)
return SegmentService.getTenantActivityTypes(subprojects)
}
async findActivityChannels() {
const qx = SequelizeRepository.getQueryExecutor(this.options)
const currentSegments = SequelizeRepository.getSegmentIds(this.options)
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
if (subprojectIds.length === 0) {
return {}
}
return SegmentService.getTenantActivityChannels(subprojectIds, this.options)
}
async query(data) {
const filter = data.filter
const orderBy = Array.isArray(data.orderBy) ? data.orderBy : [data.orderBy]
const limit = data.limit
const offset = data.offset
const countOnly = data.countOnly ?? false
const qx = SequelizeRepository.getQueryExecutor(this.options)
const currentSegments = SequelizeRepository.getSegmentIds(this.options)
const subprojects = await getSegmentSubprojects(qx, currentSegments)
if (subprojects.length === 0) {
return {
count: 0,
rows: [],
limit,
offset,
}
}
const activitiyTypes = SegmentService.getTenantActivityTypes(subprojects)
const page = await queryActivities(
{
segmentIds: subprojects.map((s) => s.id),
filter,
orderBy,
limit,
offset,
countOnly,
},
qx,
activitiyTypes,
)
return page
}
static processMemberIdentities(
member: {
username: UsernameIdentities
emails: string[]
},
platform: string,
): IMemberIdentity[] {
const identities = []
if (member.username) {
Object.keys(member.username).forEach((platform) => {
identities.push({
platform,
value: member.username[platform][0].value,
type: member.username[platform][0].type,
verified: true,
})
})
}
if (member.emails) {
member.emails.forEach((email) => {
identities.push({
platform,
value: email,
type: 'email',
verified: true,
})
})
}
return identities
}
}