law-common 10.28.2-beta.8 → 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 });
@@ -1,21 +1,25 @@
1
- export declare class ArrayChangeTracker<T> {
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> {
2
13
  private incomingArray;
3
14
  private existingArray;
4
15
  private keyProperty;
5
- private unchanged;
6
- private added;
7
- private updated;
8
- private deleted;
9
- private notFound;
16
+ private arrayComparisonCategory;
10
17
  constructor(incomingArray: T[] | undefined, existingArray: T[] | undefined, keyProperty: keyof T);
11
18
  private get existingMap();
12
- unchangedItems(): T[];
13
- addedItems(): T[];
14
- updatedItems(): T[];
15
- deletedItems(): {
16
- id: T[keyof T];
17
- }[];
18
- notFoundItems(): {
19
- id: T[keyof T];
20
- }[];
19
+ private isExistingItem;
20
+ private isMarkForDeletion;
21
+ private isIdentifierPresent;
22
+ compare(): ArrayComparisonCategory<T>;
23
+ get category(): ArrayComparisonCategory<T> | null;
24
+ merge(): T[];
21
25
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ArrayChangeTracker = void 0;
3
+ exports.ArrayComparisonCategorizer = void 0;
4
4
  function deepEqual(a, b) {
5
5
  if (a === b)
6
6
  return true;
@@ -23,53 +23,9 @@ function deepEqual(a, b) {
23
23
  return false;
24
24
  return keysA.every((key) => deepEqual(a[key], b[key]));
25
25
  }
26
- function categorizeChanges(incoming, existing) {
27
- console.log("Categorizing changes between incoming and existing items");
28
- console.log("Incoming items:", incoming);
29
- console.log("Existing items:", existing);
30
- const existingMap = new Map(existing.filter((item) => item.id !== undefined).map((item) => [item.id, item]));
31
- const incomingIds = new Set(incoming.filter((item) => item.id !== undefined).map((item) => item.id));
32
- const deleted = [];
33
- const notFound = [];
34
- const unchanged = [];
35
- const updated = [];
36
- const added = [];
37
- for (const item of incoming) {
38
- if (item.id === undefined) {
39
- added.push(item);
40
- continue;
41
- }
42
- const id = item.id;
43
- if (Object.keys(item).length === 1) {
44
- deleted.push({ id });
45
- continue;
46
- }
47
- if (!existingMap.has(id)) {
48
- added.push(item);
49
- continue;
50
- }
51
- const existingItem = existingMap.get(id);
52
- if (deepEqual(item, existingItem)) {
53
- unchanged.push(item);
54
- }
55
- else {
56
- updated.push(item);
57
- }
58
- }
59
- for (const [id] of existingMap) {
60
- if (!incomingIds.has(id)) {
61
- notFound.push({ id });
62
- }
63
- }
64
- return { unchanged, added, updated, deleted, notFound };
65
- }
66
- class ArrayChangeTracker {
26
+ class ArrayComparisonCategorizer {
67
27
  constructor(incomingArray = [], existingArray = [], keyProperty) {
68
- this.unchanged = [];
69
- this.added = [];
70
- this.updated = [];
71
- this.deleted = [];
72
- this.notFound = [];
28
+ this.arrayComparisonCategory = null;
73
29
  this.incomingArray = [...incomingArray];
74
30
  this.existingArray = [...existingArray];
75
31
  this.keyProperty = keyProperty;
@@ -77,72 +33,136 @@ class ArrayChangeTracker {
77
33
  get existingMap() {
78
34
  return new Map(this.existingArray.filter((item) => item[this.keyProperty] !== undefined).map((item) => [item[this.keyProperty], item]));
79
35
  }
80
- unchangedItems() {
81
- const existingMap = this.existingMap;
82
- this.unchanged = [];
83
- for (const incomingItem of this.incomingArray) {
84
- const isIdentifierPresent = incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
85
- const onlyIdentifierPresent = Object.keys(incomingItem).length === 1;
86
- const isExistingItem = existingMap.has(incomingItem[this.keyProperty]);
87
- if (!isIdentifierPresent || !isExistingItem || onlyIdentifierPresent) {
88
- continue;
89
- }
90
- const id = incomingItem[this.keyProperty];
91
- const existingItem = existingMap.get(id);
92
- if (deepEqual(incomingItem, existingItem)) {
93
- this.unchanged.push(incomingItem);
94
- }
95
- }
96
- return this.unchanged;
36
+ isExistingItem(incomingItem, existingMap) {
37
+ return existingMap.has(incomingItem[this.keyProperty]);
97
38
  }
98
- addedItems() {
99
- this.added = [];
100
- const existingMap = this.existingMap;
101
- for (const incomingItem of this.incomingArray) {
102
- if (!existingMap.has(incomingItem[this.keyProperty])) {
103
- this.added.push(incomingItem);
104
- continue;
105
- }
106
- }
107
- return this.added;
39
+ isMarkForDeletion(incomingItem) {
40
+ const onlyIdentifierPresent = Object.keys(incomingItem).length === 1;
41
+ return this.isIdentifierPresent(incomingItem) && onlyIdentifierPresent;
108
42
  }
109
- updatedItems() {
110
- this.updated = [];
43
+ isIdentifierPresent(incomingItem) {
44
+ return incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
45
+ }
46
+ compare() {
111
47
  const existingMap = this.existingMap;
48
+ const unchanged = [];
49
+ const added = [];
50
+ const updated = [];
51
+ const deleted = [];
52
+ const notFound = [];
112
53
  for (const incomingItem of this.incomingArray) {
113
- const isExistingItem = existingMap.has(incomingItem[this.keyProperty]);
114
- if (!isExistingItem) {
115
- continue;
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] });
116
60
  }
117
- const id = incomingItem[this.keyProperty];
118
- const existingItem = existingMap.get(id);
119
- if (!deepEqual(incomingItem, existingItem)) {
120
- this.updated.push(incomingItem);
61
+ else if (!isIdentifierPresent && !isExistingItem) {
62
+ added.push(incomingItem);
121
63
  }
122
- }
123
- return this.updated;
124
- }
125
- deletedItems() {
126
- this.deleted = [];
127
- for (const incomingItem of this.incomingArray) {
128
- const isIdentifierPresent = incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
129
- const onlyIdentifierPresent = Object.keys(incomingItem).length === 1;
130
- if (isIdentifierPresent && onlyIdentifierPresent) {
131
- this.deleted.push({ id: incomingItem[this.keyProperty] });
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] });
132
74
  }
133
75
  }
134
- return this.deleted;
76
+ const result = {
77
+ unchanged,
78
+ added,
79
+ updated,
80
+ deleted,
81
+ notFound
82
+ };
83
+ this.arrayComparisonCategory = result;
84
+ return result;
135
85
  }
136
- notFoundItems() {
137
- this.notFound = [];
138
- const existingMap = this.existingMap;
139
- for (const incomingItem of this.existingArray) {
140
- const isIdentifierPresent = incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
141
- if (isIdentifierPresent && !existingMap.has(incomingItem[this.keyProperty])) {
142
- this.notFound.push({ id: incomingItem[this.keyProperty] });
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;
143
104
  }
144
105
  }
145
- return this.notFound;
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;
146
118
  }
147
119
  }
148
- exports.ArrayChangeTracker = ArrayChangeTracker;
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
+ // }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "law-common",
3
- "version": "10.28.2-beta.8",
3
+ "version": "10.28.2-beta.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "files": [