refactor(api): extract experience multiplier support
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
import type { Prisma } from "@capakraken/db";
|
||||
import type { ExperienceMultiplierRule as EngineRule } from "@capakraken/engine";
|
||||
import {
|
||||
CreateExperienceMultiplierSetSchema,
|
||||
UpdateExperienceMultiplierSetSchema,
|
||||
} from "@capakraken/shared";
|
||||
import { z } from "zod";
|
||||
|
||||
type CreateExperienceMultiplierSetInput = z.infer<typeof CreateExperienceMultiplierSetSchema>;
|
||||
type UpdateExperienceMultiplierSetInput = z.infer<typeof UpdateExperienceMultiplierSetSchema>;
|
||||
type ExperienceMultiplierRuleRowInput =
|
||||
| CreateExperienceMultiplierSetInput["rules"][number]
|
||||
| NonNullable<UpdateExperienceMultiplierSetInput["rules"]>[number];
|
||||
|
||||
type ExperienceMultiplierRuleRecord = {
|
||||
chapter: string | null;
|
||||
location: string | null;
|
||||
level: string | null;
|
||||
costMultiplier: number;
|
||||
billMultiplier: number;
|
||||
shoringRatio: number | null;
|
||||
additionalEffortRatio: number | null;
|
||||
description: string | null;
|
||||
};
|
||||
|
||||
type DemandLineRecord = {
|
||||
id: string;
|
||||
name: string;
|
||||
chapter: string | null;
|
||||
costRateCents: number;
|
||||
billRateCents: number;
|
||||
hours: number;
|
||||
metadata: unknown;
|
||||
staffingAttributes: unknown;
|
||||
};
|
||||
|
||||
type MultiplierSetRecord = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
type ExperienceMultiplierResultRecord = {
|
||||
adjustedCostRateCents: number;
|
||||
adjustedBillRateCents: number;
|
||||
adjustedHours: number;
|
||||
appliedRules: string[];
|
||||
};
|
||||
|
||||
export const experienceMultiplierRuleInclude = {
|
||||
rules: { orderBy: { sortOrder: "asc" as const } },
|
||||
} as const;
|
||||
|
||||
function buildExperienceMultiplierRuleRow(
|
||||
input: ExperienceMultiplierRuleRowInput,
|
||||
index: number,
|
||||
) {
|
||||
return {
|
||||
...(input.chapter ? { chapter: input.chapter } : {}),
|
||||
...(input.location ? { location: input.location } : {}),
|
||||
...(input.level ? { level: input.level } : {}),
|
||||
costMultiplier: input.costMultiplier,
|
||||
billMultiplier: input.billMultiplier,
|
||||
...(input.shoringRatio !== undefined ? { shoringRatio: input.shoringRatio } : {}),
|
||||
...(input.additionalEffortRatio !== undefined ? { additionalEffortRatio: input.additionalEffortRatio } : {}),
|
||||
...(input.description ? { description: input.description } : {}),
|
||||
sortOrder: input.sortOrder ?? index,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildExperienceMultiplierNestedCreateRows(
|
||||
rules: ExperienceMultiplierRuleRowInput[],
|
||||
): Prisma.ExperienceMultiplierRuleUncheckedCreateWithoutMultiplierSetInput[] {
|
||||
return rules.map((rule, index) => ({
|
||||
...buildExperienceMultiplierRuleRow(rule, index),
|
||||
}));
|
||||
}
|
||||
|
||||
export function buildExperienceMultiplierCreateManyRows(
|
||||
rules: ExperienceMultiplierRuleRowInput[],
|
||||
multiplierSetId: string,
|
||||
): Prisma.ExperienceMultiplierRuleCreateManyInput[] {
|
||||
return rules.map((rule, index) => ({
|
||||
multiplierSetId,
|
||||
...buildExperienceMultiplierRuleRow(rule, index),
|
||||
}));
|
||||
}
|
||||
|
||||
export function buildExperienceMultiplierSetCreateData(
|
||||
input: CreateExperienceMultiplierSetInput,
|
||||
): Prisma.ExperienceMultiplierSetCreateInput {
|
||||
return {
|
||||
name: input.name,
|
||||
...(input.description ? { description: input.description } : {}),
|
||||
isDefault: input.isDefault,
|
||||
rules: {
|
||||
create: buildExperienceMultiplierNestedCreateRows(input.rules),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function buildExperienceMultiplierSetUpdateData(
|
||||
input: Omit<UpdateExperienceMultiplierSetInput, "id" | "rules">,
|
||||
): Prisma.ExperienceMultiplierSetUncheckedUpdateInput {
|
||||
return {
|
||||
...(input.name !== undefined ? { name: input.name } : {}),
|
||||
...(input.description !== undefined ? { description: input.description } : {}),
|
||||
...(input.isDefault !== undefined ? { isDefault: input.isDefault } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function toExperienceMultiplierEngineRules(
|
||||
dbRules: ExperienceMultiplierRuleRecord[],
|
||||
): EngineRule[] {
|
||||
return dbRules.map((rule) => ({
|
||||
...(rule.chapter != null ? { chapter: rule.chapter } : {}),
|
||||
...(rule.location != null ? { location: rule.location } : {}),
|
||||
...(rule.level != null ? { level: rule.level } : {}),
|
||||
costMultiplier: rule.costMultiplier,
|
||||
billMultiplier: rule.billMultiplier,
|
||||
...(rule.shoringRatio != null ? { shoringRatio: rule.shoringRatio } : {}),
|
||||
...(rule.additionalEffortRatio != null ? { additionalEffortRatio: rule.additionalEffortRatio } : {}),
|
||||
...(rule.description != null ? { description: rule.description } : {}),
|
||||
}));
|
||||
}
|
||||
|
||||
export function buildExperienceMultiplierInput(
|
||||
line: DemandLineRecord,
|
||||
) {
|
||||
return {
|
||||
costRateCents: line.costRateCents,
|
||||
billRateCents: line.billRateCents,
|
||||
hours: line.hours,
|
||||
...(line.chapter != null ? { chapter: line.chapter } : {}),
|
||||
...(line.metadata != null && typeof line.metadata === "object" && "location" in (line.metadata as Record<string, unknown>)
|
||||
? { location: (line.metadata as Record<string, unknown>).location as string }
|
||||
: {}),
|
||||
...(line.staffingAttributes != null && typeof line.staffingAttributes === "object" && "level" in (line.staffingAttributes as Record<string, unknown>)
|
||||
? { level: (line.staffingAttributes as Record<string, unknown>).level as string }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function hasExperienceMultiplierChanges(
|
||||
line: Pick<DemandLineRecord, "costRateCents" | "billRateCents" | "hours">,
|
||||
result: ExperienceMultiplierResultRecord,
|
||||
): boolean {
|
||||
return (
|
||||
result.adjustedCostRateCents !== line.costRateCents ||
|
||||
result.adjustedBillRateCents !== line.billRateCents ||
|
||||
result.adjustedHours !== line.hours
|
||||
);
|
||||
}
|
||||
|
||||
export function buildExperienceMultiplierDemandLineUpdateData(input: {
|
||||
line: DemandLineRecord;
|
||||
result: ExperienceMultiplierResultRecord;
|
||||
multiplierSet: MultiplierSetRecord;
|
||||
}): Prisma.EstimateDemandLineUncheckedUpdateInput {
|
||||
const newCostTotal = Math.round(input.result.adjustedCostRateCents * input.result.adjustedHours);
|
||||
const newPriceTotal = Math.round(input.result.adjustedBillRateCents * input.result.adjustedHours);
|
||||
|
||||
return {
|
||||
costRateCents: input.result.adjustedCostRateCents,
|
||||
billRateCents: input.result.adjustedBillRateCents,
|
||||
hours: input.result.adjustedHours,
|
||||
costTotalCents: newCostTotal,
|
||||
priceTotalCents: newPriceTotal,
|
||||
metadata: {
|
||||
...(typeof input.line.metadata === "object" && input.line.metadata !== null
|
||||
? input.line.metadata as Record<string, unknown>
|
||||
: {}),
|
||||
experienceMultiplier: {
|
||||
setId: input.multiplierSet.id,
|
||||
setName: input.multiplierSet.name,
|
||||
appliedRules: input.result.appliedRules,
|
||||
originalCostRateCents: input.line.costRateCents,
|
||||
originalBillRateCents: input.line.billRateCents,
|
||||
originalHours: input.line.hours,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import {
|
||||
applyExperienceMultipliers,
|
||||
applyExperienceMultipliersBatch,
|
||||
type ExperienceMultiplierRule as EngineRule,
|
||||
} from "@capakraken/engine";
|
||||
import {
|
||||
CreateExperienceMultiplierSetSchema,
|
||||
@@ -13,39 +12,21 @@ import { z } from "zod";
|
||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
|
||||
import { createAuditEntry } from "../lib/audit.js";
|
||||
|
||||
const ruleInclude = {
|
||||
rules: { orderBy: { sortOrder: "asc" as const } },
|
||||
} as const;
|
||||
|
||||
function toEngineRules(
|
||||
dbRules: Array<{
|
||||
chapter: string | null;
|
||||
location: string | null;
|
||||
level: string | null;
|
||||
costMultiplier: number;
|
||||
billMultiplier: number;
|
||||
shoringRatio: number | null;
|
||||
additionalEffortRatio: number | null;
|
||||
description: string | null;
|
||||
}>,
|
||||
): EngineRule[] {
|
||||
return dbRules.map((r) => ({
|
||||
...(r.chapter != null ? { chapter: r.chapter } : {}),
|
||||
...(r.location != null ? { location: r.location } : {}),
|
||||
...(r.level != null ? { level: r.level } : {}),
|
||||
costMultiplier: r.costMultiplier,
|
||||
billMultiplier: r.billMultiplier,
|
||||
...(r.shoringRatio != null ? { shoringRatio: r.shoringRatio } : {}),
|
||||
...(r.additionalEffortRatio != null ? { additionalEffortRatio: r.additionalEffortRatio } : {}),
|
||||
...(r.description != null ? { description: r.description } : {}),
|
||||
}));
|
||||
}
|
||||
import {
|
||||
buildExperienceMultiplierCreateManyRows,
|
||||
buildExperienceMultiplierDemandLineUpdateData,
|
||||
buildExperienceMultiplierInput,
|
||||
buildExperienceMultiplierSetCreateData,
|
||||
buildExperienceMultiplierSetUpdateData,
|
||||
experienceMultiplierRuleInclude,
|
||||
hasExperienceMultiplierChanges,
|
||||
toExperienceMultiplierEngineRules,
|
||||
} from "./experience-multiplier-support.js";
|
||||
|
||||
export const experienceMultiplierRouter = createTRPCRouter({
|
||||
list: controllerProcedure.query(async ({ ctx }) => {
|
||||
return ctx.db.experienceMultiplierSet.findMany({
|
||||
include: ruleInclude,
|
||||
include: experienceMultiplierRuleInclude,
|
||||
orderBy: [{ isDefault: "desc" }, { name: "asc" }],
|
||||
});
|
||||
}),
|
||||
@@ -56,7 +37,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
const set = await findUniqueOrThrow(
|
||||
ctx.db.experienceMultiplierSet.findUnique({
|
||||
where: { id: input.id },
|
||||
include: ruleInclude,
|
||||
include: experienceMultiplierRuleInclude,
|
||||
}),
|
||||
"Experience multiplier set",
|
||||
);
|
||||
@@ -74,25 +55,8 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
}
|
||||
|
||||
const set = await ctx.db.experienceMultiplierSet.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
...(input.description ? { description: input.description } : {}),
|
||||
isDefault: input.isDefault,
|
||||
rules: {
|
||||
create: input.rules.map((r, i) => ({
|
||||
...(r.chapter ? { chapter: r.chapter } : {}),
|
||||
...(r.location ? { location: r.location } : {}),
|
||||
...(r.level ? { level: r.level } : {}),
|
||||
costMultiplier: r.costMultiplier,
|
||||
billMultiplier: r.billMultiplier,
|
||||
...(r.shoringRatio !== undefined ? { shoringRatio: r.shoringRatio } : {}),
|
||||
...(r.additionalEffortRatio !== undefined ? { additionalEffortRatio: r.additionalEffortRatio } : {}),
|
||||
...(r.description ? { description: r.description } : {}),
|
||||
sortOrder: r.sortOrder ?? i,
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: ruleInclude,
|
||||
data: buildExperienceMultiplierSetCreateData(input),
|
||||
include: experienceMultiplierRuleInclude,
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -113,7 +77,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
.input(UpdateExperienceMultiplierSetSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const before = await findUniqueOrThrow(
|
||||
ctx.db.experienceMultiplierSet.findUnique({ where: { id: input.id }, include: ruleInclude }),
|
||||
ctx.db.experienceMultiplierSet.findUnique({ where: { id: input.id }, include: experienceMultiplierRuleInclude }),
|
||||
"Experience multiplier set",
|
||||
);
|
||||
|
||||
@@ -127,29 +91,14 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
if (input.rules) {
|
||||
await ctx.db.experienceMultiplierRule.deleteMany({ where: { multiplierSetId: input.id } });
|
||||
await ctx.db.experienceMultiplierRule.createMany({
|
||||
data: input.rules.map((r, i) => ({
|
||||
multiplierSetId: input.id,
|
||||
...(r.chapter ? { chapter: r.chapter } : {}),
|
||||
...(r.location ? { location: r.location } : {}),
|
||||
...(r.level ? { level: r.level } : {}),
|
||||
costMultiplier: r.costMultiplier,
|
||||
billMultiplier: r.billMultiplier,
|
||||
...(r.shoringRatio !== undefined ? { shoringRatio: r.shoringRatio } : {}),
|
||||
...(r.additionalEffortRatio !== undefined ? { additionalEffortRatio: r.additionalEffortRatio } : {}),
|
||||
...(r.description ? { description: r.description } : {}),
|
||||
sortOrder: r.sortOrder ?? i,
|
||||
})),
|
||||
data: buildExperienceMultiplierCreateManyRows(input.rules, input.id),
|
||||
});
|
||||
}
|
||||
|
||||
const updated = await ctx.db.experienceMultiplierSet.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
...(input.name !== undefined ? { name: input.name } : {}),
|
||||
...(input.description !== undefined ? { description: input.description } : {}),
|
||||
...(input.isDefault !== undefined ? { isDefault: input.isDefault } : {}),
|
||||
},
|
||||
include: ruleInclude,
|
||||
data: buildExperienceMultiplierSetUpdateData(input),
|
||||
include: experienceMultiplierRuleInclude,
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -213,7 +162,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
findUniqueOrThrow(
|
||||
ctx.db.experienceMultiplierSet.findUnique({
|
||||
where: { id: input.multiplierSetId },
|
||||
include: ruleInclude,
|
||||
include: experienceMultiplierRuleInclude,
|
||||
}),
|
||||
"Experience multiplier set",
|
||||
),
|
||||
@@ -222,23 +171,12 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
const version = estimate.versions[0];
|
||||
if (!version) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate has no versions" });
|
||||
|
||||
const engineRules = toEngineRules(multiplierSet.rules);
|
||||
const engineRules = toExperienceMultiplierEngineRules(multiplierSet.rules);
|
||||
const demandLines = version.demandLines;
|
||||
|
||||
const previews = demandLines.map((line) => {
|
||||
const result = applyExperienceMultipliers(
|
||||
{
|
||||
costRateCents: line.costRateCents,
|
||||
billRateCents: line.billRateCents,
|
||||
hours: line.hours,
|
||||
...(line.chapter != null ? { chapter: line.chapter } : {}),
|
||||
...(line.metadata != null && typeof line.metadata === "object" && "location" in (line.metadata as Record<string, unknown>)
|
||||
? { location: (line.metadata as Record<string, unknown>).location as string }
|
||||
: {}),
|
||||
...(line.staffingAttributes != null && typeof line.staffingAttributes === "object" && "level" in (line.staffingAttributes as Record<string, unknown>)
|
||||
? { level: (line.staffingAttributes as Record<string, unknown>).level as string }
|
||||
: {}),
|
||||
},
|
||||
buildExperienceMultiplierInput(line),
|
||||
engineRules,
|
||||
);
|
||||
|
||||
@@ -253,10 +191,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
adjustedBillRateCents: result.adjustedBillRateCents,
|
||||
adjustedHours: result.adjustedHours,
|
||||
appliedRules: result.appliedRules,
|
||||
hasChanges:
|
||||
result.adjustedCostRateCents !== line.costRateCents ||
|
||||
result.adjustedBillRateCents !== line.billRateCents ||
|
||||
result.adjustedHours !== line.hours,
|
||||
hasChanges: hasExperienceMultiplierChanges(line, result),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -296,7 +231,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
findUniqueOrThrow(
|
||||
ctx.db.experienceMultiplierSet.findUnique({
|
||||
where: { id: input.multiplierSetId },
|
||||
include: ruleInclude,
|
||||
include: experienceMultiplierRuleInclude,
|
||||
}),
|
||||
"Experience multiplier set",
|
||||
),
|
||||
@@ -308,21 +243,10 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "Can only apply multipliers to a WORKING version" });
|
||||
}
|
||||
|
||||
const engineRules = toEngineRules(multiplierSet.rules);
|
||||
const engineRules = toExperienceMultiplierEngineRules(multiplierSet.rules);
|
||||
const demandLines = version.demandLines;
|
||||
|
||||
const inputs = demandLines.map((line) => ({
|
||||
costRateCents: line.costRateCents,
|
||||
billRateCents: line.billRateCents,
|
||||
hours: line.hours,
|
||||
...(line.chapter != null ? { chapter: line.chapter } : {}),
|
||||
...(line.metadata != null && typeof line.metadata === "object" && "location" in (line.metadata as Record<string, unknown>)
|
||||
? { location: (line.metadata as Record<string, unknown>).location as string }
|
||||
: {}),
|
||||
...(line.staffingAttributes != null && typeof line.staffingAttributes === "object" && "level" in (line.staffingAttributes as Record<string, unknown>)
|
||||
? { level: (line.staffingAttributes as Record<string, unknown>).level as string }
|
||||
: {}),
|
||||
}));
|
||||
const inputs = demandLines.map((line) => buildExperienceMultiplierInput(line));
|
||||
|
||||
const batch = applyExperienceMultipliersBatch(inputs, engineRules);
|
||||
|
||||
@@ -332,34 +256,14 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
||||
const line = demandLines[i]!;
|
||||
const result = batch.results[i]!;
|
||||
|
||||
if (
|
||||
result.adjustedCostRateCents !== line.costRateCents ||
|
||||
result.adjustedBillRateCents !== line.billRateCents ||
|
||||
result.adjustedHours !== line.hours
|
||||
) {
|
||||
const newCostTotal = Math.round(result.adjustedCostRateCents * result.adjustedHours);
|
||||
const newPriceTotal = Math.round(result.adjustedBillRateCents * result.adjustedHours);
|
||||
|
||||
if (hasExperienceMultiplierChanges(line, result)) {
|
||||
await ctx.db.estimateDemandLine.update({
|
||||
where: { id: line.id },
|
||||
data: {
|
||||
costRateCents: result.adjustedCostRateCents,
|
||||
billRateCents: result.adjustedBillRateCents,
|
||||
hours: result.adjustedHours,
|
||||
costTotalCents: newCostTotal,
|
||||
priceTotalCents: newPriceTotal,
|
||||
metadata: {
|
||||
...(typeof line.metadata === "object" && line.metadata !== null ? line.metadata as Record<string, unknown> : {}),
|
||||
experienceMultiplier: {
|
||||
setId: multiplierSet.id,
|
||||
setName: multiplierSet.name,
|
||||
appliedRules: result.appliedRules,
|
||||
originalCostRateCents: line.costRateCents,
|
||||
originalBillRateCents: line.billRateCents,
|
||||
originalHours: line.hours,
|
||||
},
|
||||
},
|
||||
},
|
||||
data: buildExperienceMultiplierDemandLineUpdateData({
|
||||
line,
|
||||
result,
|
||||
multiplierSet,
|
||||
}),
|
||||
});
|
||||
updatedCount++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user