fix: batch billing cycle writes
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { fail } from '@sveltejs/kit';
|
import { fail } from '@sveltejs/kit';
|
||||||
|
import type { BatchItem } from 'drizzle-orm/batch';
|
||||||
import { and, asc, desc, eq, gte, inArray, isNull, like, lte, ne } from 'drizzle-orm';
|
import { and, asc, desc, eq, gte, inArray, isNull, like, lte, ne } from 'drizzle-orm';
|
||||||
import { createSearchParamsSchema, validateSearchParams } from 'runed/kit';
|
import { createSearchParamsSchema, validateSearchParams } from 'runed/kit';
|
||||||
import { db } from '$lib/server/db';
|
import { db } from '$lib/server/db';
|
||||||
@@ -458,14 +459,17 @@ export const actions: Actions = {
|
|||||||
const invoicePrefix = invoiceNumberPrefix(invoiceDate);
|
const invoicePrefix = invoiceNumberPrefix(invoiceDate);
|
||||||
const latestSequence = await latestInvoiceSequence(activeOrganizationId, invoicePrefix);
|
const latestSequence = await latestInvoiceSequence(activeOrganizationId, invoicePrefix);
|
||||||
|
|
||||||
await db.transaction(async (tx) => {
|
const now = new Date();
|
||||||
let invoiceOffset = 0;
|
const queries: BatchItem<'sqlite'>[] = [];
|
||||||
for (const client of groupedClients) {
|
let invoiceOffset = 0;
|
||||||
const invoiceId = crypto.randomUUID();
|
|
||||||
const invoiceNumber = `${invoicePrefix}-${String(latestSequence + invoiceOffset + 1).padStart(3, '0')}`;
|
|
||||||
invoiceOffset += 1;
|
|
||||||
|
|
||||||
await tx.insert(invoices).values({
|
for (const client of groupedClients) {
|
||||||
|
const invoiceId = crypto.randomUUID();
|
||||||
|
const invoiceNumber = `${invoicePrefix}-${String(latestSequence + invoiceOffset + 1).padStart(3, '0')}`;
|
||||||
|
invoiceOffset += 1;
|
||||||
|
|
||||||
|
queries.push(
|
||||||
|
db.insert(invoices).values({
|
||||||
id: invoiceId,
|
id: invoiceId,
|
||||||
organizationId: activeOrganizationId,
|
organizationId: activeOrganizationId,
|
||||||
clientId: client.clientId,
|
clientId: client.clientId,
|
||||||
@@ -476,11 +480,13 @@ export const actions: Actions = {
|
|||||||
taxGbp: 0,
|
taxGbp: 0,
|
||||||
totalGbp: client.totalGbp,
|
totalGbp: client.totalGbp,
|
||||||
notes: `Generated from billing run. Contract billing through ${preview.contractsCutoff}; services through ${preview.servicesCutoff}.`,
|
notes: `Generated from billing run. Contract billing through ${preview.contractsCutoff}; services through ${preview.servicesCutoff}.`,
|
||||||
createdAt: new Date(),
|
createdAt: now,
|
||||||
updatedAt: new Date()
|
updatedAt: now
|
||||||
});
|
})
|
||||||
|
);
|
||||||
|
|
||||||
await tx.insert(invoiceLines).values(
|
queries.push(
|
||||||
|
db.insert(invoiceLines).values(
|
||||||
client.lines.map((line) => ({
|
client.lines.map((line) => ({
|
||||||
id: crypto.randomUUID(),
|
id: crypto.randomUUID(),
|
||||||
organizationId: activeOrganizationId,
|
organizationId: activeOrganizationId,
|
||||||
@@ -494,29 +500,35 @@ export const actions: Actions = {
|
|||||||
quantity: line.quantity,
|
quantity: line.quantity,
|
||||||
unitPriceGbp: line.unitPriceGbp,
|
unitPriceGbp: line.unitPriceGbp,
|
||||||
totalGbp: line.totalGbp,
|
totalGbp: line.totalGbp,
|
||||||
createdAt: new Date(),
|
createdAt: now,
|
||||||
updatedAt: new Date()
|
updatedAt: now
|
||||||
}))
|
}))
|
||||||
);
|
)
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const contractBilledTo = new Map<string, string>();
|
const contractBilledTo = new Map<string, string>();
|
||||||
for (const line of includedLines) {
|
for (const line of includedLines) {
|
||||||
if (line.sourceType !== 'contract') continue;
|
if (line.sourceType !== 'contract') continue;
|
||||||
const current = contractBilledTo.get(line.sourceId);
|
const current = contractBilledTo.get(line.sourceId);
|
||||||
if (!current || line.periodEnd > current)
|
if (!current || line.periodEnd > current) contractBilledTo.set(line.sourceId, line.periodEnd);
|
||||||
contractBilledTo.set(line.sourceId, line.periodEnd);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (const [contractId, billedTo] of contractBilledTo) {
|
for (const [contractId, billedTo] of contractBilledTo) {
|
||||||
await tx
|
queries.push(
|
||||||
|
db
|
||||||
.update(contracts)
|
.update(contracts)
|
||||||
.set({ billedTo, updatedAt: new Date() })
|
.set({ billedTo, updatedAt: now })
|
||||||
.where(
|
.where(
|
||||||
and(eq(contracts.id, contractId), eq(contracts.organizationId, activeOrganizationId))
|
and(eq(contracts.id, contractId), eq(contracts.organizationId, activeOrganizationId))
|
||||||
);
|
)
|
||||||
}
|
);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
const [firstQuery, ...remainingQueries] = queries;
|
||||||
|
if (firstQuery) {
|
||||||
|
await db.batch([firstQuery, ...remainingQueries]);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
message: `Billing run complete. Created ${groupedClients.length} invoice${groupedClients.length === 1 ? '' : 's'}.`
|
message: `Billing run complete. Created ${groupedClients.length} invoice${groupedClients.length === 1 ? '' : 's'}.`
|
||||||
|
|||||||
Reference in New Issue
Block a user