stage 1
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
import { query } from '../db/pool.js';
|
||||
import { parseJsonColumn } from '../utils/json.js';
|
||||
|
||||
/*
|
||||
* The report service handles server-side storage of submitted reports. In
|
||||
* phase 1, reports are created locally in the browser and only uploaded when
|
||||
* the operator explicitly submits. This keeps the offline-first workflow intact
|
||||
* while giving the backend a durable copy for review, export, or archival.
|
||||
*/
|
||||
|
||||
export async function submitReport(report) {
|
||||
await query(
|
||||
`
|
||||
INSERT INTO reports (report_uuid, report_number, template_code, template_version, status, answers_json, submitted_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, NOW())
|
||||
ON DUPLICATE KEY UPDATE
|
||||
status = VALUES(status),
|
||||
answers_json = VALUES(answers_json),
|
||||
submitted_at = VALUES(submitted_at),
|
||||
updated_at = NOW()
|
||||
`,
|
||||
[
|
||||
report.id,
|
||||
report.reportNumber,
|
||||
report.templateCode,
|
||||
report.templateVersion,
|
||||
report.status,
|
||||
JSON.stringify(report.answers)
|
||||
]
|
||||
);
|
||||
|
||||
return getReport(report.id);
|
||||
}
|
||||
|
||||
export async function getReport(reportUuid) {
|
||||
const rows = await query(
|
||||
`
|
||||
SELECT
|
||||
report_uuid AS reportUuid,
|
||||
report_number AS reportNumber,
|
||||
template_code AS templateCode,
|
||||
template_version AS templateVersion,
|
||||
status,
|
||||
answers_json AS answersJson,
|
||||
submitted_at AS submittedAt,
|
||||
created_at AS createdAt,
|
||||
updated_at AS updatedAt
|
||||
FROM reports
|
||||
WHERE report_uuid = ?
|
||||
LIMIT 1
|
||||
`,
|
||||
[reportUuid]
|
||||
);
|
||||
|
||||
return rows.length ? mapReportRow(rows[0]) : null;
|
||||
}
|
||||
|
||||
export async function listReports({ status, templateCode, limit = 100, offset = 0 } = {}) {
|
||||
let sql = `
|
||||
SELECT
|
||||
report_uuid AS reportUuid,
|
||||
report_number AS reportNumber,
|
||||
template_code AS templateCode,
|
||||
template_version AS templateVersion,
|
||||
status,
|
||||
answers_json AS answersJson,
|
||||
submitted_at AS submittedAt,
|
||||
created_at AS createdAt,
|
||||
updated_at AS updatedAt
|
||||
FROM reports
|
||||
`;
|
||||
const params = [];
|
||||
const clauses = [];
|
||||
|
||||
if (status) {
|
||||
clauses.push('status = ?');
|
||||
params.push(status);
|
||||
}
|
||||
|
||||
if (templateCode) {
|
||||
clauses.push('template_code = ?');
|
||||
params.push(templateCode);
|
||||
}
|
||||
|
||||
if (clauses.length) {
|
||||
sql += ` WHERE ${clauses.join(' AND ')}`;
|
||||
}
|
||||
|
||||
sql += ' ORDER BY updated_at DESC LIMIT ? OFFSET ?';
|
||||
params.push(limit, offset);
|
||||
|
||||
const rows = await query(sql, params);
|
||||
return rows.map(mapReportRow);
|
||||
}
|
||||
|
||||
function mapReportRow(row) {
|
||||
return {
|
||||
id: row.reportUuid,
|
||||
reportNumber: row.reportNumber,
|
||||
templateCode: row.templateCode,
|
||||
templateVersion: row.templateVersion,
|
||||
status: row.status,
|
||||
answers: parseJsonColumn(row.answersJson, {}),
|
||||
submittedAt: row.submittedAt,
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user