refactor(api): extract holiday calendar procedures
This commit is contained in:
@@ -0,0 +1,235 @@
|
|||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
const { createAuditEntry } = vi.hoisted(() => ({
|
||||||
|
createAuditEntry: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../lib/audit.js", () => ({
|
||||||
|
createAuditEntry,
|
||||||
|
}));
|
||||||
|
|
||||||
|
import {
|
||||||
|
createHolidayCalendar,
|
||||||
|
createHolidayCalendarEntry,
|
||||||
|
deleteHolidayCalendarEntry,
|
||||||
|
updateHolidayCalendar,
|
||||||
|
} from "../router/holiday-calendar-procedure-support.js";
|
||||||
|
|
||||||
|
function createContext(db: Record<string, unknown>) {
|
||||||
|
return {
|
||||||
|
db: db as never,
|
||||||
|
dbUser: { id: "user_admin" } as never,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("holiday calendar procedure support", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
createAuditEntry.mockReset();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates a holiday calendar with normalized scope and audit logging", async () => {
|
||||||
|
const countryFindUnique = vi.fn().mockResolvedValue({
|
||||||
|
id: "country_de",
|
||||||
|
name: "Deutschland",
|
||||||
|
});
|
||||||
|
const metroCityFindUnique = vi.fn().mockResolvedValue(null);
|
||||||
|
const holidayCalendarFindFirst = vi.fn().mockResolvedValue(null);
|
||||||
|
const holidayCalendarCreate = vi.fn().mockResolvedValue({
|
||||||
|
id: "cal_by",
|
||||||
|
name: "Bayern Feiertage",
|
||||||
|
country: { id: "country_de", code: "DE", name: "Deutschland" },
|
||||||
|
metroCity: null,
|
||||||
|
entries: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await createHolidayCalendar(
|
||||||
|
createContext({
|
||||||
|
country: { findUnique: countryFindUnique },
|
||||||
|
metroCity: { findUnique: metroCityFindUnique },
|
||||||
|
holidayCalendar: {
|
||||||
|
findFirst: holidayCalendarFindFirst,
|
||||||
|
create: holidayCalendarCreate,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
name: "Bayern Feiertage",
|
||||||
|
scopeType: "STATE",
|
||||||
|
countryId: "country_de",
|
||||||
|
stateCode: " by ",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(countryFindUnique).toHaveBeenCalledWith({
|
||||||
|
where: { id: "country_de" },
|
||||||
|
select: { id: true, name: true },
|
||||||
|
});
|
||||||
|
expect(holidayCalendarFindFirst).toHaveBeenCalledWith({
|
||||||
|
where: {
|
||||||
|
scopeType: "STATE",
|
||||||
|
countryId: "country_de",
|
||||||
|
stateCode: "BY",
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
expect(holidayCalendarCreate).toHaveBeenCalledWith({
|
||||||
|
data: {
|
||||||
|
name: "Bayern Feiertage",
|
||||||
|
scopeType: "STATE",
|
||||||
|
countryId: "country_de",
|
||||||
|
stateCode: "BY",
|
||||||
|
isActive: true,
|
||||||
|
priority: 0,
|
||||||
|
},
|
||||||
|
include: expect.any(Object),
|
||||||
|
});
|
||||||
|
expect(result.id).toBe("cal_by");
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
entityType: "HolidayCalendar",
|
||||||
|
action: "CREATE",
|
||||||
|
entityId: "cal_by",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("updates a holiday calendar with resolved partial scope values", async () => {
|
||||||
|
const holidayCalendarFindUnique = vi.fn().mockResolvedValue({
|
||||||
|
id: "cal_city",
|
||||||
|
name: "Augsburg lokal",
|
||||||
|
scopeType: "CITY",
|
||||||
|
countryId: "country_de",
|
||||||
|
stateCode: null,
|
||||||
|
metroCityId: "city_augsburg",
|
||||||
|
});
|
||||||
|
const holidayCalendarFindFirst = vi.fn().mockResolvedValue(null);
|
||||||
|
const metroCityFindUnique = vi.fn().mockResolvedValue({
|
||||||
|
id: "city_augsburg",
|
||||||
|
countryId: "country_de",
|
||||||
|
});
|
||||||
|
const holidayCalendarUpdate = vi.fn().mockResolvedValue({
|
||||||
|
id: "cal_city",
|
||||||
|
name: "Augsburg lokal bevorzugt",
|
||||||
|
stateCode: null,
|
||||||
|
metroCityId: "city_augsburg",
|
||||||
|
country: { id: "country_de", code: "DE", name: "Deutschland" },
|
||||||
|
metroCity: { id: "city_augsburg", name: "Augsburg" },
|
||||||
|
entries: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await updateHolidayCalendar(
|
||||||
|
createContext({
|
||||||
|
holidayCalendar: {
|
||||||
|
findUnique: holidayCalendarFindUnique,
|
||||||
|
findFirst: holidayCalendarFindFirst,
|
||||||
|
update: holidayCalendarUpdate,
|
||||||
|
},
|
||||||
|
metroCity: { findUnique: metroCityFindUnique },
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
id: "cal_city",
|
||||||
|
data: {
|
||||||
|
name: "Augsburg lokal bevorzugt",
|
||||||
|
metroCityId: undefined,
|
||||||
|
priority: 7,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(holidayCalendarFindFirst).toHaveBeenCalledWith({
|
||||||
|
where: {
|
||||||
|
scopeType: "CITY",
|
||||||
|
countryId: "country_de",
|
||||||
|
metroCityId: "city_augsburg",
|
||||||
|
id: { not: "cal_city" },
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
expect(holidayCalendarUpdate).toHaveBeenCalledWith({
|
||||||
|
where: { id: "cal_city" },
|
||||||
|
data: {
|
||||||
|
name: "Augsburg lokal bevorzugt",
|
||||||
|
priority: 7,
|
||||||
|
},
|
||||||
|
include: expect.any(Object),
|
||||||
|
});
|
||||||
|
expect(result.name).toBe("Augsburg lokal bevorzugt");
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
entityType: "HolidayCalendar",
|
||||||
|
action: "UPDATE",
|
||||||
|
entityId: "cal_city",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates and deletes holiday-calendar entries with clamped dates", async () => {
|
||||||
|
const holidayCalendarFindUnique = vi.fn().mockResolvedValue({
|
||||||
|
id: "cal_de",
|
||||||
|
name: "Deutschland",
|
||||||
|
});
|
||||||
|
const holidayCalendarEntryFindFirst = vi.fn().mockResolvedValue(null);
|
||||||
|
const holidayCalendarEntryCreate = vi.fn().mockResolvedValue({
|
||||||
|
id: "entry_1",
|
||||||
|
holidayCalendarId: "cal_de",
|
||||||
|
name: "Neujahr",
|
||||||
|
date: new Date("2026-01-01T00:00:00.000Z"),
|
||||||
|
});
|
||||||
|
const holidayCalendarEntryFindUnique = vi.fn().mockResolvedValue({
|
||||||
|
id: "entry_1",
|
||||||
|
holidayCalendarId: "cal_de",
|
||||||
|
name: "Neujahr",
|
||||||
|
date: new Date("2026-01-01T00:00:00.000Z"),
|
||||||
|
});
|
||||||
|
const holidayCalendarEntryDelete = vi.fn().mockResolvedValue({ id: "entry_1" });
|
||||||
|
|
||||||
|
const created = await createHolidayCalendarEntry(
|
||||||
|
createContext({
|
||||||
|
holidayCalendar: { findUnique: holidayCalendarFindUnique },
|
||||||
|
holidayCalendarEntry: {
|
||||||
|
findFirst: holidayCalendarEntryFindFirst,
|
||||||
|
create: holidayCalendarEntryCreate,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
holidayCalendarId: "cal_de",
|
||||||
|
name: "Neujahr",
|
||||||
|
date: new Date("2026-01-01T14:30:00.000Z"),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const deleted = await deleteHolidayCalendarEntry(
|
||||||
|
createContext({
|
||||||
|
holidayCalendarEntry: {
|
||||||
|
findUnique: holidayCalendarEntryFindUnique,
|
||||||
|
delete: holidayCalendarEntryDelete,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{ id: "entry_1" },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(holidayCalendarEntryFindFirst).toHaveBeenCalledWith({
|
||||||
|
where: {
|
||||||
|
holidayCalendarId: "cal_de",
|
||||||
|
date: new Date("2026-01-01T00:00:00.000Z"),
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
expect(holidayCalendarEntryCreate).toHaveBeenCalledWith({
|
||||||
|
data: {
|
||||||
|
holidayCalendarId: "cal_de",
|
||||||
|
date: new Date("2026-01-01T00:00:00.000Z"),
|
||||||
|
name: "Neujahr",
|
||||||
|
isRecurringAnnual: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(created.id).toBe("entry_1");
|
||||||
|
expect(holidayCalendarEntryDelete).toHaveBeenCalledWith({
|
||||||
|
where: { id: "entry_1" },
|
||||||
|
});
|
||||||
|
expect(deleted).toEqual({
|
||||||
|
success: true,
|
||||||
|
id: "entry_1",
|
||||||
|
name: "Neujahr",
|
||||||
|
});
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,283 @@
|
|||||||
|
import {
|
||||||
|
CreateHolidayCalendarEntrySchema,
|
||||||
|
CreateHolidayCalendarSchema,
|
||||||
|
UpdateHolidayCalendarEntrySchema,
|
||||||
|
UpdateHolidayCalendarSchema,
|
||||||
|
} from "@capakraken/shared";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||||
|
import { createAuditEntry } from "../lib/audit.js";
|
||||||
|
import type { TRPCContext } from "../trpc.js";
|
||||||
|
import { asHolidayCalendarDb } from "./holiday-calendar-shared.js";
|
||||||
|
import {
|
||||||
|
buildHolidayCalendarCreateData,
|
||||||
|
buildHolidayCalendarEntryCreateData,
|
||||||
|
buildHolidayCalendarEntryUpdateData,
|
||||||
|
buildHolidayCalendarUpdateData,
|
||||||
|
holidayCalendarDetailInclude,
|
||||||
|
} from "./holiday-calendar-support.js";
|
||||||
|
import {
|
||||||
|
assertHolidayCalendarEntryDateAvailable,
|
||||||
|
assertHolidayCalendarScopeConsistency,
|
||||||
|
clampHolidayCalendarDate,
|
||||||
|
normalizeHolidayCalendarScopeInput,
|
||||||
|
resolveHolidayCalendarUpdateScope,
|
||||||
|
} from "./holiday-calendar-write-support.js";
|
||||||
|
|
||||||
|
type HolidayCalendarProcedureContext = Pick<TRPCContext, "db" | "dbUser">;
|
||||||
|
|
||||||
|
function withAuditUser(userId: string | undefined) {
|
||||||
|
return userId ? { userId } : {};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const holidayCalendarIdInputSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const holidayCalendarUpdateInputSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
data: UpdateHolidayCalendarSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const holidayCalendarEntryUpdateInputSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
data: UpdateHolidayCalendarEntrySchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
type HolidayCalendarIdInput = z.infer<typeof holidayCalendarIdInputSchema>;
|
||||||
|
type HolidayCalendarCreateInput = z.infer<typeof CreateHolidayCalendarSchema>;
|
||||||
|
type HolidayCalendarUpdateInput = z.infer<typeof holidayCalendarUpdateInputSchema>;
|
||||||
|
type HolidayCalendarEntryCreateInput = z.infer<typeof CreateHolidayCalendarEntrySchema>;
|
||||||
|
type HolidayCalendarEntryUpdateInput = z.infer<typeof holidayCalendarEntryUpdateInputSchema>;
|
||||||
|
|
||||||
|
export async function createHolidayCalendar(
|
||||||
|
ctx: HolidayCalendarProcedureContext,
|
||||||
|
input: HolidayCalendarCreateInput,
|
||||||
|
) {
|
||||||
|
const db = asHolidayCalendarDb(ctx.db);
|
||||||
|
|
||||||
|
await findUniqueOrThrow(
|
||||||
|
ctx.db.country.findUnique({
|
||||||
|
where: { id: input.countryId },
|
||||||
|
select: { id: true, name: true },
|
||||||
|
}),
|
||||||
|
"Country",
|
||||||
|
);
|
||||||
|
|
||||||
|
const normalizedScope = normalizeHolidayCalendarScopeInput({
|
||||||
|
stateCode: input.stateCode,
|
||||||
|
metroCityId: input.metroCityId,
|
||||||
|
});
|
||||||
|
|
||||||
|
await assertHolidayCalendarScopeConsistency(db, {
|
||||||
|
scopeType: input.scopeType,
|
||||||
|
countryId: input.countryId,
|
||||||
|
...normalizedScope,
|
||||||
|
});
|
||||||
|
|
||||||
|
const created = await db.holidayCalendar.create({
|
||||||
|
data: buildHolidayCalendarCreateData({
|
||||||
|
...input,
|
||||||
|
normalizedScope,
|
||||||
|
}),
|
||||||
|
include: holidayCalendarDetailInclude,
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "HolidayCalendar",
|
||||||
|
entityId: created.id,
|
||||||
|
entityName: created.name,
|
||||||
|
action: "CREATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
after: created as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return created;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateHolidayCalendar(
|
||||||
|
ctx: HolidayCalendarProcedureContext,
|
||||||
|
input: HolidayCalendarUpdateInput,
|
||||||
|
) {
|
||||||
|
const db = asHolidayCalendarDb(ctx.db);
|
||||||
|
const existing = await findUniqueOrThrow<any>(
|
||||||
|
db.holidayCalendar.findUnique({ where: { id: input.id } }),
|
||||||
|
"Holiday calendar",
|
||||||
|
);
|
||||||
|
|
||||||
|
const { stateCode, metroCityId } = resolveHolidayCalendarUpdateScope({
|
||||||
|
existing,
|
||||||
|
data: input.data,
|
||||||
|
});
|
||||||
|
|
||||||
|
await assertHolidayCalendarScopeConsistency(db, {
|
||||||
|
scopeType: existing.scopeType,
|
||||||
|
countryId: existing.countryId,
|
||||||
|
stateCode,
|
||||||
|
metroCityId,
|
||||||
|
}, existing.id);
|
||||||
|
|
||||||
|
const updated = await db.holidayCalendar.update({
|
||||||
|
where: { id: input.id },
|
||||||
|
data: buildHolidayCalendarUpdateData({
|
||||||
|
data: input.data,
|
||||||
|
resolvedScope: {
|
||||||
|
stateCode,
|
||||||
|
metroCityId,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
include: holidayCalendarDetailInclude,
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "HolidayCalendar",
|
||||||
|
entityId: updated.id,
|
||||||
|
entityName: updated.name,
|
||||||
|
action: "UPDATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: existing as unknown as Record<string, unknown>,
|
||||||
|
after: updated as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteHolidayCalendar(
|
||||||
|
ctx: HolidayCalendarProcedureContext,
|
||||||
|
input: HolidayCalendarIdInput,
|
||||||
|
) {
|
||||||
|
const db = asHolidayCalendarDb(ctx.db);
|
||||||
|
const existing = await findUniqueOrThrow<any>(
|
||||||
|
db.holidayCalendar.findUnique({
|
||||||
|
where: { id: input.id },
|
||||||
|
include: { entries: true },
|
||||||
|
}),
|
||||||
|
"Holiday calendar",
|
||||||
|
);
|
||||||
|
|
||||||
|
await db.holidayCalendar.delete({ where: { id: input.id } });
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "HolidayCalendar",
|
||||||
|
entityId: existing.id,
|
||||||
|
entityName: existing.name,
|
||||||
|
action: "DELETE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: existing as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return { success: true, id: existing.id, name: existing.name };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createHolidayCalendarEntry(
|
||||||
|
ctx: HolidayCalendarProcedureContext,
|
||||||
|
input: HolidayCalendarEntryCreateInput,
|
||||||
|
) {
|
||||||
|
const db = asHolidayCalendarDb(ctx.db);
|
||||||
|
|
||||||
|
await findUniqueOrThrow(
|
||||||
|
db.holidayCalendar.findUnique({
|
||||||
|
where: { id: input.holidayCalendarId },
|
||||||
|
select: { id: true, name: true },
|
||||||
|
}),
|
||||||
|
"Holiday calendar",
|
||||||
|
);
|
||||||
|
|
||||||
|
await assertHolidayCalendarEntryDateAvailable(db, {
|
||||||
|
holidayCalendarId: input.holidayCalendarId,
|
||||||
|
date: input.date,
|
||||||
|
});
|
||||||
|
|
||||||
|
const created = await db.holidayCalendarEntry.create({
|
||||||
|
data: buildHolidayCalendarEntryCreateData({
|
||||||
|
data: input,
|
||||||
|
date: clampHolidayCalendarDate(input.date),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "HolidayCalendarEntry",
|
||||||
|
entityId: created.id,
|
||||||
|
entityName: created.name,
|
||||||
|
action: "CREATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
after: created as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return created;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateHolidayCalendarEntry(
|
||||||
|
ctx: HolidayCalendarProcedureContext,
|
||||||
|
input: HolidayCalendarEntryUpdateInput,
|
||||||
|
) {
|
||||||
|
const db = asHolidayCalendarDb(ctx.db);
|
||||||
|
const existing = await findUniqueOrThrow<any>(
|
||||||
|
db.holidayCalendarEntry.findUnique({ where: { id: input.id } }),
|
||||||
|
"Holiday calendar entry",
|
||||||
|
);
|
||||||
|
const nextDate = input.data.date !== undefined
|
||||||
|
? clampHolidayCalendarDate(input.data.date)
|
||||||
|
: existing.date;
|
||||||
|
|
||||||
|
await assertHolidayCalendarEntryDateAvailable(db, {
|
||||||
|
holidayCalendarId: existing.holidayCalendarId,
|
||||||
|
date: nextDate,
|
||||||
|
}, existing.id);
|
||||||
|
|
||||||
|
const updated = await db.holidayCalendarEntry.update({
|
||||||
|
where: { id: input.id },
|
||||||
|
data: buildHolidayCalendarEntryUpdateData({
|
||||||
|
data: input.data,
|
||||||
|
date: nextDate,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "HolidayCalendarEntry",
|
||||||
|
entityId: updated.id,
|
||||||
|
entityName: updated.name,
|
||||||
|
action: "UPDATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: existing as unknown as Record<string, unknown>,
|
||||||
|
after: updated as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteHolidayCalendarEntry(
|
||||||
|
ctx: HolidayCalendarProcedureContext,
|
||||||
|
input: HolidayCalendarIdInput,
|
||||||
|
) {
|
||||||
|
const db = asHolidayCalendarDb(ctx.db);
|
||||||
|
const existing = await findUniqueOrThrow<any>(
|
||||||
|
db.holidayCalendarEntry.findUnique({ where: { id: input.id } }),
|
||||||
|
"Holiday calendar entry",
|
||||||
|
);
|
||||||
|
|
||||||
|
await db.holidayCalendarEntry.delete({ where: { id: input.id } });
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "HolidayCalendarEntry",
|
||||||
|
entityId: existing.id,
|
||||||
|
entityName: existing.name,
|
||||||
|
action: "DELETE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: existing as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return { success: true, id: existing.id, name: existing.name };
|
||||||
|
}
|
||||||
@@ -1,30 +1,21 @@
|
|||||||
|
import { createTRPCRouter, adminProcedure } from "../trpc.js";
|
||||||
|
import { holidayCalendarCatalogReadProcedures } from "./holiday-calendar-catalog-read.js";
|
||||||
|
import {
|
||||||
|
createHolidayCalendar,
|
||||||
|
createHolidayCalendarEntry,
|
||||||
|
deleteHolidayCalendar,
|
||||||
|
deleteHolidayCalendarEntry,
|
||||||
|
holidayCalendarEntryUpdateInputSchema,
|
||||||
|
holidayCalendarIdInputSchema,
|
||||||
|
holidayCalendarUpdateInputSchema,
|
||||||
|
updateHolidayCalendar,
|
||||||
|
updateHolidayCalendarEntry,
|
||||||
|
} from "./holiday-calendar-procedure-support.js";
|
||||||
|
import { holidayCalendarResolutionReadProcedures } from "./holiday-calendar-resolution-read.js";
|
||||||
import {
|
import {
|
||||||
CreateHolidayCalendarEntrySchema,
|
CreateHolidayCalendarEntrySchema,
|
||||||
CreateHolidayCalendarSchema,
|
CreateHolidayCalendarSchema,
|
||||||
UpdateHolidayCalendarEntrySchema,
|
|
||||||
UpdateHolidayCalendarSchema,
|
|
||||||
} from "@capakraken/shared";
|
} from "@capakraken/shared";
|
||||||
import { z } from "zod";
|
|
||||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
|
||||||
import { createAuditEntry } from "../lib/audit.js";
|
|
||||||
import { createTRPCRouter, adminProcedure } from "../trpc.js";
|
|
||||||
import { holidayCalendarCatalogReadProcedures } from "./holiday-calendar-catalog-read.js";
|
|
||||||
import { holidayCalendarResolutionReadProcedures } from "./holiday-calendar-resolution-read.js";
|
|
||||||
import { asHolidayCalendarDb } from "./holiday-calendar-shared.js";
|
|
||||||
import {
|
|
||||||
buildHolidayCalendarCreateData,
|
|
||||||
buildHolidayCalendarEntryCreateData,
|
|
||||||
buildHolidayCalendarEntryUpdateData,
|
|
||||||
buildHolidayCalendarUpdateData,
|
|
||||||
holidayCalendarDetailInclude,
|
|
||||||
} from "./holiday-calendar-support.js";
|
|
||||||
import {
|
|
||||||
assertHolidayCalendarEntryDateAvailable,
|
|
||||||
assertHolidayCalendarScopeConsistency,
|
|
||||||
clampHolidayCalendarDate,
|
|
||||||
normalizeHolidayCalendarScopeInput,
|
|
||||||
resolveHolidayCalendarUpdateScope,
|
|
||||||
} from "./holiday-calendar-write-support.js";
|
|
||||||
|
|
||||||
export const holidayCalendarRouter = createTRPCRouter({
|
export const holidayCalendarRouter = createTRPCRouter({
|
||||||
...holidayCalendarCatalogReadProcedures,
|
...holidayCalendarCatalogReadProcedures,
|
||||||
@@ -32,231 +23,26 @@ export const holidayCalendarRouter = createTRPCRouter({
|
|||||||
|
|
||||||
createCalendar: adminProcedure
|
createCalendar: adminProcedure
|
||||||
.input(CreateHolidayCalendarSchema)
|
.input(CreateHolidayCalendarSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => createHolidayCalendar(ctx, input)),
|
||||||
const db = asHolidayCalendarDb(ctx.db);
|
|
||||||
|
|
||||||
await findUniqueOrThrow(
|
|
||||||
ctx.db.country.findUnique({
|
|
||||||
where: { id: input.countryId },
|
|
||||||
select: { id: true, name: true },
|
|
||||||
}),
|
|
||||||
"Country",
|
|
||||||
);
|
|
||||||
|
|
||||||
await assertHolidayCalendarScopeConsistency(db, {
|
|
||||||
scopeType: input.scopeType,
|
|
||||||
countryId: input.countryId,
|
|
||||||
...normalizeHolidayCalendarScopeInput({
|
|
||||||
stateCode: input.stateCode,
|
|
||||||
metroCityId: input.metroCityId,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const normalizedScope = normalizeHolidayCalendarScopeInput({
|
|
||||||
stateCode: input.stateCode,
|
|
||||||
metroCityId: input.metroCityId,
|
|
||||||
});
|
|
||||||
|
|
||||||
const created = await db.holidayCalendar.create({
|
|
||||||
data: buildHolidayCalendarCreateData({
|
|
||||||
...input,
|
|
||||||
normalizedScope,
|
|
||||||
}),
|
|
||||||
include: holidayCalendarDetailInclude,
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "HolidayCalendar",
|
|
||||||
entityId: created.id,
|
|
||||||
entityName: created.name,
|
|
||||||
action: "CREATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
after: created as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return created;
|
|
||||||
}),
|
|
||||||
|
|
||||||
updateCalendar: adminProcedure
|
updateCalendar: adminProcedure
|
||||||
.input(z.object({ id: z.string(), data: UpdateHolidayCalendarSchema }))
|
.input(holidayCalendarUpdateInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => updateHolidayCalendar(ctx, input)),
|
||||||
const db = asHolidayCalendarDb(ctx.db);
|
|
||||||
const existing = await findUniqueOrThrow<any>(
|
|
||||||
db.holidayCalendar.findUnique({ where: { id: input.id } }),
|
|
||||||
"Holiday calendar",
|
|
||||||
);
|
|
||||||
|
|
||||||
const { stateCode, metroCityId } = resolveHolidayCalendarUpdateScope({
|
|
||||||
existing,
|
|
||||||
data: input.data,
|
|
||||||
});
|
|
||||||
|
|
||||||
await assertHolidayCalendarScopeConsistency(db, {
|
|
||||||
scopeType: existing.scopeType,
|
|
||||||
countryId: existing.countryId,
|
|
||||||
stateCode,
|
|
||||||
metroCityId,
|
|
||||||
}, existing.id);
|
|
||||||
|
|
||||||
const updated = await db.holidayCalendar.update({
|
|
||||||
where: { id: input.id },
|
|
||||||
data: buildHolidayCalendarUpdateData({
|
|
||||||
data: input.data,
|
|
||||||
resolvedScope: {
|
|
||||||
stateCode,
|
|
||||||
metroCityId,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
include: holidayCalendarDetailInclude,
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "HolidayCalendar",
|
|
||||||
entityId: updated.id,
|
|
||||||
entityName: updated.name,
|
|
||||||
action: "UPDATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: existing as unknown as Record<string, unknown>,
|
|
||||||
after: updated as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return updated;
|
|
||||||
}),
|
|
||||||
|
|
||||||
deleteCalendar: adminProcedure
|
deleteCalendar: adminProcedure
|
||||||
.input(z.object({ id: z.string() }))
|
.input(holidayCalendarIdInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => deleteHolidayCalendar(ctx, input)),
|
||||||
const db = asHolidayCalendarDb(ctx.db);
|
|
||||||
const existing = await findUniqueOrThrow<any>(
|
|
||||||
db.holidayCalendar.findUnique({
|
|
||||||
where: { id: input.id },
|
|
||||||
include: { entries: true },
|
|
||||||
}),
|
|
||||||
"Holiday calendar",
|
|
||||||
);
|
|
||||||
|
|
||||||
await db.holidayCalendar.delete({ where: { id: input.id } });
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "HolidayCalendar",
|
|
||||||
entityId: existing.id,
|
|
||||||
entityName: existing.name,
|
|
||||||
action: "DELETE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: existing as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return { success: true, id: existing.id, name: existing.name };
|
|
||||||
}),
|
|
||||||
|
|
||||||
createEntry: adminProcedure
|
createEntry: adminProcedure
|
||||||
.input(CreateHolidayCalendarEntrySchema)
|
.input(CreateHolidayCalendarEntrySchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => createHolidayCalendarEntry(ctx, input)),
|
||||||
const db = asHolidayCalendarDb(ctx.db);
|
|
||||||
|
|
||||||
await findUniqueOrThrow(
|
|
||||||
db.holidayCalendar.findUnique({
|
|
||||||
where: { id: input.holidayCalendarId },
|
|
||||||
select: { id: true, name: true },
|
|
||||||
}),
|
|
||||||
"Holiday calendar",
|
|
||||||
);
|
|
||||||
|
|
||||||
await assertHolidayCalendarEntryDateAvailable(db, {
|
|
||||||
holidayCalendarId: input.holidayCalendarId,
|
|
||||||
date: input.date,
|
|
||||||
});
|
|
||||||
|
|
||||||
const created = await db.holidayCalendarEntry.create({
|
|
||||||
data: buildHolidayCalendarEntryCreateData({
|
|
||||||
data: input,
|
|
||||||
date: clampHolidayCalendarDate(input.date),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "HolidayCalendarEntry",
|
|
||||||
entityId: created.id,
|
|
||||||
entityName: created.name,
|
|
||||||
action: "CREATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
after: created as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return created;
|
|
||||||
}),
|
|
||||||
|
|
||||||
updateEntry: adminProcedure
|
updateEntry: adminProcedure
|
||||||
.input(z.object({ id: z.string(), data: UpdateHolidayCalendarEntrySchema }))
|
.input(holidayCalendarEntryUpdateInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => updateHolidayCalendarEntry(ctx, input)),
|
||||||
const db = asHolidayCalendarDb(ctx.db);
|
|
||||||
const existing = await findUniqueOrThrow<any>(
|
|
||||||
db.holidayCalendarEntry.findUnique({ where: { id: input.id } }),
|
|
||||||
"Holiday calendar entry",
|
|
||||||
);
|
|
||||||
const nextDate = input.data.date !== undefined
|
|
||||||
? clampHolidayCalendarDate(input.data.date)
|
|
||||||
: existing.date;
|
|
||||||
|
|
||||||
await assertHolidayCalendarEntryDateAvailable(db, {
|
|
||||||
holidayCalendarId: existing.holidayCalendarId,
|
|
||||||
date: nextDate,
|
|
||||||
}, existing.id);
|
|
||||||
|
|
||||||
const updated = await db.holidayCalendarEntry.update({
|
|
||||||
where: { id: input.id },
|
|
||||||
data: buildHolidayCalendarEntryUpdateData({
|
|
||||||
data: input.data,
|
|
||||||
date: nextDate,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "HolidayCalendarEntry",
|
|
||||||
entityId: updated.id,
|
|
||||||
entityName: updated.name,
|
|
||||||
action: "UPDATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: existing as unknown as Record<string, unknown>,
|
|
||||||
after: updated as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return updated;
|
|
||||||
}),
|
|
||||||
|
|
||||||
deleteEntry: adminProcedure
|
deleteEntry: adminProcedure
|
||||||
.input(z.object({ id: z.string() }))
|
.input(holidayCalendarIdInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => deleteHolidayCalendarEntry(ctx, input)),
|
||||||
const db = asHolidayCalendarDb(ctx.db);
|
|
||||||
const existing = await findUniqueOrThrow<any>(
|
|
||||||
db.holidayCalendarEntry.findUnique({ where: { id: input.id } }),
|
|
||||||
"Holiday calendar entry",
|
|
||||||
);
|
|
||||||
|
|
||||||
await db.holidayCalendarEntry.delete({ where: { id: input.id } });
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "HolidayCalendarEntry",
|
|
||||||
entityId: existing.id,
|
|
||||||
entityName: existing.name,
|
|
||||||
action: "DELETE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: existing as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return { success: true, id: existing.id, name: existing.name };
|
|
||||||
}),
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user