refactor(application): extract vacation management into application use-cases
Moves approve, reject, cancel, and request vacation business logic out of the tRPC procedure layer into packages/application, matching the pattern used by allocation use-cases. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
import { UpdateVacationStatusSchema } from "@capakraken/shared";
|
||||
import { VacationStatus } from "@capakraken/db";
|
||||
import {
|
||||
approveVacation,
|
||||
batchApproveVacations,
|
||||
batchRejectVacations,
|
||||
cancelVacation,
|
||||
rejectVacation,
|
||||
} from "@capakraken/application";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||
import { RESOURCE_BRIEF_SELECT } from "../db/selects.js";
|
||||
import { createAuditEntry } from "../lib/audit.js";
|
||||
import { checkBatchVacationConflicts, checkVacationConflicts } from "../lib/vacation-conflicts.js";
|
||||
import { emitVacationUpdated } from "../sse/event-bus.js";
|
||||
import { adminProcedure, managerProcedure, protectedProcedure } from "../trpc.js";
|
||||
import {
|
||||
@@ -28,6 +33,7 @@ import {
|
||||
canActorCancelVacation,
|
||||
isVacationManagerRole,
|
||||
} from "./vacation-management-support.js";
|
||||
import { checkBatchVacationConflicts, checkVacationConflicts } from "../lib/vacation-conflicts.js";
|
||||
|
||||
const BatchCreatePublicHolidaysSchema = z.object({
|
||||
year: z.number().int().min(2000).max(2100),
|
||||
@@ -50,43 +56,25 @@ export const vacationManagementProcedures = {
|
||||
approve: managerProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await findUniqueOrThrow(
|
||||
ctx.db.vacation.findUnique({ where: { id: input.id } }),
|
||||
"Vacation",
|
||||
);
|
||||
assertVacationApprovable(existing.status);
|
||||
|
||||
await assertVacationStillChargeable(ctx.db, {
|
||||
resourceId: existing.resourceId,
|
||||
type: existing.type,
|
||||
startDate: existing.startDate,
|
||||
endDate: existing.endDate,
|
||||
isHalfDay: existing.isHalfDay,
|
||||
});
|
||||
const deductionSnapshotWriteData = await buildVacationApprovalWriteData(ctx.db, {
|
||||
resourceId: existing.resourceId,
|
||||
type: existing.type,
|
||||
startDate: existing.startDate,
|
||||
endDate: existing.endDate,
|
||||
isHalfDay: existing.isHalfDay,
|
||||
});
|
||||
|
||||
const userRecord = await findVacationActor(ctx.db, ctx.session.user?.email);
|
||||
const conflictResult = await checkVacationConflicts(
|
||||
|
||||
const result = await approveVacation(
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
ctx.db as any,
|
||||
input.id,
|
||||
userRecord?.id,
|
||||
{ id: input.id, actorUserId: userRecord?.id },
|
||||
{
|
||||
assertVacationApprovable,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
assertVacationStillChargeable: assertVacationStillChargeable as any,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
buildVacationApprovalWriteData: buildVacationApprovalWriteData as any,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
checkVacationConflicts: checkVacationConflicts as any,
|
||||
buildApprovedVacationUpdateData,
|
||||
},
|
||||
);
|
||||
|
||||
const updated = await ctx.db.vacation.update({
|
||||
where: { id: input.id },
|
||||
data: buildApprovedVacationUpdateData({
|
||||
deductionSnapshotWriteData,
|
||||
approvedById: userRecord?.id,
|
||||
approvedAt: new Date(),
|
||||
}),
|
||||
});
|
||||
const { vacation: updated, existingStatus, warnings } = result;
|
||||
|
||||
emitVacationUpdated({ id: updated.id, resourceId: updated.resourceId, status: updated.status });
|
||||
|
||||
@@ -97,10 +85,9 @@ export const vacationManagementProcedures = {
|
||||
entityName: `Vacation ${updated.id}`,
|
||||
action: "UPDATE",
|
||||
...(userRecord?.id ? { userId: userRecord.id } : {}),
|
||||
before: existing as unknown as Record<string, unknown>,
|
||||
after: updated as unknown as Record<string, unknown>,
|
||||
source: "ui",
|
||||
summary: `Approved vacation (was ${existing.status})`,
|
||||
summary: `Approved vacation (was ${existingStatus})`,
|
||||
});
|
||||
|
||||
dispatchVacationWebhookInBackground(ctx.db, "vacation.approved", {
|
||||
@@ -112,28 +99,23 @@ export const vacationManagementProcedures = {
|
||||
|
||||
await completeVacationApprovalTasks(ctx.db, input.id, userRecord?.id);
|
||||
|
||||
if (existing.status === VacationStatus.PENDING) {
|
||||
if (existingStatus === VacationStatus.PENDING) {
|
||||
notifyVacationStatusInBackground(ctx.db, updated.id, updated.resourceId, VacationStatus.APPROVED);
|
||||
}
|
||||
|
||||
return { ...updated, warnings: conflictResult.warnings };
|
||||
return { ...updated, warnings };
|
||||
}),
|
||||
|
||||
reject: managerProcedure
|
||||
.input(z.object({ id: z.string(), rejectionReason: z.string().max(500).optional() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await findUniqueOrThrow(
|
||||
ctx.db.vacation.findUnique({ where: { id: input.id } }),
|
||||
"Vacation",
|
||||
const result = await rejectVacation(
|
||||
ctx.db,
|
||||
{ id: input.id, rejectionReason: input.rejectionReason },
|
||||
{ assertVacationRejectable, buildRejectedVacationUpdateData },
|
||||
);
|
||||
assertVacationRejectable(existing.status);
|
||||
|
||||
const updated = await ctx.db.vacation.update({
|
||||
where: { id: input.id },
|
||||
data: buildRejectedVacationUpdateData({
|
||||
rejectionReason: input.rejectionReason,
|
||||
}),
|
||||
});
|
||||
const { vacation: updated } = result;
|
||||
|
||||
emitVacationUpdated({ id: updated.id, resourceId: updated.resourceId, status: updated.status });
|
||||
|
||||
@@ -147,7 +129,6 @@ export const vacationManagementProcedures = {
|
||||
entityName: `Vacation ${updated.id}`,
|
||||
action: "UPDATE",
|
||||
...(userRecord?.id ? { userId: userRecord.id } : {}),
|
||||
before: existing as unknown as Record<string, unknown>,
|
||||
after: updated as unknown as Record<string, unknown>,
|
||||
source: "ui",
|
||||
summary: `Rejected vacation${input.rejectionReason ? `: ${input.rejectionReason}` : ""}`,
|
||||
@@ -169,46 +150,22 @@ export const vacationManagementProcedures = {
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const userRecord = await findVacationActor(ctx.db, ctx.session.user?.email);
|
||||
|
||||
const vacations = await ctx.db.vacation.findMany({
|
||||
where: { id: { in: input.ids }, status: VacationStatus.PENDING },
|
||||
select: {
|
||||
id: true,
|
||||
resourceId: true,
|
||||
type: true,
|
||||
startDate: true,
|
||||
endDate: true,
|
||||
isHalfDay: true,
|
||||
},
|
||||
});
|
||||
|
||||
for (const vacation of vacations) {
|
||||
await assertVacationStillChargeable(ctx.db, vacation);
|
||||
}
|
||||
|
||||
const conflictMap = await checkBatchVacationConflicts(
|
||||
const result = await batchApproveVacations(
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
ctx.db as any,
|
||||
vacations.map((vacation) => vacation.id),
|
||||
userRecord?.id,
|
||||
{ ids: input.ids, actorUserId: userRecord?.id },
|
||||
{
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
assertVacationStillChargeable: assertVacationStillChargeable as any,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
buildVacationApprovalWriteData: buildVacationApprovalWriteData as any,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
checkBatchVacationConflicts: checkBatchVacationConflicts as any,
|
||||
buildApprovedVacationUpdateData,
|
||||
},
|
||||
);
|
||||
|
||||
for (const vacation of vacations) {
|
||||
const deductionSnapshotWriteData = await buildVacationApprovalWriteData(ctx.db, {
|
||||
resourceId: vacation.resourceId,
|
||||
type: vacation.type,
|
||||
startDate: vacation.startDate,
|
||||
endDate: vacation.endDate,
|
||||
isHalfDay: vacation.isHalfDay,
|
||||
});
|
||||
const updated = await ctx.db.vacation.update({
|
||||
where: { id: vacation.id },
|
||||
data: buildApprovedVacationUpdateData({
|
||||
deductionSnapshotWriteData,
|
||||
approvedById: userRecord?.id,
|
||||
approvedAt: new Date(),
|
||||
}),
|
||||
});
|
||||
|
||||
for (const updated of result.updatedVacations) {
|
||||
emitVacationUpdated({ id: updated.id, resourceId: updated.resourceId, status: updated.status });
|
||||
notifyVacationStatusInBackground(ctx.db, updated.id, updated.resourceId, VacationStatus.APPROVED);
|
||||
|
||||
@@ -219,7 +176,7 @@ export const vacationManagementProcedures = {
|
||||
entityName: `Vacation ${updated.id}`,
|
||||
action: "UPDATE",
|
||||
...(userRecord?.id ? { userId: userRecord.id } : {}),
|
||||
after: updated as unknown as Record<string, unknown>,
|
||||
after: updated.existingVacation as unknown as Record<string, unknown>,
|
||||
source: "ui",
|
||||
summary: "Batch approved vacation",
|
||||
});
|
||||
@@ -227,12 +184,7 @@ export const vacationManagementProcedures = {
|
||||
await completeVacationApprovalTasks(ctx.db, updated.id, userRecord?.id);
|
||||
}
|
||||
|
||||
const warnings: string[] = [];
|
||||
for (const [, vacationWarnings] of conflictMap) {
|
||||
warnings.push(...vacationWarnings);
|
||||
}
|
||||
|
||||
return { approved: vacations.length, warnings };
|
||||
return { approved: result.approved, warnings: result.warnings };
|
||||
}),
|
||||
|
||||
batchReject: managerProcedure
|
||||
@@ -245,19 +197,13 @@ export const vacationManagementProcedures = {
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const userRecord = await findVacationActor(ctx.db, ctx.session.user?.email);
|
||||
|
||||
const vacations = await ctx.db.vacation.findMany({
|
||||
where: { id: { in: input.ids }, status: VacationStatus.PENDING },
|
||||
select: { id: true, resourceId: true },
|
||||
});
|
||||
const result = await batchRejectVacations(
|
||||
ctx.db,
|
||||
{ ids: input.ids, rejectionReason: input.rejectionReason },
|
||||
{ buildRejectedVacationUpdateData },
|
||||
);
|
||||
|
||||
await ctx.db.vacation.updateMany({
|
||||
where: { id: { in: vacations.map((vacation) => vacation.id) } },
|
||||
data: buildRejectedVacationUpdateData({
|
||||
rejectionReason: input.rejectionReason,
|
||||
}),
|
||||
});
|
||||
|
||||
for (const vacation of vacations) {
|
||||
for (const vacation of result.vacations) {
|
||||
emitVacationUpdated({ id: vacation.id, resourceId: vacation.resourceId, status: VacationStatus.REJECTED });
|
||||
notifyVacationStatusInBackground(
|
||||
ctx.db,
|
||||
@@ -282,45 +228,32 @@ export const vacationManagementProcedures = {
|
||||
await completeVacationApprovalTasks(ctx.db, vacation.id, userRecord?.id);
|
||||
}
|
||||
|
||||
return { rejected: vacations.length };
|
||||
return { rejected: result.rejected };
|
||||
}),
|
||||
|
||||
cancel: protectedProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await findUniqueOrThrow(
|
||||
ctx.db.vacation.findUnique({ where: { id: input.id } }),
|
||||
"Vacation",
|
||||
);
|
||||
assertVacationCancelable(existing.status);
|
||||
|
||||
const userRecord = await findVacationActor(ctx.db, ctx.session.user?.email);
|
||||
if (!userRecord) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
const resource = isVacationManagerRole(userRecord.systemRole) || existing.requestedById === userRecord.id
|
||||
? null
|
||||
: await ctx.db.resource.findUnique({
|
||||
where: { id: existing.resourceId },
|
||||
select: { userId: true },
|
||||
});
|
||||
|
||||
if (!canActorCancelVacation({
|
||||
actorId: userRecord.id,
|
||||
actorRole: userRecord.systemRole,
|
||||
requestedById: existing.requestedById,
|
||||
resourceUserId: resource?.userId,
|
||||
})) {
|
||||
throw new TRPCError({
|
||||
code: "FORBIDDEN",
|
||||
message: "You can only cancel your own vacation requests",
|
||||
});
|
||||
}
|
||||
const result = await cancelVacation(
|
||||
ctx.db,
|
||||
{
|
||||
id: input.id,
|
||||
actorId: userRecord.id,
|
||||
actorRole: userRecord.systemRole,
|
||||
},
|
||||
{
|
||||
assertVacationCancelable,
|
||||
isVacationManagerRole,
|
||||
canActorCancelVacation,
|
||||
},
|
||||
);
|
||||
|
||||
const updated = await ctx.db.vacation.update({
|
||||
where: { id: input.id },
|
||||
data: { status: VacationStatus.CANCELLED },
|
||||
});
|
||||
const { vacation: updated, existingStatus } = result;
|
||||
|
||||
emitVacationUpdated({ id: updated.id, resourceId: updated.resourceId, status: updated.status });
|
||||
|
||||
@@ -331,10 +264,9 @@ export const vacationManagementProcedures = {
|
||||
entityName: `Vacation ${updated.id}`,
|
||||
action: "UPDATE",
|
||||
userId: userRecord.id,
|
||||
before: existing as unknown as Record<string, unknown>,
|
||||
after: updated as unknown as Record<string, unknown>,
|
||||
source: "ui",
|
||||
summary: `Cancelled vacation (was ${existing.status})`,
|
||||
summary: `Cancelled vacation (was ${existingStatus})`,
|
||||
});
|
||||
|
||||
return updated;
|
||||
@@ -386,10 +318,10 @@ export const vacationManagementProcedures = {
|
||||
updateStatus: protectedProcedure
|
||||
.input(UpdateVacationStatusSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await findUniqueOrThrow(
|
||||
ctx.db.vacation.findUnique({ where: { id: input.id } }),
|
||||
"Vacation",
|
||||
);
|
||||
const existing = await ctx.db.vacation.findUnique({ where: { id: input.id } });
|
||||
if (!existing) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Vacation not found" });
|
||||
}
|
||||
|
||||
const userRecord = await findVacationActor(ctx.db, ctx.session.user?.email);
|
||||
if (!userRecord) {
|
||||
|
||||
Reference in New Issue
Block a user