chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,296 @@
|
||||
import {
|
||||
expandScopeToEffort,
|
||||
aggregateByDiscipline,
|
||||
type EffortRuleInput,
|
||||
type ScopeItemInput,
|
||||
} from "@planarchy/engine";
|
||||
import {
|
||||
CreateEffortRuleSetSchema,
|
||||
UpdateEffortRuleSetSchema,
|
||||
ApplyEffortRulesSchema,
|
||||
} from "@planarchy/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
|
||||
|
||||
const ruleInclude = {
|
||||
rules: { orderBy: { sortOrder: "asc" as const } },
|
||||
} as const;
|
||||
|
||||
export const effortRuleRouter = createTRPCRouter({
|
||||
list: controllerProcedure.query(async ({ ctx }) => {
|
||||
return ctx.db.effortRuleSet.findMany({
|
||||
include: ruleInclude,
|
||||
orderBy: [{ isDefault: "desc" }, { name: "asc" }],
|
||||
});
|
||||
}),
|
||||
|
||||
getById: controllerProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const ruleSet = await ctx.db.effortRuleSet.findUnique({
|
||||
where: { id: input.id },
|
||||
include: ruleInclude,
|
||||
});
|
||||
if (!ruleSet) throw new TRPCError({ code: "NOT_FOUND", message: "Effort rule set not found" });
|
||||
return ruleSet;
|
||||
}),
|
||||
|
||||
create: managerProcedure
|
||||
.input(CreateEffortRuleSetSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// If this is set as default, unset other defaults
|
||||
if (input.isDefault) {
|
||||
await ctx.db.effortRuleSet.updateMany({
|
||||
where: { isDefault: true },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
}
|
||||
|
||||
return ctx.db.effortRuleSet.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
...(input.description ? { description: input.description } : {}),
|
||||
isDefault: input.isDefault,
|
||||
rules: {
|
||||
create: input.rules.map((r, i) => ({
|
||||
scopeType: r.scopeType,
|
||||
discipline: r.discipline,
|
||||
...(r.chapter ? { chapter: r.chapter } : {}),
|
||||
unitMode: r.unitMode,
|
||||
hoursPerUnit: r.hoursPerUnit,
|
||||
...(r.description ? { description: r.description } : {}),
|
||||
sortOrder: r.sortOrder ?? i,
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: ruleInclude,
|
||||
});
|
||||
}),
|
||||
|
||||
update: managerProcedure
|
||||
.input(UpdateEffortRuleSetSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await ctx.db.effortRuleSet.findUnique({ where: { id: input.id } });
|
||||
if (!existing) throw new TRPCError({ code: "NOT_FOUND", message: "Effort rule set not found" });
|
||||
|
||||
// If setting as default, unset others
|
||||
if (input.isDefault) {
|
||||
await ctx.db.effortRuleSet.updateMany({
|
||||
where: { isDefault: true, id: { not: input.id } },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
}
|
||||
|
||||
// If rules are provided, replace all existing rules
|
||||
if (input.rules) {
|
||||
await ctx.db.effortRule.deleteMany({ where: { ruleSetId: input.id } });
|
||||
await ctx.db.effortRule.createMany({
|
||||
data: input.rules.map((r, i) => ({
|
||||
ruleSetId: input.id,
|
||||
scopeType: r.scopeType,
|
||||
discipline: r.discipline,
|
||||
...(r.chapter ? { chapter: r.chapter } : {}),
|
||||
unitMode: r.unitMode,
|
||||
hoursPerUnit: r.hoursPerUnit,
|
||||
...(r.description ? { description: r.description } : {}),
|
||||
sortOrder: r.sortOrder ?? i,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
return ctx.db.effortRuleSet.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,
|
||||
});
|
||||
}),
|
||||
|
||||
delete: managerProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await ctx.db.effortRuleSet.findUnique({ where: { id: input.id } });
|
||||
if (!existing) throw new TRPCError({ code: "NOT_FOUND", message: "Effort rule set not found" });
|
||||
await ctx.db.effortRuleSet.delete({ where: { id: input.id } });
|
||||
return { id: input.id };
|
||||
}),
|
||||
|
||||
/** Preview the expansion result without persisting */
|
||||
preview: controllerProcedure
|
||||
.input(z.object({
|
||||
estimateId: z.string(),
|
||||
ruleSetId: z.string(),
|
||||
}))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const [estimate, ruleSet] = await Promise.all([
|
||||
ctx.db.estimate.findUnique({
|
||||
where: { id: input.estimateId },
|
||||
include: {
|
||||
versions: {
|
||||
orderBy: { versionNumber: "desc" },
|
||||
take: 1,
|
||||
include: { scopeItems: { orderBy: { sortOrder: "asc" } } },
|
||||
},
|
||||
},
|
||||
}),
|
||||
ctx.db.effortRuleSet.findUnique({
|
||||
where: { id: input.ruleSetId },
|
||||
include: ruleInclude,
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!estimate) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate not found" });
|
||||
if (!ruleSet) throw new TRPCError({ code: "NOT_FOUND", message: "Effort rule set not found" });
|
||||
|
||||
const version = estimate.versions[0];
|
||||
if (!version) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate has no versions" });
|
||||
|
||||
const scopeItems: ScopeItemInput[] = version.scopeItems.map((s) => ({
|
||||
name: s.name,
|
||||
scopeType: s.scopeType,
|
||||
frameCount: s.frameCount,
|
||||
itemCount: s.itemCount,
|
||||
unitMode: s.unitMode,
|
||||
}));
|
||||
|
||||
const rules: EffortRuleInput[] = ruleSet.rules.map((r) => ({
|
||||
scopeType: r.scopeType,
|
||||
discipline: r.discipline,
|
||||
chapter: r.chapter,
|
||||
unitMode: r.unitMode as "per_frame" | "per_item" | "flat",
|
||||
hoursPerUnit: r.hoursPerUnit,
|
||||
sortOrder: r.sortOrder,
|
||||
}));
|
||||
|
||||
const result = expandScopeToEffort(scopeItems, rules);
|
||||
const aggregated = aggregateByDiscipline(result.lines);
|
||||
|
||||
return {
|
||||
...result,
|
||||
aggregated,
|
||||
scopeItemCount: scopeItems.length,
|
||||
ruleCount: rules.length,
|
||||
};
|
||||
}),
|
||||
|
||||
/** Apply effort rules to generate demand lines on the working version */
|
||||
apply: managerProcedure
|
||||
.input(ApplyEffortRulesSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const [estimate, ruleSet] = await Promise.all([
|
||||
ctx.db.estimate.findUnique({
|
||||
where: { id: input.estimateId },
|
||||
include: {
|
||||
versions: {
|
||||
orderBy: { versionNumber: "desc" },
|
||||
take: 1,
|
||||
include: {
|
||||
scopeItems: { orderBy: { sortOrder: "asc" } },
|
||||
demandLines: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
ctx.db.effortRuleSet.findUnique({
|
||||
where: { id: input.ruleSetId },
|
||||
include: ruleInclude,
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!estimate) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate not found" });
|
||||
if (!ruleSet) throw new TRPCError({ code: "NOT_FOUND", message: "Effort rule set not found" });
|
||||
|
||||
const version = estimate.versions[0];
|
||||
if (!version) throw new TRPCError({ code: "NOT_FOUND", message: "Estimate has no versions" });
|
||||
if (version.status !== "WORKING") {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "Can only apply rules to a WORKING version" });
|
||||
}
|
||||
|
||||
const scopeItems: ScopeItemInput[] = version.scopeItems.map((s) => ({
|
||||
name: s.name,
|
||||
scopeType: s.scopeType,
|
||||
frameCount: s.frameCount,
|
||||
itemCount: s.itemCount,
|
||||
unitMode: s.unitMode,
|
||||
}));
|
||||
|
||||
const rules: EffortRuleInput[] = ruleSet.rules.map((r) => ({
|
||||
scopeType: r.scopeType,
|
||||
discipline: r.discipline,
|
||||
chapter: r.chapter,
|
||||
unitMode: r.unitMode as "per_frame" | "per_item" | "flat",
|
||||
hoursPerUnit: r.hoursPerUnit,
|
||||
sortOrder: r.sortOrder,
|
||||
}));
|
||||
|
||||
const result = expandScopeToEffort(scopeItems, rules);
|
||||
|
||||
// In replace mode, delete existing demand lines first
|
||||
if (input.mode === "replace") {
|
||||
await ctx.db.estimateDemandLine.deleteMany({
|
||||
where: { estimateVersionId: version.id },
|
||||
});
|
||||
}
|
||||
|
||||
// Create demand lines from expanded results
|
||||
if (result.lines.length > 0) {
|
||||
await ctx.db.estimateDemandLine.createMany({
|
||||
data: result.lines.map((line) => ({
|
||||
estimateVersionId: version.id,
|
||||
lineType: "LABOR",
|
||||
name: `${line.discipline} — ${line.scopeItemName}`,
|
||||
...(line.chapter ? { chapter: line.chapter } : {}),
|
||||
hours: line.hours,
|
||||
costRateCents: 0,
|
||||
billRateCents: 0,
|
||||
currency: estimate.baseCurrency,
|
||||
costTotalCents: 0,
|
||||
priceTotalCents: 0,
|
||||
monthlySpread: {},
|
||||
staffingAttributes: {},
|
||||
metadata: {
|
||||
effortRule: {
|
||||
ruleSetId: ruleSet.id,
|
||||
ruleSetName: ruleSet.name,
|
||||
discipline: line.discipline,
|
||||
unitMode: line.unitMode,
|
||||
unitCount: line.unitCount,
|
||||
hoursPerUnit: line.hoursPerUnit,
|
||||
},
|
||||
},
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
// Log audit
|
||||
await ctx.db.auditLog.create({
|
||||
data: {
|
||||
entityType: "Estimate",
|
||||
entityId: estimate.id,
|
||||
action: "UPDATE",
|
||||
...(ctx.dbUser?.id ? { userId: ctx.dbUser.id } : {}),
|
||||
changes: {
|
||||
after: {
|
||||
effortRulesApplied: {
|
||||
ruleSetId: ruleSet.id,
|
||||
ruleSetName: ruleSet.name,
|
||||
mode: input.mode,
|
||||
linesGenerated: result.lines.length,
|
||||
warnings: result.warnings,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
linesGenerated: result.lines.length,
|
||||
warnings: result.warnings,
|
||||
unmatchedScopeItems: result.unmatchedScopeItems,
|
||||
};
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user