157 lines
5.2 KiB
JavaScript
157 lines
5.2 KiB
JavaScript
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
function defaultRegistryPath(root = path.resolve(__dirname, '..')) {
|
|
return path.join(root, 'runtime', 'erp-order-entry', 'order-registry', 'orders.jsonl');
|
|
}
|
|
|
|
function normalizeIdentifier(value) {
|
|
return String(value || '').trim().toUpperCase();
|
|
}
|
|
|
|
function ensureDir(filePath) {
|
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
}
|
|
|
|
function appendRawRecord(record, options = {}) {
|
|
const registryPath = options.registryPath || defaultRegistryPath();
|
|
ensureDir(registryPath);
|
|
const normalized = {
|
|
...record,
|
|
identifier: normalizeIdentifier(record.identifier),
|
|
updatedAt: record.updatedAt || new Date().toISOString(),
|
|
};
|
|
fs.appendFileSync(registryPath, `${JSON.stringify(normalized)}\n`, 'utf8');
|
|
return normalized;
|
|
}
|
|
|
|
function findGroupMetadata(result, groupNo, departureDate = '') {
|
|
const stageResults = (result && result.stageResults)
|
|
|| (result && result.rawExecutionResult && result.rawExecutionResult.stageResults)
|
|
|| {};
|
|
const submit = stageResults.submit || {};
|
|
const groups = [
|
|
...(submit && submit.group ? [submit.group] : []),
|
|
...(Array.isArray(submit && submit.groups) ? submit.groups : []),
|
|
...(Array.isArray(submit && submit.duplicateRows) ? submit.duplicateRows : []),
|
|
];
|
|
return groups.find((group) => {
|
|
if (!group) return false;
|
|
const sameGroup = normalizeIdentifier(group.groupNo || group.group || group.teamNo || group.group_number)
|
|
=== normalizeIdentifier(groupNo);
|
|
const sameDate = !departureDate || !group.date || String(group.date) === String(departureDate);
|
|
return sameGroup && sameDate;
|
|
}) || {};
|
|
}
|
|
|
|
function locatorFields(metadata = {}) {
|
|
const fields = {};
|
|
['ddid', 'tid', 'did', 'parentTid', 'childDid'].forEach((key) => {
|
|
if (metadata[key] !== undefined && metadata[key] !== null && String(metadata[key]).trim() !== '') {
|
|
fields[key] = String(metadata[key]);
|
|
}
|
|
});
|
|
return fields;
|
|
}
|
|
|
|
function extractCreateRecords(result) {
|
|
const identifiers = result.identifiers || {};
|
|
const task = result.task || {};
|
|
const fields = task.fields || {};
|
|
const route = result.route || task.route || '';
|
|
const base = {
|
|
operation: task.operation || result.operation || 'create_order',
|
|
route,
|
|
product: fields.productName || fields.productRoute || '',
|
|
customer: fields.bookingCustomer || fields.channelCustomer || '',
|
|
auditPath: result.auditPath || '',
|
|
};
|
|
|
|
if (identifiers.groupNo) {
|
|
const metadata = findGroupMetadata(result, identifiers.groupNo);
|
|
return [{
|
|
...base,
|
|
identifier: identifiers.groupNo,
|
|
groupNo: identifiers.groupNo,
|
|
departureDate: metadata.date || fields.departureDate || '',
|
|
...locatorFields(metadata),
|
|
}];
|
|
}
|
|
if (identifiers.childOrderNo) {
|
|
const submit = result.stageResults && result.stageResults.submit || {};
|
|
return [{
|
|
...base,
|
|
identifier: identifiers.childOrderNo,
|
|
childOrderNo: identifiers.childOrderNo,
|
|
parentGroupNo: identifiers.parentGroupNo || submit.parentGroupNo || '',
|
|
parentTid: identifiers.parentTid || submit.parentTid || '',
|
|
departureDate: submit.departureDate || fields.departureDate || '',
|
|
...locatorFields(submit),
|
|
}];
|
|
}
|
|
if (identifiers.parentGroupNo) {
|
|
return [{ ...base, identifier: identifiers.parentGroupNo, parentGroupNo: identifiers.parentGroupNo }];
|
|
}
|
|
if (identifiers.dateToGroupNo && typeof identifiers.dateToGroupNo === 'object') {
|
|
return Object.entries(identifiers.dateToGroupNo).map(([departureDate, groupNo]) => ({
|
|
...base,
|
|
identifier: groupNo,
|
|
groupNo,
|
|
departureDate,
|
|
...locatorFields(findGroupMetadata(result, groupNo, departureDate)),
|
|
}));
|
|
}
|
|
if (identifiers.dateToParentGroupNo && typeof identifiers.dateToParentGroupNo === 'object') {
|
|
return Object.entries(identifiers.dateToParentGroupNo).map(([departureDate, parentGroupNo]) => ({
|
|
...base,
|
|
identifier: parentGroupNo,
|
|
parentGroupNo,
|
|
departureDate,
|
|
}));
|
|
}
|
|
if (Array.isArray(identifiers.parentGroupNumbers)) {
|
|
return identifiers.parentGroupNumbers.map((parentGroupNo) => ({
|
|
...base,
|
|
identifier: parentGroupNo,
|
|
parentGroupNo,
|
|
}));
|
|
}
|
|
return [];
|
|
}
|
|
|
|
function appendOrderRecord(result, options = {}) {
|
|
if (!result || result.status === 'blocked') return [];
|
|
return extractCreateRecords(result).map((record) => appendRawRecord(record, options));
|
|
}
|
|
|
|
function readRecords(options = {}) {
|
|
const registryPath = options.registryPath || defaultRegistryPath();
|
|
if (!fs.existsSync(registryPath)) return [];
|
|
return fs.readFileSync(registryPath, 'utf8')
|
|
.split(/\r?\n/)
|
|
.filter(Boolean)
|
|
.map((line) => JSON.parse(line));
|
|
}
|
|
|
|
function findOrderByIdentifier(identifier, options = {}) {
|
|
const target = normalizeIdentifier(identifier);
|
|
return readRecords(options)
|
|
.filter((record) => [
|
|
record.identifier,
|
|
record.groupNo,
|
|
record.parentGroupNo,
|
|
record.childOrderNo,
|
|
].some((value) => normalizeIdentifier(value) === target))
|
|
.sort((a, b) => String(b.updatedAt || '').localeCompare(String(a.updatedAt || '')))[0] || null;
|
|
}
|
|
|
|
module.exports = {
|
|
defaultRegistryPath,
|
|
normalizeIdentifier,
|
|
appendRawRecord,
|
|
appendOrderRecord,
|
|
readRecords,
|
|
findOrderByIdentifier,
|
|
extractCreateRecords,
|
|
};
|