This commit is contained in:
@@ -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;
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user