law-common 10.28.2-beta.2 → 10.28.2-beta.4

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.
@@ -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,19 @@ 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
+ private toEntityModelIndexMap;
29
+ populateRelationsByEntityEnums(entityEnums: EntityEnum[]): EntityModelRelationHelper;
30
+ populateRelationsByEntityEnum(entityEnum: EntityEnum): void;
31
+ populateRelationsByEntityModel(entityModel: BaseEntityModel<EntityEnum | VirtualEntityEnum>): void;
32
+ addEntityModel<T extends EntityEnum | VirtualEntityEnum>(entityModel: BaseEntityModel<T>, config?: {
33
+ populateRelations?: boolean;
34
+ }): void;
35
+ }
@@ -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,67 @@ 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
+ toEntityModelIndexMap() {
157
+ this.entityModelIndexMap = Object.keys(this.entityModelMap).reduce((acc, key) => {
158
+ // @ts-ignore
159
+ acc[key] = this.entityModelMap[key].reduce((innerAcc, entity) => {
160
+ innerAcc[entity.id] = entity;
161
+ return innerAcc;
162
+ }, {});
163
+ return acc;
164
+ }, {});
165
+ return this;
166
+ }
167
+ populateRelationsByEntityEnums(entityEnums) {
168
+ entityEnums.forEach((entityEnum) => {
169
+ this.populateRelationsByEntityEnum(entityEnum);
170
+ });
171
+ return this;
172
+ }
173
+ populateRelationsByEntityEnum(entityEnum) {
174
+ for (const key of Object.keys(this.entityModelIndexMap[entityEnum] || {})) {
175
+ const entityModel = this.entityModelIndexMap[entityEnum][key];
176
+ entityModel.populateRelationsByIndex(this.entityModelIndexMap);
177
+ }
178
+ }
179
+ populateRelationsByEntityModel(entityModel) {
180
+ entityModel.populateRelationsByIndex(this.entityModelIndexMap);
181
+ }
182
+ addEntityModel(entityModel, config = { populateRelations: false }) {
183
+ const entityEnum = entityModel.entityName;
184
+ this.entityModelIndexMap[entityEnum] = this.entityModelIndexMap[entityEnum] || {};
185
+ // @ts-ignore
186
+ this.entityModelIndexMap[entityEnum][entityModel.id] = entityModel;
187
+ if (config.populateRelations) {
188
+ this.populateRelationsByEntityModel(entityModel);
189
+ }
190
+ }
191
+ }
192
+ exports.EntityModelRelationHelper = EntityModelRelationHelper;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "law-common",
3
- "version": "10.28.2-beta.2",
3
+ "version": "10.28.2-beta.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "files": [