feat: add admin inventory Excel export
Quality / test (push) Canceled after 0s

This commit is contained in:
2026-09-10 01:11:07 -04:00
parent d6b4dbec5f
commit 24f51212e5
8 changed files with 91 additions and 0 deletions
@@ -37,4 +37,15 @@ export async function registerAdminExportRoutes(app, options) {
.header('content-disposition', 'attachment; filename="inventory.csv"')
.send(data);
});
app.get('/exports/inventory.xls', async (request, reply) => {
const data = await adminExportService.inventoryExcel({
sellerId: request.admin.sellerId,
});
return reply
.type('application/vnd.ms-excel; charset=utf-8')
.header('content-disposition', 'attachment; filename="inventory.xls"')
.send(data);
});
}
@@ -19,9 +19,34 @@ export function createAdminExportService({ inventoryItemService }) {
...rows.map((row) => columns.map((column) => escapeCsvValue(row[column])).join(',')),
].join('\r\n');
},
async inventoryExcel({ sellerId }) {
const rows = await inventoryRows(sellerId);
const columns = ['id', 'productId', 'conditionId', 'status', 'notes', 'createdAt', 'updatedAt'];
const header = columns.map((column) => excelCell(column, true)).join('');
const body = rows.map((row) => (
`<Row>${columns.map((column) => excelCell(row[column])).join('')}</Row>`
)).join('');
return `<?xml version="1.0"?>\n<?mso-application progid="Excel.Sheet"?>\n<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"><Styles><Style ss:ID="Header"><Font ss:Bold="1" /></Style></Styles><Worksheet ss:Name="Inventory"><Table><Row>${header}</Row>${body}</Table></Worksheet></Workbook>`;
},
};
}
function excelCell(value, header = false) {
const style = header ? ' ss:StyleID="Header"' : '';
return `<Cell${style}><Data ss:Type="String">${escapeXml(value)}</Data></Cell>`;
}
function escapeXml(value) {
return String(value ?? '')
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&apos;');
}
function escapeCsvValue(value) {
const text = value == null ? '' : String(value);
return `"${text.replaceAll('"', '""')}"`;