@omersx/bemms 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +203 -2
- package/drizzle.config.ts +11 -0
- package/next.config.ts +51 -0
- package/package.json +99 -3
- package/postcss.config.mjs +7 -0
- package/public/hospital_demo_devices.csv +32 -0
- package/public/icons/icon-192.png +0 -0
- package/public/icons/icon-512.png +0 -0
- package/public/icons/icon-maskable-512.png +0 -0
- package/public/manifest.webmanifest +31 -0
- package/public/sw.js +177 -0
- package/src/app/(auth)/layout.tsx +20 -0
- package/src/app/(auth)/login/page.tsx +135 -0
- package/src/app/(dashboard)/admin/audit-logs/page.tsx +178 -0
- package/src/app/(dashboard)/admin/data/page.tsx +37 -0
- package/src/app/(dashboard)/admin/departments/[deptId]/edit/page.tsx +206 -0
- package/src/app/(dashboard)/admin/departments/[deptId]/page.tsx +71 -0
- package/src/app/(dashboard)/admin/departments/new/page.tsx +6 -0
- package/src/app/(dashboard)/admin/departments/page.tsx +6 -0
- package/src/app/(dashboard)/admin/general/page.tsx +374 -0
- package/src/app/(dashboard)/admin/hospitals/[hospitalId]/edit/page.tsx +304 -0
- package/src/app/(dashboard)/admin/hospitals/[hospitalId]/page.tsx +94 -0
- package/src/app/(dashboard)/admin/hospitals/new/page.tsx +302 -0
- package/src/app/(dashboard)/admin/hospitals/page.tsx +6 -0
- package/src/app/(dashboard)/admin/layout.tsx +49 -0
- package/src/app/(dashboard)/admin/organizations/[orgId]/edit/page.tsx +172 -0
- package/src/app/(dashboard)/admin/organizations/[orgId]/page.tsx +82 -0
- package/src/app/(dashboard)/admin/organizations/new/page.tsx +150 -0
- package/src/app/(dashboard)/admin/organizations/page.tsx +6 -0
- package/src/app/(dashboard)/admin/page.tsx +258 -0
- package/src/app/(dashboard)/admin/roles/page.tsx +153 -0
- package/src/app/(dashboard)/admin/settings/page.tsx +69 -0
- package/src/app/(dashboard)/admin/users/[userId]/edit/page.tsx +153 -0
- package/src/app/(dashboard)/admin/users/[userId]/page.tsx +317 -0
- package/src/app/(dashboard)/admin/users/invite/page.tsx +3 -0
- package/src/app/(dashboard)/admin/users/new/page.tsx +5 -0
- package/src/app/(dashboard)/admin/users/page.tsx +23 -0
- package/src/app/(dashboard)/dashboard/page.tsx +462 -0
- package/src/app/(dashboard)/departments/[deptId]/page.tsx +39 -0
- package/src/app/(dashboard)/departments/new/page.tsx +330 -0
- package/src/app/(dashboard)/departments/page.tsx +52 -0
- package/src/app/(dashboard)/devices/[id]/edit/page.tsx +131 -0
- package/src/app/(dashboard)/devices/[id]/page.tsx +408 -0
- package/src/app/(dashboard)/devices/[id]/qr/page.tsx +54 -0
- package/src/app/(dashboard)/devices/new/page.tsx +382 -0
- package/src/app/(dashboard)/devices/page.tsx +80 -0
- package/src/app/(dashboard)/engineering-team/page.tsx +41 -0
- package/src/app/(dashboard)/layout.tsx +43 -0
- package/src/app/(dashboard)/maintenance/checklists/page.tsx +57 -0
- package/src/app/(dashboard)/maintenance/page.tsx +231 -0
- package/src/app/(dashboard)/maintenance/plans/page.tsx +142 -0
- package/src/app/(dashboard)/maintenance/tasks/[id]/page.tsx +997 -0
- package/src/app/(dashboard)/maintenance/tasks/page.tsx +119 -0
- package/src/app/(dashboard)/more/page.tsx +310 -0
- package/src/app/(dashboard)/my-department/page.tsx +151 -0
- package/src/app/(dashboard)/notifications/notification-list.tsx +103 -0
- package/src/app/(dashboard)/notifications/page.tsx +59 -0
- package/src/app/(dashboard)/profile/page.tsx +27 -0
- package/src/app/(dashboard)/reports/costs/page.tsx +127 -0
- package/src/app/(dashboard)/reports/history/page.tsx +105 -0
- package/src/app/(dashboard)/reports/inventory/page.tsx +116 -0
- package/src/app/(dashboard)/reports/maintenance/page.tsx +136 -0
- package/src/app/(dashboard)/reports/page.tsx +120 -0
- package/src/app/(dashboard)/reports/tickets/page.tsx +139 -0
- package/src/app/(dashboard)/reports/workload/page.tsx +135 -0
- package/src/app/(dashboard)/rounds/[id]/page.tsx +197 -0
- package/src/app/(dashboard)/rounds/history/page.tsx +82 -0
- package/src/app/(dashboard)/rounds/page.tsx +107 -0
- package/src/app/(dashboard)/scan/[code]/page.tsx +154 -0
- package/src/app/(dashboard)/scan/page.tsx +73 -0
- package/src/app/(dashboard)/team/page.tsx +42 -0
- package/src/app/(dashboard)/tickets/[id]/page.tsx +465 -0
- package/src/app/(dashboard)/tickets/create/page.tsx +722 -0
- package/src/app/(dashboard)/tickets/page.tsx +55 -0
- package/src/app/(dashboard)/tickets/triage/page.tsx +161 -0
- package/src/app/api/admin/data/export/route.ts +316 -0
- package/src/app/api/auth/[...nextauth]/route.ts +3 -0
- package/src/app/api/health/route.ts +46 -0
- package/src/app/api/notifications/sse/route.ts +91 -0
- package/src/app/api/notifications/subscribe/route.ts +54 -0
- package/src/app/api/notifications/vapid-key/route.ts +7 -0
- package/src/app/api/reports/export/route.ts +102 -0
- package/src/app/api/tickets/[id]/attachments/route.ts +129 -0
- package/src/app/api/tickets/[id]/live-sync/route.ts +52 -0
- package/src/app/error.tsx +54 -0
- package/src/app/globals.css +89 -0
- package/src/app/layout.tsx +59 -0
- package/src/app/not-found.tsx +32 -0
- package/src/app/page.tsx +5 -0
- package/src/components/admin/add-department-user-dialog.tsx +759 -0
- package/src/components/admin/add-engineer-dialog.tsx +773 -0
- package/src/components/admin/add-user-type-dialog.tsx +89 -0
- package/src/components/admin/audit-log-export.tsx +113 -0
- package/src/components/admin/audit-log-filters.tsx +255 -0
- package/src/components/admin/audit-log-table.tsx +383 -0
- package/src/components/admin/data-management/danger-zone.tsx +301 -0
- package/src/components/admin/data-management/data-management-tabs.tsx +61 -0
- package/src/components/admin/data-management/export-center.tsx +301 -0
- package/src/components/admin/data-management/import-wizard.tsx +558 -0
- package/src/components/admin/settings-forms/features-form.tsx +115 -0
- package/src/components/admin/settings-forms/health-panel.tsx +182 -0
- package/src/components/admin/settings-forms/localization-form.tsx +203 -0
- package/src/components/admin/settings-forms/retention-form.tsx +164 -0
- package/src/components/admin/settings-forms/security-form.tsx +186 -0
- package/src/components/admin/system-settings-tabs.tsx +88 -0
- package/src/components/admin/user-role-dialog.tsx +293 -0
- package/src/components/admin/user-status-dialog.tsx +166 -0
- package/src/components/admin/users-page-client.tsx +514 -0
- package/src/components/dashboard/action-buttons.tsx +93 -0
- package/src/components/dashboard/admin-alerts.tsx +86 -0
- package/src/components/dashboard/availability-alerts.tsx +65 -0
- package/src/components/dashboard/maintenance-due-list.tsx +87 -0
- package/src/components/dashboard/round-widget.tsx +110 -0
- package/src/components/dashboard/stat-card.tsx +38 -0
- package/src/components/dashboard/ticket-list-compact.tsx +86 -0
- package/src/components/departments/department-cards-view.tsx +244 -0
- package/src/components/departments/department-detail-view.tsx +467 -0
- package/src/components/departments/department-visuals.tsx +190 -0
- package/src/components/departments/my-department-view.tsx +333 -0
- package/src/components/devices/change-status-dialog.tsx +146 -0
- package/src/components/devices/device-action-buttons.tsx +125 -0
- package/src/components/devices/device-availability-banner.tsx +59 -0
- package/src/components/devices/device-badges.tsx +55 -0
- package/src/components/devices/device-documents-tab.tsx +93 -0
- package/src/components/devices/device-maintenance-tab.tsx +113 -0
- package/src/components/devices/device-mini-qr.tsx +109 -0
- package/src/components/devices/device-qr-card.tsx +187 -0
- package/src/components/devices/device-signatures-tab.tsx +607 -0
- package/src/components/devices/device-tickets-tab.tsx +139 -0
- package/src/components/devices/device-timelines.tsx +124 -0
- package/src/components/devices/qr-camera-scanner.tsx +167 -0
- package/src/components/devices/qr-code-display.tsx +139 -0
- package/src/components/devices/quick-add-category-dialog.tsx +135 -0
- package/src/components/devices/quick-add-manufacturer-dialog.tsx +212 -0
- package/src/components/devices/transfer-device-dialog.tsx +190 -0
- package/src/components/layout/header.tsx +80 -0
- package/src/components/layout/mobile-nav.tsx +91 -0
- package/src/components/layout/more-item-badge.tsx +15 -0
- package/src/components/layout/nav-badge.tsx +72 -0
- package/src/components/layout/sidebar.tsx +297 -0
- package/src/components/maintenance/maintenance-badges.tsx +579 -0
- package/src/components/maintenance/maintenance-nav.tsx +107 -0
- package/src/components/maintenance/parts-costs-manager.tsx +909 -0
- package/src/components/maintenance/signature-card.tsx +413 -0
- package/src/components/maintenance/task-action-bar.tsx +955 -0
- package/src/components/notifications/notification-bell.tsx +367 -0
- package/src/components/notifications/notification-item.tsx +93 -0
- package/src/components/notifications/notification-preferences.tsx +182 -0
- package/src/components/profile/user-profile-view.tsx +626 -0
- package/src/components/providers/nav-counts-provider.tsx +235 -0
- package/src/components/providers/session-provider.tsx +7 -0
- package/src/components/providers/theme-provider.tsx +124 -0
- package/src/components/pwa/install-prompt.tsx +82 -0
- package/src/components/pwa/sw-register.tsx +43 -0
- package/src/components/reports/export-button.tsx +62 -0
- package/src/components/reports/filter-chips.tsx +47 -0
- package/src/components/reports/summary-cards.tsx +40 -0
- package/src/components/reports/transparency-header.tsx +54 -0
- package/src/components/rounds/department-visit-sheet.tsx +199 -0
- package/src/components/rounds/mention-textarea.tsx +187 -0
- package/src/components/rounds/round-calendar.tsx +233 -0
- package/src/components/rounds/round-department-list.tsx +281 -0
- package/src/components/rounds/round-history-client.tsx +218 -0
- package/src/components/rounds/start-round-card.tsx +184 -0
- package/src/components/shared/confirm-dialog.tsx +67 -0
- package/src/components/shared/data-table.tsx +104 -0
- package/src/components/shared/page-header.tsx +20 -0
- package/src/components/shared/sign-out-button.tsx +52 -0
- package/src/components/shared/status-badge.tsx +42 -0
- package/src/components/shared/table-search.tsx +55 -0
- package/src/components/signatures/sign-action-dialog.tsx +253 -0
- package/src/components/signatures/signature-history.tsx +121 -0
- package/src/components/signatures/signature-status-panel.tsx +164 -0
- package/src/components/team/department-coverage.tsx +141 -0
- package/src/components/team/edit-engineer-dialog.tsx +242 -0
- package/src/components/team/engineer-card.tsx +180 -0
- package/src/components/team/engineer-details-dialog.tsx +432 -0
- package/src/components/team/schedule-calendar.tsx +204 -0
- package/src/components/team/staff-engineer-team-view.tsx +368 -0
- package/src/components/team/team-header-stats.tsx +88 -0
- package/src/components/team/team-page-client.tsx +216 -0
- package/src/components/theme/theme-selector-card.tsx +63 -0
- package/src/components/theme/theme-toggle.tsx +65 -0
- package/src/components/tickets/staff-status-label.tsx +58 -0
- package/src/components/tickets/staff-ticket-card.tsx +109 -0
- package/src/components/tickets/staff-ticket-progress-tracker.tsx +405 -0
- package/src/components/tickets/staff-tickets-view.tsx +182 -0
- package/src/components/tickets/ticket-action-buttons.tsx +873 -0
- package/src/components/tickets/ticket-auto-refresh.tsx +197 -0
- package/src/components/tickets/ticket-badges.tsx +43 -0
- package/src/components/tickets/ticket-card.tsx +197 -0
- package/src/components/tickets/ticket-cards-view.tsx +395 -0
- package/src/components/tickets/ticket-comment-section.tsx +684 -0
- package/src/components/tickets/ticket-dispatch-status.tsx +184 -0
- package/src/components/tickets/ticket-list-auto-refresh.tsx +55 -0
- package/src/components/tickets/ticket-reporter-profile.tsx +235 -0
- package/src/components/tickets/ticket-signatures-panel.tsx +32 -0
- package/src/components/tickets/ticket-timeline.tsx +131 -0
- package/src/components/tickets/ticket-triage-panel.tsx +124 -0
- package/src/components/tickets/tickets-nav.tsx +107 -0
- package/src/components/ui/avatar.tsx +43 -0
- package/src/components/ui/badge.tsx +34 -0
- package/src/components/ui/button.tsx +58 -0
- package/src/components/ui/card.tsx +77 -0
- package/src/components/ui/checkbox.tsx +31 -0
- package/src/components/ui/dialog.tsx +123 -0
- package/src/components/ui/dropdown-menu.tsx +126 -0
- package/src/components/ui/form.tsx +179 -0
- package/src/components/ui/input.tsx +24 -0
- package/src/components/ui/label.tsx +27 -0
- package/src/components/ui/popover.tsx +34 -0
- package/src/components/ui/progress.tsx +36 -0
- package/src/components/ui/scroll-area.tsx +47 -0
- package/src/components/ui/select.tsx +160 -0
- package/src/components/ui/separator.tsx +25 -0
- package/src/components/ui/sheet.tsx +131 -0
- package/src/components/ui/switch.tsx +30 -0
- package/src/components/ui/table.tsx +121 -0
- package/src/components/ui/tabs.tsx +62 -0
- package/src/components/ui/textarea.tsx +23 -0
- package/src/components/ui/tooltip.tsx +27 -0
- package/src/lib/actions/__tests__/devices.test.ts +176 -0
- package/src/lib/actions/__tests__/signatures.test.ts +303 -0
- package/src/lib/actions/__tests__/tickets.test.ts +243 -0
- package/src/lib/actions/audit-logs.ts +298 -0
- package/src/lib/actions/checklists.ts +429 -0
- package/src/lib/actions/dashboard.ts +358 -0
- package/src/lib/actions/data-management.ts +723 -0
- package/src/lib/actions/departments.ts +369 -0
- package/src/lib/actions/device-categories.ts +117 -0
- package/src/lib/actions/devices.ts +748 -0
- package/src/lib/actions/engineer-rounds.ts +973 -0
- package/src/lib/actions/engineers.ts +285 -0
- package/src/lib/actions/hospitals.ts +148 -0
- package/src/lib/actions/locations.ts +115 -0
- package/src/lib/actions/maintenance-plans.ts +316 -0
- package/src/lib/actions/maintenance-tasks.ts +415 -0
- package/src/lib/actions/manufacturers.ts +132 -0
- package/src/lib/actions/nav-counts.ts +144 -0
- package/src/lib/actions/notifications.ts +136 -0
- package/src/lib/actions/organizations.ts +99 -0
- package/src/lib/actions/qr.ts +147 -0
- package/src/lib/actions/report-history.ts +77 -0
- package/src/lib/actions/reports.ts +192 -0
- package/src/lib/actions/roles.ts +32 -0
- package/src/lib/actions/signature-auth.ts +377 -0
- package/src/lib/actions/signatures.ts +532 -0
- package/src/lib/actions/system-settings.ts +274 -0
- package/src/lib/actions/tickets.ts +1445 -0
- package/src/lib/actions/users.ts +740 -0
- package/src/lib/audit/index.ts +103 -0
- package/src/lib/auth/__tests__/rbac.test.ts +33 -0
- package/src/lib/auth/index.ts +338 -0
- package/src/lib/auth/password.ts +10 -0
- package/src/lib/auth/rbac.ts +103 -0
- package/src/lib/auth/types.ts +57 -0
- package/src/lib/constants/departments.ts +16 -0
- package/src/lib/constants/manufacturers.ts +32 -0
- package/src/lib/db/index.ts +23 -0
- package/src/lib/db/schema/audit-logs.ts +42 -0
- package/src/lib/db/schema/departments.ts +39 -0
- package/src/lib/db/schema/device-categories.ts +38 -0
- package/src/lib/db/schema/devices.ts +195 -0
- package/src/lib/db/schema/engineer-profiles.ts +63 -0
- package/src/lib/db/schema/engineer-rounds.ts +115 -0
- package/src/lib/db/schema/enums.ts +148 -0
- package/src/lib/db/schema/hospitals.ts +34 -0
- package/src/lib/db/schema/index.ts +24 -0
- package/src/lib/db/schema/locations.ts +46 -0
- package/src/lib/db/schema/maintenance-plans.ts +99 -0
- package/src/lib/db/schema/maintenance-records.ts +161 -0
- package/src/lib/db/schema/maintenance-tasks.ts +196 -0
- package/src/lib/db/schema/manufacturers.ts +60 -0
- package/src/lib/db/schema/notifications.ts +48 -0
- package/src/lib/db/schema/organizations.ts +24 -0
- package/src/lib/db/schema/report-history.ts +28 -0
- package/src/lib/db/schema/roles.ts +59 -0
- package/src/lib/db/schema/sessions.ts +46 -0
- package/src/lib/db/schema/signature-events.ts +48 -0
- package/src/lib/db/schema/signature-policies.ts +60 -0
- package/src/lib/db/schema/signatures.ts +87 -0
- package/src/lib/db/schema/tickets.ts +169 -0
- package/src/lib/db/schema/user-assignments.ts +85 -0
- package/src/lib/db/schema/users.ts +39 -0
- package/src/lib/db/seed.ts +131 -0
- package/src/lib/logger/__tests__/logger.test.ts +154 -0
- package/src/lib/logger/formatters.ts +159 -0
- package/src/lib/logger/index.ts +184 -0
- package/src/lib/logger/types.ts +48 -0
- package/src/lib/notifications/create.ts +71 -0
- package/src/lib/notifications/dispatcher.ts +490 -0
- package/src/lib/notifications/index.ts +7 -0
- package/src/lib/notifications/push-sender.ts +128 -0
- package/src/lib/notifications/push.ts +64 -0
- package/src/lib/notifications/triggers.ts +166 -0
- package/src/lib/utils/__tests__/crypto.test.ts +30 -0
- package/src/lib/utils/__tests__/export.test.ts +55 -0
- package/src/lib/utils/__tests__/nav-badge.test.ts +147 -0
- package/src/lib/utils/__tests__/staff-status.test.ts +50 -0
- package/src/lib/utils/attestations.ts +19 -0
- package/src/lib/utils/crypto.ts +18 -0
- package/src/lib/utils/csv-parser.ts +181 -0
- package/src/lib/utils/export.ts +45 -0
- package/src/lib/utils/nav-badge.ts +225 -0
- package/src/lib/utils/sound.ts +152 -0
- package/src/lib/utils/staff-status.ts +115 -0
- package/src/lib/utils.ts +6 -0
- package/src/lib/validators/__tests__/auth.test.ts +127 -0
- package/src/lib/validators/__tests__/data-management.test.ts +182 -0
- package/src/lib/validators/__tests__/devices-profile.test.ts +129 -0
- package/src/lib/validators/__tests__/devices.test.ts +98 -0
- package/src/lib/validators/__tests__/engineer-rounds.test.ts +170 -0
- package/src/lib/validators/__tests__/maintenance.test.ts +178 -0
- package/src/lib/validators/__tests__/signatures.test.ts +154 -0
- package/src/lib/validators/__tests__/system-settings.test.ts +119 -0
- package/src/lib/validators/__tests__/tickets.test.ts +176 -0
- package/src/lib/validators/__tests__/users.test.ts +92 -0
- package/src/lib/validators/auth.ts +32 -0
- package/src/lib/validators/data-management.ts +67 -0
- package/src/lib/validators/departments.ts +14 -0
- package/src/lib/validators/devices.ts +94 -0
- package/src/lib/validators/engineer-rounds.ts +70 -0
- package/src/lib/validators/hospitals.ts +17 -0
- package/src/lib/validators/locations.ts +15 -0
- package/src/lib/validators/maintenance.ts +211 -0
- package/src/lib/validators/organizations.ts +11 -0
- package/src/lib/validators/reports.ts +31 -0
- package/src/lib/validators/signatures.ts +49 -0
- package/src/lib/validators/system-settings.ts +55 -0
- package/src/lib/validators/tickets.ts +125 -0
- package/src/lib/validators/users.ts +43 -0
- package/src/middleware.ts +45 -0
- package/tailwind.config.ts +76 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
'use server';
|
|
2
|
+
|
|
3
|
+
import { db } from '@/lib/db';
|
|
4
|
+
import {
|
|
5
|
+
checklistTemplates,
|
|
6
|
+
checklistTemplateVersions,
|
|
7
|
+
maintenanceChecklistResults,
|
|
8
|
+
maintenanceTasks,
|
|
9
|
+
maintenanceParts,
|
|
10
|
+
maintenanceCosts,
|
|
11
|
+
} from '@/lib/db/schema';
|
|
12
|
+
import { requireAuth, requireScope } from '@/lib/auth/rbac';
|
|
13
|
+
import { createAuditLog } from '@/lib/audit';
|
|
14
|
+
import { eq, and, desc, count } from 'drizzle-orm';
|
|
15
|
+
import {
|
|
16
|
+
createChecklistTemplateSchema,
|
|
17
|
+
createChecklistVersionSchema,
|
|
18
|
+
saveChecklistProgressSchema,
|
|
19
|
+
maintenancePartSchema,
|
|
20
|
+
maintenanceCostSchema,
|
|
21
|
+
} from '@/lib/validators/maintenance';
|
|
22
|
+
import { z } from 'zod';
|
|
23
|
+
|
|
24
|
+
// ── Checklist Template Actions ──────────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
export async function createChecklistTemplate(input: z.infer<typeof createChecklistTemplateSchema>) {
|
|
27
|
+
const user = await requireAuth();
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const data = createChecklistTemplateSchema.parse(input);
|
|
31
|
+
await requireScope({ organizationId: data.organizationId });
|
|
32
|
+
|
|
33
|
+
// Create the template
|
|
34
|
+
const [template] = await db.insert(checklistTemplates).values({
|
|
35
|
+
organizationId: data.organizationId,
|
|
36
|
+
deviceCategoryId: data.deviceCategoryId,
|
|
37
|
+
name: data.name,
|
|
38
|
+
code: data.code,
|
|
39
|
+
maintenanceType: data.maintenanceType,
|
|
40
|
+
}).returning();
|
|
41
|
+
|
|
42
|
+
// Create initial version (v1)
|
|
43
|
+
const [version] = await db.insert(checklistTemplateVersions).values({
|
|
44
|
+
checklistTemplateId: template.id,
|
|
45
|
+
versionNumber: 1,
|
|
46
|
+
itemsJsonb: data.items,
|
|
47
|
+
isActive: true,
|
|
48
|
+
createdByUserId: user.id,
|
|
49
|
+
}).returning();
|
|
50
|
+
|
|
51
|
+
await createAuditLog({
|
|
52
|
+
actionType: 'CREATE',
|
|
53
|
+
entityType: 'CHECKLIST_TEMPLATE',
|
|
54
|
+
entityId: template.id,
|
|
55
|
+
organizationId: data.organizationId,
|
|
56
|
+
userId: user.id,
|
|
57
|
+
details: { name: data.name, itemCount: data.items.length },
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return { success: true, data: { ...template, activeVersion: version } };
|
|
61
|
+
} catch (error: any) {
|
|
62
|
+
return { success: false, error: error.message };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function createChecklistVersion(
|
|
67
|
+
templateId: string,
|
|
68
|
+
input: z.infer<typeof createChecklistVersionSchema>
|
|
69
|
+
) {
|
|
70
|
+
const user = await requireAuth();
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const data = createChecklistVersionSchema.parse(input);
|
|
74
|
+
|
|
75
|
+
const template = await db.query.checklistTemplates.findFirst({
|
|
76
|
+
where: eq(checklistTemplates.id, templateId),
|
|
77
|
+
});
|
|
78
|
+
if (!template) return { success: false, error: 'Template not found' };
|
|
79
|
+
await requireScope({ organizationId: template.organizationId });
|
|
80
|
+
|
|
81
|
+
// Deactivate all existing versions
|
|
82
|
+
await db
|
|
83
|
+
.update(checklistTemplateVersions)
|
|
84
|
+
.set({ isActive: false })
|
|
85
|
+
.where(eq(checklistTemplateVersions.checklistTemplateId, templateId));
|
|
86
|
+
|
|
87
|
+
// Get current max version
|
|
88
|
+
const existing = await db.query.checklistTemplateVersions.findMany({
|
|
89
|
+
where: eq(checklistTemplateVersions.checklistTemplateId, templateId),
|
|
90
|
+
orderBy: [desc(checklistTemplateVersions.versionNumber)],
|
|
91
|
+
limit: 1,
|
|
92
|
+
});
|
|
93
|
+
const nextVersion = (existing[0]?.versionNumber ?? 0) + 1;
|
|
94
|
+
|
|
95
|
+
const [version] = await db.insert(checklistTemplateVersions).values({
|
|
96
|
+
checklistTemplateId: templateId,
|
|
97
|
+
versionNumber: nextVersion,
|
|
98
|
+
itemsJsonb: data.items,
|
|
99
|
+
isActive: true,
|
|
100
|
+
createdByUserId: user.id,
|
|
101
|
+
}).returning();
|
|
102
|
+
|
|
103
|
+
await createAuditLog({
|
|
104
|
+
actionType: 'UPDATE',
|
|
105
|
+
entityType: 'CHECKLIST_TEMPLATE',
|
|
106
|
+
entityId: templateId,
|
|
107
|
+
organizationId: template.organizationId,
|
|
108
|
+
userId: user.id,
|
|
109
|
+
details: { versionNumber: nextVersion, itemCount: data.items.length },
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return { success: true, data: version };
|
|
113
|
+
} catch (error: any) {
|
|
114
|
+
return { success: false, error: error.message };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function listChecklistTemplates(organizationId: string) {
|
|
119
|
+
const user = await requireAuth();
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
await requireScope({ organizationId });
|
|
123
|
+
|
|
124
|
+
const templates = await db.query.checklistTemplates.findMany({
|
|
125
|
+
where: and(
|
|
126
|
+
eq(checklistTemplates.organizationId, organizationId),
|
|
127
|
+
eq(checklistTemplates.isActive, true)
|
|
128
|
+
),
|
|
129
|
+
orderBy: [desc(checklistTemplates.createdAt)],
|
|
130
|
+
with: {
|
|
131
|
+
versions: {
|
|
132
|
+
where: eq(checklistTemplateVersions.isActive, true),
|
|
133
|
+
limit: 1,
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return { success: true, data: templates };
|
|
139
|
+
} catch (error: any) {
|
|
140
|
+
return { success: false, error: error.message };
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function getChecklistTemplateById(id: string) {
|
|
145
|
+
const user = await requireAuth();
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
const template = await db.query.checklistTemplates.findFirst({
|
|
149
|
+
where: eq(checklistTemplates.id, id),
|
|
150
|
+
with: {
|
|
151
|
+
versions: {
|
|
152
|
+
orderBy: [desc(checklistTemplateVersions.versionNumber)],
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
if (!template) return { success: false, error: 'Template not found' };
|
|
158
|
+
await requireScope({ organizationId: template.organizationId });
|
|
159
|
+
|
|
160
|
+
return { success: true, data: template };
|
|
161
|
+
} catch (error: any) {
|
|
162
|
+
return { success: false, error: error.message };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ── Checklist Execution Actions ─────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
export async function saveChecklistProgress(
|
|
169
|
+
taskId: string,
|
|
170
|
+
input: z.infer<typeof saveChecklistProgressSchema>
|
|
171
|
+
) {
|
|
172
|
+
const user = await requireAuth();
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
const data = saveChecklistProgressSchema.parse(input);
|
|
176
|
+
|
|
177
|
+
const task = await db.query.maintenanceTasks.findFirst({
|
|
178
|
+
where: eq(maintenanceTasks.id, taskId),
|
|
179
|
+
with: { checklistTemplateVersion: true },
|
|
180
|
+
});
|
|
181
|
+
if (!task) return { success: false, error: 'Task not found' };
|
|
182
|
+
await requireScope({ organizationId: task.organizationId, hospitalId: task.hospitalId });
|
|
183
|
+
|
|
184
|
+
// Get the template items to snapshot labels
|
|
185
|
+
const templateItems: any[] = (task.checklistTemplateVersion?.itemsJsonb as any[]) || [];
|
|
186
|
+
|
|
187
|
+
for (const item of data.items) {
|
|
188
|
+
const templateItem = templateItems.find((ti: any) => ti.id === item.templateItemId);
|
|
189
|
+
const labelSnapshot = templateItem?.label || item.templateItemId;
|
|
190
|
+
const orderNum = templateItem?.itemOrder ?? 0;
|
|
191
|
+
|
|
192
|
+
// Upsert: update if exists, insert if new
|
|
193
|
+
const existing = await db.query.maintenanceChecklistResults.findFirst({
|
|
194
|
+
where: and(
|
|
195
|
+
eq(maintenanceChecklistResults.maintenanceTaskId, taskId),
|
|
196
|
+
eq(maintenanceChecklistResults.templateItemId, item.templateItemId)
|
|
197
|
+
),
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
if (existing) {
|
|
201
|
+
await db
|
|
202
|
+
.update(maintenanceChecklistResults)
|
|
203
|
+
.set({
|
|
204
|
+
resultCode: item.resultCode,
|
|
205
|
+
measuredValue: item.measuredValue,
|
|
206
|
+
unit: item.unit,
|
|
207
|
+
notes: item.notes,
|
|
208
|
+
performedByUserId: user.id,
|
|
209
|
+
completedAt: item.resultCode ? new Date() : null,
|
|
210
|
+
})
|
|
211
|
+
.where(eq(maintenanceChecklistResults.id, existing.id));
|
|
212
|
+
} else {
|
|
213
|
+
await db.insert(maintenanceChecklistResults).values({
|
|
214
|
+
maintenanceTaskId: taskId,
|
|
215
|
+
templateItemId: item.templateItemId,
|
|
216
|
+
itemLabelSnapshot: labelSnapshot,
|
|
217
|
+
itemOrder: orderNum,
|
|
218
|
+
resultCode: item.resultCode,
|
|
219
|
+
measuredValue: item.measuredValue,
|
|
220
|
+
unit: item.unit,
|
|
221
|
+
notes: item.notes,
|
|
222
|
+
performedByUserId: user.id,
|
|
223
|
+
completedAt: item.resultCode ? new Date() : undefined,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { success: true };
|
|
229
|
+
} catch (error: any) {
|
|
230
|
+
return { success: false, error: error.message };
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export async function validateChecklistCompletion(taskId: string): Promise<{
|
|
235
|
+
valid: boolean;
|
|
236
|
+
errors: string[];
|
|
237
|
+
}> {
|
|
238
|
+
const task = await db.query.maintenanceTasks.findFirst({
|
|
239
|
+
where: eq(maintenanceTasks.id, taskId),
|
|
240
|
+
with: {
|
|
241
|
+
checklistTemplateVersion: true,
|
|
242
|
+
checklistResults: true,
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
if (!task) return { valid: false, errors: ['Task not found'] };
|
|
247
|
+
|
|
248
|
+
// If no checklist is attached, it's valid
|
|
249
|
+
if (!task.checklistTemplateVersionId || !task.checklistTemplateVersion) {
|
|
250
|
+
return { valid: true, errors: [] };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const templateItems: any[] = (task.checklistTemplateVersion.itemsJsonb as any[]) || [];
|
|
254
|
+
const results = task.checklistResults || [];
|
|
255
|
+
const errors: string[] = [];
|
|
256
|
+
|
|
257
|
+
for (const item of templateItems) {
|
|
258
|
+
if (!item.isRequired) continue;
|
|
259
|
+
|
|
260
|
+
const result = results.find((r: any) => r.templateItemId === item.id);
|
|
261
|
+
|
|
262
|
+
if (!result || !result.resultCode) {
|
|
263
|
+
errors.push(`Required item "${item.label}" has not been completed`);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Failed items must have notes
|
|
268
|
+
if ((result.resultCode === 'failed' || result.resultCode === 'requires_follow_up') && !result.notes) {
|
|
269
|
+
errors.push(`Item "${item.label}" is marked ${result.resultCode} but has no explanation notes`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return { valid: errors.length === 0, errors };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ── Parts Actions ───────────────────────────────────────────────────────────
|
|
277
|
+
|
|
278
|
+
export async function addMaintenancePart(taskId: string, input: z.infer<typeof maintenancePartSchema>) {
|
|
279
|
+
const user = await requireAuth();
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
const data = maintenancePartSchema.parse(input);
|
|
283
|
+
|
|
284
|
+
const task = await db.query.maintenanceTasks.findFirst({
|
|
285
|
+
where: eq(maintenanceTasks.id, taskId),
|
|
286
|
+
});
|
|
287
|
+
if (!task) return { success: false, error: 'Task not found' };
|
|
288
|
+
await requireScope({ organizationId: task.organizationId, hospitalId: task.hospitalId });
|
|
289
|
+
|
|
290
|
+
const [part] = await db.insert(maintenanceParts).values({
|
|
291
|
+
maintenanceTaskId: taskId,
|
|
292
|
+
partNumber: data.partNumber,
|
|
293
|
+
partName: data.partName,
|
|
294
|
+
quantity: data.quantity,
|
|
295
|
+
unitCost: data.unitCost,
|
|
296
|
+
currency: data.currency,
|
|
297
|
+
supplierName: data.supplierName,
|
|
298
|
+
notes: data.notes,
|
|
299
|
+
recordedByUserId: user.id,
|
|
300
|
+
}).returning();
|
|
301
|
+
|
|
302
|
+
return { success: true, data: part };
|
|
303
|
+
} catch (error: any) {
|
|
304
|
+
return { success: false, error: error.message };
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export async function deleteMaintenancePart(partId: string) {
|
|
309
|
+
const user = await requireAuth();
|
|
310
|
+
|
|
311
|
+
try {
|
|
312
|
+
const part = await db.query.maintenanceParts.findFirst({
|
|
313
|
+
where: eq(maintenanceParts.id, partId),
|
|
314
|
+
with: { maintenanceTask: true },
|
|
315
|
+
});
|
|
316
|
+
if (!part) return { success: false, error: 'Part not found' };
|
|
317
|
+
|
|
318
|
+
const task = part.maintenanceTask;
|
|
319
|
+
if (!task) return { success: false, error: 'Linked task not found' };
|
|
320
|
+
await requireScope({ organizationId: task.organizationId, hospitalId: task.hospitalId });
|
|
321
|
+
|
|
322
|
+
// Only allow deletion while task is in active work state
|
|
323
|
+
const editableStates = ['in_progress', 'returned_for_rework', 'work_complete'];
|
|
324
|
+
if (!editableStates.includes(task.statusCode)) {
|
|
325
|
+
return { success: false, error: `Cannot modify parts in ${task.statusCode} state` };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
await db.delete(maintenanceParts).where(eq(maintenanceParts.id, partId));
|
|
329
|
+
|
|
330
|
+
return { success: true };
|
|
331
|
+
} catch (error: any) {
|
|
332
|
+
return { success: false, error: error.message };
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// ── Costs Actions ───────────────────────────────────────────────────────────
|
|
337
|
+
|
|
338
|
+
export async function addMaintenanceCost(taskId: string, input: z.infer<typeof maintenanceCostSchema>) {
|
|
339
|
+
const user = await requireAuth();
|
|
340
|
+
|
|
341
|
+
try {
|
|
342
|
+
const data = maintenanceCostSchema.parse(input);
|
|
343
|
+
|
|
344
|
+
const task = await db.query.maintenanceTasks.findFirst({
|
|
345
|
+
where: eq(maintenanceTasks.id, taskId),
|
|
346
|
+
});
|
|
347
|
+
if (!task) return { success: false, error: 'Task not found' };
|
|
348
|
+
await requireScope({ organizationId: task.organizationId, hospitalId: task.hospitalId });
|
|
349
|
+
|
|
350
|
+
const [cost] = await db.insert(maintenanceCosts).values({
|
|
351
|
+
maintenanceTaskId: taskId,
|
|
352
|
+
costType: data.costType,
|
|
353
|
+
amount: data.amount,
|
|
354
|
+
currency: data.currency,
|
|
355
|
+
vendorName: data.vendorName,
|
|
356
|
+
invoiceNumber: data.invoiceNumber,
|
|
357
|
+
description: data.description,
|
|
358
|
+
recordedByUserId: user.id,
|
|
359
|
+
}).returning();
|
|
360
|
+
|
|
361
|
+
return { success: true, data: cost };
|
|
362
|
+
} catch (error: any) {
|
|
363
|
+
return { success: false, error: error.message };
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export async function deleteMaintenanceCost(costId: string) {
|
|
368
|
+
const user = await requireAuth();
|
|
369
|
+
|
|
370
|
+
try {
|
|
371
|
+
const cost = await db.query.maintenanceCosts.findFirst({
|
|
372
|
+
where: eq(maintenanceCosts.id, costId),
|
|
373
|
+
with: { maintenanceTask: true },
|
|
374
|
+
});
|
|
375
|
+
if (!cost) return { success: false, error: 'Cost entry not found' };
|
|
376
|
+
|
|
377
|
+
const task = cost.maintenanceTask;
|
|
378
|
+
if (!task) return { success: false, error: 'Linked task not found' };
|
|
379
|
+
await requireScope({ organizationId: task.organizationId, hospitalId: task.hospitalId });
|
|
380
|
+
|
|
381
|
+
const editableStates = ['in_progress', 'returned_for_rework', 'work_complete'];
|
|
382
|
+
if (!editableStates.includes(task.statusCode)) {
|
|
383
|
+
return { success: false, error: `Cannot modify costs in ${task.statusCode} state` };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
await db.delete(maintenanceCosts).where(eq(maintenanceCosts.id, costId));
|
|
387
|
+
|
|
388
|
+
return { success: true };
|
|
389
|
+
} catch (error: any) {
|
|
390
|
+
return { success: false, error: error.message };
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export async function getTaskPartsAndCosts(taskId: string) {
|
|
395
|
+
const user = await requireAuth();
|
|
396
|
+
|
|
397
|
+
try {
|
|
398
|
+
const task = await db.query.maintenanceTasks.findFirst({
|
|
399
|
+
where: eq(maintenanceTasks.id, taskId),
|
|
400
|
+
});
|
|
401
|
+
if (!task) return { success: false, error: 'Task not found' };
|
|
402
|
+
await requireScope({ organizationId: task.organizationId, hospitalId: task.hospitalId });
|
|
403
|
+
|
|
404
|
+
const parts = await db.query.maintenanceParts.findMany({
|
|
405
|
+
where: eq(maintenanceParts.maintenanceTaskId, taskId),
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
const costs = await db.query.maintenanceCosts.findMany({
|
|
409
|
+
where: eq(maintenanceCosts.maintenanceTaskId, taskId),
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
const totalPartsCost = parts.reduce((sum, p) => sum + (p.unitCost || 0) * p.quantity, 0);
|
|
413
|
+
const totalOtherCosts = costs.reduce((sum, c) => sum + c.amount, 0);
|
|
414
|
+
|
|
415
|
+
return {
|
|
416
|
+
success: true,
|
|
417
|
+
data: {
|
|
418
|
+
parts,
|
|
419
|
+
costs,
|
|
420
|
+
totalPartsCost,
|
|
421
|
+
totalOtherCosts,
|
|
422
|
+
grandTotal: totalPartsCost + totalOtherCosts,
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
} catch (error: any) {
|
|
426
|
+
return { success: false, error: error.message };
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|