import { z } from "zod"; import { writeAuditLog } from "@/server/audit"; import { assertCan, ServiceError, type ServiceCtx } from "@/server/services/context"; import { assertPlanFeature } from "@/server/plan"; import { parseInput } from "@/server/services/work-orders/_shared"; /** Planning settings of a team = crew (L13): crew working day in minutes + working days bit mask. */ const settingsSchema = z.object({ dailyCapacityMinutes: z.coerce.number().int().min(0).max(24 * 60), workingDays: z.coerce.number().int().min(0).max(127), }); export type TeamPlanningSettingsInput = z.input; export async function updateTeamPlanningSettings(ctx: ServiceCtx, teamId: string, raw: TeamPlanningSettingsInput) { assertCan(ctx, "team:manage"); await assertPlanFeature(ctx.tenantId, "planning"); // L17 Pakete: Kolonnenkapazität gehört zur Planung const input = parseInput(settingsSchema, raw); const before = await ctx.db.team.findFirst({ where: { id: teamId, deletedAt: null }, select: { id: true, dailyCapacityMinutes: true, workingDays: true }, }); if (!before) throw new ServiceError("not_found", "team_not_found"); const after = await ctx.db.team.update({ where: { id: teamId }, data: input, select: { id: true, dailyCapacityMinutes: true, workingDays: true }, }); await writeAuditLog({ tenantId: ctx.tenantId, actorId: ctx.userId, action: "update", entity: "team", entityId: teamId, before, after }); return after; }