'use strict'; // HCM pack REST facade, internal API and UI (MOCK Fusion-style; NOT Oracle software; SYNTHETIC people only). Labels follow the HCM case YAML // steps (ui-glossary has HCM nav keys only); automation keeps them in automation/src/modules/hcm/labels.ts. const express = require('express'); const S = require('./store'); const U = require('../../core/ui'); const { list, filt } = require('../../core/kit'); const { AppError } = require('../../core/errors'); const { esc, inp, sel, hid, btn, form, table, tr, money, dl, layout, wrap, created } = U; const page = (fn) => U.page(fn, 'hcm'); const act = (fn) => U.act(fn, 'hcm'); const guard = (role, a) => { if (!S.can(role, a)) throw new AppError(403, 'FORBIDDEN', `You are not authorized to access this page (persona ${role} lacks ${a}).`); }; const none = (rows) => (rows.length ? '' : '
No results found.
'); const linkBtn = (href, label) => `${esc(label)}`; const T = (s) => esc(String(s || '').toLowerCase().split('_').map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join(' ')); const name = (p) => `${p.first} ${p.last}`; const opt = (o) => (o === undefined ? '' : o); const TASKS = [ ['Hire an Employee', '/hcm/hire', (r) => S.can(r, 'person.manage')], ['Transfer', '/hcm/transfer', (r) => S.can(r, 'person.manage')], ['Pending Transactions', '/hcm/pending-transactions', (r) => S.can(r, 'person.manage')], ['Person Management', '/hcm/person-management', (r) => S.can(r, 'person.manage')], ['Manage Element Entries', '/hcm/payroll/element-entries', (r) => S.can(r, 'payroll.manage')], ]; const SCHEMAS = { workers: [['PersonNumber', 'string'], ['DisplayName', 'string'], ['LegalEmployerName', 'string'], ['WorkerStatus', 'string']], userAccounts: [['Username', 'string'], ['PersonNumber', 'string'], ['UserStatus', 'string']], payrollRunResults: [['PayrollFlow', 'string'], ['PayrollRelationshipNumber', 'string'], ['PersonNumber', 'string'], ['ElementName', 'string'], ['ElementClassification', 'string'], ['ResultValue', 'number']], payrollBalances: [['PayrollRelationshipNumber', 'string'], ['BalanceName', 'string'], ['Dimension', 'string'], ['Value', 'number']], }; const restOk = (role, ok) => { if (!ok) throw new AppError(403, 'FORBIDDEN', 'Not authorized.'); }; const workerOut = (p) => ({ PersonNumber: p.number, DisplayName: name(p), LastName: p.last, FirstName: p.first, WorkEmail: p.email, NationalId: `***${p.nid.slice(-4)}`, LegalEmployerName: p.le, WorkerStatus: T(p.status), TerminationDate: p.terminatedOn, workRelationships: [{ LegalEmployerName: p.le, Status: T(p.status), assignments: [{ BusinessUnitName: p.bu, JobName: p.job, GradeCode: p.grade, PositionCode: p.position, DepartmentName: p.dept, ManagerPersonNumber: p.manager }] }], payrollRelationships: p.payrollRel ? [{ PayrollRelationshipNumber: p.payrollRel, PayrollName: p.payroll }] : [], salaries: p.annual ? [{ SalaryBasisName: S.SALARY_BASIS, SalaryAmount: p.annual, MonthlySalary: Math.round((p.annual / 12) * 100) / 100 }] : [] }); const fx = express.Router(); fx.get('/workers', wrap((req, role) => { restOk(role, S.can(role, 'person.manage') || role === 'hcm_integration_user' || S.can(role, 'team.view')); return list(filt(S.peopleFor(role).map(workerOut), req.query.q)); })); fx.get('/userAccounts', wrap((req, role) => { restOk(role, ['it_security_manager', 'hr_specialist', 'hcm_integration_user'].includes(role)); return list(filt(S.list('users').map((u) => ({ Username: u.username, PersonNumber: u.person, UserStatus: T(u.status), PendingRequest: u.pending, roles: u.roles.map((r) => ({ RoleName: r })) })), req.query.q)); })); const resultRows = (role) => S.list('results').filter((r) => !r.rolledBack && S.payrollScope(role).includes(r.payroll)); fx.get('/payrollRunResults', wrap((req, role) => { restOk(role, S.payrollScope(role).length > 0); return list(filt(resultRows(role).map((r) => ({ PayrollFlow: r.flow, PayrollName: r.payroll, Period: r.period, PayrollRelationshipNumber: r.rel, PersonNumber: r.person, ElementName: r.element, ElementClassification: r.cls, ResultValue: r.value })), req.query.q)); })); fx.get('/payrollBalances', wrap((req, role) => { restOk(role, S.payrollScope(role).length > 0); const rels = S.list('people').filter((p) => p.payrollRel && S.payrollScope(role).includes(p.payroll)); const rows = rels.flatMap((p) => { const last = resultRows(role).filter((r) => r.rel === p.payrollRel && r.element === 'Gross Earnings').pop(); return [{ PayrollRelationshipNumber: p.payrollRel, PersonNumber: p.number, BalanceName: 'Gross Earnings', Dimension: 'PTD', Value: last ? last.value : 0 }, { PayrollRelationshipNumber: p.payrollRel, PersonNumber: p.number, BalanceName: 'Gross Earnings', Dimension: 'YTD', Value: S.ytd(p.payrollRel) || 0 }]; }); return list(filt(rows, req.query.q)); })); fx.get('/payslips', wrap((req, role) => { restOk(role, S.payrollScope(role).length > 0); return list(filt(S.list('payslips').map((s) => ({ PayslipNumber: s.number, PersonNumber: s.person, Period: s.period, GrossPay: s.gross, NetPay: s.net, PayrollFlow: s.flow })), req.query.q)); })); fx.get('/payrollCostResults', wrap((req, role) => { restOk(role, S.payrollScope(role).length > 0); return list(filt(S.list('costing').map((c) => ({ PersonNumber: c.person, ElementName: c.element, Account: c.account, Amount: c.amount, AccountingStatus: c.accounting ? 'Accounted' : 'Unaccounted', DebitAccount: c.accounting ? c.accounting.dr : null, CreditAccount: c.accounting ? c.accounting.cr : null, TransferredToGL: c.accounting && c.accounting.transferred ? 'Y' : 'N', PostedFlag: c.accounting && c.accounting.posted ? 'Y' : 'N' })), req.query.q)); })); const api = express.Router(); api.post('/hires', wrap((req, role) => created(S.submitHire(role, req.body)))); api.post('/transactions/:n/:action(approve|reject|withdraw)', wrap((req, role) => S.decide(role, req.params.n, req.params.action))); api.post('/workers/:pn/terminate', wrap((req, role) => S.terminate(role, req.params.pn, req.body && req.body.date))); api.post('/absences', wrap((req, role) => created(S.submitAbsence(role, req.body)))); api.post('/absences/:n/approve', wrap((req, role) => S.approveAbsence(role, req.params.n))); api.post('/absenceAdjustments', wrap((req, role) => created(S.addAdjustment(role, req.body)))); api.get('/absenceBalances/:pn', wrap((req, role) => { restOk(role, S.can(role, 'absence.admin') || S.PERSON_OF[role] === req.params.pn); return { person: req.params.pn, plan: S.PLAN, balance: S.balance(req.params.pn) }; })); api.post('/timeCards', wrap((req, role) => created(S.submitTimeCard(role, req.body)))); api.post('/timeCards/:n/approve', wrap((req, role) => S.approveTimeCard(role, req.params.n))); api.post('/elementEntries', wrap((req, role) => created(S.addEntry(role, req.body)))); api.post('/hdl/files', wrap((req, role) => created(S.stageHdl(role, req.body)))); api.get('/eft/:n', wrap((req, role) => { restOk(role, S.can(role, 'payroll.manage')); const f = S.list('eft').find((x) => x.number === req.params.n); if (!f) throw new AppError(404, 'NOT_FOUND', 'EFT file not found.'); return f; })); function mount(app, rest) { app.use('/api/hcm', api); app.use(rest, fx); app.get('/hcm/client-groups', page((req, role) => { guard(role, 'person.manage'); return layout(req, role, 'My Client Groups', 'Use the Tasks panel: Hire an Employee, Transfer, Pending Transactions, Person Management.
'); })); // Hire app.get('/hcm/hire', page((req, role) => { guard(role, 'person.manage'); return layout(req, role, 'Hire an Employee', form('/hcm/hire', inp('last', 'Last Name') + inp('first', 'First Name') + inp('email', 'Email') + inp('nid', 'National ID') + sel('le', 'Legal Employer', S.leScope(role)) + sel('bu', 'Business Unit', [S.BU]) + sel('job', 'Job', Object.keys(S.JOBS)) + sel('grade', 'Grade', S.GRADES) + sel('manager', 'Manager', S.list('people').filter((p) => ['ZZ-MGR', 'ZZ-MGR-2', 'ZZ-LEAD'].includes(p.number)).map((p) => p.number)) + sel('payroll', 'Payroll', [S.PAYROLL]) + sel('salaryBasis', 'Salary Basis', [S.SALARY_BASIS]) + inp('salary', 'Salary Amount') + hid('__back', '/hcm/hire') + btn('Submit'))); })); app.post('/hcm/hire', act((req, role) => { const t = S.submitHire(role, req.body); return [`/hcm/transactions/${t.number}`, `${t.subject} (${t.number}) submitted; routed for approval to ${S.PERSONAS[t.approver].label}.`]; })); app.get('/hcm/transactions/:n', page((req, role) => { const t = S.txn(req.params.n); if (t.submittedBy !== role && t.approver !== role && !S.can(role, 'person.manage') && !S.can(role, 'hire.approve')) guard(role, 'person.manage'); const d = t.data; return layout(req, role, `${t.subject}`, dl([['Transaction', esc(t.number), 'hcm-txn-number'], ['Type', esc(t.type)], ['Status', T(t.status), 'hcm-txn-status'], ['Name', esc(`${d.first} ${d.last}`)], ['Legal Employer', esc(d.le)], ['Job', esc(d.job)], ['Grade', esc(d.grade), 'hcm-txn-grade'], ['Manager', esc(d.manager)], ['Payroll', esc(d.payroll)], ['Salary', money(d.annual), 'hcm-txn-salary'], ['Person Number', esc(t.personNumber || ''), 'hcm-txn-person']]) + (t.status === 'PENDING_APPROVAL' && t.approver === role ? form(`/hcm/transactions/${t.number}/approve`, btn('Approve')) + form(`/hcm/transactions/${t.number}/reject`, btn('Reject')) : '') + (t.status === 'PENDING_APPROVAL' && t.submittedBy === role ? form(`/hcm/transactions/${t.number}/withdraw`, btn('Withdraw')) : '')); })); app.post('/hcm/transactions/:n/:action(approve|reject|withdraw)', act((req, role) => { const t = S.decide(role, req.params.n, req.params.action); return [`/hcm/transactions/${t.number}`, t.status === 'COMPLETED' ? `Approved; transaction completes: person ${t.personNumber} created.` : `${t.number} ${T(t.status)}.`]; })); app.get('/hcm/pending-transactions', page((req, role) => { guard(role, 'person.manage'); const rows = S.list('txns').filter((t) => t.submittedBy === role); return layout(req, role, 'Pending Transactions', table('Pending Transactions', ['Transaction', 'Type', 'Subject', 'Status'], rows.map((t) => tr([`${esc(t.number)}`, esc(t.type), esc(t.subject), T(t.status)]))) + none(rows)); })); // Person management (security profile) app.get('/hcm/person-management', page((req, role) => { guard(role, 'person.manage'); const { name: q, number } = req.query; const searched = q !== undefined || number !== undefined; const rows = searched ? S.peopleFor(role).filter((p) => (!q || name(p).includes(q)) && (!number || p.number === number)) : []; return layout(req, role, 'Person Management', `