This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { and, asc, eq } from 'drizzle-orm';
|
||||
import { and, asc, desc, eq } from 'drizzle-orm';
|
||||
|
||||
import { pricingRefreshJobs } from '../../../infrastructure/database/schema.js';
|
||||
|
||||
@@ -38,6 +38,20 @@ export function createPricingRefreshJobRepository(db) {
|
||||
return rows.length;
|
||||
},
|
||||
|
||||
async listForSeller(sellerId) {
|
||||
return db.select().from(pricingRefreshJobs)
|
||||
.where(eq(pricingRefreshJobs.sellerId, sellerId))
|
||||
.orderBy(desc(pricingRefreshJobs.createdAt));
|
||||
},
|
||||
|
||||
async retryForSeller(id, sellerId) {
|
||||
const [job] = await db.update(pricingRefreshJobs)
|
||||
.set({ status: 'PENDING', error: null, observations: [], valuation: null, updatedAt: new Date() })
|
||||
.where(and(eq(pricingRefreshJobs.id, id), eq(pricingRefreshJobs.sellerId, sellerId), eq(pricingRefreshJobs.status, 'FAILED')))
|
||||
.returning();
|
||||
return job ?? null;
|
||||
},
|
||||
|
||||
async complete(id, observations, valuation) {
|
||||
const [completed] = await db.update(pricingRefreshJobs)
|
||||
.set({ status: 'COMPLETED', observations, valuation, error: null, updatedAt: new Date() })
|
||||
|
||||
@@ -80,4 +80,17 @@ export async function registerAdminPricingRoutes(app, options) {
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
app.get('/jobs', async (request) => ({
|
||||
data: (await pricingService.listJobs({ sellerId: request.admin.sellerId })).map((job) => ({
|
||||
id: job.id, status: job.status, providerId: job.providerId, inventoryItemId: job.inventoryItemId,
|
||||
error: job.error ?? null, createdAt: job.createdAt, updatedAt: job.updatedAt,
|
||||
})),
|
||||
}));
|
||||
|
||||
app.post('/jobs/:id/retry', async (request, reply) => {
|
||||
await authorizeUnsafeRequest?.(request.cookies[sessionCookieName], request.headers['x-csrf-token']);
|
||||
const job = await pricingService.retryJob({ sellerId: request.admin.sellerId, jobId: request.params.id });
|
||||
return reply.code(202).send({ data: { id: job.id, status: job.status, createdAt: job.createdAt } });
|
||||
});
|
||||
}
|
||||
@@ -72,5 +72,15 @@ export function createPricingService({ pricingRepository, pricingProviderRegistr
|
||||
if (!job) throw createApplicationError('PRICING_JOB_NOT_FOUND', 'Pricing job was not found.', 404);
|
||||
return job;
|
||||
},
|
||||
|
||||
async listJobs({ sellerId }) {
|
||||
return pricingRefreshJobRepository?.listForSeller(sellerId) ?? [];
|
||||
},
|
||||
|
||||
async retryJob({ sellerId, jobId }) {
|
||||
const job = await pricingRefreshJobRepository?.retryForSeller(jobId, sellerId);
|
||||
if (!job) throw createApplicationError('PRICING_JOB_NOT_RETRYABLE', 'Pricing job was not found or is not failed.', 409);
|
||||
return job;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user