refactor: consolidate duplicated code across web and API packages

- Extract shared render helpers (vacation blocks, range overlay, overbooking blink) into renderHelpers.tsx
- Centralize status badge styles and vacation color maps into status-styles.ts
- Extract dragMath.ts utility from useTimelineDrag for reuse
- Split useInvalidatePlanningViews into useInvalidateTimeline (4 queries) + useInvalidatePlanningViews (8 queries)
- Adopt findUniqueOrThrow() and Prisma select constants across API routers
- Add shared fmtEur() helper for API-side money formatting
- Wrap TimelineResourcePanel and TimelineProjectPanel with React.memo
- Fix pre-existing TS2589 deep type errors in TeamCalendar and VacationModal
- 38 files changed, reducing ~400 lines of duplicated code

Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
2026-03-19 00:10:08 +01:00
parent ddec3a927a
commit e7b74f13bd
38 changed files with 637 additions and 652 deletions
+18 -18
View File
@@ -2,29 +2,29 @@ import {
CreateCalculationRuleSchema,
UpdateCalculationRuleSchema,
} from "@planarchy/shared";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { findUniqueOrThrow } from "../db/helpers.js";
import { PROJECT_BRIEF_SELECT } from "../db/selects.js";
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
export const calculationRuleRouter = createTRPCRouter({
list: controllerProcedure.query(async ({ ctx }) => {
return ctx.db.calculationRule.findMany({
orderBy: [{ priority: "desc" }, { name: "asc" }],
include: { project: { select: { id: true, name: true, shortCode: true } } },
include: { project: { select: PROJECT_BRIEF_SELECT } },
});
}),
getById: controllerProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const rule = await ctx.db.calculationRule.findUnique({
where: { id: input.id },
include: { project: { select: { id: true, name: true, shortCode: true } } },
});
if (!rule) {
throw new TRPCError({ code: "NOT_FOUND", message: "Calculation rule not found" });
}
return rule;
return findUniqueOrThrow(
ctx.db.calculationRule.findUnique({
where: { id: input.id },
include: { project: { select: PROJECT_BRIEF_SELECT } },
}),
"CalculationRule",
);
}),
/** Get all active rules (optimized for engine use — no project include) */
@@ -58,10 +58,10 @@ export const calculationRuleRouter = createTRPCRouter({
.input(UpdateCalculationRuleSchema)
.mutation(async ({ ctx, input }) => {
const { id, ...data } = input;
const existing = await ctx.db.calculationRule.findUnique({ where: { id } });
if (!existing) {
throw new TRPCError({ code: "NOT_FOUND", message: "Calculation rule not found" });
}
await findUniqueOrThrow(
ctx.db.calculationRule.findUnique({ where: { id } }),
"CalculationRule",
);
// Build update data using exactOptionalPropertyTypes pattern
const updateData: Record<string, unknown> = {};
@@ -85,10 +85,10 @@ export const calculationRuleRouter = createTRPCRouter({
delete: managerProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const existing = await ctx.db.calculationRule.findUnique({ where: { id: input.id } });
if (!existing) {
throw new TRPCError({ code: "NOT_FOUND", message: "Calculation rule not found" });
}
await findUniqueOrThrow(
ctx.db.calculationRule.findUnique({ where: { id: input.id } }),
"CalculationRule",
);
await ctx.db.calculationRule.delete({ where: { id: input.id } });
return { success: true };
}),