This commit is contained in:
@@ -26,4 +26,15 @@ export async function registerAdminExportRoutes(app, options) {
|
||||
.header('content-disposition', 'attachment; filename="inventory.json"')
|
||||
.send(JSON.stringify(data));
|
||||
});
|
||||
|
||||
app.get('/exports/inventory.csv', async (request, reply) => {
|
||||
const data = await adminExportService.inventoryCsv({
|
||||
sellerId: request.admin.sellerId,
|
||||
});
|
||||
|
||||
return reply
|
||||
.type('text/csv; charset=utf-8')
|
||||
.header('content-disposition', 'attachment; filename="inventory.csv"')
|
||||
.send(data);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,10 +1,28 @@
|
||||
import { toAdminInventoryItemDto } from '@pokemon-inventory/contracts/inventory';
|
||||
|
||||
export function createAdminExportService({ inventoryItemService }) {
|
||||
async function inventoryRows(sellerId) {
|
||||
const inventoryItems = await inventoryItemService.listBySellerId(sellerId);
|
||||
return inventoryItems.map(toAdminInventoryItemDto);
|
||||
}
|
||||
|
||||
return {
|
||||
async inventoryJson({ sellerId }) {
|
||||
const inventoryItems = await inventoryItemService.listBySellerId(sellerId);
|
||||
return inventoryItems.map(toAdminInventoryItemDto);
|
||||
return inventoryRows(sellerId);
|
||||
},
|
||||
|
||||
async inventoryCsv({ sellerId }) {
|
||||
const rows = await inventoryRows(sellerId);
|
||||
const columns = ['id', 'productId', 'conditionId', 'status', 'notes', 'createdAt', 'updatedAt'];
|
||||
return [
|
||||
columns.join(','),
|
||||
...rows.map((row) => columns.map((column) => escapeCsvValue(row[column])).join(',')),
|
||||
].join('\r\n');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function escapeCsvValue(value) {
|
||||
const text = value == null ? '' : String(value);
|
||||
return `"${text.replaceAll('"', '""')}"`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user