refactor: scope dashboard records by organization

Attach clients, rooms, services, bookings, contacts, addresses, invoices, and contracts to organizations.

Filter dashboard loads and related option lists by the active organization from the session.

Ensure create, update, and archive actions only affect records in the active organization.
This commit is contained in:
2026-06-06 14:14:12 +01:00
parent b5ba84b19f
commit cbd2fb2921
23 changed files with 369 additions and 122 deletions
@@ -3,15 +3,16 @@ 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 { invoiceCreateSchema, invoiceEditSchema } from '$lib/schemas/invoices.schema';
import type { Actions, PageServerLoad } from './$types';
async function loadOptions() {
async function loadOptions(organizationId: string) {
const clientRows = await db
.select({ id: clients.id, name: clients.name })
.from(clients)
.where(isNull(clients.archivedAt))
.where(and(eq(clients.organizationId, organizationId), isNull(clients.archivedAt)))
.orderBy(asc(clients.name));
return {
@@ -19,11 +20,18 @@ async function loadOptions() {
};
}
export const load: PageServerLoad = async ({ params }) => {
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), isNull(invoices.archivedAt)))
.where(
and(
eq(invoices.clientId, params.id),
eq(invoices.organizationId, activeOrganizationId),
isNull(invoices.archivedAt)
)
)
.orderBy(asc(invoices.invoiceNumber));
return {
@@ -42,7 +50,7 @@ export const load: PageServerLoad = async ({ params }) => {
notes: record.notes ?? ''
}
})),
options: await loadOptions(),
options: await loadOptions(activeOrganizationId),
createForm: await superValidate({ clientId: params.id }, zod4(invoiceCreateSchema), {
id: 'invoices-create'
}),
@@ -52,7 +60,8 @@ export const load: PageServerLoad = async ({ params }) => {
};
export const actions: Actions = {
create: async ({ params, request }) => {
create: 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(invoiceCreateSchema), {
@@ -64,6 +73,7 @@ export const actions: Actions = {
try {
await db.insert(invoices).values({
id: crypto.randomUUID(),
organizationId: activeOrganizationId,
clientId: form.data.clientId,
invoiceNumber: form.data.invoiceNumber,
issueDate: form.data.issueDate,
@@ -83,7 +93,8 @@ export const actions: Actions = {
return message(form, 'Invoice created.');
},
edit: async ({ params, request }) => {
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' });
@@ -105,7 +116,13 @@ export const actions: Actions = {
notes: form.data.notes || null,
updatedAt: new Date()
})
.where(and(eq(invoices.id, form.data.id), eq(invoices.clientId, params.id)));
.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 });
}
@@ -113,7 +130,8 @@ export const actions: Actions = {
return message(form, 'Invoice updated.');
},
archive: async ({ params, request }) => {
archive: async ({ locals, params, request }) => {
const { activeOrganizationId } = await loadOrganizationContext(locals);
const form = await superValidate(await request.formData(), zod4(archiveSchema), {
id: 'invoices-archive'
});
@@ -123,7 +141,13 @@ export const actions: Actions = {
await db
.update(invoices)
.set({ archivedAt: new Date(), updatedAt: new Date() })
.where(and(eq(invoices.id, form.data.id), eq(invoices.clientId, params.id)));
.where(
and(
eq(invoices.id, form.data.id),
eq(invoices.clientId, params.id),
eq(invoices.organizationId, activeOrganizationId)
)
);
return message(form, 'Invoice archived.');
}