law-common 10.28.2-beta.7 → 10.28.2-beta.9

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.
@@ -50,3 +50,5 @@ export * from "./interface/cron-job-manual-trigger.dto.interface";
50
50
  export * from "./interface/cron-job.entity.response";
51
51
  export * from "./interface/address-book.create.dto.interface";
52
52
  export * from "./enums/crud.enum";
53
+ export * from "./interface/api.utils.interface";
54
+ export * from "./interface/address-book.update.dto.interface";
@@ -70,3 +70,5 @@ __exportStar(require("./interface/cron-job.entity.response"), exports);
70
70
  // export * from "./interface/project-user-mapping.entity.api";
71
71
  __exportStar(require("./interface/address-book.create.dto.interface"), exports);
72
72
  __exportStar(require("./enums/crud.enum"), exports);
73
+ __exportStar(require("./interface/api.utils.interface"), exports);
74
+ __exportStar(require("./interface/address-book.update.dto.interface"), exports);
@@ -0,0 +1,5 @@
1
+ import { IAddressBookContactDetail, IAddressBookCreateDto } from "./address-book.create.dto.interface";
2
+ import { DeepPartialButRequired } from "./api.utils.interface";
3
+ export type IAddressBookUpdateDto = DeepPartialButRequired<Omit<IAddressBookCreateDto, 'contactDetails'>, never> & {
4
+ contactDetails?: DeepPartialButRequired<IAddressBookContactDetail, 'id'>[];
5
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // const a: IAddressBookUpdateDto = {
4
+ // organizationName: "Test Name",
5
+ // contactDetails: [
6
+ // {
7
+ // id: 1,
8
+ // email: "test@example.com",
9
+ // phone: "123-456-7890"
10
+ // }
11
+ // ]
12
+ // };
13
+ // const b: IAddressBookUpdateDto = {
14
+ // organizationName: "Test Name",
15
+ // address: {
16
+ // city: "Sample City"
17
+ // }
18
+ // };
@@ -0,0 +1,4 @@
1
+ export type DeepPartial<T> = T extends object ? {
2
+ [P in keyof T]?: T[P] extends Array<infer U> ? Array<DeepPartial<U>> : T[P] extends object ? DeepPartial<T[P]> : T[P];
3
+ } : T;
4
+ export type DeepPartialButRequired<T, K extends keyof T> = DeepPartial<T> & Required<Pick<T, K>>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,25 @@
1
+ export type ArrayComparisonCategory<T> = {
2
+ unchanged: T[];
3
+ added: T[];
4
+ updated: T[];
5
+ deleted: {
6
+ [x: string]: T[keyof T];
7
+ }[];
8
+ notFound: {
9
+ [x: string]: T[keyof T];
10
+ }[];
11
+ };
12
+ export declare class ArrayComparisonCategorizer<T> {
13
+ private incomingArray;
14
+ private existingArray;
15
+ private keyProperty;
16
+ private arrayComparisonCategory;
17
+ constructor(incomingArray: T[] | undefined, existingArray: T[] | undefined, keyProperty: keyof T);
18
+ private get existingMap();
19
+ private isExistingItem;
20
+ private isMarkForDeletion;
21
+ private isIdentifierPresent;
22
+ compare(): ArrayComparisonCategory<T>;
23
+ get category(): ArrayComparisonCategory<T> | null;
24
+ merge(): T[];
25
+ }
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayComparisonCategorizer = void 0;
4
+ function deepEqual(a, b) {
5
+ if (a === b)
6
+ return true;
7
+ if (a == null || b == null)
8
+ return false;
9
+ if (typeof a !== typeof b)
10
+ return false;
11
+ if (typeof a !== "object")
12
+ return a === b;
13
+ if (Array.isArray(a) !== Array.isArray(b))
14
+ return false;
15
+ if (Array.isArray(a)) {
16
+ if (a.length !== b.length)
17
+ return false;
18
+ return a.every((v, i) => deepEqual(v, b[i]));
19
+ }
20
+ const keysA = Object.keys(a).sort();
21
+ const keysB = Object.keys(b).sort();
22
+ if (keysA.length !== keysB.length)
23
+ return false;
24
+ return keysA.every((key) => deepEqual(a[key], b[key]));
25
+ }
26
+ class ArrayComparisonCategorizer {
27
+ constructor(incomingArray = [], existingArray = [], keyProperty) {
28
+ this.arrayComparisonCategory = null;
29
+ this.incomingArray = [...incomingArray];
30
+ this.existingArray = [...existingArray];
31
+ this.keyProperty = keyProperty;
32
+ }
33
+ get existingMap() {
34
+ return new Map(this.existingArray.filter((item) => item[this.keyProperty] !== undefined).map((item) => [item[this.keyProperty], item]));
35
+ }
36
+ isExistingItem(incomingItem, existingMap) {
37
+ return existingMap.has(incomingItem[this.keyProperty]);
38
+ }
39
+ isMarkForDeletion(incomingItem) {
40
+ const onlyIdentifierPresent = Object.keys(incomingItem).length === 1;
41
+ return this.isIdentifierPresent(incomingItem) && onlyIdentifierPresent;
42
+ }
43
+ isIdentifierPresent(incomingItem) {
44
+ return incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
45
+ }
46
+ compare() {
47
+ const existingMap = this.existingMap;
48
+ const unchanged = [];
49
+ const added = [];
50
+ const updated = [];
51
+ const deleted = [];
52
+ const notFound = [];
53
+ for (const incomingItem of this.incomingArray) {
54
+ const isIdentifierPresent = this.isIdentifierPresent(incomingItem);
55
+ const isMarkForDeletion = isIdentifierPresent && this.isMarkForDeletion(incomingItem);
56
+ const isExistingItem = isIdentifierPresent && this.isExistingItem(incomingItem, existingMap);
57
+ const existingItem = isExistingItem ? existingMap.get(incomingItem[this.keyProperty]) : null;
58
+ if (isMarkForDeletion) {
59
+ deleted.push({ [this.keyProperty]: incomingItem[this.keyProperty] });
60
+ }
61
+ else if (!isIdentifierPresent && !isExistingItem) {
62
+ added.push(incomingItem);
63
+ }
64
+ else if (isExistingItem && isIdentifierPresent) {
65
+ if (deepEqual(incomingItem, existingItem)) {
66
+ unchanged.push(incomingItem);
67
+ }
68
+ else {
69
+ updated.push(incomingItem);
70
+ }
71
+ }
72
+ else {
73
+ notFound.push({ [this.keyProperty]: incomingItem[this.keyProperty] });
74
+ }
75
+ }
76
+ const result = {
77
+ unchanged,
78
+ added,
79
+ updated,
80
+ deleted,
81
+ notFound
82
+ };
83
+ this.arrayComparisonCategory = result;
84
+ return result;
85
+ }
86
+ get category() {
87
+ return this.arrayComparisonCategory;
88
+ }
89
+ merge() {
90
+ if (!this.arrayComparisonCategory) {
91
+ throw new Error("ArrayComparisonCategory is not computed yet. Call compare() first.");
92
+ }
93
+ const changes = this.arrayComparisonCategory;
94
+ let newState = this.existingArray.filter(this.isIdentifierPresent.bind(this));
95
+ // Remove deleted items
96
+ const deletedIds = new Set(changes.deleted.map((d) => d[this.keyProperty]));
97
+ newState = newState.filter((item) => !deletedIds.has(item[this.keyProperty]));
98
+ // Update items
99
+ for (let i = 0; i < newState.length; i++) {
100
+ const item = newState[i];
101
+ const updatedItem = changes.updated.find((u) => u[this.keyProperty] === item[this.keyProperty]);
102
+ if (updatedItem) {
103
+ newState[i] = updatedItem;
104
+ }
105
+ }
106
+ // Get Max Id
107
+ const existingWithId = this.existingArray.filter(this.isIdentifierPresent.bind(this));
108
+ const originalMaxId = existingWithId.length > 0
109
+ ? Math.max(...existingWithId.map((item) => item[this.keyProperty]))
110
+ : 0;
111
+ // Add new items with new identifier
112
+ let currentMaxId = originalMaxId;
113
+ for (const item of changes.added) {
114
+ const id = ++currentMaxId;
115
+ newState.push(Object.assign(Object.assign({}, item), { [this.keyProperty]: id }));
116
+ }
117
+ return newState;
118
+ }
119
+ }
120
+ exports.ArrayComparisonCategorizer = ArrayComparisonCategorizer;
121
+ // function categorizeChanges<T extends Item>(
122
+ // incoming: T[],
123
+ // existing: T[]
124
+ // ): {
125
+ // unchanged: T[];
126
+ // added: T[];
127
+ // updated: T[];
128
+ // deleted: { id: number }[];
129
+ // notFound: { id: number }[];
130
+ // } {
131
+ // console.log("Categorizing changes between incoming and existing items");
132
+ // console.log("Incoming items:", incoming);
133
+ // console.log("Existing items:", existing);
134
+ // const existingMap = new Map<number, T>(existing.filter((item) => item.id !== undefined).map((item) => [item.id!, item]));
135
+ // const incomingIds = new Set<number>(incoming.filter((item) => item.id !== undefined).map((item) => item.id!));
136
+ // const deleted: { id: number }[] = [];
137
+ // const notFound: { id: number }[] = [];
138
+ // const unchanged: T[] = [];
139
+ // const updated: T[] = [];
140
+ // const added: T[] = [];
141
+ // for (const item of incoming) {
142
+ // if (item.id === undefined) {
143
+ // added.push(item);
144
+ // continue;
145
+ // }
146
+ // const id = item.id;
147
+ // if (Object.keys(item).length === 1) {
148
+ // deleted.push({ id });
149
+ // continue;
150
+ // }
151
+ // if (!existingMap.has(id)) {
152
+ // added.push(item);
153
+ // continue;
154
+ // }
155
+ // const existingItem = existingMap.get(id)!;
156
+ // if (deepEqual(item, existingItem)) {
157
+ // unchanged.push(item);
158
+ // } else {
159
+ // updated.push(item);
160
+ // }
161
+ // }
162
+ // for (const [id] of existingMap) {
163
+ // if (!incomingIds.has(id)) {
164
+ // notFound.push({ id });
165
+ // }
166
+ // }
167
+ // return { unchanged, added, updated, deleted, notFound };
168
+ // }
@@ -3,3 +3,4 @@ export * from "./helper.fn.util";
3
3
  export * from "./models/date-code.model.util";
4
4
  export * from "./string.util";
5
5
  export * from "./entity.flow.util";
6
+ export * from "./array.util";
@@ -19,3 +19,4 @@ __exportStar(require("./helper.fn.util"), exports);
19
19
  __exportStar(require("./models/date-code.model.util"), exports);
20
20
  __exportStar(require("./string.util"), exports);
21
21
  __exportStar(require("./entity.flow.util"), exports);
22
+ __exportStar(require("./array.util"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "law-common",
3
- "version": "10.28.2-beta.7",
3
+ "version": "10.28.2-beta.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "files": [