law-common 10.21.1-beta.1 → 10.21.1-beta.3

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.
@@ -14,9 +14,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.ConvertToCommonEntity = ConvertToCommonEntity;
15
15
  exports.ConvertApiToEntity = ConvertApiToEntity;
16
16
  function ConvertToCommonEntity(obj) {
17
- var _a, _b;
18
17
  const { createdOn: _, updatedOn: __ } = obj, objectToAssign = __rest(obj, ["createdOn", "updatedOn"]);
19
- return Object.assign({ createdOn: (_a = obj.createdOn) === null || _a === void 0 ? void 0 : _a.toISOString(), updatedOn: (_b = obj.updatedOn) === null || _b === void 0 ? void 0 : _b.toISOString() }, objectToAssign);
18
+ const dateTransformerObj = {};
19
+ if (obj.createdOn) {
20
+ dateTransformerObj["createdOn"] = obj.createdOn.toISOString();
21
+ }
22
+ if (obj.updatedOn) {
23
+ dateTransformerObj["updatedOn"] = obj.updatedOn.toISOString();
24
+ }
25
+ return Object.assign(Object.assign({}, dateTransformerObj), objectToAssign);
20
26
  }
21
27
  function ConvertApiToEntity(obj) {
22
28
  const { createdOn: _, updatedOn: __ } = obj, objectToAssign = __rest(obj, ["createdOn", "updatedOn"]);
@@ -11,3 +11,8 @@ export declare const entityEnumToEntityModel: {
11
11
  export declare function entityMapToModels(entityMap: EntityMap): void;
12
12
  export declare function parseEntities<T extends EnumEntityType<EntityEnum | VirtualEntityEnum>>(json: IBaseEntityApiResponse<T>, baseEntity: EntityEnum | VirtualEntityEnum, entityMap: EntityMap): EntityMap;
13
13
  export declare function entityMapToEntityIndexMap(entityMap: EntityMap, entityRelationsFor: EntityEnum[]): EntityIndexMap;
14
+ export declare function parseEntitiesWithoutModels<T extends EnumEntityType<EntityEnum | VirtualEntityEnum>>(json: IBaseEntityApiResponse<T>, baseEntity: EntityEnum | VirtualEntityEnum, entityMap?: {
15
+ [E in EntityEnum | VirtualEntityEnum]?: IApiEntity<EnumEntityType<E>>[];
16
+ }): {
17
+ [E in EntityEnum | VirtualEntityEnum]?: IApiEntity<EnumEntityType<E>>[];
18
+ };
@@ -7,6 +7,7 @@ exports.populateRelationsFor = populateRelationsFor;
7
7
  exports.entityMapToModels = entityMapToModels;
8
8
  exports.parseEntities = parseEntities;
9
9
  exports.entityMapToEntityIndexMap = entityMapToEntityIndexMap;
10
+ exports.parseEntitiesWithoutModels = parseEntitiesWithoutModels;
10
11
  const entity_utils_interface_1 = require("../interface/entity.utils.interface");
11
12
  const bank_entity_model_1 = require("./bank.entity.model");
12
13
  const base_entity_model_1 = require("./base.entity.model");
@@ -103,3 +104,19 @@ function entityMapToEntityIndexMap(entityMap, entityRelationsFor) {
103
104
  populateRelationsFor(entityIndexMap, entityRelationsFor);
104
105
  return entityIndexMap;
105
106
  }
107
+ function parseEntitiesWithoutModels(json, baseEntity, entityMap = {}) {
108
+ var _a;
109
+ if (!(baseEntity in exports.entityEnumToEntityModel)) {
110
+ throw new Error(`Unknown entity: ${baseEntity}`);
111
+ }
112
+ entityMap[baseEntity] = ((_a = json["baseEntities"]) !== null && _a !== void 0 ? _a : []);
113
+ if (json["relatedEntities"]) {
114
+ const relatedEntities = json["relatedEntities"];
115
+ for (const entityName in relatedEntities) {
116
+ parseEntitiesWithoutModels(
117
+ // @ts-ignore
118
+ relatedEntities[entityName], entityName, entityMap);
119
+ }
120
+ }
121
+ return entityMap;
122
+ }
@@ -9,22 +9,24 @@ export type LeaveStatusActionPair = {
9
9
  * @param currentStatus - The current status of the leave
10
10
  * @param userPermissions - Array of permissions the user has
11
11
  * @param combineActions - Array of action values to combine into a single action
12
- * @param data - Optional function to provide additional visibility conditions based on the entity data
12
+ * @param actionLabels - Object mapping actions to custom labels
13
+ * @param data - Optional function to provide additional visibility conditions
13
14
  * @returns Array of available actions with labels and values
14
- * @throws Error if user has multiple permissions for a single action
15
15
  */
16
16
  export declare function getPermittedActions<T extends LeaveStatusActionPair, E>(entityFlowConfig: {
17
- [key in T['status']]?: {
17
+ [key in T["status"]]?: {
18
18
  actions: {
19
- [key in T['action']]?: {
19
+ [key in T["action"]]?: {
20
20
  permissions: string[];
21
- next: () => T['status'];
21
+ next: () => T["status"];
22
22
  };
23
23
  };
24
24
  };
25
- }, currentStatus: T['status'] | null, userPermissions: string[], combineActions?: T['action'][], data?: (a: E) => {
25
+ }, currentStatus: T["status"] | null, userPermissions: string[], combineActions?: T["action"][], actionLabels?: {
26
+ [key in T["action"]]?: string;
27
+ }, data?: (a: E) => {
26
28
  [key: string]: () => boolean;
27
29
  }): {
28
30
  label: string;
29
- value: T['action'] | string;
31
+ value: T["action"] | string;
30
32
  }[];
@@ -1,54 +1,83 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getPermittedActions = getPermittedActions;
4
- const entities_1 = require("../entities");
4
+ /**
5
+ * Converts an action or combined action to a value-label pair
6
+ * @param action - The action or combined action key
7
+ * @param actionLabels - Object mapping actions to custom labels
8
+ * @returns Object with label and value
9
+ */
10
+ function convertToValueLabel(action, actionLabels) {
11
+ return {
12
+ label: actionLabels[action] || action,
13
+ value: action,
14
+ };
15
+ }
16
+ /**
17
+ * Filters actions based on user permissions and visibility conditions
18
+ * @param actionConfig - The actions configuration for the current status
19
+ * @param userPermissions - Array of permissions the user has
20
+ * @param data - Optional function to provide additional visibility conditions
21
+ * @returns Array of permitted action keys
22
+ * @throws Error if multiple permissions are found for a single action
23
+ */
24
+ function _getPermittedActions(actionConfig, userPermissions, data) {
25
+ return Object.entries(actionConfig)
26
+ .filter(([actionKey, config]) => {
27
+ var _a;
28
+ const actionRequiredPermissions = (_a = config === null || config === void 0 ? void 0 : config.permissions) !== null && _a !== void 0 ? _a : [];
29
+ const matchingPermissions = actionRequiredPermissions.filter((permission) => userPermissions.includes(permission));
30
+ if (matchingPermissions.length > 1) {
31
+ throw new Error(`Multiple permissions found for action ${actionKey}: ${matchingPermissions.join(", ")}`);
32
+ }
33
+ return (matchingPermissions.length > 0 &&
34
+ (!data ||
35
+ !data({})[matchingPermissions[0]] ||
36
+ data({})[matchingPermissions[0]]()));
37
+ })
38
+ .map(([actionKey, _]) => actionKey);
39
+ }
40
+ /**
41
+ * Combines specified actions into a single action if all are present
42
+ * @param permittedActions - Array of permitted action keys
43
+ * @param combineActions - Array of actions to combine
44
+ * @returns Array of action keys, with combined action if applicable
45
+ */
46
+ function combinePermittedActions(permittedActions, combineActions) {
47
+ if (combineActions.length === 0) {
48
+ return permittedActions;
49
+ }
50
+ const isCombinedActionPresent = combineActions.every((action) => permittedActions.includes(action));
51
+ if (isCombinedActionPresent) {
52
+ const combinedValue = combineActions.join("_");
53
+ return [
54
+ ...permittedActions.filter((action) => !combineActions.includes(action)),
55
+ combinedValue,
56
+ ];
57
+ }
58
+ return permittedActions;
59
+ }
5
60
  /**
6
61
  * Gets available actions for a leave based on current status and user permissions
7
62
  * @param entityFlowConfig - The flow configuration for the entity
8
63
  * @param currentStatus - The current status of the leave
9
64
  * @param userPermissions - Array of permissions the user has
10
65
  * @param combineActions - Array of action values to combine into a single action
11
- * @param data - Optional function to provide additional visibility conditions based on the entity data
66
+ * @param actionLabels - Object mapping actions to custom labels
67
+ * @param data - Optional function to provide additional visibility conditions
12
68
  * @returns Array of available actions with labels and values
13
- * @throws Error if user has multiple permissions for a single action
14
69
  */
15
- function getPermittedActions(entityFlowConfig, currentStatus, userPermissions, combineActions = [], data = (_) => ({})) {
70
+ function getPermittedActions(entityFlowConfig, currentStatus, userPermissions, combineActions = [], actionLabels = {}, data = (_) => ({})) {
16
71
  var _a;
17
72
  // Handle invalid inputs
18
73
  if (!currentStatus || !entityFlowConfig[currentStatus]) {
19
74
  return [];
20
75
  }
21
76
  const actionConfig = ((_a = entityFlowConfig[currentStatus]) === null || _a === void 0 ? void 0 : _a.actions) || {};
22
- // Filter actions based on user permissions
23
- const permittedActions = Object.entries(actionConfig)
24
- .filter(([actionKey, config]) => {
25
- var _a;
26
- const actionRequiredPermissions = (_a = config.permissions) !== null && _a !== void 0 ? _a : [];
27
- const matchingPermissions = actionRequiredPermissions.filter((permission) => userPermissions.includes(permission));
28
- // Check for multiple permissions
29
- if (matchingPermissions.length > 1) {
30
- throw new Error(`Multiple permissions found for action ${actionKey}: ${matchingPermissions.join(', ')}`);
31
- }
32
- return matchingPermissions.length > 0 && (!data || !data({})[matchingPermissions[0]] || data({})[matchingPermissions[0]]());
33
- })
34
- .map(([actionKey, _]) => ({
35
- label: entities_1.LeaveActionEnum.getLabel(actionKey) || '',
36
- value: actionKey,
37
- }));
38
- // // Combine specified actions if all are present and combineActions is not empty
39
- if (combineActions.length > 0) {
40
- const isCombinedActionPresent = combineActions.every((action) => permittedActions.some((a) => a.value === action));
41
- if (isCombinedActionPresent) {
42
- return [
43
- ...permittedActions.filter((action) => !combineActions.includes(action.value)),
44
- {
45
- label: combineActions
46
- .map((a) => entities_1.LeaveActionEnum.getLabel(a) || '')
47
- .join('/'),
48
- value: combineActions.join('_'),
49
- },
50
- ];
51
- }
52
- }
53
- return permittedActions;
77
+ // Get permitted actions
78
+ const permittedActions = _getPermittedActions(actionConfig, userPermissions, data);
79
+ // Combine actions if specified
80
+ const finalActions = combinePermittedActions(permittedActions, combineActions);
81
+ // Convert to value-label pairs
82
+ return finalActions.map((action) => convertToValueLabel(action, actionLabels));
54
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "law-common",
3
- "version": "10.21.1-beta.1",
3
+ "version": "10.21.1-beta.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "files": [