refactor(api): extract rate card write support

This commit is contained in:
2026-03-31 13:29:27 +02:00
parent 0a10a440ee
commit b57f7e6d2e
3 changed files with 252 additions and 93 deletions
+16 -93
View File
@@ -1,4 +1,3 @@
import type { Prisma } from "@capakraken/db";
import {
CreateRateCardLineSchema,
CreateRateCardSchema,
@@ -15,6 +14,14 @@ import {
resolveBestRate,
resolveBestRateLineMatch,
} from "./rate-card-read.js";
import {
buildRateCardCreateData,
buildRateCardLineCreateData,
buildRateCardLineUpdateData,
buildRateCardListWhere,
buildRateCardUpdateData,
buildRateCardNestedLineCreateData,
} from "./rate-card-write-support.js";
export const rateCardRouter = createTRPCRouter({
list: controllerProcedure
@@ -28,29 +35,7 @@ export const rateCardRouter = createTRPCRouter({
)
.query(async ({ ctx, input }) => {
return ctx.db.rateCard.findMany({
where: {
...(input?.isActive !== undefined ? { isActive: input.isActive } : {}),
...(input?.clientId !== undefined ? { clientId: input.clientId } : {}),
...(input?.search
? { name: { contains: input.search, mode: "insensitive" as const } }
: {}),
...(input?.effectiveAt
? {
OR: [
{ effectiveFrom: null },
{ effectiveFrom: { lte: input.effectiveAt } },
],
AND: [
{
OR: [
{ effectiveTo: null },
{ effectiveTo: { gte: input.effectiveAt } },
],
},
],
}
: {}),
},
where: buildRateCardListWhere(input),
include: {
_count: { select: { lines: true } },
client: { select: { id: true, name: true, code: true } },
@@ -99,31 +84,10 @@ export const rateCardRouter = createTRPCRouter({
create: managerProcedure
.input(CreateRateCardSchema)
.mutation(async ({ ctx, input }) => {
const { lines, ...cardData } = input;
const { lines, name, currency } = input;
const rateCard = await ctx.db.rateCard.create({
data: {
name: cardData.name,
currency: cardData.currency,
...(cardData.effectiveFrom !== undefined ? { effectiveFrom: cardData.effectiveFrom } : {}),
...(cardData.effectiveTo !== undefined ? { effectiveTo: cardData.effectiveTo } : {}),
...(cardData.source !== undefined ? { source: cardData.source } : {}),
...(cardData.clientId !== undefined ? { clientId: cardData.clientId } : {}),
lines: {
create: lines.map((line) => ({
...(line.roleId !== undefined ? { roleId: line.roleId } : {}),
...(line.chapter !== undefined ? { chapter: line.chapter } : {}),
...(line.location !== undefined ? { location: line.location } : {}),
...(line.seniority !== undefined ? { seniority: line.seniority } : {}),
...(line.workType !== undefined ? { workType: line.workType } : {}),
...(line.serviceGroup !== undefined ? { serviceGroup: line.serviceGroup } : {}),
costRateCents: line.costRateCents,
...(line.billRateCents !== undefined ? { billRateCents: line.billRateCents } : {}),
...(line.machineRateCents !== undefined ? { machineRateCents: line.machineRateCents } : {}),
attributes: line.attributes as Prisma.InputJsonValue,
})),
},
},
data: buildRateCardCreateData(input),
include: {
lines: { select: rateCardLineSelect },
},
@@ -136,7 +100,7 @@ export const rateCardRouter = createTRPCRouter({
entityName: rateCard.name,
action: "CREATE",
userId: ctx.dbUser?.id,
after: { name: cardData.name, currency: cardData.currency, lineCount: lines.length },
after: { name, currency, lineCount: lines.length },
source: "ui",
});
@@ -153,15 +117,7 @@ export const rateCardRouter = createTRPCRouter({
const updated = await ctx.db.rateCard.update({
where: { id: input.id },
data: {
...(input.data.name !== undefined ? { name: input.data.name } : {}),
...(input.data.currency !== undefined ? { currency: input.data.currency } : {}),
...(input.data.effectiveFrom !== undefined ? { effectiveFrom: input.data.effectiveFrom } : {}),
...(input.data.effectiveTo !== undefined ? { effectiveTo: input.data.effectiveTo } : {}),
...(input.data.source !== undefined ? { source: input.data.source } : {}),
...(input.data.clientId !== undefined ? { clientId: input.data.clientId } : {}),
...(input.data.isActive !== undefined ? { isActive: input.data.isActive } : {}),
},
data: buildRateCardUpdateData(input.data),
include: {
_count: { select: { lines: true } },
client: { select: { id: true, name: true, code: true } },
@@ -216,19 +172,7 @@ export const rateCardRouter = createTRPCRouter({
);
const line = await ctx.db.rateCardLine.create({
data: {
rateCardId: input.rateCardId,
...(input.line.roleId !== undefined ? { roleId: input.line.roleId } : {}),
...(input.line.chapter !== undefined ? { chapter: input.line.chapter } : {}),
...(input.line.location !== undefined ? { location: input.line.location } : {}),
...(input.line.seniority !== undefined ? { seniority: input.line.seniority } : {}),
...(input.line.workType !== undefined ? { workType: input.line.workType } : {}),
...(input.line.serviceGroup !== undefined ? { serviceGroup: input.line.serviceGroup } : {}),
costRateCents: input.line.costRateCents,
...(input.line.billRateCents !== undefined ? { billRateCents: input.line.billRateCents } : {}),
...(input.line.machineRateCents !== undefined ? { machineRateCents: input.line.machineRateCents } : {}),
attributes: input.line.attributes as Prisma.InputJsonValue,
},
data: buildRateCardLineCreateData(input.rateCardId, input.line),
select: rateCardLineSelect,
});
@@ -254,21 +198,9 @@ export const rateCardRouter = createTRPCRouter({
"Rate card line",
);
const updateData: Prisma.RateCardLineUpdateInput = {};
if (input.data.roleId !== undefined) updateData.role = input.data.roleId ? { connect: { id: input.data.roleId } } : { disconnect: true };
if (input.data.chapter !== undefined) updateData.chapter = input.data.chapter;
if (input.data.location !== undefined) updateData.location = input.data.location;
if (input.data.seniority !== undefined) updateData.seniority = input.data.seniority;
if (input.data.workType !== undefined) updateData.workType = input.data.workType;
if (input.data.serviceGroup !== undefined) updateData.serviceGroup = input.data.serviceGroup;
if (input.data.costRateCents !== undefined) updateData.costRateCents = input.data.costRateCents;
if (input.data.billRateCents !== undefined) updateData.billRateCents = input.data.billRateCents;
if (input.data.machineRateCents !== undefined) updateData.machineRateCents = input.data.machineRateCents;
if (input.data.attributes !== undefined) updateData.attributes = input.data.attributes as Prisma.InputJsonValue;
const updated = await ctx.db.rateCardLine.update({
where: { id: input.lineId },
data: updateData,
data: buildRateCardLineUpdateData(input.data),
select: rateCardLineSelect,
});
@@ -330,16 +262,7 @@ export const rateCardRouter = createTRPCRouter({
tx.rateCardLine.create({
data: {
rateCardId: input.rateCardId,
...(line.roleId !== undefined ? { roleId: line.roleId } : {}),
...(line.chapter !== undefined ? { chapter: line.chapter } : {}),
...(line.location !== undefined ? { location: line.location } : {}),
...(line.seniority !== undefined ? { seniority: line.seniority } : {}),
...(line.workType !== undefined ? { workType: line.workType } : {}),
...(line.serviceGroup !== undefined ? { serviceGroup: line.serviceGroup } : {}),
costRateCents: line.costRateCents,
...(line.billRateCents !== undefined ? { billRateCents: line.billRateCents } : {}),
...(line.machineRateCents !== undefined ? { machineRateCents: line.machineRateCents } : {}),
attributes: line.attributes as Prisma.InputJsonValue,
...buildRateCardNestedLineCreateData(line),
},
select: rateCardLineSelect,
}),