'use strict'; // Small shared helpers for module packs (MOCK app; NOT Oracle software): REST q-filter, list envelope, staged synthetic files, money rounding. const { err } = require('./errors'); const r2 = (n) => (Math.round(Number(n) * 100) / 100) || 0; const list = (items) => ({ items, count: items.length, hasMore: false }); /** Fusion-style q filter: conditions separated by ';' - A='x', A=5, A LIKE 'x%', A IS NOT NULL, A IS NULL. */ function filt(items, q) { if (!q) return items; const conds = String(q).split(';').map((c) => c.trim()).filter(Boolean).map((c) => { let m = /^(\w+)\s+IS\s+(NOT\s+)?NULL$/i.exec(c); if (m) return (it) => ((it[m[1]] ?? null) !== null) === !!m[2]; m = /^(\w+)\s+LIKE\s+'([^']*)'$/i.exec(c); if (m) { const re = new RegExp(`^${m[2].replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/%/g, '.*')}$`); return (it) => re.test(String(it[m[1]] ?? '')); } m = /^(\w+)\s*=\s*(?:'([^']*)'|([^'\s]+))$/.exec(c); if (m) { const v = m[2] ?? m[3]; return (it) => String(it[m[1]] ?? '') === v; } throw err(400, 'BAD_QUERY', `Unsupported q condition: ${c}`); }); return items.filter((it) => conds.every((f) => f(it))); } /** Staged synthetic data files (stand-in for UCM uploads): CSV text only. */ function files() { let store = []; return { reset() { store = []; }, /** Staged files not yet consumed by an import process (the process sets f.imported = true). */ pending() { return store.filter((f) => !f.imported); }, stage(role, { name, content }) { if (!name || !content) throw err(400, 'VALIDATION', 'name and content are required.'); store = store.filter((f) => f.name !== name); store.push({ name, content, by: role }); return { name }; }, rows(name) { const f = store.find((x) => x.name === name); if (!f) throw err(400, 'VALIDATION', `Data file ${name} not found.`); const rows = f.content.trim().split(/\r?\n/); const head = rows.shift().split(',').map((s) => s.trim()); return rows.filter(Boolean).map((r) => Object.fromEntries(r.split(',').map((v, i) => [head[i], v.trim()]))); }, }; } const today = (offsetDays = 0) => { const d = new Date(); d.setDate(d.getDate() + offsetDays); return d.toISOString().slice(0, 10); }; module.exports = { r2, list, filt, files, today };