Expose public condition labels and safe market values on listing details, with Storybook coverage for market value presentation.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { and, asc, count, desc, eq, ilike, inArray, or, sql } from 'drizzle-orm';
|
||||
|
||||
import {
|
||||
conditions,
|
||||
inventoryItems,
|
||||
listings,
|
||||
marketValueOverrides,
|
||||
marketValuations,
|
||||
products,
|
||||
} from '../../../infrastructure/database/schema.js';
|
||||
|
||||
@@ -134,17 +137,31 @@ export function createListingRepository(db) {
|
||||
|
||||
async findPublicById(listingId) {
|
||||
const [result] = await db
|
||||
.select({ listing: listings })
|
||||
.select({ listing: listings, condition: conditions })
|
||||
.from(listings)
|
||||
.innerJoin(inventoryItems, eq(inventoryItems.id, listings.inventoryItemId))
|
||||
.innerJoin(products, eq(products.id, inventoryItems.productId))
|
||||
.innerJoin(conditions, eq(conditions.id, inventoryItems.conditionId))
|
||||
.where(and(
|
||||
eq(listings.id, listingId),
|
||||
inArray(listings.status, ['AVAILABLE', 'PENDING']),
|
||||
eq(inventoryItems.status, 'ACTIVE'),
|
||||
));
|
||||
|
||||
return result?.listing ?? null;
|
||||
if (!result) return null;
|
||||
const [override] = await db.select().from(marketValueOverrides)
|
||||
.where(eq(marketValueOverrides.inventoryItemId, result.listing.inventoryItemId))
|
||||
.orderBy(desc(marketValueOverrides.createdAt)).limit(1);
|
||||
const [valuation] = await db.select().from(marketValuations)
|
||||
.where(eq(marketValuations.inventoryItemId, result.listing.inventoryItemId))
|
||||
.orderBy(desc(marketValuations.calculatedAt)).limit(1);
|
||||
return {
|
||||
...result.listing,
|
||||
conditionName: result.condition.name,
|
||||
conditionCode: result.condition.code,
|
||||
marketValueMinor: override?.valueMinor ?? valuation?.valueMinor ?? null,
|
||||
marketValueCurrency: override?.currency ?? valuation?.currency ?? null,
|
||||
};
|
||||
},
|
||||
|
||||
async updateStatus(listingId, status) {
|
||||
|
||||
Reference in New Issue
Block a user