feat: add admin dashboard summary
Quality / test (push) Canceled after 0s

Add seller-scoped dashboard aggregation and API, private financial summary DTOs, responsive admin metrics, and authenticated E2E coverage.
This commit is contained in:
2026-09-09 01:00:28 -04:00
parent 1d13c79a04
commit 455971431f
11 changed files with 177 additions and 1 deletions
@@ -0,0 +1,20 @@
import { toAdminDashboardDto } from '@pokemon-inventory/contracts/dashboard';
import { createApplicationError } from '../../../shared/errors/application-error.js';
export async function registerAdminDashboardRoutes(app, options) {
const {
authenticateAdmin = async () => { throw createApplicationError('UNAUTHORIZED', 'Authentication is required.', 401); },
dashboardService,
} = 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('/dashboard', async (request) => ({
data: toAdminDashboardDto(await dashboardService.get({ sellerId: request.admin.sellerId })),
}));
}
@@ -0,0 +1,38 @@
export function createDashboardService({ productService, inventoryItemService, listingService, saleService, acquisitionService }) {
return {
async get({ sellerId }) {
const [products, inventory, listings, sales, acquisitions] = await Promise.all([
productService.listBySellerId(sellerId),
inventoryItemService.listBySellerId(sellerId),
listingService.list({ sellerId }),
saleService.list({ sellerId }),
acquisitionService.list({ sellerId }),
]);
const activeListings = listings.filter(({ status }) => ['AVAILABLE', 'PENDING'].includes(status));
const completedSales = sales.filter(({ status }) => status !== 'VOIDED');
const total = (values, field) => values.reduce((sum, value) => sum + (value[field] ?? 0), 0);
return {
counts: {
products: products.length,
inventory: inventory.length,
activeInventory: inventory.filter(({ status }) => status === 'ACTIVE').length,
soldInventory: inventory.filter(({ status }) => status === 'SOLD').length,
availableListings: activeListings.length,
sales: completedSales.length,
acquisitions: acquisitions.length,
missingImages: listings.filter(({ imageCount }) => imageCount < 1).length,
},
financials: {
totalAcquisitionCostMinor: total(acquisitions, 'costBasisMinor'),
totalAskingValueMinor: total(activeListings, 'askingPriceMinor'),
totalNetSaleProceedsMinor: total(completedSales, 'netProceedsMinor'),
totalProfitMinor: total(completedSales, 'profitMinor'),
},
recentSales: completedSales.slice(0, 5),
recentAcquisitions: acquisitions.slice(0, 5),
};
},
};
}