Files
buildfor_life_repair/src/routes/(app)/+page.server.ts
T
grabowski 6f0e0ad6c6 Initial commit: buildfor_life_repair inventory system
SvelteKit + PostgreSQL app for tracking vintage computers, audio equipment,
components, and installation history. Features device/component CRUD, operation
logs, QR code labels, global search, image uploads, and dark mode.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 17:11:05 +07:00

72 lines
2.1 KiB
TypeScript

import type { PageServerLoad } from './$types';
import { db } from '$lib/server/db/index.js';
import { devices, components, installationLog } from '$lib/server/db/schema.js';
import { count, eq, or, and, desc, sql } from 'drizzle-orm';
export const load: PageServerLoad = async () => {
const active = eq(devices.disabled, false);
// Total counts
const [totalDevices] = await db.select({ value: count() }).from(devices).where(active);
const [computers] = await db
.select({ value: count() })
.from(devices)
.where(and(active, eq(devices.category, 'Computer')));
const [audio] = await db
.select({ value: count() })
.from(devices)
.where(and(active, eq(devices.category, 'Audio Equipment')));
const [totalComponents] = await db.select({ value: count() }).from(components);
// Condition breakdown
const conditionBreakdown = await db
.select({
condition: devices.condition,
count: count()
})
.from(devices)
.where(active)
.groupBy(devices.condition);
// Recent installation activity
const recentActivity = await db
.select({
action: installationLog.action,
performedAt: installationLog.performedAt,
componentId: installationLog.componentId,
deviceId: installationLog.deviceId,
componentTitle: components.title,
deviceTitle: devices.title
})
.from(installationLog)
.innerJoin(components, eq(installationLog.componentId, components.id))
.innerJoin(devices, eq(installationLog.deviceId, devices.id))
.orderBy(desc(installationLog.performedAt))
.limit(10);
// Devices needing repair
const needsRepair = await db
.select({
id: devices.id,
title: devices.title,
category: devices.category,
condition: devices.condition,
faultDescription: devices.faultDescription
})
.from(devices)
.where(and(active, or(eq(devices.condition, 'In Repair'), eq(devices.condition, 'Waiting for Repair'))))
.limit(10);
return {
stats: {
totalDevices: totalDevices?.value ?? 0,
computers: computers?.value ?? 0,
audio: audio?.value ?? 0,
totalComponents: totalComponents?.value ?? 0
},
conditionBreakdown,
recentActivity,
needsRepair
};
};