law-common 10.21.1-beta.2 → 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.
@@ -9,24 +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'][], actionLabels?: {
26
- [key in T['action']]?: string;
25
+ }, currentStatus: T["status"] | null, userPermissions: string[], combineActions?: T["action"][], actionLabels?: {
26
+ [key in T["action"]]?: string;
27
27
  }, data?: (a: E) => {
28
28
  [key: string]: () => boolean;
29
29
  }): {
30
30
  label: string;
31
- value: T['action'] | string;
31
+ value: T["action"] | string;
32
32
  }[];
@@ -1,15 +1,71 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getPermittedActions = getPermittedActions;
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
+ }
4
60
  /**
5
61
  * Gets available actions for a leave based on current status and user permissions
6
62
  * @param entityFlowConfig - The flow configuration for the entity
7
63
  * @param currentStatus - The current status of the leave
8
64
  * @param userPermissions - Array of permissions the user has
9
65
  * @param combineActions - Array of action values to combine into a single action
10
- * @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
11
68
  * @returns Array of available actions with labels and values
12
- * @throws Error if user has multiple permissions for a single action
13
69
  */
14
70
  function getPermittedActions(entityFlowConfig, currentStatus, userPermissions, combineActions = [], actionLabels = {}, data = (_) => ({})) {
15
71
  var _a;
@@ -18,34 +74,10 @@ function getPermittedActions(entityFlowConfig, currentStatus, userPermissions, c
18
74
  return [];
19
75
  }
20
76
  const actionConfig = ((_a = entityFlowConfig[currentStatus]) === null || _a === void 0 ? void 0 : _a.actions) || {};
21
- // Filter actions based on user permissions
22
- const permittedActions = Object.entries(actionConfig)
23
- .filter(([actionKey, config]) => {
24
- var _a;
25
- const actionRequiredPermissions = (_a = config.permissions) !== null && _a !== void 0 ? _a : [];
26
- const matchingPermissions = actionRequiredPermissions.filter((permission) => userPermissions.includes(permission));
27
- // Check for multiple permissions
28
- if (matchingPermissions.length > 1) {
29
- throw new Error(`Multiple permissions found for action ${actionKey}: ${matchingPermissions.join(', ')}`);
30
- }
31
- return matchingPermissions.length > 0 && (!data || !data({})[matchingPermissions[0]] || data({})[matchingPermissions[0]]());
32
- })
33
- .map(([actionKey, _]) => ({
34
- label: actionLabels[actionKey] || actionKey,
35
- value: actionKey,
36
- }));
37
- // // Combine specified actions if all are present and combineActions is not empty
38
- if (combineActions.length > 0) {
39
- const isCombinedActionPresent = combineActions.every((action) => permittedActions.some((a) => a.value === action));
40
- if (isCombinedActionPresent) {
41
- return [
42
- ...permittedActions.filter((action) => !combineActions.includes(action.value)),
43
- {
44
- label: actionLabels[combineActions.join('_')] || combineActions.join('_'),
45
- value: combineActions.join('_'),
46
- },
47
- ];
48
- }
49
- }
50
- 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));
51
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "law-common",
3
- "version": "10.21.1-beta.2",
3
+ "version": "10.21.1-beta.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "files": [