Add repairs domain with tickets, line items, batches, and service templates

Full-stack implementation of instrument repair tracking: DB schema with
repair_ticket, repair_line_item, repair_batch, and repair_service_template
tables. Backend services and routes with pagination/search/sort. 20 API
tests covering CRUD, status workflow, line items, and batch operations.
Admin frontend with ticket list, detail with status progression, line item
management, batch list/detail with approval workflow, and new ticket form
with searchable account picker and intake photo uploads.
This commit is contained in:
Ryan Moon
2026-03-29 09:12:40 -05:00
parent 1d48f0befa
commit f17bbff02c
20 changed files with 2791 additions and 1 deletions

View File

@@ -0,0 +1,78 @@
-- Repair domain enums
CREATE TYPE "repair_ticket_status" AS ENUM ('intake', 'diagnosing', 'pending_approval', 'approved', 'in_progress', 'pending_parts', 'ready', 'picked_up', 'delivered', 'cancelled');
CREATE TYPE "repair_line_item_type" AS ENUM ('labor', 'part', 'flat_rate', 'misc');
CREATE TYPE "repair_condition_in" AS ENUM ('excellent', 'good', 'fair', 'poor');
CREATE TYPE "repair_batch_status" AS ENUM ('intake', 'in_progress', 'pending_approval', 'approved', 'completed', 'delivered', 'cancelled');
CREATE TYPE "repair_batch_approval" AS ENUM ('pending', 'approved', 'rejected');
-- Repair batches (defined first — tickets FK to it)
CREATE TABLE "repair_batch" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"company_id" uuid NOT NULL REFERENCES "company"("id"),
"location_id" uuid REFERENCES "location"("id"),
"batch_number" varchar(50),
"account_id" uuid NOT NULL REFERENCES "account"("id"),
"contact_name" varchar(255),
"contact_phone" varchar(50),
"contact_email" varchar(255),
"status" "repair_batch_status" NOT NULL DEFAULT 'intake',
"approval_status" "repair_batch_approval" NOT NULL DEFAULT 'pending',
"approved_by" uuid REFERENCES "user"("id"),
"approved_at" timestamp with time zone,
"pickup_date" timestamp with time zone,
"due_date" timestamp with time zone,
"completed_date" timestamp with time zone,
"delivered_date" timestamp with time zone,
"instrument_count" integer NOT NULL DEFAULT 0,
"received_count" integer NOT NULL DEFAULT 0,
"estimated_total" numeric(10, 2),
"actual_total" numeric(10, 2),
"notes" text,
"legacy_id" varchar(255),
"created_at" timestamp with time zone NOT NULL DEFAULT now(),
"updated_at" timestamp with time zone NOT NULL DEFAULT now()
);
-- Repair tickets
CREATE TABLE "repair_ticket" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"company_id" uuid NOT NULL REFERENCES "company"("id"),
"location_id" uuid REFERENCES "location"("id"),
"repair_batch_id" uuid REFERENCES "repair_batch"("id"),
"ticket_number" varchar(50),
"account_id" uuid REFERENCES "account"("id"),
"customer_name" varchar(255) NOT NULL,
"customer_phone" varchar(50),
"inventory_unit_id" uuid REFERENCES "inventory_unit"("id"),
"instrument_description" text,
"serial_number" varchar(255),
"condition_in" "repair_condition_in",
"condition_in_notes" text,
"problem_description" text NOT NULL,
"technician_notes" text,
"status" "repair_ticket_status" NOT NULL DEFAULT 'intake',
"assigned_technician_id" uuid REFERENCES "user"("id"),
"estimated_cost" numeric(10, 2),
"actual_cost" numeric(10, 2),
"intake_date" timestamp with time zone NOT NULL DEFAULT now(),
"promised_date" timestamp with time zone,
"completed_date" timestamp with time zone,
"legacy_id" varchar(255),
"created_at" timestamp with time zone NOT NULL DEFAULT now(),
"updated_at" timestamp with time zone NOT NULL DEFAULT now()
);
-- Repair line items
CREATE TABLE "repair_line_item" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"repair_ticket_id" uuid NOT NULL REFERENCES "repair_ticket"("id"),
"item_type" "repair_line_item_type" NOT NULL,
"description" varchar(255) NOT NULL,
"product_id" uuid REFERENCES "product"("id"),
"qty" numeric(10, 3) NOT NULL DEFAULT 1,
"unit_price" numeric(10, 2) NOT NULL DEFAULT 0,
"total_price" numeric(10, 2) NOT NULL DEFAULT 0,
"cost" numeric(10, 2),
"technician_id" uuid REFERENCES "user"("id"),
"created_at" timestamp with time zone NOT NULL DEFAULT now()
);

View File

@@ -0,0 +1,15 @@
CREATE TABLE "repair_service_template" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"company_id" uuid NOT NULL REFERENCES "company"("id"),
"name" varchar(255) NOT NULL,
"instrument_type" varchar(100),
"size" varchar(50),
"description" text,
"item_type" "repair_line_item_type" NOT NULL DEFAULT 'flat_rate',
"default_price" numeric(10, 2) NOT NULL DEFAULT 0,
"default_cost" numeric(10, 2),
"sort_order" integer NOT NULL DEFAULT 0,
"is_active" boolean NOT NULL DEFAULT true,
"created_at" timestamp with time zone NOT NULL DEFAULT now(),
"updated_at" timestamp with time zone NOT NULL DEFAULT now()
);

View File

@@ -106,6 +106,20 @@
"when": 1774740000000,
"tag": "0014_user_is_active",
"breakpoints": true
},
{
"idx": 15,
"version": "7",
"when": 1774750000000,
"tag": "0015_repairs",
"breakpoints": true
},
{
"idx": 16,
"version": "7",
"when": 1774760000000,
"tag": "0016_repair_service_templates",
"breakpoints": true
}
]
}

View File

@@ -0,0 +1,169 @@
import {
pgTable,
uuid,
varchar,
text,
timestamp,
boolean,
integer,
numeric,
pgEnum,
} from 'drizzle-orm/pg-core'
import { companies, locations } from './stores.js'
import { accounts } from './accounts.js'
import { inventoryUnits, products } from './inventory.js'
import { users } from './users.js'
// --- Enums ---
export const repairTicketStatusEnum = pgEnum('repair_ticket_status', [
'intake',
'diagnosing',
'pending_approval',
'approved',
'in_progress',
'pending_parts',
'ready',
'picked_up',
'delivered',
'cancelled',
])
export const repairLineItemTypeEnum = pgEnum('repair_line_item_type', [
'labor',
'part',
'flat_rate',
'misc',
])
export const repairConditionInEnum = pgEnum('repair_condition_in', [
'excellent',
'good',
'fair',
'poor',
])
export const repairBatchStatusEnum = pgEnum('repair_batch_status', [
'intake',
'in_progress',
'pending_approval',
'approved',
'completed',
'delivered',
'cancelled',
])
export const repairBatchApprovalEnum = pgEnum('repair_batch_approval', [
'pending',
'approved',
'rejected',
])
// --- Tables ---
// Defined before repairTickets because tickets FK to batches
export const repairBatches = pgTable('repair_batch', {
id: uuid('id').primaryKey().defaultRandom(),
companyId: uuid('company_id')
.notNull()
.references(() => companies.id),
locationId: uuid('location_id').references(() => locations.id),
batchNumber: varchar('batch_number', { length: 50 }),
accountId: uuid('account_id')
.notNull()
.references(() => accounts.id),
contactName: varchar('contact_name', { length: 255 }),
contactPhone: varchar('contact_phone', { length: 50 }),
contactEmail: varchar('contact_email', { length: 255 }),
status: repairBatchStatusEnum('status').notNull().default('intake'),
approvalStatus: repairBatchApprovalEnum('approval_status').notNull().default('pending'),
approvedBy: uuid('approved_by').references(() => users.id),
approvedAt: timestamp('approved_at', { withTimezone: true }),
pickupDate: timestamp('pickup_date', { withTimezone: true }),
dueDate: timestamp('due_date', { withTimezone: true }),
completedDate: timestamp('completed_date', { withTimezone: true }),
deliveredDate: timestamp('delivered_date', { withTimezone: true }),
instrumentCount: integer('instrument_count').notNull().default(0),
receivedCount: integer('received_count').notNull().default(0),
estimatedTotal: numeric('estimated_total', { precision: 10, scale: 2 }),
actualTotal: numeric('actual_total', { precision: 10, scale: 2 }),
notes: text('notes'),
legacyId: varchar('legacy_id', { length: 255 }),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
})
export const repairTickets = pgTable('repair_ticket', {
id: uuid('id').primaryKey().defaultRandom(),
companyId: uuid('company_id')
.notNull()
.references(() => companies.id),
locationId: uuid('location_id').references(() => locations.id),
repairBatchId: uuid('repair_batch_id').references(() => repairBatches.id),
ticketNumber: varchar('ticket_number', { length: 50 }),
accountId: uuid('account_id').references(() => accounts.id),
customerName: varchar('customer_name', { length: 255 }).notNull(),
customerPhone: varchar('customer_phone', { length: 50 }),
inventoryUnitId: uuid('inventory_unit_id').references(() => inventoryUnits.id),
instrumentDescription: text('instrument_description'),
serialNumber: varchar('serial_number', { length: 255 }),
conditionIn: repairConditionInEnum('condition_in'),
conditionInNotes: text('condition_in_notes'),
problemDescription: text('problem_description').notNull(),
technicianNotes: text('technician_notes'),
status: repairTicketStatusEnum('status').notNull().default('intake'),
assignedTechnicianId: uuid('assigned_technician_id').references(() => users.id),
estimatedCost: numeric('estimated_cost', { precision: 10, scale: 2 }),
actualCost: numeric('actual_cost', { precision: 10, scale: 2 }),
intakeDate: timestamp('intake_date', { withTimezone: true }).notNull().defaultNow(),
promisedDate: timestamp('promised_date', { withTimezone: true }),
completedDate: timestamp('completed_date', { withTimezone: true }),
legacyId: varchar('legacy_id', { length: 255 }),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
})
export const repairLineItems = pgTable('repair_line_item', {
id: uuid('id').primaryKey().defaultRandom(),
repairTicketId: uuid('repair_ticket_id')
.notNull()
.references(() => repairTickets.id),
itemType: repairLineItemTypeEnum('item_type').notNull(),
description: varchar('description', { length: 255 }).notNull(),
productId: uuid('product_id').references(() => products.id),
qty: numeric('qty', { precision: 10, scale: 3 }).notNull().default('1'),
unitPrice: numeric('unit_price', { precision: 10, scale: 2 }).notNull().default('0'),
totalPrice: numeric('total_price', { precision: 10, scale: 2 }).notNull().default('0'),
cost: numeric('cost', { precision: 10, scale: 2 }),
technicianId: uuid('technician_id').references(() => users.id),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})
export const repairServiceTemplates = pgTable('repair_service_template', {
id: uuid('id').primaryKey().defaultRandom(),
companyId: uuid('company_id')
.notNull()
.references(() => companies.id),
name: varchar('name', { length: 255 }).notNull(),
instrumentType: varchar('instrument_type', { length: 100 }),
size: varchar('size', { length: 50 }),
description: text('description'),
itemType: repairLineItemTypeEnum('item_type').notNull().default('flat_rate'),
defaultPrice: numeric('default_price', { precision: 10, scale: 2 }).notNull().default('0'),
defaultCost: numeric('default_cost', { precision: 10, scale: 2 }),
sortOrder: integer('sort_order').notNull().default(0),
isActive: boolean('is_active').notNull().default(true),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
})
// --- Type exports ---
export type RepairTicket = typeof repairTickets.$inferSelect
export type RepairTicketInsert = typeof repairTickets.$inferInsert
export type RepairLineItem = typeof repairLineItems.$inferSelect
export type RepairLineItemInsert = typeof repairLineItems.$inferInsert
export type RepairBatch = typeof repairBatches.$inferSelect
export type RepairBatchInsert = typeof repairBatches.$inferInsert
export type RepairServiceTemplate = typeof repairServiceTemplates.$inferSelect
export type RepairServiceTemplateInsert = typeof repairServiceTemplates.$inferInsert