refactor(api): extract calculation rule support

This commit is contained in:
2026-03-31 14:00:22 +02:00
parent 46aa038229
commit 6aa0625c8c
3 changed files with 126 additions and 26 deletions
+6 -26
View File
@@ -7,6 +7,10 @@ import { findUniqueOrThrow } from "../db/helpers.js";
import { PROJECT_BRIEF_SELECT } from "../db/selects.js";
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
import { createAuditEntry } from "../lib/audit.js";
import {
buildCalculationRuleCreateData,
buildCalculationRuleUpdateData,
} from "./calculation-rule-support.js";
export const calculationRuleRouter = createTRPCRouter({
list: controllerProcedure.query(async ({ ctx }) => {
@@ -40,18 +44,7 @@ export const calculationRuleRouter = createTRPCRouter({
.input(CreateCalculationRuleSchema)
.mutation(async ({ ctx, input }) => {
const rule = await ctx.db.calculationRule.create({
data: {
name: input.name,
triggerType: input.triggerType,
costEffect: input.costEffect,
chargeabilityEffect: input.chargeabilityEffect,
...(input.description !== undefined ? { description: input.description } : {}),
...(input.projectId !== undefined ? { projectId: input.projectId } : {}),
...(input.orderType !== undefined ? { orderType: input.orderType as never } : {}),
...(input.costReductionPercent !== undefined ? { costReductionPercent: input.costReductionPercent } : {}),
priority: input.priority,
isActive: input.isActive,
},
data: buildCalculationRuleCreateData(input),
});
void createAuditEntry({
@@ -77,22 +70,9 @@ export const calculationRuleRouter = createTRPCRouter({
"CalculationRule",
);
// Build update data using exactOptionalPropertyTypes pattern
const updateData: Record<string, unknown> = {};
if (data.name !== undefined) updateData.name = data.name;
if (data.description !== undefined) updateData.description = data.description;
if (data.triggerType !== undefined) updateData.triggerType = data.triggerType;
if (data.projectId !== undefined) updateData.projectId = data.projectId;
if (data.orderType !== undefined) updateData.orderType = data.orderType;
if (data.costEffect !== undefined) updateData.costEffect = data.costEffect;
if (data.costReductionPercent !== undefined) updateData.costReductionPercent = data.costReductionPercent;
if (data.chargeabilityEffect !== undefined) updateData.chargeabilityEffect = data.chargeabilityEffect;
if (data.priority !== undefined) updateData.priority = data.priority;
if (data.isActive !== undefined) updateData.isActive = data.isActive;
const updated = await ctx.db.calculationRule.update({
where: { id },
data: updateData,
data: buildCalculationRuleUpdateData(data),
});
void createAuditEntry({