feat: persist pricing refresh jobs
Quality / test (push) Canceled after 0s

This commit is contained in:
2026-09-09 11:53:14 -04:00
parent 39b4aab058
commit 409d2cd0ca
9 changed files with 136 additions and 5 deletions
@@ -0,0 +1,44 @@
import { and, asc, eq } from 'drizzle-orm';
import { pricingRefreshJobs } from '../../../infrastructure/database/schema.js';
export function createPricingRefreshJobRepository(db) {
return {
async create(job) {
const [created] = await db.insert(pricingRefreshJobs).values(job).returning();
return created;
},
async claimNext() {
return db.transaction(async (transaction) => {
const [job] = await transaction.select().from(pricingRefreshJobs)
.where(eq(pricingRefreshJobs.status, 'PENDING'))
.orderBy(asc(pricingRefreshJobs.createdAt))
.limit(1)
.for('update', { skipLocked: true });
if (!job) return null;
const [claimed] = await transaction.update(pricingRefreshJobs)
.set({ status: 'PROCESSING', updatedAt: new Date(), error: null })
.where(and(eq(pricingRefreshJobs.id, job.id), eq(pricingRefreshJobs.status, 'PENDING')))
.returning();
return claimed ?? null;
});
},
async complete(id, observations, valuation) {
const [completed] = await db.update(pricingRefreshJobs)
.set({ status: 'COMPLETED', observations, valuation, error: null, updatedAt: new Date() })
.where(and(eq(pricingRefreshJobs.id, id), eq(pricingRefreshJobs.status, 'PROCESSING')))
.returning();
return completed ?? null;
},
async fail(id, message) {
const [failed] = await db.update(pricingRefreshJobs)
.set({ status: 'FAILED', error: message, updatedAt: new Date() })
.where(and(eq(pricingRefreshJobs.id, id), eq(pricingRefreshJobs.status, 'PROCESSING')))
.returning();
return failed ?? null;
},
};
}