feat: resolve refresh providers from mappings
Quality / test (push) Canceled after 0s

This commit is contained in:
2026-09-09 13:01:16 -04:00
parent 4d3af37cd7
commit 4c041e069d
7 changed files with 55 additions and 4 deletions
+1 -1
View File
@@ -139,7 +139,7 @@ async function startServer() {
saleService: createSaleService({ saleRepository }),
acquisitionService: createAcquisitionService({ acquisitionRepository }),
}),
pricingService: createPricingService({ pricingRepository, pricingRefreshJobRepository }),
pricingService: createPricingService({ pricingRepository, pricingRefreshJobRepository, providerMappingRepository }),
providerMappingService: createProviderMappingService({ providerMappingRepository }),
publicListingService: createPublicListingService({ listingRepository, publicImageService }),
productService: createProductService({ productRepository }),
@@ -23,6 +23,19 @@ export function createProviderMappingRepository(db) {
.then((rows) => rows.map(({ mapping }) => mapping));
},
async findActiveForSeller(productId, sellerId, providerId) {
const conditions = [
eq(pricingProviderMappings.productId, productId),
eq(products.sellerId, sellerId),
eq(pricingProviderMappings.status, 'ACTIVE'),
];
if (providerId) conditions.push(eq(pricingProviderMappings.providerId, providerId));
const [row] = await db.select({ mapping: pricingProviderMappings }).from(pricingProviderMappings)
.innerJoin(products, eq(products.id, pricingProviderMappings.productId))
.where(and(...conditions));
return row?.mapping ?? null;
},
async disableForSeller(id, sellerId) {
const [mapping] = await db.select().from(pricingProviderMappings)
.innerJoin(products, eq(products.id, pricingProviderMappings.productId))
@@ -1,7 +1,7 @@
import { createPricingObservation } from '../domain/pricing-observation.js';
import { createApplicationError } from '../../../shared/errors/application-error.js';
export function createPricingService({ pricingRepository, pricingProviderRegistry, pricingRefreshJobRepository }) {
export function createPricingService({ pricingRepository, pricingProviderRegistry, pricingRefreshJobRepository, providerMappingRepository }) {
return {
async addObservation({ sellerId, ...input }) {
const observation = createPricingObservation(input);
@@ -55,7 +55,15 @@ export function createPricingService({ pricingRepository, pricingProviderRegistr
async enqueueRefresh({ sellerId, inventoryItemId, providerId, productId, conditionId }) {
if (!pricingRefreshJobRepository) throw createApplicationError('PRICING_QUEUE_UNAVAILABLE', 'Pricing refresh queue is unavailable.', 503);
const job = await pricingRefreshJobRepository.create({ sellerId, inventoryItemId, providerId, productId, conditionId });
const mapping = await providerMappingRepository?.findActiveForSeller(productId, sellerId, providerId);
if (!mapping) throw createApplicationError('PRICING_PROVIDER_MAPPING_NOT_FOUND', 'No active pricing provider mapping exists for this product.', 409);
const job = await pricingRefreshJobRepository.create({
sellerId,
inventoryItemId,
providerId: mapping.providerId,
productId,
conditionId,
});
return job;
},