This commit is contained in:
@@ -44,6 +44,7 @@ import { createAcquisitionService } from '../modules/acquisitions/services/acqui
|
||||
import { createDashboardService } from '../modules/dashboard/services/dashboard-service.js';
|
||||
import { createPricingRepository } from '../modules/pricing/repositories/pricing-repository.js';
|
||||
import { createPricingService } from '../modules/pricing/services/pricing-service.js';
|
||||
import { createPricingRefreshJobRepository } from '../modules/pricing/repositories/pricing-refresh-job-repository.js';
|
||||
|
||||
function parsePort(value) {
|
||||
const port = Number.parseInt(value ?? '3000', 10);
|
||||
@@ -70,6 +71,7 @@ async function startServer() {
|
||||
const saleRepository = createSaleRepository(database.db);
|
||||
const acquisitionRepository = createAcquisitionRepository(database.db);
|
||||
const pricingRepository = createPricingRepository(database.db);
|
||||
const pricingRefreshJobRepository = createPricingRefreshJobRepository(database.db);
|
||||
const userRepository = createUserRepository(database.db);
|
||||
const sessionRepository = createSessionRepository(database.db);
|
||||
const passwordResetRepository = createPasswordResetRepository(database.db);
|
||||
@@ -134,7 +136,7 @@ async function startServer() {
|
||||
saleService: createSaleService({ saleRepository }),
|
||||
acquisitionService: createAcquisitionService({ acquisitionRepository }),
|
||||
}),
|
||||
pricingService: createPricingService({ pricingRepository }),
|
||||
pricingService: createPricingService({ pricingRepository, pricingRefreshJobRepository }),
|
||||
publicListingService: createPublicListingService({ listingRepository, publicImageService }),
|
||||
productService: createProductService({ productRepository }),
|
||||
publicProductService: createPublicProductService({ productRepository }),
|
||||
|
||||
@@ -25,6 +25,12 @@ export function createPricingRefreshJobRepository(db) {
|
||||
});
|
||||
},
|
||||
|
||||
async findByIdForSeller(id, sellerId) {
|
||||
const [job] = await db.select().from(pricingRefreshJobs)
|
||||
.where(and(eq(pricingRefreshJobs.id, id), eq(pricingRefreshJobs.sellerId, sellerId)));
|
||||
return job ?? null;
|
||||
},
|
||||
|
||||
async complete(id, observations, valuation) {
|
||||
const [completed] = await db.update(pricingRefreshJobs)
|
||||
.set({ status: 'COMPLETED', observations, valuation, error: null, updatedAt: new Date() })
|
||||
|
||||
@@ -57,13 +57,26 @@ export async function registerAdminPricingRoutes(app, options) {
|
||||
app.post('/inventory/:id/pricing/refresh', async (request, reply) => {
|
||||
await authorizeUnsafeRequest?.(request.cookies[sessionCookieName], request.headers['x-csrf-token']);
|
||||
const { providerId, productId, conditionId } = request.body ?? {};
|
||||
const result = await pricingService.refresh({
|
||||
const job = await pricingService.enqueueRefresh({
|
||||
sellerId: request.admin.sellerId,
|
||||
inventoryItemId: request.params.id,
|
||||
providerId,
|
||||
productId,
|
||||
conditionId,
|
||||
});
|
||||
return reply.code(200).send({ data: result });
|
||||
return reply.code(202).send({ data: { id: job.id, status: job.status, createdAt: job.createdAt } });
|
||||
});
|
||||
|
||||
app.get('/jobs/:id', async (request) => {
|
||||
const job = await pricingService.getJob({ sellerId: request.admin.sellerId, jobId: request.params.id });
|
||||
return {
|
||||
data: {
|
||||
id: job.id,
|
||||
status: job.status,
|
||||
error: job.error ?? null,
|
||||
createdAt: job.createdAt,
|
||||
updatedAt: job.updatedAt,
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createPricingObservation } from '../domain/pricing-observation.js';
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
export function createPricingService({ pricingRepository, pricingProviderRegistry }) {
|
||||
export function createPricingService({ pricingRepository, pricingProviderRegistry, pricingRefreshJobRepository }) {
|
||||
return {
|
||||
async addObservation({ sellerId, ...input }) {
|
||||
const observation = createPricingObservation(input);
|
||||
@@ -52,5 +52,17 @@ export function createPricingService({ pricingRepository, pricingProviderRegistr
|
||||
|
||||
return { observations, valuation: await this.calculate({ sellerId, inventoryItemId }) };
|
||||
},
|
||||
|
||||
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 });
|
||||
return job;
|
||||
},
|
||||
|
||||
async getJob({ sellerId, jobId }) {
|
||||
const job = await pricingRefreshJobRepository?.findByIdForSeller(jobId, sellerId);
|
||||
if (!job) throw createApplicationError('PRICING_JOB_NOT_FOUND', 'Pricing job was not found.', 404);
|
||||
return job;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user