This commit is contained in:
@@ -144,6 +144,7 @@ async function startServer() {
|
||||
saleService: createSaleService({ saleRepository }),
|
||||
acquisitionService: createAcquisitionService({ acquisitionRepository }),
|
||||
pricingRepository,
|
||||
pricingRefreshJobRepository,
|
||||
}),
|
||||
pricingService: createPricingService({ pricingRepository, pricingRefreshJobRepository, providerMappingRepository, pricingProviderRegistry: pricingProviders }),
|
||||
providerMappingService: createProviderMappingService({ providerMappingRepository }),
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
export function createDashboardService({ productService, inventoryItemService, listingService, saleService, acquisitionService, pricingRepository }) {
|
||||
export function createDashboardService({ productService, inventoryItemService, listingService, saleService, acquisitionService, pricingRepository, pricingRefreshJobRepository }) {
|
||||
return {
|
||||
async get({ sellerId }) {
|
||||
const [products, inventory, listings, sales, acquisitions, valuations] = await Promise.all([
|
||||
const [products, inventory, listings, sales, acquisitions, valuations, failedPricingJobs] = await Promise.all([
|
||||
productService.listBySellerId(sellerId),
|
||||
inventoryItemService.listBySellerId(sellerId),
|
||||
listingService.list({ sellerId }),
|
||||
saleService.list({ sellerId }),
|
||||
acquisitionService.list({ sellerId }),
|
||||
pricingRepository?.listLatestValuationsForSeller(sellerId) ?? [],
|
||||
pricingRefreshJobRepository?.countFailedForSeller(sellerId) ?? 0,
|
||||
]);
|
||||
|
||||
const activeListings = listings.filter(({ status }) => ['AVAILABLE', 'PENDING'].includes(status));
|
||||
@@ -28,6 +29,7 @@ export function createDashboardService({ productService, inventoryItemService, l
|
||||
missingImages: listings.filter(({ imageCount }) => imageCount < 1).length,
|
||||
missingPricing: inventory.filter(({ id }) => !valuationByItem.has(id)).length,
|
||||
stalePricing: valuations.filter(({ calculatedAt }) => Date.now() - new Date(calculatedAt).getTime() > staleThreshold).length,
|
||||
failedPricingJobs,
|
||||
},
|
||||
financials: {
|
||||
totalAcquisitionCostMinor: total(acquisitions, 'costBasisMinor'),
|
||||
|
||||
@@ -31,6 +31,13 @@ export function createPricingRefreshJobRepository(db) {
|
||||
return job ?? null;
|
||||
},
|
||||
|
||||
async countFailedForSeller(sellerId) {
|
||||
const rows = await db.select({ id: pricingRefreshJobs.id })
|
||||
.from(pricingRefreshJobs)
|
||||
.where(and(eq(pricingRefreshJobs.sellerId, sellerId), eq(pricingRefreshJobs.status, 'FAILED')));
|
||||
return rows.length;
|
||||
},
|
||||
|
||||
async complete(id, observations, valuation) {
|
||||
const [completed] = await db.update(pricingRefreshJobs)
|
||||
.set({ status: 'COMPLETED', observations, valuation, error: null, updatedAt: new Date() })
|
||||
|
||||
@@ -11,6 +11,7 @@ test('aggregates seller dashboard counts and financial totals', async () => {
|
||||
saleService: { async list() { return [{ status: 'COMPLETED', netProceedsMinor: 2200, profitMinor: 700 }]; } },
|
||||
acquisitionService: { async list() { return [{ costBasisMinor: 1500 }]; } },
|
||||
pricingRepository: { async listLatestValuationsForSeller() { return [{ inventoryItemId: 'item-1', valueMinor: 2800, calculatedAt: new Date() }]; } },
|
||||
pricingRefreshJobRepository: { async countFailedForSeller() { return 2; } },
|
||||
});
|
||||
|
||||
const dashboard = await service.get({ sellerId: 'seller-1' });
|
||||
@@ -25,6 +26,7 @@ test('aggregates seller dashboard counts and financial totals', async () => {
|
||||
missingImages: 0,
|
||||
missingPricing: 1,
|
||||
stalePricing: 0,
|
||||
failedPricingJobs: 2,
|
||||
});
|
||||
assert.deepEqual(dashboard.financials, {
|
||||
totalAcquisitionCostMinor: 1500,
|
||||
|
||||
Reference in New Issue
Block a user