UNPKG

@tasolutions/express-core

Version:
111 lines (98 loc) 4.15 kB
const { genApiConfig } = require("../utils/generate"); const { getActions } = require("./constants"); module.exports = { buildResult: (Collection, path, permissionActions, fieldList, resultInterfaces = [], actionIncludes = [], workflowsObj = null, hook = null) => { const moduleName = Collection.collection.collectionName; const schema = Collection.schema.paths; const actions = getActions(schema); const buttonActionsMap = { DETAIL: { name: 'Detail', color: 'blue' }, UPDATE: { name: 'Update', color: 'orange' }, DELETE: { name: 'Delete', color: 'red' } }; const buttonActions = Object.keys(buttonActionsMap) .filter(actionType => permissionActions.some(permission => permission.type === actionType && actions[actionType]) ) .map(actionType => ({ action: actionType.toLowerCase(), is_enable: true, color: buttonActionsMap[actionType].color, method: actions[actionType].method, type: actions[actionType].type, name: buttonActionsMap[actionType].name })); const result = { type: moduleName, page: { title: `${moduleName}`, actions: [] } }; if (permissionActions.some(permission => permission.type === 'CREATE' && actions.CREATE)) { result.page.actions.push({ action: 'create', name: 'Create', is_enable: true, color: 'green', method: actions.CREATE.method, type: actions.CREATE.type, }); } const actionsMap = { LIST: () => genApiConfig(moduleName, path, 'LIST', actions, fieldList, { actions: buttonActions, filters: [{ key: 'search', label: 'Search', type: 'TEXT', color: 'green', }] }), DETAIL: () => genApiConfig(moduleName, path, 'DETAIL', actions, fieldList, { actions: buttonActions }), CREATE: () => genApiConfig(moduleName, path, 'CREATE', actions, fieldList), UPDATE: () => genApiConfig(moduleName, path, 'UPDATE', actions, fieldList), DELETE: () => genApiConfig(moduleName, path, 'DELETE', actions, fieldList), EXPORT: () => genApiConfig(moduleName, path, 'EXPORT', actions, fieldList, { actions: [], filters: [] }), COUNT: () => genApiConfig(moduleName, path, 'COUNT', actions, fieldList, { filters: [] }) }; permissionActions.forEach(action => { const actionType = action.type; if (actionsMap[actionType]) { result[actionType.toLowerCase()] = actionsMap[actionType](); } }); if (Array.isArray(resultInterfaces)) { resultInterfaces.forEach(interfaceResult => { Object.assign(result, interfaceResult); }); } actionIncludes.forEach(actionItem => { const action = actionItem.action; const functions = actionItem.function; functions.forEach(func => { if (result[func]) { if (!result[func].actions) { result[func].actions = []; } result[func].actions.push(action); } }); }); if (workflowsObj && typeof workflowsObj === 'object') { Object.keys(result).forEach(key => { if (workflowsObj[key]) { result[key].workflows = workflowsObj[key]; } else { result[key].workflows = null; } }); } if (hook && typeof hook === 'object') { Object.keys(result).forEach(key => { Object.assign(result[key], hook[key]); }); } return result; } };