update: split workspace pages into reusable components
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
import { and, asc, eq, isNull } from 'drizzle-orm';
|
||||
import { message, superValidate } from 'sveltekit-superforms/server';
|
||||
import { zod4 } from 'sveltekit-superforms/adapters';
|
||||
import { db } from '$lib/server/db';
|
||||
import { invoices, clients } from '$lib/server/db/schema';
|
||||
import { loadOrganizationContext } from '$lib/server/organizations';
|
||||
import { archiveSchema } from '$lib/schemas/shared.schema';
|
||||
import { invoiceEditSchema } from '$lib/schemas/invoices.schema';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
async function loadOptions(organizationId: string) {
|
||||
const clientRows = await db
|
||||
@@ -36,89 +32,8 @@ export const load: PageServerLoad = async ({ locals, params }) => {
|
||||
|
||||
return {
|
||||
records: records.map((record) => ({
|
||||
...record,
|
||||
formValues: {
|
||||
id: record.id,
|
||||
clientId: record.clientId ?? '',
|
||||
invoiceNumber: record.invoiceNumber ?? '',
|
||||
issueDate: record.issueDate ?? '',
|
||||
dueDate: record.dueDate ?? '',
|
||||
status: record.status ?? '',
|
||||
subtotalGbp: record.subtotalGbp ?? '',
|
||||
taxGbp: record.taxGbp ?? '',
|
||||
totalGbp: record.totalGbp ?? '',
|
||||
notes: record.notes ?? ''
|
||||
}
|
||||
...record
|
||||
})),
|
||||
options: await loadOptions(activeOrganizationId),
|
||||
editForm: await superValidate(zod4(invoiceEditSchema), {
|
||||
id: 'invoices-edit',
|
||||
errors: false
|
||||
}),
|
||||
archiveForm: await superValidate(zod4(archiveSchema), {
|
||||
id: 'invoices-archive',
|
||||
errors: false
|
||||
})
|
||||
options: await loadOptions(activeOrganizationId)
|
||||
};
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
edit: async ({ locals, params, request }) => {
|
||||
const { activeOrganizationId } = await loadOrganizationContext(locals);
|
||||
const formData = await request.formData();
|
||||
formData.set('clientId', params.id);
|
||||
const form = await superValidate(formData, zod4(invoiceEditSchema), { id: 'invoices-edit' });
|
||||
|
||||
if (!form.valid) return message(form, 'Check the highlighted fields.', { status: 400 });
|
||||
|
||||
try {
|
||||
await db
|
||||
.update(invoices)
|
||||
.set({
|
||||
clientId: form.data.clientId,
|
||||
invoiceNumber: form.data.invoiceNumber,
|
||||
issueDate: form.data.issueDate,
|
||||
dueDate: form.data.dueDate,
|
||||
status: form.data.status,
|
||||
subtotalGbp: form.data.subtotalGbp,
|
||||
taxGbp: form.data.taxGbp,
|
||||
totalGbp: form.data.totalGbp,
|
||||
notes: form.data.notes || null,
|
||||
updatedAt: new Date()
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(invoices.id, form.data.id),
|
||||
eq(invoices.clientId, params.id),
|
||||
eq(invoices.organizationId, activeOrganizationId)
|
||||
)
|
||||
);
|
||||
} catch {
|
||||
return message(form, 'Unable to update invoice.', { status: 400 });
|
||||
}
|
||||
|
||||
return message(form, 'Invoice updated.');
|
||||
},
|
||||
|
||||
archive: async ({ locals, params, request }) => {
|
||||
const { activeOrganizationId } = await loadOrganizationContext(locals);
|
||||
const form = await superValidate(await request.formData(), zod4(archiveSchema), {
|
||||
id: 'invoices-archive'
|
||||
});
|
||||
|
||||
if (!form.valid) return message(form, 'Invoice id is required.', { status: 400 });
|
||||
|
||||
await db
|
||||
.update(invoices)
|
||||
.set({ archivedAt: new Date(), updatedAt: new Date() })
|
||||
.where(
|
||||
and(
|
||||
eq(invoices.id, form.data.id),
|
||||
eq(invoices.clientId, params.id),
|
||||
eq(invoices.organizationId, activeOrganizationId)
|
||||
)
|
||||
);
|
||||
|
||||
return message(form, 'Invoice archived.');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user