update: split workspace pages into reusable components
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { and, asc, eq, isNull } from 'drizzle-orm';
|
||||
import { and, asc, eq, inArray, 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, rooms, services } from '$lib/server/db/schema';
|
||||
import { contracts, contractRooms, clients, rooms, services } from '$lib/server/db/schema';
|
||||
import { loadOrganizationContext } from '$lib/server/organizations';
|
||||
import { archiveSchema } from '$lib/schemas/shared.schema';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
@@ -24,19 +24,36 @@ export const load: PageServerLoad = async ({ locals }) => {
|
||||
const records = await db
|
||||
.select({
|
||||
contract: contracts,
|
||||
roomName: rooms.name,
|
||||
serviceName: services.name
|
||||
})
|
||||
.from(contracts)
|
||||
.leftJoin(rooms, eq(contracts.roomId, rooms.id))
|
||||
.leftJoin(services, eq(contracts.serviceId, services.id))
|
||||
.where(and(eq(contracts.organizationId, activeOrganizationId), isNull(contracts.archivedAt)))
|
||||
.orderBy(asc(contracts.startDate));
|
||||
const contractIds = records.map((record) => record.contract.id);
|
||||
const roomLinks =
|
||||
contractIds.length > 0
|
||||
? await db
|
||||
.select({
|
||||
contractId: contractRooms.contractId,
|
||||
roomName: rooms.name
|
||||
})
|
||||
.from(contractRooms)
|
||||
.innerJoin(rooms, eq(contractRooms.roomId, rooms.id))
|
||||
.where(inArray(contractRooms.contractId, contractIds))
|
||||
.orderBy(asc(rooms.name))
|
||||
: [];
|
||||
const roomNamesByContract = new Map<string, string[]>();
|
||||
for (const link of roomLinks) {
|
||||
const existing = roomNamesByContract.get(link.contractId) ?? [];
|
||||
existing.push(link.roomName);
|
||||
roomNamesByContract.set(link.contractId, existing);
|
||||
}
|
||||
|
||||
return {
|
||||
records: records.map((record) => ({
|
||||
...record.contract,
|
||||
roomName: record.roomName,
|
||||
roomName: (roomNamesByContract.get(record.contract.id) ?? []).join(', '),
|
||||
serviceName: record.serviceName
|
||||
})),
|
||||
options: await loadOptions(activeOrganizationId),
|
||||
|
||||
Reference in New Issue
Block a user