@stamhoofd/backend 2.79.8 → 2.80.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,514 +0,0 @@
1
- import { ArrayDecoder, AutoEncoder, EnumDecoder, Field, getOptionalId, isPatchMap, SymbolDecoder } from '@simonbackx/simple-encoding';
2
- import { AuditLogPatchItem, AuditLogPatchItemType, AuditLogReplacement, AuditLogReplacementType, Version } from '@stamhoofd/structures';
3
- import { DataValidator, Formatter } from '@stamhoofd/utility';
4
-
5
- export type PatchExplainer = {
6
- key: string;
7
- handler: (oldValue: unknown, value: unknown) => AuditLogPatchItem[];
8
- };
9
-
10
- function diffEnum(oldValue: unknown, value: unknown, key?: AuditLogReplacement) {
11
- if (oldValue === value) {
12
- return [];
13
- }
14
-
15
- if (value === undefined) {
16
- // Not altered
17
- return [];
18
- }
19
-
20
- return [
21
- AuditLogPatchItem.create({
22
- key,
23
- oldValue: typeof oldValue === 'string' ? AuditLogReplacement.key(oldValue) : undefined,
24
- value: typeof value === 'string' ? AuditLogReplacement.key(value) : undefined,
25
- }).autoType(),
26
- ];
27
- }
28
-
29
- function diffDate(oldValue: unknown, value: unknown, key?: AuditLogReplacement) {
30
- if (!(oldValue instanceof Date) && oldValue !== null) {
31
- return [];
32
- }
33
-
34
- if ((!(value instanceof Date)) && value !== null) {
35
- return [];
36
- }
37
-
38
- if (oldValue?.getTime() === value?.getTime()) {
39
- return [];
40
- }
41
- let dno = oldValue ? Formatter.dateNumber(oldValue, true) : undefined;
42
- let dn = value ? Formatter.dateNumber(value, true) : undefined;
43
-
44
- if (dno && dn && (dno === dn || (Formatter.time(oldValue!) !== Formatter.time(value!))) && key?.toKey() !== 'birthDay') {
45
- // Add time
46
- dno += ' ' + Formatter.time(oldValue!);
47
- dn += ' ' + Formatter.time(value!);
48
- }
49
-
50
- return [
51
- AuditLogPatchItem.create({
52
- key,
53
- oldValue: dno ? AuditLogReplacement.string(dno) : undefined,
54
- value: dn ? AuditLogReplacement.string(dn) : undefined,
55
- }).autoType(),
56
- ];
57
- }
58
-
59
- function getDiffKey(autoEncoder: string): AuditLogReplacement;
60
- function getDiffKey(autoEncoder: unknown): AuditLogReplacement | undefined;
61
- function getDiffKey(autoEncoder: unknown): AuditLogReplacement | undefined {
62
- if (typeof autoEncoder === 'string') {
63
- if (DataValidator.isUuid(autoEncoder)) {
64
- return AuditLogReplacement.uuid(autoEncoder);
65
- }
66
- return AuditLogReplacement.key(autoEncoder);
67
- }
68
- return undefined;
69
- }
70
-
71
- /**
72
- * For arrays or maps, use this name instead of the index/key to identify an object
73
- */
74
- function getDiffName(autoEncoder: unknown): AuditLogReplacement | null {
75
- if (typeof autoEncoder === 'string') {
76
- if (DataValidator.isUuid(autoEncoder)) {
77
- return AuditLogReplacement.uuid(autoEncoder);
78
- }
79
- return AuditLogReplacement.string(autoEncoder);
80
- }
81
-
82
- if (typeof autoEncoder === 'object' && autoEncoder !== null && 'getDiffName' in autoEncoder && typeof autoEncoder.getDiffName === 'function') {
83
- const name = autoEncoder.getDiffName();
84
- if (typeof name === 'string') {
85
- return name ? AuditLogReplacement.string(name) : AuditLogReplacement.key('untitled');
86
- }
87
- if (name instanceof AuditLogReplacement) {
88
- return name;
89
- }
90
- }
91
-
92
- if (typeof autoEncoder === 'object' && autoEncoder !== null && 'name' in autoEncoder && typeof autoEncoder.name === 'string') {
93
- return autoEncoder.name ? AuditLogReplacement.string(autoEncoder.name) : AuditLogReplacement.key('untitled');
94
- }
95
- return null;
96
- }
97
- function getDiffPutValue(autoEncoder: unknown, key?: AuditLogReplacement): AuditLogReplacement | null {
98
- if (typeof autoEncoder === 'object' && autoEncoder !== null && 'getDiffPutName' in autoEncoder && typeof autoEncoder.getDiffPutName === 'function') {
99
- const name = autoEncoder.getDiffPutName();
100
- if (typeof name === 'string') {
101
- return AuditLogReplacement.string(name);
102
- }
103
- if (name instanceof AuditLogReplacement) {
104
- return name;
105
- }
106
- }
107
- return getDiffValue(autoEncoder, key);
108
- }
109
-
110
- function getDiffValue(autoEncoder: unknown, key?: AuditLogReplacement): AuditLogReplacement | null {
111
- if (typeof autoEncoder === 'string') {
112
- if (DataValidator.isUuid(autoEncoder)) {
113
- return AuditLogReplacement.uuid(autoEncoder);
114
- }
115
-
116
- // Is html
117
- if (autoEncoder.startsWith('<!DOCTYPE html>')) {
118
- return AuditLogReplacement.html(autoEncoder);
119
- }
120
-
121
- if (key && key?.lastValue() === 'status') {
122
- // Will be an enum
123
- return AuditLogReplacement.key(autoEncoder);
124
- }
125
- return AuditLogReplacement.string(autoEncoder);
126
- }
127
-
128
- if (typeof autoEncoder === 'symbol') {
129
- const name = Symbol.keyFor(autoEncoder);
130
- if (name) {
131
- return AuditLogReplacement.key(name);
132
- }
133
- return AuditLogReplacement.key('unknown');
134
- }
135
-
136
- if (typeof autoEncoder === 'number') {
137
- const k = key?.lastValue();
138
- if (k && (k.toLowerCase().includes('price') || k.toLowerCase().includes('fee'))) {
139
- return AuditLogReplacement.string(Formatter.price(autoEncoder));
140
- }
141
- return AuditLogReplacement.string(Formatter.integer(autoEncoder));
142
- }
143
-
144
- if (autoEncoder instanceof Date) {
145
- return AuditLogReplacement.string(Formatter.dateTime(autoEncoder, true, true));
146
- }
147
-
148
- if (autoEncoder === true) {
149
- return AuditLogReplacement.key('on');
150
- }
151
-
152
- if (autoEncoder === false) {
153
- return AuditLogReplacement.key('off');
154
- }
155
-
156
- if (typeof autoEncoder === 'object' && autoEncoder !== null && 'getDiffValue' in autoEncoder && typeof autoEncoder.getDiffValue === 'function') {
157
- const name = autoEncoder.getDiffValue();
158
- if (typeof name === 'string') {
159
- return AuditLogReplacement.string(name);
160
- }
161
- if (name instanceof AuditLogReplacement) {
162
- return name;
163
- }
164
- }
165
-
166
- return null;
167
- }
168
-
169
- function findOriginalById(id: unknown, oldArray: unknown[]): unknown | null {
170
- return id ? oldArray.find(v => getId(v) === id) : null;
171
- }
172
-
173
- function findOriginalIndexById(id: unknown, oldArray: unknown[]): number {
174
- return id ? oldArray.findIndex(v => getId(v) === id) : -1;
175
- }
176
-
177
- function getId(object: unknown): string | number | null {
178
- const id = getOptionalId(object);
179
- if (typeof id !== 'string' && typeof id !== 'number') {
180
- if (object instanceof AutoEncoder) {
181
- const encoded = object.encode({ version: Version });
182
- return JSON.stringify(encoded);
183
- }
184
- return JSON.stringify(object);
185
- }
186
- return id;
187
- }
188
-
189
- function findOriginal(put: unknown, oldArray: unknown[]): unknown | null {
190
- return findOriginalById(getId(put), oldArray);
191
- }
192
-
193
- function findIndex(put: unknown, oldArray: unknown[]): number {
194
- return findOriginalIndexById(getId(put), oldArray);
195
- }
196
-
197
- function diffArray(oldValue: unknown, value: unknown, key?: AuditLogReplacement) {
198
- if (!Array.isArray(oldValue) || !Array.isArray(value)) {
199
- // Not supported
200
- return [];
201
- }
202
- const items: AuditLogPatchItem[] = [];
203
-
204
- // Search for puts
205
- let orderChanged = false;
206
- let added = 0;
207
- for (const [index, newItem] of value.entries()) {
208
- const originalIndex = findIndex(newItem, oldValue);
209
-
210
- if (originalIndex === -1) {
211
- // Has been added
212
- items.push(...diffUnknown(
213
- null,
214
- newItem,
215
- (getDiffName(newItem) || AuditLogReplacement.key('item')).prepend(key),
216
- ));
217
- added++;
218
- }
219
- else {
220
- // Has been overwritten
221
- const original = oldValue[originalIndex];
222
- items.push(...diffUnknown(
223
- original,
224
- newItem,
225
- (getDiffName(original) || getDiffName(newItem) || AuditLogReplacement.key('item')).prepend(key),
226
- ));
227
-
228
- if ((index - added) !== originalIndex) {
229
- // Order has changed
230
- orderChanged = true;
231
- }
232
- }
233
- }
234
-
235
- // Search for deletes
236
- for (const original of oldValue) {
237
- const newItem = findOriginal(original, value);
238
- if (!newItem) {
239
- // Has been deleted
240
- items.push(...diffUnknown(
241
- original,
242
- null,
243
- (getDiffName(original) || AuditLogReplacement.key('item')).prepend(key),
244
- ));
245
-
246
- orderChanged = false; // ignore order changed as delete will have set it to true
247
- }
248
- }
249
-
250
- if (orderChanged) {
251
- // Check if order has changed
252
- items.push(
253
- AuditLogPatchItem.create({
254
- key,
255
- type: AuditLogPatchItemType.Reordered,
256
- }),
257
- );
258
- }
259
- // Not supported
260
- return items;
261
- };
262
-
263
- function diffMap(oldValue: unknown, value: unknown, key?: AuditLogReplacement) {
264
- if (!(value instanceof Map)) {
265
- // Not supported
266
- return [];
267
- }
268
- if (!(oldValue instanceof Map) && oldValue !== undefined && oldValue !== null) {
269
- // Not supported
270
- return [];
271
- }
272
-
273
- const items: AuditLogPatchItem[] = [];
274
- const isPatch = isPatchMap(value);
275
-
276
- for (const [k, v] of value.entries()) {
277
- if (typeof k !== 'string') {
278
- // Not supported
279
- continue;
280
- }
281
- const original = oldValue?.get(k);
282
-
283
- if (v === null && isPatch) {
284
- // Delete
285
- if (original) {
286
- items.push(
287
- ...diffUnknown(
288
- null,
289
- original,
290
- (getDiffName(original) || getDiffKey(k)).prepend(key),
291
- ),
292
- );
293
- }
294
- continue;
295
- }
296
-
297
- // Changed
298
- items.push(
299
- ...diffUnknown(
300
- original,
301
- v,
302
- (getDiffName(original) || getDiffName(v) || getDiffKey(k)).prepend(key),
303
- ),
304
- );
305
- }
306
-
307
- if (!isPatch && oldValue) {
308
- // Loop old values
309
- for (const [k, v] of oldValue.entries()) {
310
- if (typeof k !== 'string') {
311
- // Not supported
312
- continue;
313
- }
314
-
315
- if (value.has(k)) {
316
- continue;
317
- }
318
-
319
- items.push(
320
- ...diffUnknown(
321
- null,
322
- v,
323
- (getDiffName(v) || getDiffKey(k)).prepend(key),
324
- ),
325
- );
326
- }
327
- }
328
-
329
- return items;
330
- }
331
-
332
- export function transformValueForDiff(value: unknown) {
333
- if (typeof value === 'object' && value !== null && 'transformForDiff' in value && typeof value.transformForDiff === 'function') {
334
- return value.transformForDiff();
335
- }
336
- return value;
337
- }
338
-
339
- export function diffUnknown(oldValue: unknown, value: unknown, key?: AuditLogReplacement) {
340
- oldValue = transformValueForDiff(oldValue);
341
- value = transformValueForDiff(value);
342
-
343
- if (oldValue === value) {
344
- return [];
345
- }
346
-
347
- if ((oldValue === null || oldValue === undefined) && (value === null || value === undefined)) {
348
- // Both removed
349
- return [];
350
- }
351
-
352
- if (Array.isArray(oldValue) && Array.isArray(value)) {
353
- return diffArray(oldValue, value, key);
354
- }
355
-
356
- if (value instanceof Map && (oldValue instanceof Map || oldValue === null || oldValue === undefined)) {
357
- return diffMap(oldValue, value, key);
358
- }
359
-
360
- if ((value instanceof Date || value === null) && (oldValue instanceof Date || oldValue === null) && (oldValue !== null || value !== null)) {
361
- return diffDate(oldValue, value, key);
362
- }
363
-
364
- if ((oldValue === null || oldValue === undefined) && (value !== null && value !== undefined)) {
365
- // Simplify addition
366
- return [
367
- AuditLogPatchItem.create({
368
- key,
369
- value: getDiffPutValue(value, key) || undefined,
370
- type: AuditLogPatchItemType.Added,
371
- }),
372
- ];
373
- }
374
-
375
- if ((oldValue !== null && oldValue !== undefined) && (value === null || value === undefined)) {
376
- return [
377
- AuditLogPatchItem.create({
378
- key,
379
- oldValue: getDiffPutValue(oldValue, key) || undefined,
380
- type: AuditLogPatchItemType.Removed,
381
- }),
382
- ];
383
- }
384
-
385
- const items = diffObject(oldValue, value, key);
386
- let v = getDiffValue(value, key);
387
- let ov = getDiffValue(oldValue, key);
388
-
389
- if (oldValue !== undefined && oldValue !== null && value !== undefined && value !== null && getDiffValue(value, key) && items.length === 0 && value instanceof AutoEncoder && value.isPatch()) {
390
- return [
391
- AuditLogPatchItem.create({
392
- key,
393
- value: v || undefined,
394
- oldValue: ov || undefined,
395
- type: AuditLogPatchItemType.Changed,
396
- }),
397
- ];
398
- }
399
-
400
- if (v && ov) {
401
- // Simplify change
402
- if (v.toKey() === ov.toKey()) {
403
- v = null;
404
- ov = null;
405
-
406
- // if (items.length === 0) {
407
- // Probably no change
408
- return [];
409
- // }
410
- }
411
-
412
- return [
413
- AuditLogPatchItem.create({
414
- key,
415
- value: v || undefined,
416
- oldValue: ov || undefined,
417
- type: AuditLogPatchItemType.Changed,
418
- }),
419
- ];
420
- }
421
- return items;
422
- };
423
-
424
- /**
425
- * Uses the autoencoder type information to provide a better change handler
426
- */
427
- function diffField(field: Field<any>, oldValue: unknown, value: unknown, key?: AuditLogReplacement): AuditLogPatchItem[] {
428
- if (field.decoder instanceof EnumDecoder) {
429
- return diffEnum(oldValue, value, key);
430
- }
431
-
432
- if (field.decoder instanceof SymbolDecoder) {
433
- if (field.decoder.decoder instanceof EnumDecoder) {
434
- return diffEnum(oldValue, value, key); ;
435
- }
436
- }
437
-
438
- if (field.decoder instanceof ArrayDecoder) {
439
- if (field.decoder.decoder instanceof EnumDecoder) {
440
- // Map values to keys
441
- const items = diffArray(oldValue, value, key);
442
-
443
- for (const item of items) {
444
- if (item.oldValue && !item.oldValue.type) {
445
- item.oldValue.type = AuditLogReplacementType.Key;
446
- }
447
- if (item.value && !item.value.type) {
448
- item.value.type = AuditLogReplacementType.Key;
449
- }
450
-
451
- // If item.key is an array that ends with a 'value', also change it
452
- if (item.key.type === AuditLogReplacementType.Array) {
453
- const lastKeyItem = item.key.values[item.key.values.length - 1];
454
- if (!lastKeyItem.type) {
455
- lastKeyItem.type = AuditLogReplacementType.Key;
456
- }
457
- }
458
- else {
459
- if (!item.key.type) {
460
- item.key.type = AuditLogReplacementType.Key;
461
- }
462
- }
463
- }
464
- return items;
465
- }
466
-
467
- return diffArray(oldValue, value, key);
468
- }
469
-
470
- return diffUnknown(oldValue, value, key);
471
- }
472
-
473
- export function diffObject(original: unknown | null, patch: unknown, rootKey?: AuditLogReplacement): AuditLogPatchItem[] {
474
- if (typeof patch !== 'object' || patch === null) {
475
- return [];
476
- }
477
-
478
- if (original && typeof original !== 'object') {
479
- return [];
480
- }
481
-
482
- const items: AuditLogPatchItem[] = [];
483
-
484
- for (const key in patch) {
485
- if (key === 'id') {
486
- continue;
487
- }
488
-
489
- const oldValue = original?.[key] ?? null;
490
- const value = patch[key];
491
-
492
- if (!(patch instanceof AutoEncoder) && !(oldValue instanceof AutoEncoder)) {
493
- // try manual without type information
494
- items.push(...diffUnknown(oldValue, value, getDiffKey(key).prepend(rootKey)));
495
- continue;
496
- }
497
-
498
- const field = original instanceof AutoEncoder
499
- ? original.static.latestFields.find(f => f.property === key)
500
- : (
501
- patch instanceof AutoEncoder
502
- ? patch.static.latestFields.find(f => f.property === key)
503
- : null
504
- );
505
-
506
- if (!field) {
507
- // Ignore: probably an internal field
508
- continue;
509
- }
510
-
511
- items.push(...diffField(field, oldValue, value, getDiffKey(key).prepend(rootKey)));
512
- }
513
- return items;
514
- }