UNPKG

tuain-form-manager

Version:

Component library to perform operations on Tuain Development Framework forms to interchange information on web or mobile applications based on the data interchange of abstract forms making trnasformation on the data upon actions required on both sides (fr

75 lines (69 loc) 2.63 kB
const COMPARISON_FUNCTIONS_FIELD = 'functionCode'; function userAllowed(requiredFunctions, userFunctions, matchFld = null) { for (let index = 0; index < requiredFunctions?.length; index++) { const requiredFunction = requiredFunctions[index]; const hasFunction = userFunctions?.find((usrFnc) => (matchFld ? usrFnc[matchFld] : usrFnc) === requiredFunction); if (!hasFunction) { return false; } } return true; } function removeUnsupportedActions(actions, userFunctions) { const actIndexesToRemove = []; for (let index = 0; index < actions?.length; index++) { const actionFunctions = actions[index]?.functions ?? []; if (!actionFunctions.every((fnc) => userFunctions?.includes(fnc))) { actIndexesToRemove.unshift(index); } delete actions[index].functions; } for (let index = 0; index < actIndexesToRemove.length; index++) { actions.splice(actIndexesToRemove[index], 1); } return actions; } function removeUnsupportedTableActions(tables, userFunctions) { for (let index = 0; index < tables?.length; index++) { const table = tables[index]; const actIndexesToRemove = []; for (let actIndex = 0; actIndex < table?.actions?.length; actIndex++) { const actionFunctions = table.actions[actIndex]?.functions ?? []; if (!userAllowed(actionFunctions, userFunctions, COMPARISON_FUNCTIONS_FIELD)) { actIndexesToRemove.unshift(actIndex); } delete table.actions[actIndex].functions; } for (let actIndex = 0; actIndex < actIndexesToRemove.length; actIndex++) { table.actions.splice(actIndexesToRemove[actIndex], 1); } } return tables; } function customizeFormDefinition(formDefinition, userFunctions) { const { transitions, actions, tables, states = [] } = formDefinition; formDefinition.states = states.map((item) => item.name); formDefinition.transitions = transitions ?.map((trns) => ({ name: trns.name, source: trns.source, destination: trns.destination, functions: trns.functions ?? [], })) .filter((trns) => { const { functions: transitionFunctions = [] } = trns; for (let index = 0; index < transitionFunctions.length; index++) { if (!userFunctions?.includes(transitionFunctions[index])) { return false; } } return true; }) ?? []; formDefinition.actions = removeUnsupportedActions(actions, userFunctions) ?? []; formDefinition.tables = removeUnsupportedTableActions(tables, userFunctions); return formDefinition; } module.exports = { customizeFormDefinition, };