This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
export async function runPricingRefreshJob({ job, providerRegistry, pricingService }) {
|
||||
if (!job || job.type !== 'PRICING_REFRESH') {
|
||||
throw new Error('Unsupported pricing refresh job.');
|
||||
}
|
||||
|
||||
const provider = providerRegistry?.get(job.providerId);
|
||||
if (!provider) {
|
||||
return {
|
||||
...job,
|
||||
status: 'FAILED',
|
||||
error: 'Pricing provider is not configured.',
|
||||
failedAt: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const observations = await provider.observe({
|
||||
inventoryItemId: job.inventoryItemId,
|
||||
productId: job.productId,
|
||||
conditionId: job.conditionId,
|
||||
});
|
||||
const savedObservations = [];
|
||||
for (const observation of observations) {
|
||||
savedObservations.push(await pricingService.addObservation({
|
||||
sellerId: job.sellerId,
|
||||
...observation,
|
||||
providerId: provider.id,
|
||||
inventoryItemId: job.inventoryItemId,
|
||||
productId: job.productId,
|
||||
conditionId: job.conditionId,
|
||||
}));
|
||||
}
|
||||
const valuation = await pricingService.calculate({
|
||||
sellerId: job.sellerId,
|
||||
inventoryItemId: job.inventoryItemId,
|
||||
});
|
||||
|
||||
return {
|
||||
...job,
|
||||
status: 'COMPLETED',
|
||||
observations: savedObservations,
|
||||
valuation,
|
||||
completedAt: new Date(),
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
...job,
|
||||
status: 'FAILED',
|
||||
error: error instanceof Error ? error.message : 'Pricing refresh failed.',
|
||||
failedAt: new Date(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user