This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
export async function registerAdminExportRoutes(app, options) {
|
||||
const {
|
||||
adminExportService,
|
||||
authenticateAdmin = async () => {
|
||||
throw createApplicationError('UNAUTHORIZED', 'Authentication is required.', 401);
|
||||
},
|
||||
} = options;
|
||||
|
||||
app.addHook('preHandler', async (request) => {
|
||||
const admin = await authenticateAdmin(request);
|
||||
if (!admin?.sellerId) {
|
||||
throw createApplicationError('UNAUTHORIZED', 'Authentication is required.', 401);
|
||||
}
|
||||
request.admin = admin;
|
||||
});
|
||||
|
||||
app.get('/exports/inventory.json', async (request, reply) => {
|
||||
const data = await adminExportService.inventoryJson({
|
||||
sellerId: request.admin.sellerId,
|
||||
});
|
||||
|
||||
return reply
|
||||
.type('application/json; charset=utf-8')
|
||||
.header('content-disposition', 'attachment; filename="inventory.json"')
|
||||
.send(JSON.stringify(data));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { toAdminInventoryItemDto } from '@pokemon-inventory/contracts/inventory';
|
||||
|
||||
export function createAdminExportService({ inventoryItemService }) {
|
||||
return {
|
||||
async inventoryJson({ sellerId }) {
|
||||
const inventoryItems = await inventoryItemService.listBySellerId(sellerId);
|
||||
return inventoryItems.map(toAdminInventoryItemDto);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user