-
Notifications
You must be signed in to change notification settings - Fork 741
/
Copy pathmember.service.ts
700 lines (616 loc) · 21.3 KB
/
member.service.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import { Client as TemporalClient, WorkflowIdReusePolicy } from '@crowd/temporal'
import {
IDbMember,
IDbMemberUpdateData,
} from '@crowd/data-access-layer/src/old/apps/data_sink_worker/repo/member.data'
import MemberRepository from '@crowd/data-access-layer/src/old/apps/data_sink_worker/repo/member.repo'
import {
firstArrayContainsSecondArray,
isObjectEmpty,
singleOrDefault,
isDomainExcluded,
isEmail,
} from '@crowd/common'
import { DbStore } from '@crowd/data-access-layer/src/database'
import { Logger, LoggerBase, getChildLogger } from '@crowd/logging'
import {
IMemberData,
IMemberIdentity,
PlatformType,
OrganizationSource,
IOrganizationIdSource,
TemporalWorkflowId,
} from '@crowd/types'
import mergeWith from 'lodash.mergewith'
import isEqual from 'lodash.isequal'
import moment from 'moment-timezone'
import { IMemberCreateData, IMemberUpdateData } from './member.data'
import MemberAttributeService from './memberAttribute.service'
import IntegrationRepository from '@crowd/data-access-layer/src/old/apps/data_sink_worker/repo/integration.repo'
import { OrganizationService } from './organization.service'
import uniqby from 'lodash.uniqby'
import { Unleash } from '@crowd/feature-flags'
import { TEMPORAL_CONFIG } from '../conf'
import { RedisClient } from '@crowd/redis'
import { NodejsWorkerEmitter, SearchSyncWorkerEmitter } from '@crowd/common_services'
export default class MemberService extends LoggerBase {
constructor(
private readonly store: DbStore,
private readonly nodejsWorkerEmitter: NodejsWorkerEmitter,
private readonly searchSyncWorkerEmitter: SearchSyncWorkerEmitter,
private readonly unleash: Unleash | undefined,
private readonly temporal: TemporalClient,
private readonly redisClient: RedisClient,
parentLog: Logger,
) {
super(parentLog)
}
public async create(
tenantId: string,
onboarding: boolean,
segmentId: string,
integrationId: string,
data: IMemberCreateData,
fireSync = true,
releaseMemberLock?: () => Promise<void>,
): Promise<string> {
try {
this.log.debug('Creating a new member!')
if (data.identities.length === 0) {
throw new Error('Member must have at least one identity!')
}
const { id, organizations } = await this.store.transactionally(async (txStore) => {
const txRepo = new MemberRepository(txStore, this.log)
const txMemberAttributeService = new MemberAttributeService(txStore, this.log)
let attributes: Record<string, unknown> = {}
if (data.attributes) {
attributes = await txMemberAttributeService.validateAttributes(tenantId, data.attributes)
attributes = await txMemberAttributeService.setAttributesDefaultValues(
tenantId,
attributes,
)
}
// validate emails
if (data.emails) {
data.emails = this.validateEmails(data.emails)
}
// check if any weak identities are actually strong
await this.checkForStrongWeakIdentities(txRepo, tenantId, data)
const id = await txRepo.create(tenantId, {
displayName: data.displayName,
emails: data.emails,
joinedAt: data.joinedAt.toISOString(),
attributes,
weakIdentities: data.weakIdentities || [],
identities: data.identities,
reach: MemberService.calculateReach({}, data.reach),
})
await txRepo.addToSegment(id, tenantId, segmentId)
await txRepo.insertIdentities(id, tenantId, integrationId, data.identities)
if (releaseMemberLock) {
await releaseMemberLock()
}
const organizations = []
const orgService = new OrganizationService(txStore, this.log)
if (data.organizations) {
for (const org of data.organizations) {
const id = await orgService.findOrCreate(tenantId, segmentId, integrationId, org)
organizations.push({
id,
source: org.source,
})
}
}
if (data.emails) {
const orgs = await this.assignOrganizationByEmailDomain(
tenantId,
segmentId,
integrationId,
data.emails,
)
if (orgs.length > 0) {
organizations.push(...orgs)
}
}
if (organizations.length > 0) {
// remove dups
const uniqOrganizations = uniqby(organizations, 'id')
await orgService.addToMember(tenantId, segmentId, id, uniqOrganizations)
}
return {
id,
organizations,
}
})
try {
const handle = await this.temporal.workflow.start('processNewMemberAutomation', {
workflowId: `${TemporalWorkflowId.NEW_MEMBER_AUTOMATION}/${id}`,
taskQueue: TEMPORAL_CONFIG().automationsTaskQueue,
workflowIdReusePolicy: WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE,
retry: {
maximumAttempts: 100,
},
args: [
{
tenantId,
memberId: id,
},
],
searchAttributes: {
TenantId: [tenantId],
},
})
this.log.info(
{ workflowId: handle.workflowId },
'Started temporal workflow to process new member automation!',
)
} catch (err) {
this.log.error(
err,
'Error while starting temporal workflow to process new member automation!',
)
throw err
}
if (fireSync) {
await this.searchSyncWorkerEmitter.triggerMemberSync(tenantId, id, onboarding, segmentId)
}
for (const org of organizations) {
await this.searchSyncWorkerEmitter.triggerOrganizationSync(
tenantId,
org.id,
onboarding,
segmentId,
)
}
return id
} catch (err) {
this.log.error(err, 'Error while creating a new member!')
throw err
}
}
public async update(
id: string,
tenantId: string,
onboarding: boolean,
segmentId: string,
integrationId: string,
data: IMemberUpdateData,
original: IDbMember,
fireSync = true,
releaseMemberLock?: () => Promise<void>,
): Promise<void> {
try {
const { updated, organizations } = await this.store.transactionally(async (txStore) => {
const txRepo = new MemberRepository(txStore, this.log)
const txMemberAttributeService = new MemberAttributeService(txStore, this.log)
const dbIdentities = await txRepo.getIdentities(id, tenantId)
if (data.attributes) {
data.attributes = await txMemberAttributeService.validateAttributes(
tenantId,
data.attributes,
)
}
// validate emails
if (data.emails) {
data.emails = this.validateEmails(data.emails)
}
// check if any weak identities are actually strong
await this.checkForStrongWeakIdentities(txRepo, tenantId, data, id)
const toUpdate = MemberService.mergeData(original, dbIdentities, data)
if (toUpdate.attributes) {
toUpdate.attributes = await txMemberAttributeService.setAttributesDefaultValues(
tenantId,
toUpdate.attributes,
)
}
let updated = false
if (!isObjectEmpty(toUpdate)) {
this.log.debug({ memberId: id }, 'Updating a member!')
const dateToUpdate = Object.entries(toUpdate).reduce((acc, [key, value]) => {
if (key === 'identities') {
return acc
}
if (value) {
acc[key] = value
}
return acc
}, {} as IDbMemberUpdateData)
await txRepo.update(id, tenantId, dateToUpdate)
await txRepo.addToSegment(id, tenantId, segmentId)
updated = true
} else {
this.log.debug({ memberId: id }, 'Nothing to update in a member!')
await txRepo.addToSegment(id, tenantId, segmentId)
}
if (toUpdate.identities) {
await txRepo.insertIdentities(id, tenantId, integrationId, toUpdate.identities)
updated = true
}
if (releaseMemberLock) {
await releaseMemberLock()
}
const organizations = []
const orgService = new OrganizationService(txStore, this.log)
if (data.organizations) {
for (const org of data.organizations) {
const id = await orgService.findOrCreate(tenantId, segmentId, integrationId, org)
organizations.push({
id,
source: data.source,
})
}
if (organizations.length > 0) {
await orgService.addToMember(tenantId, segmentId, id, organizations)
updated = true
}
}
if (data.emails) {
const orgs = await this.assignOrganizationByEmailDomain(
tenantId,
segmentId,
integrationId,
data.emails,
)
if (orgs.length > 0) {
organizations.push(...orgs)
}
}
if (organizations.length > 0) {
// remove dups
const uniqOrganizations = uniqby(organizations, 'id')
await orgService.addToMember(tenantId, segmentId, id, uniqOrganizations)
updated = true
}
return { updated, organizations }
})
if (updated && fireSync) {
await this.searchSyncWorkerEmitter.triggerMemberSync(tenantId, id, onboarding, segmentId)
}
for (const org of organizations) {
await this.searchSyncWorkerEmitter.triggerOrganizationSync(
tenantId,
org.id,
onboarding,
segmentId,
)
}
} catch (err) {
this.log.error(err, { memberId: id }, 'Error while updating a member!')
throw err
}
}
public async assignOrganizationByEmailDomain(
tenantId: string,
segmentId: string,
integrationId: string,
emails: string[],
): Promise<IOrganizationIdSource[]> {
const orgService = new OrganizationService(this.store, this.log)
const organizations: IOrganizationIdSource[] = []
const emailDomains = new Set<string>()
// Collect unique domains
for (const email of emails) {
if (email) {
const domain = email.split('@')[1]
// domain can be undefined if email is invalid
if (domain) {
if (!isDomainExcluded(domain)) {
emailDomains.add(domain)
}
}
}
}
// Assign member to organization based on email domain
for (const domain of emailDomains) {
const orgId = await orgService.findOrCreate(tenantId, segmentId, integrationId, {
website: domain,
identities: [
{
name: domain,
platform: 'email',
},
],
})
if (orgId) {
organizations.push({
id: orgId,
source: OrganizationSource.EMAIL_DOMAIN,
})
}
}
return organizations
}
public async processMemberEnrich(
tenantId: string,
integrationId: string,
platform: PlatformType,
member: IMemberData,
): Promise<void> {
this.log = getChildLogger('MemberService.processMemberEnrich', this.log, {
integrationId,
tenantId,
})
try {
this.log.debug('Processing member enrich.')
if (
(!member.emails || member.emails.length === 0) &&
(!member.identities || member.identities.length === 0)
) {
const errorMessage = `Member can't be enriched. It is missing both emails and identities fields.`
this.log.warn(errorMessage)
return
}
await this.store.transactionally(async (txStore) => {
const txRepo = new MemberRepository(txStore, this.log)
const txIntegrationRepo = new IntegrationRepository(txStore, this.log)
const txService = new MemberService(
txStore,
this.nodejsWorkerEmitter,
this.searchSyncWorkerEmitter,
this.unleash,
this.temporal,
this.redisClient,
this.log,
)
const dbIntegration = await txIntegrationRepo.findById(integrationId)
const segmentId = dbIntegration.segmentId
// first try finding the member using the identity
const identity = singleOrDefault(
member.identities,
(i) => i.platform === platform && i.sourceId !== undefined && i.sourceId !== null,
)
let dbMember = await txRepo.findMember(tenantId, segmentId, platform, identity.username)
if (!dbMember && member.emails && member.emails.length > 0) {
const email = member.emails[0]
// try finding the member using e-mail
dbMember = await txRepo.findMemberByEmail(tenantId, email)
}
if (dbMember) {
this.log.trace({ memberId: dbMember.id }, 'Found existing member.')
// set a record in membersSyncRemote to save the sourceId
// these rows will be used for outgoing data
if (member.attributes?.sourceId?.[platform]) {
await txRepo.addToSyncRemote(
dbMember.id,
dbIntegration.id,
member.attributes?.sourceId?.[platform],
)
}
await txService.update(
dbMember.id,
tenantId,
false,
segmentId,
integrationId,
{
attributes: member.attributes,
emails: member.emails || [],
joinedAt: member.joinedAt ? new Date(member.joinedAt) : undefined,
weakIdentities: member.weakIdentities || undefined,
identities: member.identities,
organizations: member.organizations,
displayName: member.displayName || undefined,
},
dbMember,
false,
)
} else {
this.log.debug('No member found for enriching. This member enrich process had no affect.')
}
})
} catch (err) {
this.log.error(err, 'Error while processing a member enrich!')
throw err
}
}
public async processMemberUpdate(
tenantId: string,
integrationId: string,
platform: PlatformType,
member: IMemberData,
): Promise<void> {
this.log = getChildLogger('MemberService.processMemberUpdate', this.log, {
integrationId,
tenantId,
})
try {
this.log.debug('Processing member update.')
if (!member.identities || member.identities.length === 0) {
const errorMessage = `Member can't be updated. It is missing identities fields.`
this.log.warn(errorMessage)
return
}
await this.store.transactionally(async (txStore) => {
const txRepo = new MemberRepository(txStore, this.log)
const txIntegrationRepo = new IntegrationRepository(txStore, this.log)
const txService = new MemberService(
txStore,
this.nodejsWorkerEmitter,
this.searchSyncWorkerEmitter,
this.unleash,
this.temporal,
this.redisClient,
this.log,
)
const dbIntegration = await txIntegrationRepo.findById(integrationId)
const segmentId = dbIntegration.segmentId
// first try finding the member using the identity
const identity = singleOrDefault(member.identities, (i) => i.platform === platform)
const dbMember = await txRepo.findMember(tenantId, segmentId, platform, identity.username)
if (dbMember) {
this.log.trace({ memberId: dbMember.id }, 'Found existing member.')
await txService.update(
dbMember.id,
tenantId,
false,
segmentId,
integrationId,
{
attributes: member.attributes,
emails: member.emails || [],
joinedAt: member.joinedAt ? new Date(member.joinedAt) : undefined,
weakIdentities: member.weakIdentities || undefined,
identities: member.identities,
organizations: member.organizations,
displayName: member.displayName || undefined,
reach: member.reach || undefined,
},
dbMember,
false,
)
} else {
this.log.debug('No member found for updating. This member update process had no affect.')
}
})
} catch (err) {
this.log.error(err, 'Error while processing a member update!')
throw err
}
}
private async checkForStrongWeakIdentities(
repo: MemberRepository,
tenantId: string,
data: IMemberCreateData | IMemberUpdateData,
memberId?: string,
): Promise<void> {
if (data.weakIdentities && data.weakIdentities.length > 0) {
const results = await repo.findIdentities(tenantId, data.weakIdentities, memberId)
const strongIdentities = []
for (const weakIdentity of data.weakIdentities) {
if (!results.has(`${weakIdentity.platform}:${weakIdentity.username}`)) {
strongIdentities.push(weakIdentity)
}
}
if (strongIdentities.length > 0) {
data.weakIdentities = data.weakIdentities.filter(
(i) =>
strongIdentities.find((s) => s.platform === i.platform && s.username === i.username) ===
undefined,
)
for (const identity of strongIdentities) {
if (
data.identities.find(
(i) => i.platform === identity.platform && i.username === identity.username,
) === undefined
) {
data.identities.push(identity)
}
}
}
}
}
private validateEmails(emails: string[]): string[] {
let newEmails = emails.filter((email) => isEmail(email))
if (newEmails.length > 0) {
const emailSet = new Set(newEmails)
newEmails = Array.from(emailSet)
} else {
newEmails = []
}
return newEmails
}
private static mergeData(
dbMember: IDbMember,
dbIdentities: IMemberIdentity[],
member: IMemberUpdateData,
): IDbMemberUpdateData {
let joinedAt: string | undefined
if (member.joinedAt) {
const newDate = member.joinedAt
const oldDate = new Date(dbMember.joinedAt)
// If either the new or the old date are earlier than 1970
// it means they come from an activity without timestamp
// and we want to keep the other one
if (moment(oldDate).subtract(5, 'days').unix() < 0) {
joinedAt = newDate.toISOString()
}
if (moment(newDate).unix() < 0) {
joinedAt = undefined
}
if (oldDate <= newDate) {
// we already have the oldest date in the db, so we don't need to update it
joinedAt = undefined
} else {
// we have a new date and it's older, so we need to update it
joinedAt = newDate.toISOString()
}
}
let emails: string[] | undefined
if (member.emails && member.emails.length > 0) {
if (!firstArrayContainsSecondArray(dbMember.emails, member.emails)) {
emails = [...new Set([...member.emails, ...dbMember.emails])]
}
}
let weakIdentities: IMemberIdentity[] | undefined
if (member.weakIdentities && member.weakIdentities.length > 0) {
const newWeakIdentities: IMemberIdentity[] = []
for (const identity of member.weakIdentities) {
if (
!dbMember.weakIdentities.find(
(t) => t.platform === identity.platform && t.username === identity.username,
)
) {
newWeakIdentities.push(identity)
}
}
if (newWeakIdentities.length > 0) {
weakIdentities = newWeakIdentities
}
}
let identities: IMemberIdentity[] | undefined
if (member.identities && member.identities.length > 0) {
const newIdentities: IMemberIdentity[] = []
for (const identity of member.identities) {
if (
!dbIdentities.find(
(t) => t.platform === identity.platform && t.username === identity.username,
)
) {
newIdentities.push(identity)
}
}
if (newIdentities.length > 0) {
identities = newIdentities
}
}
let attributes: Record<string, unknown> | undefined
if (member.attributes) {
const temp = mergeWith({}, dbMember.attributes, member.attributes)
if (!isEqual(temp, dbMember.attributes)) {
attributes = temp
}
}
let reach: Partial<Record<PlatformType, number>> | undefined
if (member.reach) {
const temp = MemberService.calculateReach(dbMember.reach, member.reach)
if (!isEqual(temp, dbMember.reach)) {
reach = temp
}
}
return {
emails,
joinedAt,
attributes,
weakIdentities,
identities,
// we don't want to update the display name if it's already set
// returned value should be undefined here otherwise it will cause an update!
displayName: dbMember.displayName ? undefined : member.displayName,
reach,
}
}
private static calculateReach(
oldReach: Partial<Record<PlatformType | 'total', number>>,
newReach: Partial<Record<PlatformType, number>>,
) {
// Totals are recomputed, so we delete them first
delete oldReach.total
const out = mergeWith({}, oldReach, newReach)
if (Object.keys(out).length === 0) {
return { total: -1 }
}
// Total is the sum of all attributes
out.total = Object.values(out).reduce((a: number, b: number) => a + b, 0)
return out
}
}