update export request

This commit is contained in:
2026-11-10 11:01:24 +07:00
parent 63866b14a1
commit 00122e5b4a
16 changed files with 1108 additions and 382 deletions

View File

@@ -6,7 +6,8 @@ function parseAllocations(value) {
return rows.map(row => ({
projectName: String(row.projectName || '').trim(),
borrowerName: String(row.borrowerName || '').trim(),
quantity: Number(row.quantity)
quantity: Number(row.quantity),
...(row.allocationType === 'export' ? { allocationType: 'export' } : {})
})).filter(row => row.projectName && Number.isSafeInteger(row.quantity) && row.quantity > 0);
}
@@ -38,9 +39,13 @@ function addAllocations(current, rows, borrowerName) {
return [...parseAllocations(current), ...rows.map(row => ({ ...row, borrowerName }))];
}
function addProjectExports(current, rows) {
return [...parseAllocations(current), ...rows.map(row => ({ ...row, borrowerName: '', allocationType: 'export' }))];
}
function releaseAllocations(current, borrowerName, returnedQuantity, borrowerOutstanding) {
const rows = parseAllocations(current);
const matches = row => row.borrowerName.toLowerCase() === borrowerName.toLowerCase();
const matches = row => row.allocationType !== 'export' && row.borrowerName.toLowerCase() === borrowerName.toLowerCase();
const attributed = rows.filter(matches).reduce((sum, row) => sum + row.quantity, 0);
// Older loans without a project (including request-based loans) are consumed first.
let remaining = Math.max(returnedQuantity - Math.max(borrowerOutstanding - attributed, 0), 0);
@@ -53,4 +58,4 @@ function releaseAllocations(current, borrowerName, returnedQuantity, borrowerOut
return rows.filter(row => row.quantity > 0);
}
module.exports = { parseAllocations, normalizeExportAllocations, addAllocations, releaseAllocations };
module.exports = { parseAllocations, normalizeExportAllocations, addAllocations, addProjectExports, releaseAllocations };