law-common 10.18.2-beta.5 → 10.18.2-beta.6
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,30 @@
|
|
|
1
|
+
import { LeaveActionEnum, LeaveStatusEnum } from "../entities";
|
|
2
|
+
export type LeaveStatusActionPair = {
|
|
3
|
+
status: LeaveStatusEnum;
|
|
4
|
+
action: LeaveActionEnum;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Gets available actions for a leave based on current status and user permissions
|
|
8
|
+
* @param entityFlowConfig - The flow configuration for the entity
|
|
9
|
+
* @param currentStatus - The current status of the leave
|
|
10
|
+
* @param userPermissions - Array of permissions the user has
|
|
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
|
|
13
|
+
* @returns Array of available actions with labels and values
|
|
14
|
+
* @throws Error if user has multiple permissions for a single action
|
|
15
|
+
*/
|
|
16
|
+
export declare function getPermittedActions<T extends LeaveStatusActionPair, E>(entityFlowConfig: {
|
|
17
|
+
[key in T['status']]?: {
|
|
18
|
+
actions: {
|
|
19
|
+
[key in T['action']]?: {
|
|
20
|
+
permissions: string[];
|
|
21
|
+
next: () => T['status'];
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
}, currentStatus: T['status'] | null, userPermissions: string[], combineActions?: T['action'][], data?: (a: E) => {
|
|
26
|
+
[key: string]: () => boolean;
|
|
27
|
+
}): {
|
|
28
|
+
label: string;
|
|
29
|
+
value: T['action'] | string;
|
|
30
|
+
}[];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPermittedActions = getPermittedActions;
|
|
4
|
+
const entities_1 = require("../entities");
|
|
5
|
+
/**
|
|
6
|
+
* Gets available actions for a leave based on current status and user permissions
|
|
7
|
+
* @param entityFlowConfig - The flow configuration for the entity
|
|
8
|
+
* @param currentStatus - The current status of the leave
|
|
9
|
+
* @param userPermissions - Array of permissions the user has
|
|
10
|
+
* @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
|
|
12
|
+
* @returns Array of available actions with labels and values
|
|
13
|
+
* @throws Error if user has multiple permissions for a single action
|
|
14
|
+
*/
|
|
15
|
+
function getPermittedActions(entityFlowConfig, currentStatus, userPermissions, combineActions = [], data = (_) => ({})) {
|
|
16
|
+
var _a;
|
|
17
|
+
// Handle invalid inputs
|
|
18
|
+
if (!currentStatus || !entityFlowConfig[currentStatus]) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
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;
|
|
54
|
+
}
|
package/dist/src/utils/index.js
CHANGED
|
@@ -18,3 +18,4 @@ __exportStar(require("./date.util"), exports);
|
|
|
18
18
|
__exportStar(require("./helper.fn.util"), exports);
|
|
19
19
|
__exportStar(require("./models/date-code.model.util"), exports);
|
|
20
20
|
__exportStar(require("./string.util"), exports);
|
|
21
|
+
__exportStar(require("./entity.flow.util"), exports);
|