40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { and, asc, eq, isNull } from 'drizzle-orm';
|
|
import { db } from '$lib/server/db';
|
|
import { invoices, clients } from '$lib/server/db/schema';
|
|
import { loadOrganizationContext } from '$lib/server/organizations';
|
|
import type { PageServerLoad } from './$types';
|
|
|
|
async function loadOptions(organizationId: string) {
|
|
const clientRows = await db
|
|
.select({ id: clients.id, name: clients.name })
|
|
.from(clients)
|
|
.where(and(eq(clients.organizationId, organizationId), isNull(clients.archivedAt)))
|
|
.orderBy(asc(clients.name));
|
|
|
|
return {
|
|
clients: clientRows.map((client) => ({ label: client.name, value: client.id }))
|
|
};
|
|
}
|
|
|
|
export const load: PageServerLoad = async ({ locals, params }) => {
|
|
const { activeOrganizationId } = await loadOrganizationContext(locals);
|
|
const records = await db
|
|
.select()
|
|
.from(invoices)
|
|
.where(
|
|
and(
|
|
eq(invoices.clientId, params.id),
|
|
eq(invoices.organizationId, activeOrganizationId),
|
|
isNull(invoices.archivedAt)
|
|
)
|
|
)
|
|
.orderBy(asc(invoices.invoiceNumber));
|
|
|
|
return {
|
|
records: records.map((record) => ({
|
|
...record
|
|
})),
|
|
options: await loadOptions(activeOrganizationId)
|
|
};
|
|
};
|