feat(GRO-1427): add buffer rules CRUD — enums, table, routes
Re-implement lost commit from worktree cleanup. PR #12 already has UAT_PLAYBOOK + factories fix; add all missing core implementation: - Add petSizeCategoryEnum/coatTypeEnum to schema - Add bufferRules table with service FK + unique constraint - Add defaultBufferMinutes column to services table - Change pets.coatType/petSizeCategory text columns to use enums - Add routes/buffer-rules.ts: GET/POST/PATCH/DELETE, manager role guard - Register /api/buffer-rules in index.ts - Update services.ts PATCH to accept defaultBufferMinutes - Update pets.ts POST/PATCH to accept sizeCategory/coatType - Cast coatType/petSizeCategory in book.ts insert to match new enums - Add 0031_buffer_rules.sql migration - Fix factories.ts buildService to include defaultBufferMinutes: null Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
import { Hono } from "hono";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod/v3";
|
||||
import {
|
||||
and,
|
||||
eq,
|
||||
isNull,
|
||||
getDb,
|
||||
bufferRules,
|
||||
services,
|
||||
} from "@groombook/db";
|
||||
import type { AppEnv } from "../middleware/rbac.js";
|
||||
import { requireRole } from "../middleware/rbac.js";
|
||||
|
||||
export const bufferRulesRouter = new Hono<AppEnv>();
|
||||
|
||||
// Apply manager role guard to all routes
|
||||
bufferRulesRouter.use("*", requireRole("manager"));
|
||||
|
||||
const createBufferRuleSchema = z.object({
|
||||
serviceId: z.string().uuid(),
|
||||
sizeCategory: z.enum(["small", "medium", "large", "xlarge"]).optional(),
|
||||
coatType: z.enum(["smooth", "double", "wire", "curly", "long", "hairless"]).optional(),
|
||||
bufferMinutes: z.number().int().positive(),
|
||||
});
|
||||
|
||||
const updateBufferRuleSchema = z.object({
|
||||
bufferMinutes: z.number().int().positive(),
|
||||
});
|
||||
|
||||
bufferRulesRouter.get("/", async (c) => {
|
||||
const db = getDb();
|
||||
const serviceId = c.req.query("serviceId");
|
||||
|
||||
const conditions = [];
|
||||
if (serviceId) conditions.push(eq(bufferRules.serviceId, serviceId));
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: bufferRules.id,
|
||||
serviceId: bufferRules.serviceId,
|
||||
serviceName: services.name,
|
||||
sizeCategory: bufferRules.sizeCategory,
|
||||
coatType: bufferRules.coatType,
|
||||
bufferMinutes: bufferRules.bufferMinutes,
|
||||
createdAt: bufferRules.createdAt,
|
||||
updatedAt: bufferRules.updatedAt,
|
||||
})
|
||||
.from(bufferRules)
|
||||
.leftJoin(services, eq(bufferRules.serviceId, services.id))
|
||||
.where(conditions.length > 0 ? and(...conditions) : undefined);
|
||||
|
||||
return c.json(rows);
|
||||
});
|
||||
|
||||
bufferRulesRouter.post(
|
||||
"/",
|
||||
zValidator("json", createBufferRuleSchema),
|
||||
async (c) => {
|
||||
const db = getDb();
|
||||
const body = c.req.valid("json");
|
||||
|
||||
// Validate serviceId exists
|
||||
const [svc] = await db
|
||||
.select({ id: services.id })
|
||||
.from(services)
|
||||
.where(eq(services.id, body.serviceId))
|
||||
.limit(1);
|
||||
if (!svc) return c.json({ error: "Service not found" }, 404);
|
||||
|
||||
// Check for duplicate — sizeCategory/coatType are nullable, use isNull for null check
|
||||
const conditions = [eq(bufferRules.serviceId, body.serviceId)];
|
||||
if (body.sizeCategory) {
|
||||
conditions.push(eq(bufferRules.sizeCategory, body.sizeCategory));
|
||||
} else {
|
||||
conditions.push(isNull(bufferRules.sizeCategory));
|
||||
}
|
||||
if (body.coatType) {
|
||||
conditions.push(eq(bufferRules.coatType, body.coatType));
|
||||
} else {
|
||||
conditions.push(isNull(bufferRules.coatType));
|
||||
}
|
||||
const [existing] = await db
|
||||
.select({ id: bufferRules.id })
|
||||
.from(bufferRules)
|
||||
.where(and(...conditions))
|
||||
.limit(1);
|
||||
if (existing) return c.json({ error: "Duplicate rule for this service and attributes" }, 409);
|
||||
|
||||
const [row] = await db
|
||||
.insert(bufferRules)
|
||||
.values({
|
||||
serviceId: body.serviceId,
|
||||
sizeCategory: body.sizeCategory,
|
||||
coatType: body.coatType,
|
||||
bufferMinutes: body.bufferMinutes,
|
||||
})
|
||||
.returning();
|
||||
|
||||
return c.json(row, 201);
|
||||
}
|
||||
);
|
||||
|
||||
bufferRulesRouter.patch(
|
||||
"/:id",
|
||||
zValidator("json", updateBufferRuleSchema),
|
||||
async (c) => {
|
||||
const db = getDb();
|
||||
const body = c.req.valid("json");
|
||||
const [row] = await db
|
||||
.update(bufferRules)
|
||||
.set({ bufferMinutes: body.bufferMinutes, updatedAt: new Date() })
|
||||
.where(eq(bufferRules.id, c.req.param("id")))
|
||||
.returning();
|
||||
if (!row) return c.json({ error: "Not found" }, 404);
|
||||
return c.json(row);
|
||||
}
|
||||
);
|
||||
|
||||
bufferRulesRouter.delete("/:id", async (c) => {
|
||||
const db = getDb();
|
||||
const [row] = await db
|
||||
.delete(bufferRules)
|
||||
.where(eq(bufferRules.id, c.req.param("id")))
|
||||
.returning();
|
||||
if (!row) return c.json({ error: "Not found" }, 404);
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
Reference in New Issue
Block a user