refactor(api): extract experience multiplier support
This commit is contained in:
@@ -0,0 +1,195 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
buildExperienceMultiplierCreateManyRows,
|
||||||
|
buildExperienceMultiplierDemandLineUpdateData,
|
||||||
|
buildExperienceMultiplierInput,
|
||||||
|
buildExperienceMultiplierNestedCreateRows,
|
||||||
|
buildExperienceMultiplierSetCreateData,
|
||||||
|
buildExperienceMultiplierSetUpdateData,
|
||||||
|
experienceMultiplierRuleInclude,
|
||||||
|
hasExperienceMultiplierChanges,
|
||||||
|
toExperienceMultiplierEngineRules,
|
||||||
|
} from "../router/experience-multiplier-support.js";
|
||||||
|
|
||||||
|
describe("experience multiplier support", () => {
|
||||||
|
it("exposes the rule include ordering", () => {
|
||||||
|
expect(experienceMultiplierRuleInclude).toEqual({
|
||||||
|
rules: { orderBy: { sortOrder: "asc" } },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("builds create-many and nested-create rule rows", () => {
|
||||||
|
expect(buildExperienceMultiplierCreateManyRows([
|
||||||
|
{
|
||||||
|
chapter: "VFX",
|
||||||
|
costMultiplier: 1.2,
|
||||||
|
billMultiplier: 1.3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
location: "India",
|
||||||
|
level: "Senior",
|
||||||
|
costMultiplier: 0.8,
|
||||||
|
billMultiplier: 0.9,
|
||||||
|
shoringRatio: 0.5,
|
||||||
|
additionalEffortRatio: 0.1,
|
||||||
|
description: "Offshore senior",
|
||||||
|
sortOrder: 5,
|
||||||
|
},
|
||||||
|
], "ems_1")).toEqual([
|
||||||
|
{
|
||||||
|
multiplierSetId: "ems_1",
|
||||||
|
chapter: "VFX",
|
||||||
|
costMultiplier: 1.2,
|
||||||
|
billMultiplier: 1.3,
|
||||||
|
sortOrder: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
multiplierSetId: "ems_1",
|
||||||
|
location: "India",
|
||||||
|
level: "Senior",
|
||||||
|
costMultiplier: 0.8,
|
||||||
|
billMultiplier: 0.9,
|
||||||
|
shoringRatio: 0.5,
|
||||||
|
additionalEffortRatio: 0.1,
|
||||||
|
description: "Offshore senior",
|
||||||
|
sortOrder: 5,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(buildExperienceMultiplierNestedCreateRows([
|
||||||
|
{
|
||||||
|
chapter: "Animation",
|
||||||
|
costMultiplier: 1.1,
|
||||||
|
billMultiplier: 1.2,
|
||||||
|
},
|
||||||
|
])).toEqual([
|
||||||
|
{
|
||||||
|
chapter: "Animation",
|
||||||
|
costMultiplier: 1.1,
|
||||||
|
billMultiplier: 1.2,
|
||||||
|
sortOrder: 0,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("builds create and sparse update payloads", () => {
|
||||||
|
expect(buildExperienceMultiplierSetCreateData({
|
||||||
|
name: "Standard Multipliers",
|
||||||
|
description: "Default adjustments",
|
||||||
|
isDefault: true,
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
chapter: "VFX",
|
||||||
|
costMultiplier: 1.2,
|
||||||
|
billMultiplier: 1.2,
|
||||||
|
sortOrder: 0,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})).toEqual({
|
||||||
|
name: "Standard Multipliers",
|
||||||
|
description: "Default adjustments",
|
||||||
|
isDefault: true,
|
||||||
|
rules: {
|
||||||
|
create: [
|
||||||
|
{
|
||||||
|
chapter: "VFX",
|
||||||
|
costMultiplier: 1.2,
|
||||||
|
billMultiplier: 1.2,
|
||||||
|
sortOrder: 0,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(buildExperienceMultiplierSetUpdateData({
|
||||||
|
description: null,
|
||||||
|
isDefault: false,
|
||||||
|
})).toEqual({
|
||||||
|
description: null,
|
||||||
|
isDefault: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("maps db rules and demand lines into engine-facing inputs", () => {
|
||||||
|
expect(toExperienceMultiplierEngineRules([
|
||||||
|
{
|
||||||
|
chapter: "VFX",
|
||||||
|
location: null,
|
||||||
|
level: null,
|
||||||
|
costMultiplier: 1.2,
|
||||||
|
billMultiplier: 1.1,
|
||||||
|
shoringRatio: null,
|
||||||
|
additionalEffortRatio: null,
|
||||||
|
description: null,
|
||||||
|
},
|
||||||
|
])).toEqual([
|
||||||
|
{
|
||||||
|
chapter: "VFX",
|
||||||
|
costMultiplier: 1.2,
|
||||||
|
billMultiplier: 1.1,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(buildExperienceMultiplierInput({
|
||||||
|
id: "dl_1",
|
||||||
|
name: "Comp Senior",
|
||||||
|
chapter: "VFX",
|
||||||
|
costRateCents: 10000,
|
||||||
|
billRateCents: 15000,
|
||||||
|
hours: 100,
|
||||||
|
metadata: { location: "India" },
|
||||||
|
staffingAttributes: { level: "Senior" },
|
||||||
|
})).toEqual({
|
||||||
|
chapter: "VFX",
|
||||||
|
costRateCents: 10000,
|
||||||
|
billRateCents: 15000,
|
||||||
|
hours: 100,
|
||||||
|
location: "India",
|
||||||
|
level: "Senior",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("detects changes and builds demand line update data", () => {
|
||||||
|
const line = {
|
||||||
|
id: "dl_1",
|
||||||
|
name: "Comp Senior",
|
||||||
|
chapter: "VFX",
|
||||||
|
costRateCents: 10000,
|
||||||
|
billRateCents: 15000,
|
||||||
|
hours: 100,
|
||||||
|
metadata: { location: "India", existing: true },
|
||||||
|
staffingAttributes: { level: "Senior" },
|
||||||
|
};
|
||||||
|
const result = {
|
||||||
|
adjustedCostRateCents: 12000,
|
||||||
|
adjustedBillRateCents: 18000,
|
||||||
|
adjustedHours: 110,
|
||||||
|
appliedRules: ["VFX uplift"],
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(hasExperienceMultiplierChanges(line, result)).toBe(true);
|
||||||
|
expect(buildExperienceMultiplierDemandLineUpdateData({
|
||||||
|
line,
|
||||||
|
result,
|
||||||
|
multiplierSet: { id: "ems_1", name: "Standard" },
|
||||||
|
})).toEqual({
|
||||||
|
costRateCents: 12000,
|
||||||
|
billRateCents: 18000,
|
||||||
|
hours: 110,
|
||||||
|
costTotalCents: Math.round(12000 * 110),
|
||||||
|
priceTotalCents: Math.round(18000 * 110),
|
||||||
|
metadata: {
|
||||||
|
location: "India",
|
||||||
|
existing: true,
|
||||||
|
experienceMultiplier: {
|
||||||
|
setId: "ems_1",
|
||||||
|
setName: "Standard",
|
||||||
|
appliedRules: ["VFX uplift"],
|
||||||
|
originalCostRateCents: 10000,
|
||||||
|
originalBillRateCents: 15000,
|
||||||
|
originalHours: 100,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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 {
|
import {
|
||||||
applyExperienceMultipliers,
|
applyExperienceMultipliers,
|
||||||
applyExperienceMultipliersBatch,
|
applyExperienceMultipliersBatch,
|
||||||
type ExperienceMultiplierRule as EngineRule,
|
|
||||||
} from "@capakraken/engine";
|
} from "@capakraken/engine";
|
||||||
import {
|
import {
|
||||||
CreateExperienceMultiplierSetSchema,
|
CreateExperienceMultiplierSetSchema,
|
||||||
@@ -13,39 +12,21 @@ import { z } from "zod";
|
|||||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||||
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
|
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
|
||||||
import { createAuditEntry } from "../lib/audit.js";
|
import { createAuditEntry } from "../lib/audit.js";
|
||||||
|
import {
|
||||||
const ruleInclude = {
|
buildExperienceMultiplierCreateManyRows,
|
||||||
rules: { orderBy: { sortOrder: "asc" as const } },
|
buildExperienceMultiplierDemandLineUpdateData,
|
||||||
} as const;
|
buildExperienceMultiplierInput,
|
||||||
|
buildExperienceMultiplierSetCreateData,
|
||||||
function toEngineRules(
|
buildExperienceMultiplierSetUpdateData,
|
||||||
dbRules: Array<{
|
experienceMultiplierRuleInclude,
|
||||||
chapter: string | null;
|
hasExperienceMultiplierChanges,
|
||||||
location: string | null;
|
toExperienceMultiplierEngineRules,
|
||||||
level: string | null;
|
} from "./experience-multiplier-support.js";
|
||||||
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 } : {}),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
export const experienceMultiplierRouter = createTRPCRouter({
|
export const experienceMultiplierRouter = createTRPCRouter({
|
||||||
list: controllerProcedure.query(async ({ ctx }) => {
|
list: controllerProcedure.query(async ({ ctx }) => {
|
||||||
return ctx.db.experienceMultiplierSet.findMany({
|
return ctx.db.experienceMultiplierSet.findMany({
|
||||||
include: ruleInclude,
|
include: experienceMultiplierRuleInclude,
|
||||||
orderBy: [{ isDefault: "desc" }, { name: "asc" }],
|
orderBy: [{ isDefault: "desc" }, { name: "asc" }],
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
@@ -56,7 +37,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
const set = await findUniqueOrThrow(
|
const set = await findUniqueOrThrow(
|
||||||
ctx.db.experienceMultiplierSet.findUnique({
|
ctx.db.experienceMultiplierSet.findUnique({
|
||||||
where: { id: input.id },
|
where: { id: input.id },
|
||||||
include: ruleInclude,
|
include: experienceMultiplierRuleInclude,
|
||||||
}),
|
}),
|
||||||
"Experience multiplier set",
|
"Experience multiplier set",
|
||||||
);
|
);
|
||||||
@@ -74,25 +55,8 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const set = await ctx.db.experienceMultiplierSet.create({
|
const set = await ctx.db.experienceMultiplierSet.create({
|
||||||
data: {
|
data: buildExperienceMultiplierSetCreateData(input),
|
||||||
name: input.name,
|
include: experienceMultiplierRuleInclude,
|
||||||
...(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,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
void createAuditEntry({
|
void createAuditEntry({
|
||||||
@@ -113,7 +77,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
.input(UpdateExperienceMultiplierSetSchema)
|
.input(UpdateExperienceMultiplierSetSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const before = await findUniqueOrThrow(
|
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",
|
"Experience multiplier set",
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -127,29 +91,14 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
if (input.rules) {
|
if (input.rules) {
|
||||||
await ctx.db.experienceMultiplierRule.deleteMany({ where: { multiplierSetId: input.id } });
|
await ctx.db.experienceMultiplierRule.deleteMany({ where: { multiplierSetId: input.id } });
|
||||||
await ctx.db.experienceMultiplierRule.createMany({
|
await ctx.db.experienceMultiplierRule.createMany({
|
||||||
data: input.rules.map((r, i) => ({
|
data: buildExperienceMultiplierCreateManyRows(input.rules, input.id),
|
||||||
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,
|
|
||||||
})),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const updated = await ctx.db.experienceMultiplierSet.update({
|
const updated = await ctx.db.experienceMultiplierSet.update({
|
||||||
where: { id: input.id },
|
where: { id: input.id },
|
||||||
data: {
|
data: buildExperienceMultiplierSetUpdateData(input),
|
||||||
...(input.name !== undefined ? { name: input.name } : {}),
|
include: experienceMultiplierRuleInclude,
|
||||||
...(input.description !== undefined ? { description: input.description } : {}),
|
|
||||||
...(input.isDefault !== undefined ? { isDefault: input.isDefault } : {}),
|
|
||||||
},
|
|
||||||
include: ruleInclude,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
void createAuditEntry({
|
void createAuditEntry({
|
||||||
@@ -213,7 +162,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
findUniqueOrThrow(
|
findUniqueOrThrow(
|
||||||
ctx.db.experienceMultiplierSet.findUnique({
|
ctx.db.experienceMultiplierSet.findUnique({
|
||||||
where: { id: input.multiplierSetId },
|
where: { id: input.multiplierSetId },
|
||||||
include: ruleInclude,
|
include: experienceMultiplierRuleInclude,
|
||||||
}),
|
}),
|
||||||
"Experience multiplier set",
|
"Experience multiplier set",
|
||||||
),
|
),
|
||||||
@@ -222,23 +171,12 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
const version = estimate.versions[0];
|
const version = estimate.versions[0];
|
||||||
if (!version) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate has no versions" });
|
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 demandLines = version.demandLines;
|
||||||
|
|
||||||
const previews = demandLines.map((line) => {
|
const previews = demandLines.map((line) => {
|
||||||
const result = applyExperienceMultipliers(
|
const result = applyExperienceMultipliers(
|
||||||
{
|
buildExperienceMultiplierInput(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 }
|
|
||||||
: {}),
|
|
||||||
},
|
|
||||||
engineRules,
|
engineRules,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -253,10 +191,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
adjustedBillRateCents: result.adjustedBillRateCents,
|
adjustedBillRateCents: result.adjustedBillRateCents,
|
||||||
adjustedHours: result.adjustedHours,
|
adjustedHours: result.adjustedHours,
|
||||||
appliedRules: result.appliedRules,
|
appliedRules: result.appliedRules,
|
||||||
hasChanges:
|
hasChanges: hasExperienceMultiplierChanges(line, result),
|
||||||
result.adjustedCostRateCents !== line.costRateCents ||
|
|
||||||
result.adjustedBillRateCents !== line.billRateCents ||
|
|
||||||
result.adjustedHours !== line.hours,
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -296,7 +231,7 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
findUniqueOrThrow(
|
findUniqueOrThrow(
|
||||||
ctx.db.experienceMultiplierSet.findUnique({
|
ctx.db.experienceMultiplierSet.findUnique({
|
||||||
where: { id: input.multiplierSetId },
|
where: { id: input.multiplierSetId },
|
||||||
include: ruleInclude,
|
include: experienceMultiplierRuleInclude,
|
||||||
}),
|
}),
|
||||||
"Experience multiplier set",
|
"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" });
|
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 demandLines = version.demandLines;
|
||||||
|
|
||||||
const inputs = demandLines.map((line) => ({
|
const inputs = demandLines.map((line) => buildExperienceMultiplierInput(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 batch = applyExperienceMultipliersBatch(inputs, engineRules);
|
const batch = applyExperienceMultipliersBatch(inputs, engineRules);
|
||||||
|
|
||||||
@@ -332,34 +256,14 @@ export const experienceMultiplierRouter = createTRPCRouter({
|
|||||||
const line = demandLines[i]!;
|
const line = demandLines[i]!;
|
||||||
const result = batch.results[i]!;
|
const result = batch.results[i]!;
|
||||||
|
|
||||||
if (
|
if (hasExperienceMultiplierChanges(line, result)) {
|
||||||
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);
|
|
||||||
|
|
||||||
await ctx.db.estimateDemandLine.update({
|
await ctx.db.estimateDemandLine.update({
|
||||||
where: { id: line.id },
|
where: { id: line.id },
|
||||||
data: {
|
data: buildExperienceMultiplierDemandLineUpdateData({
|
||||||
costRateCents: result.adjustedCostRateCents,
|
line,
|
||||||
billRateCents: result.adjustedBillRateCents,
|
result,
|
||||||
hours: result.adjustedHours,
|
multiplierSet,
|
||||||
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,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
updatedCount++;
|
updatedCount++;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user