feat: add client workspace management
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
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 { contracts, clients } from '$lib/server/db/schema';
|
||||
import { archiveSchema } from '$lib/schemas/shared.schema';
|
||||
import { contractCreateSchema, contractEditSchema } from '$lib/schemas/contracts.schema';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
|
||||
async function loadOptions() {
|
||||
const clientRows = await db
|
||||
.select({ id: clients.id, name: clients.name })
|
||||
.from(clients)
|
||||
.where(isNull(clients.archivedAt))
|
||||
.orderBy(asc(clients.name));
|
||||
|
||||
return {
|
||||
clients: clientRows.map((client) => ({ label: client.name, value: client.id }))
|
||||
};
|
||||
}
|
||||
|
||||
export const load: PageServerLoad = async ({ params }) => {
|
||||
const records = await db
|
||||
.select()
|
||||
.from(contracts)
|
||||
.where(and(eq(contracts.clientId, params.id), isNull(contracts.archivedAt)))
|
||||
.orderBy(asc(contracts.title));
|
||||
|
||||
return {
|
||||
records: records.map((record) => ({
|
||||
...record,
|
||||
formValues: {
|
||||
id: record.id,
|
||||
clientId: record.clientId ?? '',
|
||||
title: record.title ?? '',
|
||||
startDate: record.startDate ?? '',
|
||||
endDate: record.endDate ?? '',
|
||||
status: record.status ?? '',
|
||||
valueGbp: record.valueGbp ?? '',
|
||||
notes: record.notes ?? ''
|
||||
}
|
||||
})),
|
||||
options: await loadOptions(),
|
||||
createForm: await superValidate({ clientId: params.id }, zod4(contractCreateSchema), {
|
||||
id: 'contracts-create'
|
||||
}),
|
||||
editForm: await superValidate(zod4(contractEditSchema), { id: 'contracts-edit' }),
|
||||
archiveForm: await superValidate(zod4(archiveSchema), { id: 'contracts-archive' })
|
||||
};
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
create: async ({ params, request }) => {
|
||||
const formData = await request.formData();
|
||||
formData.set('clientId', params.id);
|
||||
const form = await superValidate(formData, zod4(contractCreateSchema), {
|
||||
id: 'contracts-create'
|
||||
});
|
||||
|
||||
if (!form.valid) return message(form, 'Check the highlighted fields.', { status: 400 });
|
||||
|
||||
try {
|
||||
await db.insert(contracts).values({
|
||||
id: crypto.randomUUID(),
|
||||
clientId: form.data.clientId,
|
||||
title: form.data.title,
|
||||
startDate: form.data.startDate,
|
||||
endDate: form.data.endDate || null,
|
||||
status: form.data.status,
|
||||
valueGbp: form.data.valueGbp,
|
||||
notes: form.data.notes || null,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date()
|
||||
});
|
||||
} catch {
|
||||
return message(form, 'Unable to create contract.', { status: 400 });
|
||||
}
|
||||
|
||||
return message(form, 'Contract created.');
|
||||
},
|
||||
|
||||
edit: async ({ params, request }) => {
|
||||
const formData = await request.formData();
|
||||
formData.set('clientId', params.id);
|
||||
const form = await superValidate(formData, zod4(contractEditSchema), {
|
||||
id: 'contracts-edit'
|
||||
});
|
||||
|
||||
if (!form.valid) return message(form, 'Check the highlighted fields.', { status: 400 });
|
||||
|
||||
try {
|
||||
await db
|
||||
.update(contracts)
|
||||
.set({
|
||||
clientId: form.data.clientId,
|
||||
title: form.data.title,
|
||||
startDate: form.data.startDate,
|
||||
endDate: form.data.endDate || null,
|
||||
status: form.data.status,
|
||||
valueGbp: form.data.valueGbp,
|
||||
notes: form.data.notes || null,
|
||||
updatedAt: new Date()
|
||||
})
|
||||
.where(and(eq(contracts.id, form.data.id), eq(contracts.clientId, params.id)));
|
||||
} catch {
|
||||
return message(form, 'Unable to update contract.', { status: 400 });
|
||||
}
|
||||
|
||||
return message(form, 'Contract updated.');
|
||||
},
|
||||
|
||||
archive: async ({ params, request }) => {
|
||||
const form = await superValidate(await request.formData(), zod4(archiveSchema), {
|
||||
id: 'contracts-archive'
|
||||
});
|
||||
|
||||
if (!form.valid) return message(form, 'Contract id is required.', { status: 400 });
|
||||
|
||||
await db
|
||||
.update(contracts)
|
||||
.set({ archivedAt: new Date(), updatedAt: new Date() })
|
||||
.where(and(eq(contracts.id, form.data.id), eq(contracts.clientId, params.id)));
|
||||
|
||||
return message(form, 'Contract archived.');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user