|
| 1 | +import mergeWith from 'lodash.mergewith'; |
| 2 | +import cloneDeep from 'lodash.clonedeep'; |
| 3 | +import { patch } from '@n1ru4l/json-patch-plus'; |
| 4 | +import { GroupedEventDBScheme, RepetitionDBScheme } from '@hawk.so/types'; |
| 5 | + |
| 6 | +/** |
| 7 | + * One of the features of the events is that their repetition is the difference |
| 8 | + * between the original, which greatly optimizes storage. So we need to restore |
| 9 | + * the original repetition payload using the very first event and its difference |
| 10 | + * between its repetition |
| 11 | + * |
| 12 | + * @deprecated remove after 6 september 2025 |
| 13 | + * @param originalEvent - the very first event we received |
| 14 | + * @param repetition - the difference with its repetition, for the repetition we want to display |
| 15 | + * @returns fully assembled payload of the current repetition |
| 16 | + */ |
| 17 | +export function repetitionAssembler(originalEvent: GroupedEventDBScheme['payload'], repetition: GroupedEventDBScheme['payload']): GroupedEventDBScheme['payload'] { |
| 18 | + const customizer = (originalParam: any, repetitionParam: any): any => { |
| 19 | + if (repetitionParam === null) { |
| 20 | + return originalParam; |
| 21 | + } |
| 22 | + |
| 23 | + if (typeof repetitionParam === 'object' && typeof originalParam === 'object') { |
| 24 | + /** |
| 25 | + * If original event has null but repetition has some value, we need to return repetition value |
| 26 | + */ |
| 27 | + if (originalParam === null) { |
| 28 | + return repetitionParam; |
| 29 | + /** |
| 30 | + * Otherwise, we need to recursively merge original and repetition values |
| 31 | + */ |
| 32 | + } else { |
| 33 | + return repetitionAssembler(originalParam, repetitionParam); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return repetitionParam; |
| 38 | + }; |
| 39 | + |
| 40 | + return mergeWith(cloneDeep(originalEvent), cloneDeep(repetition), customizer); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Parse addons and context fields from string to object, in db it stores as string |
| 45 | + * |
| 46 | + * @param payload - the payload of the event |
| 47 | + * @param field - the field to parse, can be 'addons' or 'context' |
| 48 | + * @returns the payload with parsed field |
| 49 | + */ |
| 50 | +function parsePayloadField(payload: GroupedEventDBScheme['payload'], field: 'addons' | 'context') { |
| 51 | + if (payload && payload[field] && typeof payload[field] === 'string') { |
| 52 | + payload[field] = JSON.parse(payload[field] as string); |
| 53 | + } |
| 54 | + |
| 55 | + return payload; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Stringify addons and context fields from object to string, in db it stores as string |
| 60 | + * |
| 61 | + * @param payload - the payload of the event |
| 62 | + * @param field - the field to stringify, can be 'addons' or 'context' |
| 63 | + * @returns the payload with stringified field |
| 64 | + */ |
| 65 | +function stringifyPayloadField(payload: GroupedEventDBScheme['payload'], field: 'addons' | 'context') { |
| 66 | + if (payload && payload[field]) { |
| 67 | + payload[field] = JSON.stringify(payload[field]); |
| 68 | + } |
| 69 | + |
| 70 | + return payload; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Helps to merge original event and repetition due to delta format, |
| 75 | + * in case of old delta format, we need to patch the payload |
| 76 | + * in case of new delta format, we need to assemble the payload |
| 77 | + * |
| 78 | + * @param originalEvent {HawkEvent} - The original event |
| 79 | + * @param repetition {HawkEventRepetition} - The repetition to process |
| 80 | + * @returns {HawkEvent} Updated event with processed repetition payload |
| 81 | + */ |
| 82 | +export function composeFullRepetitionEvent(originalEvent: GroupedEventDBScheme, repetition: RepetitionDBScheme | undefined): GroupedEventDBScheme { |
| 83 | + /** |
| 84 | + * Make a deep copy of the original event, because we need to avoid mutating the original event |
| 85 | + */ |
| 86 | + const event = cloneDeep(originalEvent); |
| 87 | + |
| 88 | + if (!repetition) { |
| 89 | + return event; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * New delta format (repetition.delta is not null) |
| 94 | + */ |
| 95 | + if (repetition.delta) { |
| 96 | + /** |
| 97 | + * Parse addons and context fields from string to object before patching |
| 98 | + */ |
| 99 | + event.payload = parsePayloadField(event.payload, 'addons'); |
| 100 | + event.payload = parsePayloadField(event.payload, 'context'); |
| 101 | + |
| 102 | + event.payload = patch({ |
| 103 | + left: event.payload, |
| 104 | + delta: JSON.parse(repetition.delta), |
| 105 | + }); |
| 106 | + |
| 107 | + /** |
| 108 | + * Stringify addons and context fields from object to string after patching |
| 109 | + */ |
| 110 | + event.payload = stringifyPayloadField(event.payload, 'addons'); |
| 111 | + event.payload = stringifyPayloadField(event.payload, 'context'); |
| 112 | + |
| 113 | + return event; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * New delta format (repetition.payload is null) and repetition.delta is null (there is no delta between original and repetition) |
| 118 | + */ |
| 119 | + if (!repetition.payload) { |
| 120 | + return event; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Old delta format (repetition.payload is not null) |
| 125 | + * @todo remove after 6 september 2025 |
| 126 | + */ |
| 127 | + event.payload = repetitionAssembler(event.payload, repetition.payload); |
| 128 | + |
| 129 | + return event; |
| 130 | +} |
0 commit comments