This commit is contained in:
@@ -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('&', '&')
|
||||
.replaceAll('<', '<')
|
||||
.replaceAll('>', '>')
|
||||
.replaceAll('"', '"')
|
||||
.replaceAll("'", ''');
|
||||
}
|
||||
|
||||
function escapeCsvValue(value) {
|
||||
const text = value == null ? '' : String(value);
|
||||
return `"${text.replaceAll('"', '""')}"`;
|
||||
|
||||
Reference in New Issue
Block a user