* Upsert an activity. If the member exists, it updates it. If it does not exist, it creates it. * The update is done with a deep merge of the original and the new activities. * @param data Activity data * data.sourceId is the platform specific id given by the platform. * data.sourceParen
(
data,
existing: boolean | any = false,
fireCrowdWebhooks: boolean = true,
fireSync: boolean = true,
)
| 50 | * @returns The upserted activity |
| 51 | */ |
| 52 | async upsert( |
| 53 | data, |
| 54 | existing: boolean | any = false, |
| 55 | fireCrowdWebhooks: boolean = true, |
| 56 | fireSync: boolean = true, |
| 57 | ) { |
| 58 | const transaction = await SequelizeRepository.createTransaction(this.options) |
| 59 | const searchSyncService = new SearchSyncService(this.options) |
| 60 | const repositoryOptions = { ...this.options, transaction } |
| 61 | |
| 62 | try { |
| 63 | if (data.member) { |
| 64 | data.member = await MemberRepository.filterIdInTenant(data.member, repositoryOptions) |
| 65 | } |
| 66 | |
| 67 | // check type exists, if doesn't exist, create a placeholder type with activity type key |
| 68 | if ( |
| 69 | data.platform && |
| 70 | data.type && |
| 71 | !SegmentRepository.activityTypeExists(data.platform, data.type, repositoryOptions) |
| 72 | ) { |
| 73 | await new SegmentService(repositoryOptions).createActivityType( |
| 74 | { type: data.type }, |
| 75 | data.platform, |
| 76 | ) |
| 77 | await SegmentService.refreshSegments(repositoryOptions) |
| 78 | } |
| 79 | |
| 80 | // check if channel exists in settings for respective platform. If not, update by adding channel to settings |
| 81 | if (data.platform && data.channel) { |
| 82 | await new SegmentService(repositoryOptions).updateActivityChannels(data) |
| 83 | await SegmentService.refreshSegments(repositoryOptions) |
| 84 | } |
| 85 | |
| 86 | // If a sourceParentId is sent, try to find it in our db |
| 87 | if ('sourceParentId' in data && data.sourceParentId) { |
| 88 | const parent = await ActivityRepository.findOne( |
| 89 | { sourceId: data.sourceParentId }, |
| 90 | repositoryOptions, |
| 91 | ) |
| 92 | if (parent) { |
| 93 | data.parent = await ActivityRepository.filterIdInTenant(parent.id, repositoryOptions) |
| 94 | } else { |
| 95 | data.parent = null |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (!existing) { |
| 100 | existing = await this._activityExists(data, transaction) |
| 101 | } |
| 102 | |
| 103 | let record |
| 104 | if (existing) { |
| 105 | const { id } = existing |
| 106 | delete existing.id |
| 107 | const toUpdate = merge(existing, data, { |
| 108 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 109 | timestamp: (oldValue, _newValue) => oldValue, |
no test coverage detected