import { BusError, TASK_VERBS, ASSIGNED_STATES, STORE_POSITION_MAX } from '../../bus/src/broker.mjs'; import { loadFields } from './fields.mjs'; // U4a (#1873, REQ-TASK-1): the task store built into each organization, the default task-tracker // module. It answers the eight task verbs as verbs.mjs does for Vikunja, with the same arguments // where the store has the thing they name, and writes through broker.storeTask, which keeps the rows // in the organization's own bus.sqlite. Nothing here talks to a network or holds a credential. // // What the module checks before the broker sees the call, in this order: // - the caller is one of the organization's roles (tracker-role); // - the field table's writer for the verb (packages/tasks/fields.json): a pm verb needs the pm // definition (field-writer), task.update.assigned needs the role among the task's assignees // (not-assigned), task.scope.change any role; // - the arguments, as verbs.mjs takes them. The store has no Vikunja projects, relations or scoped // requirement snapshots, so `project`, `relations`, `project_key` and `requirement_snapshot` // refuse (invalid-request). task.schedule also takes `group` and `position`, task.create too. // The broker then holds the write to the fields STORE_VERB_FIELDS gives the verb, whatever this // module asked for, and checks the holder, the authority and a close's verdict as it does for Vikunja. export const STORE_TIMEOUT = 60000; const refuse = (code) => { throw new BusError(code); }; const object = (x) => x !== null && typeof x === 'object' && !Array.isArray(x); function keys(x, allowed, required = []) { if (!object(x) || Object.keys(x).some((k) => !allowed.includes(k)) || required.some((k) => x[k] === undefined)) refuse('invalid-request'); } const text = (x, max, min = 0) => { if (typeof x !== 'string' || x.length < min || x.length > max) refuse('invalid-request'); return x; }; const canonical = (x) => typeof x === 'string' && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(x) && Number.isFinite(Date.parse(x)) && new Date(x).toISOString() === x; const due = (x) => (x === null || canonical(x) ? x : refuse('invalid-request')); const priority = (x) => (Number.isSafeInteger(x) && x >= 0 && x <= 5 ? x : refuse('invalid-request')); const percent = (x) => (typeof x === 'number' && Number.isFinite(x) && x >= 0 && x <= 1 ? x : refuse('invalid-request')); const ids = (list) => { if (!Array.isArray(list) || list.length > 50 || list.some((x) => !Number.isSafeInteger(x) || x < 1)) refuse('invalid-request'); return [...new Set(list)]; }; const group = (x) => (x === null || (typeof x === 'string' && x.length <= 64 && x.trim() && !/[\u0000-\u001f\u007f]/.test(x)) ? x : refuse('invalid-request')); const position = (x) => (Number.isSafeInteger(x) && x >= 0 && x <= STORE_POSITION_MAX ? x : refuse('invalid-request')); const decision = (x) => (x === undefined ? null : text(x, 128, 1)); // Arguments a Vikunja verb takes that name something the store doesn't have. const VIKUNJA_ONLY = ['project', 'relations', 'project_key', 'requirement_snapshot']; const COMMON = ['task_ref', 'decision', 'expect']; export function storeAdapter({ fields = loadFields() } = {}) { const writers = fields.verbs; return async function factory({ broker }) { let closed = false; function prepare(verb, args, roles, role) { if (object(args) && VIKUNJA_ONLY.some((k) => Object.hasOwn(args, k))) refuse('invalid-request'); if (args?.expect !== undefined && !(typeof args.expect === 'string' && /^[0-9a-f]{64}$/.test(args.expect))) refuse('invalid-request'); switch (verb) { case 'task.create': { keys(args, ['title', 'description', 'due_date', 'priority', 'labels', 'request', 'requirement', 'decision', 'op', 'group', 'position'], ['title', 'request', 'requirement']); const set = { title: text(args.title, 250, 1) }; if (!set.title.trim()) refuse('invalid-request'); if (args.description !== undefined) set.description = text(args.description, 65536); if (args.due_date !== undefined) set.due_date = due(args.due_date); if (args.priority !== undefined) set.priority = priority(args.priority); if (args.labels !== undefined) set.labels = ids(args.labels).sort((a, b) => a - b); if (args.group !== undefined) set.group = group(args.group); if (args.position !== undefined) set.position = position(args.position); set.request = text(args.request, 64, 1); set.requirement = text(args.requirement, 64); if (!/^REQ-[A-Z]+-[1-9][0-9]*$/.test(set.requirement)) refuse('invalid-request'); if (args.op !== undefined) { set.op = text(args.op, 256, 1); if (/[;\r\n]/.test(set.op)) refuse('invalid-request'); } return (f) => ({ ...f, ...set }); } case 'task.assign': case 'task.reassign': { keys(args, [...COMMON, 'role'], ['task_ref', 'role']); const to = text(args.role, 64, 1); if (!Object.hasOwn(roles, to)) refuse('unknown-role'); return (f) => { const held = f.assignees; if (verb === 'task.assign' && held.length) refuse('task-assigned'); if (verb === 'task.reassign' && !held.length) refuse('task-unassigned'); if (verb === 'task.reassign' && held.length === 1 && held[0] === to) refuse('task-assigned'); return { ...f, assignees: [to] }; }; } case 'task.schedule': { keys(args, [...COMMON, 'due_date', 'labels', 'group', 'position'], ['task_ref']); if (['due_date', 'labels', 'group', 'position'].every((k) => args[k] === undefined)) refuse('invalid-request'); const dueDate = args.due_date === undefined ? undefined : due(args.due_date); const labels = { add: [], remove: [] }; if (args.labels !== undefined) { keys(args.labels, ['add', 'remove']); labels.add = ids(args.labels.add ?? []); labels.remove = ids(args.labels.remove ?? []); } const g = args.group === undefined ? undefined : group(args.group); const p = args.position === undefined ? undefined : position(args.position); return (f) => { const out = { ...f }; if (dueDate !== undefined) out.due_date = dueDate; out.labels = [...new Set([...f.labels.filter((l) => !labels.remove.includes(l)), ...labels.add])].sort((a, b) => a - b); // A move to another group with no position goes to the end of it (the broker settles a null). if (g !== undefined && g !== f.group) Object.assign(out, { group: g, position: p ?? null }); else if (p !== undefined) out.position = p; return out; }; } case 'task.priority.change': { keys(args, [...COMMON, 'priority'], ['task_ref', 'priority']); const p = priority(args.priority); return (f) => ({ ...f, priority: p }); } case 'task.scope.change': { keys(args, [...COMMON, 'title', 'description'], ['task_ref']); const set = {}; if (args.title !== undefined) set.title = text(args.title, 250, 1); if (set.title !== undefined && !set.title.trim()) refuse('invalid-request'); if (args.description !== undefined) set.description = text(args.description, 65536); if (!Object.keys(set).length) refuse('invalid-request'); return (f) => ({ ...f, ...set }); } case 'task.update.assigned': { keys(args, [...COMMON, 'percent_done', 'state', 'comment'], ['task_ref']); const pct = args.percent_done === undefined ? undefined : percent(args.percent_done); // done is not a target here: closing is the pm's task.close after a review verdict. if (args.state !== undefined && !ASSIGNED_STATES.includes(args.state)) refuse('invalid-state'); const note = args.comment === undefined ? undefined : text(args.comment, 10000, 1); if (pct === undefined && args.state === undefined && note === undefined) refuse('invalid-request'); return (f) => { if (!f.assignees.includes(role)) refuse('not-assigned'); const out = { ...f }; if (pct !== undefined) out.percent_done = pct; if (args.state !== undefined) Object.assign(out, { state: args.state, bucket: args.state }); // The store keeps the latest comment and the count; the event records only that there was one. if (note !== undefined) Object.assign(out, { comments: f.comments + 1, comment: { text: note, by: role } }); return out; }; } case 'task.close': { keys(args, [...COMMON, 'verdict'], ['task_ref', 'verdict']); const verdict = text(args.verdict, 256, 1); return (f) => ({ ...f, state: 'done', bucket: 'done', done: true, verdict }); } default: return refuse('unknown-verb'); } } return { timeout: STORE_TIMEOUT, async handle(cap, verb, args) { if (closed) refuse('tracker-closed'); const me = broker.identity(cap); const roles = broker.taskRoles(me.business); const mine = roles[me.role]; if (!mine || typeof mine.definition !== 'string') refuse('tracker-role'); if (!TASK_VERBS.includes(verb)) refuse('unknown-verb'); if (writers[verb] === 'pm' && mine.definition !== 'pm') refuse('field-writer'); const apply = prepare(verb, args, roles, me.role); const creating = verb === 'task.create'; const row = broker.storeTask(cap, { verb, task_ref: creating ? null : args.task_ref, decision: decision(args.decision), expect: args.expect ?? null, verdict: verb === 'task.close' ? args.verdict : null, // Reopening a done task is a person's act; no role verb writes to one. change: (f) => { if (f?.done) refuse('task-done'); return apply(f); }, }); return row.deleted ? { task_ref: row.task_ref, deleted: true } : { task_ref: row.task_ref, digest: row.digest, updated: row.updated }; }, close() { closed = true; }, }; }; }