refactor(api): extract estimate commercial procedures
This commit is contained in:
@@ -0,0 +1,88 @@
|
|||||||
|
import type { Prisma } from "@capakraken/db";
|
||||||
|
import { CommercialTermsSchema, PermissionKey, UpdateCommercialTermsSchema } from "@capakraken/shared";
|
||||||
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { controllerProcedure, managerProcedure, requirePermission } from "../trpc.js";
|
||||||
|
|
||||||
|
export const estimateCommercialProcedures = {
|
||||||
|
getCommercialTerms: controllerProcedure
|
||||||
|
.input(z.object({ estimateId: z.string(), versionId: z.string().optional() }))
|
||||||
|
.query(async ({ ctx, input }) => {
|
||||||
|
const estimate = await ctx.db.estimate.findUnique({
|
||||||
|
where: { id: input.estimateId },
|
||||||
|
include: {
|
||||||
|
versions: {
|
||||||
|
...(input.versionId
|
||||||
|
? { where: { id: input.versionId } }
|
||||||
|
: { orderBy: { versionNumber: "desc" as const }, take: 1 }),
|
||||||
|
select: { id: true, commercialTerms: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!estimate || estimate.versions.length === 0) {
|
||||||
|
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate version not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const version = estimate.versions[0]!;
|
||||||
|
const raw = version.commercialTerms;
|
||||||
|
const terms = raw
|
||||||
|
? CommercialTermsSchema.parse(raw)
|
||||||
|
: CommercialTermsSchema.parse({});
|
||||||
|
|
||||||
|
return { versionId: version.id, terms };
|
||||||
|
}),
|
||||||
|
|
||||||
|
updateCommercialTerms: managerProcedure
|
||||||
|
.input(UpdateCommercialTermsSchema)
|
||||||
|
.mutation(async ({ ctx, input }) => {
|
||||||
|
requirePermission(ctx, PermissionKey.MANAGE_PROJECTS);
|
||||||
|
|
||||||
|
const estimate = await ctx.db.estimate.findUnique({
|
||||||
|
where: { id: input.estimateId },
|
||||||
|
include: {
|
||||||
|
versions: {
|
||||||
|
...(input.versionId
|
||||||
|
? { where: { id: input.versionId } }
|
||||||
|
: { where: { status: "WORKING" }, take: 1 }),
|
||||||
|
select: { id: true, status: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!estimate || estimate.versions.length === 0) {
|
||||||
|
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate version not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const version = estimate.versions[0]!;
|
||||||
|
|
||||||
|
if (version.status !== "WORKING") {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "PRECONDITION_FAILED",
|
||||||
|
message: "Commercial terms can only be edited on working versions",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const validated = CommercialTermsSchema.parse(input.terms);
|
||||||
|
|
||||||
|
await ctx.db.estimateVersion.update({
|
||||||
|
where: { id: version.id },
|
||||||
|
data: { commercialTerms: validated as unknown as Prisma.InputJsonValue },
|
||||||
|
});
|
||||||
|
|
||||||
|
await ctx.db.auditLog.create({
|
||||||
|
data: {
|
||||||
|
entityType: "Estimate",
|
||||||
|
entityId: estimate.id,
|
||||||
|
action: "UPDATE",
|
||||||
|
...(ctx.dbUser?.id ? { userId: ctx.dbUser.id } : {}),
|
||||||
|
changes: {
|
||||||
|
field: "commercialTerms",
|
||||||
|
after: validated,
|
||||||
|
} as Prisma.InputJsonValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return { versionId: version.id, terms: validated };
|
||||||
|
}),
|
||||||
|
};
|
||||||
@@ -21,7 +21,6 @@ import {
|
|||||||
import {
|
import {
|
||||||
ApproveEstimateVersionSchema,
|
ApproveEstimateVersionSchema,
|
||||||
CloneEstimateSchema,
|
CloneEstimateSchema,
|
||||||
CommercialTermsSchema,
|
|
||||||
CreateEstimateExportSchema,
|
CreateEstimateExportSchema,
|
||||||
CreateEstimatePlanningHandoffSchema,
|
CreateEstimatePlanningHandoffSchema,
|
||||||
CreateEstimateSchema,
|
CreateEstimateSchema,
|
||||||
@@ -29,7 +28,6 @@ import {
|
|||||||
GenerateWeeklyPhasingSchema,
|
GenerateWeeklyPhasingSchema,
|
||||||
PermissionKey,
|
PermissionKey,
|
||||||
SubmitEstimateVersionSchema,
|
SubmitEstimateVersionSchema,
|
||||||
UpdateCommercialTermsSchema,
|
|
||||||
UpdateEstimateDraftSchema,
|
UpdateEstimateDraftSchema,
|
||||||
} from "@capakraken/shared";
|
} from "@capakraken/shared";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
@@ -43,6 +41,7 @@ import {
|
|||||||
requirePermission,
|
requirePermission,
|
||||||
} from "../trpc.js";
|
} from "../trpc.js";
|
||||||
import { emitAllocationCreated } from "../sse/event-bus.js";
|
import { emitAllocationCreated } from "../sse/event-bus.js";
|
||||||
|
import { estimateCommercialProcedures } from "./estimate-commercial.js";
|
||||||
import { estimateReadProcedures } from "./estimate-read.js";
|
import { estimateReadProcedures } from "./estimate-read.js";
|
||||||
|
|
||||||
type EstimateRouterErrorCode = "NOT_FOUND" | "PRECONDITION_FAILED";
|
type EstimateRouterErrorCode = "NOT_FOUND" | "PRECONDITION_FAILED";
|
||||||
@@ -244,6 +243,7 @@ async function autoFillDemandLineRates(
|
|||||||
|
|
||||||
export const estimateRouter = createTRPCRouter({
|
export const estimateRouter = createTRPCRouter({
|
||||||
...estimateReadProcedures,
|
...estimateReadProcedures,
|
||||||
|
...estimateCommercialProcedures,
|
||||||
|
|
||||||
create: managerProcedure
|
create: managerProcedure
|
||||||
.input(CreateEstimateSchema)
|
.input(CreateEstimateSchema)
|
||||||
@@ -834,91 +834,6 @@ export const estimateRouter = createTRPCRouter({
|
|||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// ─── Commercial Terms ───────────────────────────────────────────────────
|
|
||||||
|
|
||||||
getCommercialTerms: controllerProcedure
|
|
||||||
.input(z.object({ estimateId: z.string(), versionId: z.string().optional() }))
|
|
||||||
.query(async ({ ctx, input }) => {
|
|
||||||
const estimate = await ctx.db.estimate.findUnique({
|
|
||||||
where: { id: input.estimateId },
|
|
||||||
include: {
|
|
||||||
versions: {
|
|
||||||
...(input.versionId
|
|
||||||
? { where: { id: input.versionId } }
|
|
||||||
: { orderBy: { versionNumber: "desc" as const }, take: 1 }),
|
|
||||||
select: { id: true, commercialTerms: true },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!estimate || estimate.versions.length === 0) {
|
|
||||||
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate version not found" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const version = estimate.versions[0]!;
|
|
||||||
const raw = version.commercialTerms;
|
|
||||||
|
|
||||||
// Parse stored JSON through Zod for type safety, fall back to defaults
|
|
||||||
const terms = raw
|
|
||||||
? CommercialTermsSchema.parse(raw)
|
|
||||||
: CommercialTermsSchema.parse({});
|
|
||||||
|
|
||||||
return { versionId: version.id, terms };
|
|
||||||
}),
|
|
||||||
|
|
||||||
updateCommercialTerms: managerProcedure
|
|
||||||
.input(UpdateCommercialTermsSchema)
|
|
||||||
.mutation(async ({ ctx, input }) => {
|
|
||||||
requirePermission(ctx, PermissionKey.MANAGE_PROJECTS);
|
|
||||||
|
|
||||||
const estimate = await ctx.db.estimate.findUnique({
|
|
||||||
where: { id: input.estimateId },
|
|
||||||
include: {
|
|
||||||
versions: {
|
|
||||||
...(input.versionId
|
|
||||||
? { where: { id: input.versionId } }
|
|
||||||
: { where: { status: "WORKING" }, take: 1 }),
|
|
||||||
select: { id: true, status: true },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!estimate || estimate.versions.length === 0) {
|
|
||||||
throw new TRPCError({ code: "NOT_FOUND", message: "Estimate version not found" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const version = estimate.versions[0]!;
|
|
||||||
|
|
||||||
if (version.status !== "WORKING") {
|
|
||||||
throw new TRPCError({
|
|
||||||
code: "PRECONDITION_FAILED",
|
|
||||||
message: "Commercial terms can only be edited on working versions",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const validated = CommercialTermsSchema.parse(input.terms);
|
|
||||||
|
|
||||||
await ctx.db.estimateVersion.update({
|
|
||||||
where: { id: version.id },
|
|
||||||
data: { commercialTerms: validated as unknown as Prisma.InputJsonValue },
|
|
||||||
});
|
|
||||||
|
|
||||||
await ctx.db.auditLog.create({
|
|
||||||
data: {
|
|
||||||
entityType: "Estimate",
|
|
||||||
entityId: estimate.id,
|
|
||||||
action: "UPDATE",
|
|
||||||
...(ctx.dbUser?.id ? { userId: ctx.dbUser.id } : {}),
|
|
||||||
changes: {
|
|
||||||
field: "commercialTerms",
|
|
||||||
after: validated,
|
|
||||||
} as Prisma.InputJsonValue,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return { versionId: version.id, terms: validated };
|
|
||||||
}),
|
|
||||||
|
|
||||||
// ─── Rate Card Lookup for Demand Lines ──────────────────────────────────
|
// ─── Rate Card Lookup for Demand Lines ──────────────────────────────────
|
||||||
|
|
||||||
lookupDemandLineRate: controllerProcedure
|
lookupDemandLineRate: controllerProcedure
|
||||||
|
|||||||
Reference in New Issue
Block a user