refactor(api): share rate card support shapes
This commit is contained in:
@@ -19,6 +19,45 @@ type CreateRateCardLineInput = z.infer<typeof CreateRateCardLineSchema>;
|
||||
type UpdateRateCardInput = z.infer<typeof UpdateRateCardSchema>;
|
||||
type UpdateRateCardLineInput = z.infer<typeof UpdateRateCardLineSchema>;
|
||||
|
||||
export const rateCardSummaryInclude = {
|
||||
_count: { select: { lines: true } },
|
||||
client: { select: { id: true, name: true, code: true } },
|
||||
} as const;
|
||||
|
||||
export const rateCardLineOrderBy: Prisma.RateCardLineOrderByWithRelationInput[] = [
|
||||
{ chapter: "asc" },
|
||||
{ seniority: "asc" },
|
||||
{ createdAt: "asc" },
|
||||
];
|
||||
|
||||
export const rateCardDetailInclude = {
|
||||
client: { select: { id: true, name: true, code: true } },
|
||||
lines: {
|
||||
select: {
|
||||
id: true,
|
||||
rateCardId: true,
|
||||
roleId: true,
|
||||
chapter: true,
|
||||
seniority: true,
|
||||
location: true,
|
||||
workType: true,
|
||||
serviceGroup: true,
|
||||
costRateCents: true,
|
||||
billRateCents: true,
|
||||
machineRateCents: true,
|
||||
attributes: true,
|
||||
role: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: rateCardLineOrderBy,
|
||||
},
|
||||
} as const;
|
||||
|
||||
export const rateCardCreateInclude = {
|
||||
lines: {
|
||||
select: rateCardDetailInclude.lines.select,
|
||||
},
|
||||
} as const;
|
||||
|
||||
function buildRateCardLineCoreData(
|
||||
line: CreateRateCardLineInput,
|
||||
) {
|
||||
@@ -134,3 +173,33 @@ export function buildRateCardLineUpdateData(
|
||||
|
||||
return updateData;
|
||||
}
|
||||
|
||||
export function buildRateCardCreateAuditAfter(
|
||||
input: Pick<CreateRateCardInput, "name" | "currency" | "lines">,
|
||||
) {
|
||||
return {
|
||||
name: input.name,
|
||||
currency: input.currency,
|
||||
lineCount: input.lines.length,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildRateCardLineCreateAuditAfter(
|
||||
input: {
|
||||
rateCardId: string;
|
||||
line: Pick<CreateRateCardLineInput, "costRateCents" | "billRateCents" | "chapter">;
|
||||
},
|
||||
) {
|
||||
return {
|
||||
rateCardId: input.rateCardId,
|
||||
costRateCents: input.line.costRateCents,
|
||||
billRateCents: input.line.billRateCents,
|
||||
chapter: input.line.chapter ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildRateCardReplaceLinesAuditAfter(
|
||||
replacedLineCount: number,
|
||||
) {
|
||||
return { replacedLineCount };
|
||||
}
|
||||
|
||||
@@ -15,12 +15,18 @@ import {
|
||||
resolveBestRateLineMatch,
|
||||
} from "./rate-card-read.js";
|
||||
import {
|
||||
buildRateCardCreateAuditAfter,
|
||||
buildRateCardCreateData,
|
||||
buildRateCardLineCreateAuditAfter,
|
||||
buildRateCardLineCreateData,
|
||||
buildRateCardLineUpdateData,
|
||||
buildRateCardListWhere,
|
||||
buildRateCardReplaceLinesAuditAfter,
|
||||
buildRateCardUpdateData,
|
||||
rateCardCreateInclude,
|
||||
rateCardDetailInclude,
|
||||
buildRateCardNestedLineCreateData,
|
||||
rateCardSummaryInclude,
|
||||
} from "./rate-card-write-support.js";
|
||||
|
||||
export const rateCardRouter = createTRPCRouter({
|
||||
@@ -36,10 +42,7 @@ export const rateCardRouter = createTRPCRouter({
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.db.rateCard.findMany({
|
||||
where: buildRateCardListWhere(input),
|
||||
include: {
|
||||
_count: { select: { lines: true } },
|
||||
client: { select: { id: true, name: true, code: true } },
|
||||
},
|
||||
include: rateCardSummaryInclude,
|
||||
orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
|
||||
});
|
||||
}),
|
||||
@@ -50,13 +53,7 @@ export const rateCardRouter = createTRPCRouter({
|
||||
const rateCard = await findUniqueOrThrow(
|
||||
ctx.db.rateCard.findUnique({
|
||||
where: { id: input.id },
|
||||
include: {
|
||||
client: { select: { id: true, name: true, code: true } },
|
||||
lines: {
|
||||
select: rateCardLineSelect,
|
||||
orderBy: [{ chapter: "asc" }, { seniority: "asc" }, { createdAt: "asc" }],
|
||||
},
|
||||
},
|
||||
include: rateCardDetailInclude,
|
||||
}),
|
||||
"Rate card",
|
||||
);
|
||||
@@ -88,9 +85,7 @@ export const rateCardRouter = createTRPCRouter({
|
||||
|
||||
const rateCard = await ctx.db.rateCard.create({
|
||||
data: buildRateCardCreateData(input),
|
||||
include: {
|
||||
lines: { select: rateCardLineSelect },
|
||||
},
|
||||
include: rateCardCreateInclude,
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -100,7 +95,7 @@ export const rateCardRouter = createTRPCRouter({
|
||||
entityName: rateCard.name,
|
||||
action: "CREATE",
|
||||
userId: ctx.dbUser?.id,
|
||||
after: { name, currency, lineCount: lines.length },
|
||||
after: buildRateCardCreateAuditAfter({ name, currency, lines }),
|
||||
source: "ui",
|
||||
});
|
||||
|
||||
@@ -118,10 +113,7 @@ export const rateCardRouter = createTRPCRouter({
|
||||
const updated = await ctx.db.rateCard.update({
|
||||
where: { id: input.id },
|
||||
data: buildRateCardUpdateData(input.data),
|
||||
include: {
|
||||
_count: { select: { lines: true } },
|
||||
client: { select: { id: true, name: true, code: true } },
|
||||
},
|
||||
include: rateCardSummaryInclude,
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -183,7 +175,10 @@ export const rateCardRouter = createTRPCRouter({
|
||||
entityName: `${rateCard.name} — ${input.line.chapter ?? "line"}`,
|
||||
action: "CREATE",
|
||||
userId: ctx.dbUser?.id,
|
||||
after: { rateCardId: input.rateCardId, costRateCents: input.line.costRateCents, billRateCents: input.line.billRateCents },
|
||||
after: buildRateCardLineCreateAuditAfter({
|
||||
rateCardId: input.rateCardId,
|
||||
line: input.line,
|
||||
}),
|
||||
source: "ui",
|
||||
});
|
||||
|
||||
@@ -279,7 +274,7 @@ export const rateCardRouter = createTRPCRouter({
|
||||
entityName: rateCard.name,
|
||||
action: "UPDATE",
|
||||
userId: ctx.dbUser?.id,
|
||||
after: { replacedLineCount: result.length },
|
||||
after: buildRateCardReplaceLinesAuditAfter(result.length),
|
||||
source: "ui",
|
||||
summary: `Replaced all lines with ${result.length} new lines`,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user