|
| 1 | +import OneSignalError from '../../shared/errors/OneSignalError'; |
| 2 | +import Database from '../../shared/services/Database'; |
| 3 | +import { OSModel } from '../modelRepo/OSModel'; |
| 4 | +import { CoreChangeType } from '../models/CoreChangeType'; |
| 5 | +import { CoreDelta } from '../models/CoreDeltas'; |
| 6 | +import { ModelName, SupportedModel } from '../models/SupportedModels'; |
| 7 | +import { isPropertyDelta, isPureObject } from '../utils/typePredicates'; |
| 8 | + |
| 9 | +export class LegacyOperation<Model> { |
| 10 | + operationId: string; |
| 11 | + timestamp: number; |
| 12 | + payload?: Partial<SupportedModel>; |
| 13 | + model?: OSModel<Model>; |
| 14 | + applyToRecordId?: string; |
| 15 | + jwtTokenAvailable: Promise<void>; |
| 16 | + jwtToken?: string | null; |
| 17 | + |
| 18 | + constructor( |
| 19 | + readonly changeType: CoreChangeType, |
| 20 | + readonly modelName: ModelName, |
| 21 | + deltas?: CoreDelta<Model>[], |
| 22 | + ) { |
| 23 | + this.operationId = Math.random().toString(36).substring(2); |
| 24 | + this.payload = deltas ? this.getPayload(deltas) : undefined; |
| 25 | + this.model = deltas ? deltas[deltas.length - 1].model : undefined; |
| 26 | + this.applyToRecordId = deltas?.[deltas.length - 1]?.applyToRecordId; |
| 27 | + this.timestamp = Date.now(); |
| 28 | + // eslint-disable-next-line no-async-promise-executor |
| 29 | + this.jwtTokenAvailable = new Promise<void>(async (resolve) => { |
| 30 | + this.jwtToken = await Database.getJWTToken(); |
| 31 | + resolve(); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + private getPayload(deltas: CoreDelta<Model>[]): any { |
| 36 | + // for update operations, we send the aggregated property-level changes |
| 37 | + if (isPropertyDelta(deltas[0])) { |
| 38 | + return this.aggregateDeltas(deltas); |
| 39 | + } |
| 40 | + |
| 41 | + // for add and remove operations, we send the model data itself |
| 42 | + return deltas[0].model.data; |
| 43 | + } |
| 44 | + |
| 45 | + private aggregateDeltas(deltas: CoreDelta<Model>[]): { |
| 46 | + [key: string]: unknown; |
| 47 | + } { |
| 48 | + const result: { [key: string]: Record<string, unknown> } = {}; |
| 49 | + |
| 50 | + deltas.forEach((delta) => { |
| 51 | + if (isPropertyDelta(delta)) { |
| 52 | + /** |
| 53 | + * If the delta is a property delta, we need to check if the property is an object. |
| 54 | + * If the object has been previously set, we need to merge the new value with the old value. |
| 55 | + * Example: |
| 56 | + * 1. Initial value: { a: { b: 1 } } |
| 57 | + * 2. Delta 1: { a: { b: 2 } } |
| 58 | + * 3. Delta 2: { a: { c: 3 } } |
| 59 | + * 4. Result: { a: { b: 2, c: 3 } } |
| 60 | + */ |
| 61 | + // eslint-disable-next-line no-prototype-builtins |
| 62 | + const hasExistingProperty = result.hasOwnProperty(delta.property); |
| 63 | + const newValueIsPureObject = isPureObject(delta.newValue); |
| 64 | + const oldValueIsPureObject = isPureObject(delta.oldValue); |
| 65 | + const isNewAndOldCompatible = |
| 66 | + newValueIsPureObject === oldValueIsPureObject || |
| 67 | + delta.oldValue === undefined; |
| 68 | + |
| 69 | + if (!isNewAndOldCompatible) { |
| 70 | + throw new Error('Cannot merge incompatible values'); |
| 71 | + } |
| 72 | + |
| 73 | + const shouldMergeExistingAndNew = |
| 74 | + hasExistingProperty && newValueIsPureObject; |
| 75 | + |
| 76 | + const mergedObject = { ...result[delta.property], ...delta.newValue }; |
| 77 | + |
| 78 | + result[delta.property] = shouldMergeExistingAndNew |
| 79 | + ? mergedObject |
| 80 | + : delta.newValue; |
| 81 | + } |
| 82 | + }); |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + static getInstanceWithModelReference( |
| 87 | + rawOperation: LegacyOperation<SupportedModel>, |
| 88 | + ): LegacyOperation<SupportedModel> | undefined { |
| 89 | + const { operationId, payload, modelName, changeType, timestamp, model } = |
| 90 | + rawOperation; |
| 91 | + if (!model) { |
| 92 | + throw new OneSignalError('Operation.fromJSON: model is undefined'); |
| 93 | + } |
| 94 | + const osModel = OneSignal.coreDirector?.getModelByTypeAndId( |
| 95 | + modelName, |
| 96 | + model.modelId, |
| 97 | + ); |
| 98 | + |
| 99 | + if (!!osModel) { |
| 100 | + const operation = new LegacyOperation<SupportedModel>( |
| 101 | + changeType, |
| 102 | + modelName, |
| 103 | + ); |
| 104 | + operation.model = osModel; |
| 105 | + operation.operationId = operationId; |
| 106 | + operation.timestamp = timestamp; |
| 107 | + operation.payload = payload; |
| 108 | + operation.jwtToken = rawOperation.jwtToken; |
| 109 | + operation.jwtTokenAvailable = Promise.resolve(); |
| 110 | + return operation; |
| 111 | + } else { |
| 112 | + throw new Error( |
| 113 | + `Could not find model with id ${model.modelId} of type ${modelName}. Maybe user logged out?`, |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments