law-common 10.28.2-beta.0 → 10.28.2-beta.10

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.
@@ -0,0 +1,7 @@
1
+ export declare enum CRUDEnum {
2
+ CREATE = "CREATE",
3
+ READ = "READ",
4
+ UPDATE = "UPDATE",
5
+ DELETE = "DELETE",
6
+ UNCHANGED = "UNCHANGED"
7
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CRUDEnum = void 0;
4
+ var CRUDEnum;
5
+ (function (CRUDEnum) {
6
+ CRUDEnum["CREATE"] = "CREATE";
7
+ CRUDEnum["READ"] = "READ";
8
+ CRUDEnum["UPDATE"] = "UPDATE";
9
+ CRUDEnum["DELETE"] = "DELETE";
10
+ CRUDEnum["UNCHANGED"] = "UNCHANGED";
11
+ })(CRUDEnum || (exports.CRUDEnum = CRUDEnum = {}));
@@ -49,3 +49,6 @@ export * from "./interface/work.from.home.update.interface";
49
49
  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
+ export * from "./enums/crud.enum";
53
+ export * from "./interface/api.utils.interface";
54
+ export * from "./interface/address-book.update.dto.interface";
@@ -69,3 +69,6 @@ __exportStar(require("./interface/cron-job.entity.response"), exports);
69
69
  // export * from "./interface/billing-reimbursement-expense-history.api";
70
70
  // export * from "./interface/project-user-mapping.entity.api";
71
71
  __exportStar(require("./interface/address-book.create.dto.interface"), exports);
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);
@@ -1,10 +1,12 @@
1
1
  import { IAddressBookEntity, IEntityCreateDto } from "../../entities";
2
+ import { CRUDEnum } from "../enums/crud.enum";
2
3
  export type IAddressBookCreateExclude = "contactDetails" | "address" | "organizationId";
3
4
  export interface IAddressBookContactDetail {
4
5
  id?: number;
5
6
  name: string;
6
7
  email?: string;
7
8
  phone?: string;
9
+ operation: CRUDEnum;
8
10
  }
9
11
  export interface IAddressBookAddress {
10
12
  addressLine1?: string;
@@ -0,0 +1,3 @@
1
+ import { IAddressBookCreateDto } from "./address-book.create.dto.interface";
2
+ import { DeepPartialButRequired } from "./api.utils.interface";
3
+ export type IAddressBookUpdateDto = DeepPartialButRequired<IAddressBookCreateDto, never>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // export type IAddressBookUpdateDto = DeepPartialButRequired<
4
+ // Omit<IAddressBookCreateDto, 'contactDetails'>,
5
+ // never
6
+ // > & {
7
+ // contactDetails?: DeepPartialButRequired<IAddressBookContactDetail, 'id'>[]; // id required per item
8
+ // // address?: DeepPartial<IAddressBookAddress>;
9
+ // };
10
+ // const a: IAddressBookUpdateDto = {
11
+ // organizationName: "Test Name",
12
+ // contactDetails: [
13
+ // {
14
+ // id: 1,
15
+ // email: "test@example.com",
16
+ // phone: "123-456-7890"
17
+ // }
18
+ // ]
19
+ // };
20
+ // const b: IAddressBookUpdateDto = {
21
+ // organizationName: "Test Name",
22
+ // address: {
23
+ // city: "Sample City"
24
+ // }
25
+ // };
@@ -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,6 +1,8 @@
1
+ import { RelationType } from "../enums/relation-type.enum";
1
2
  import { IAddressBookEntity } from "../interface/address-book.entity.interface";
2
3
  import { EntityEnum } from "../interface/entity.utils.interface";
3
4
  import { BaseEntityModel } from "./base.entity.model";
5
+ import { UserEntityModel } from "./user.entity.model";
4
6
  export declare class AddressBookEntityModel extends BaseEntityModel<EntityEnum.ADDRESS_BOOK> implements IAddressBookEntity {
5
7
  id: number;
6
8
  organizationName: string;
@@ -14,7 +16,16 @@ export declare class AddressBookEntityModel extends BaseEntityModel<EntityEnum.A
14
16
  updatedOn: number;
15
17
  createdBy: number;
16
18
  updatedBy: number;
19
+ introducedByUser?: UserEntityModel | undefined;
17
20
  static fromEntity(entity: IAddressBookEntity): AddressBookEntityModel;
18
- static relationConfigs: never[];
21
+ static relationConfigs: {
22
+ name: EntityEnum;
23
+ relation: RelationType;
24
+ key: string;
25
+ mapKeyConfig: {
26
+ relationKey: string;
27
+ key: string;
28
+ };
29
+ }[];
19
30
  getRelationConfigs(): any[];
20
31
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AddressBookEntityModel = void 0;
4
+ const relation_type_enum_1 = require("../enums/relation-type.enum");
4
5
  const entity_utils_interface_1 = require("../interface/entity.utils.interface");
5
6
  const base_entity_model_1 = require("./base.entity.model");
6
7
  class AddressBookEntityModel extends base_entity_model_1.BaseEntityModel {
@@ -29,4 +30,14 @@ class AddressBookEntityModel extends base_entity_model_1.BaseEntityModel {
29
30
  }
30
31
  }
31
32
  exports.AddressBookEntityModel = AddressBookEntityModel;
32
- AddressBookEntityModel.relationConfigs = [];
33
+ AddressBookEntityModel.relationConfigs = [
34
+ {
35
+ name: entity_utils_interface_1.EntityEnum.USER,
36
+ relation: relation_type_enum_1.RelationType.ONE,
37
+ key: "introducedByUser",
38
+ mapKeyConfig: {
39
+ relationKey: "id",
40
+ key: "introducedBy",
41
+ },
42
+ }
43
+ ];
@@ -4,7 +4,7 @@ export declare enum RelationType {
4
4
  MANY = "many"
5
5
  }
6
6
  export declare abstract class BaseEntityModel<T extends EntityEnum | VirtualEntityEnum> {
7
- protected entityName: T;
7
+ readonly entityName: T;
8
8
  constructor(entityName: T);
9
9
  abstract getRelationConfigs(): any[];
10
10
  populateRelationsByIndex(entityIndexMap: EntityIndexMap): void;
@@ -1,4 +1,5 @@
1
1
  import { EntityEnum, EntityIndexMap, EntityMap, EnumEntityType, EnumToModel, IApiEntity, IBaseEntityApiResponse, VirtualEntityEnum } from "../interface/entity.utils.interface";
2
+ import { BaseEntityModel } from "./base.entity.model";
2
3
  export declare function mapToIndex(entityMap: EntityMap): EntityIndexMap;
3
4
  export declare function getEntityIndexMap<T extends EntityEnum>(data: IBaseEntityApiResponse<EnumEntityType<T>>, baseEntity: T, reusedConfig?: {
4
5
  existingEntityIndexMap: EntityIndexMap;
@@ -16,3 +17,23 @@ export declare function parseEntitiesWithoutModels<T extends EnumEntityType<Enti
16
17
  }): {
17
18
  [E in EntityEnum | VirtualEntityEnum]?: IApiEntity<EnumEntityType<E>>[];
18
19
  };
20
+ export declare function removeEntityById(entityIndexMap: EntityIndexMap, entity: EntityEnum, id: number): void;
21
+ export declare class EntityModelRelationHelper {
22
+ entityMap: EntityMap;
23
+ entityModelMap: EntityMap;
24
+ entityModelIndexMap: EntityIndexMap;
25
+ constructor(entityMap: EntityMap);
26
+ private init;
27
+ private toEntityModelMap;
28
+ fromEntityToEntityModel<T extends EntityEnum | VirtualEntityEnum>(entity: EnumEntityType<T>, entityEnum: T): BaseEntityModel<T>;
29
+ private toEntityModelIndexMap;
30
+ populateRelationsByEntityEnums(entityEnums: EntityEnum[]): EntityModelRelationHelper;
31
+ populateRelationsByEntityEnum(entityEnum: EntityEnum): void;
32
+ populateRelationsByEntityModel(entityModel: BaseEntityModel<EntityEnum | VirtualEntityEnum>): void;
33
+ addEntityModel<T extends EntityEnum | VirtualEntityEnum>(entityModel: BaseEntityModel<T>, config?: {
34
+ populateRelations?: boolean;
35
+ }): void;
36
+ addEntity<T extends EntityEnum | VirtualEntityEnum>(entity: EnumEntityType<T>, entityEnum: T, config?: {
37
+ populateRelations?: boolean;
38
+ }): void;
39
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.entityEnumToEntityModel = void 0;
3
+ exports.EntityModelRelationHelper = exports.entityEnumToEntityModel = void 0;
4
4
  exports.mapToIndex = mapToIndex;
5
5
  exports.getEntityIndexMap = getEntityIndexMap;
6
6
  exports.populateRelationsFor = populateRelationsFor;
@@ -8,6 +8,7 @@ exports.entityMapToModels = entityMapToModels;
8
8
  exports.parseEntities = parseEntities;
9
9
  exports.entityMapToEntityIndexMap = entityMapToEntityIndexMap;
10
10
  exports.parseEntitiesWithoutModels = parseEntitiesWithoutModels;
11
+ exports.removeEntityById = removeEntityById;
11
12
  const entity_utils_interface_1 = require("../interface/entity.utils.interface");
12
13
  const bank_entity_model_1 = require("./bank.entity.model");
13
14
  const base_entity_model_1 = require("./base.entity.model");
@@ -125,3 +126,79 @@ function parseEntitiesWithoutModels(json, baseEntity, entityMap = {}) {
125
126
  }
126
127
  return entityMap;
127
128
  }
129
+ function removeEntityById(entityIndexMap, entity, id) {
130
+ if (entityIndexMap[entity]) {
131
+ delete entityIndexMap[entity][id];
132
+ }
133
+ }
134
+ class EntityModelRelationHelper {
135
+ constructor(entityMap) {
136
+ this.entityMap = entityMap;
137
+ this.entityModelMap = {};
138
+ this.entityModelIndexMap = {};
139
+ this.init();
140
+ }
141
+ init() {
142
+ this.toEntityModelMap();
143
+ this.toEntityModelIndexMap();
144
+ }
145
+ toEntityModelMap() {
146
+ for (const entityName in this.entityMap) {
147
+ if (!(entityName in exports.entityEnumToEntityModel)) {
148
+ throw new Error(`Unknown entity: ${entityName}`);
149
+ }
150
+ this.entityModelMap[entityName] = this.entityMap[entityName].map((entity) =>
151
+ // @ts-ignore
152
+ exports.entityEnumToEntityModel[entityName](entity));
153
+ }
154
+ return this;
155
+ }
156
+ fromEntityToEntityModel(entity, entityEnum) {
157
+ if (!(entityEnum in exports.entityEnumToEntityModel)) {
158
+ throw new Error(`Unknown entity: ${entityEnum}`);
159
+ }
160
+ return exports.entityEnumToEntityModel[entityEnum](entity);
161
+ }
162
+ toEntityModelIndexMap() {
163
+ this.entityModelIndexMap = Object.keys(this.entityModelMap).reduce((acc, key) => {
164
+ // @ts-ignore
165
+ acc[key] = this.entityModelMap[key].reduce((innerAcc, entity) => {
166
+ innerAcc[entity.id] = entity;
167
+ return innerAcc;
168
+ }, {});
169
+ return acc;
170
+ }, {});
171
+ return this;
172
+ }
173
+ populateRelationsByEntityEnums(entityEnums) {
174
+ entityEnums.forEach((entityEnum) => {
175
+ this.populateRelationsByEntityEnum(entityEnum);
176
+ });
177
+ return this;
178
+ }
179
+ populateRelationsByEntityEnum(entityEnum) {
180
+ for (const key of Object.keys(this.entityModelIndexMap[entityEnum] || {})) {
181
+ const entityModel = this.entityModelIndexMap[entityEnum][key];
182
+ entityModel.populateRelationsByIndex(this.entityModelIndexMap);
183
+ }
184
+ }
185
+ populateRelationsByEntityModel(entityModel) {
186
+ entityModel.populateRelationsByIndex(this.entityModelIndexMap);
187
+ }
188
+ addEntityModel(entityModel, config = { populateRelations: false }) {
189
+ const entityEnum = entityModel.entityName;
190
+ this.entityModelIndexMap[entityEnum] = this.entityModelIndexMap[entityEnum] || {};
191
+ // @ts-ignore
192
+ this.entityModelIndexMap[entityEnum][entityModel.id] = entityModel;
193
+ if (config.populateRelations) {
194
+ this.populateRelationsByEntityModel(entityModel);
195
+ }
196
+ }
197
+ addEntity(entity, entityEnum, config = { populateRelations: false }) {
198
+ if (!(entityEnum in exports.entityEnumToEntityModel)) {
199
+ throw new Error(`Unknown entity: ${entityEnum}`);
200
+ }
201
+ this.addEntityModel(this.fromEntityToEntityModel(entity, entityEnum), config);
202
+ }
203
+ }
204
+ exports.EntityModelRelationHelper = EntityModelRelationHelper;
@@ -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.0",
3
+ "version": "10.28.2-beta.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "files": [