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:
@@ -3,15 +3,16 @@ import { message, superValidate } from 'sveltekit-superforms/server';
|
||||
import { zod4 } from 'sveltekit-superforms/adapters';
|
||||
import { db } from '$lib/server/db';
|
||||
import { contacts, clients } from '$lib/server/db/schema';
|
||||
import { loadOrganizationContext } from '$lib/server/organizations';
|
||||
import { archiveSchema } from '$lib/schemas/shared.schema';
|
||||
import { contactCreateSchema, contactEditSchema } from '$lib/schemas/contacts.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(contacts)
|
||||
.where(and(eq(contacts.clientId, params.id), isNull(contacts.archivedAt)))
|
||||
.where(
|
||||
and(
|
||||
eq(contacts.clientId, params.id),
|
||||
eq(contacts.organizationId, activeOrganizationId),
|
||||
isNull(contacts.archivedAt)
|
||||
)
|
||||
)
|
||||
.orderBy(asc(contacts.name));
|
||||
|
||||
return {
|
||||
@@ -40,7 +48,7 @@ export const load: PageServerLoad = async ({ params }) => {
|
||||
notes: record.notes ?? ''
|
||||
}
|
||||
})),
|
||||
options: await loadOptions(),
|
||||
options: await loadOptions(activeOrganizationId),
|
||||
createForm: await superValidate({ clientId: params.id }, zod4(contactCreateSchema), {
|
||||
id: 'contacts-create'
|
||||
}),
|
||||
@@ -50,7 +58,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(contactCreateSchema), {
|
||||
@@ -62,6 +71,7 @@ export const actions: Actions = {
|
||||
try {
|
||||
await db.insert(contacts).values({
|
||||
id: crypto.randomUUID(),
|
||||
organizationId: activeOrganizationId,
|
||||
clientId: form.data.clientId,
|
||||
name: form.data.name,
|
||||
role: form.data.role || null,
|
||||
@@ -79,7 +89,8 @@ export const actions: Actions = {
|
||||
return message(form, 'Contact 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(contactEditSchema), { id: 'contacts-edit' });
|
||||
@@ -99,7 +110,13 @@ export const actions: Actions = {
|
||||
notes: form.data.notes || null,
|
||||
updatedAt: new Date()
|
||||
})
|
||||
.where(and(eq(contacts.id, form.data.id), eq(contacts.clientId, params.id)));
|
||||
.where(
|
||||
and(
|
||||
eq(contacts.id, form.data.id),
|
||||
eq(contacts.clientId, params.id),
|
||||
eq(contacts.organizationId, activeOrganizationId)
|
||||
)
|
||||
);
|
||||
} catch {
|
||||
return message(form, 'Unable to update contact.', { status: 400 });
|
||||
}
|
||||
@@ -107,7 +124,8 @@ export const actions: Actions = {
|
||||
return message(form, 'Contact 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: 'contacts-archive'
|
||||
});
|
||||
@@ -117,7 +135,13 @@ export const actions: Actions = {
|
||||
await db
|
||||
.update(contacts)
|
||||
.set({ archivedAt: new Date(), updatedAt: new Date() })
|
||||
.where(and(eq(contacts.id, form.data.id), eq(contacts.clientId, params.id)));
|
||||
.where(
|
||||
and(
|
||||
eq(contacts.id, form.data.id),
|
||||
eq(contacts.clientId, params.id),
|
||||
eq(contacts.organizationId, activeOrganizationId)
|
||||
)
|
||||
);
|
||||
|
||||
return message(form, 'Contact archived.');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user