refactor(api): extract holiday calendar support
This commit is contained in:
@@ -3,6 +3,10 @@ import { z } from "zod";
|
||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||
import { adminProcedure } from "../trpc.js";
|
||||
import { asHolidayCalendarDb, type HolidayReadContext } from "./holiday-calendar-shared.js";
|
||||
import {
|
||||
holidayCalendarDetailInclude,
|
||||
holidayCalendarListInclude,
|
||||
} from "./holiday-calendar-support.js";
|
||||
|
||||
const HolidayCalendarCatalogInputSchema = z.object({
|
||||
includeInactive: z.boolean().optional(),
|
||||
@@ -100,12 +104,7 @@ async function readCalendarsSnapshot(
|
||||
|
||||
return db.holidayCalendar.findMany({
|
||||
where,
|
||||
include: {
|
||||
country: { select: { id: true, code: true, name: true } },
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
_count: { select: { entries: true } },
|
||||
entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
|
||||
},
|
||||
include: holidayCalendarListInclude,
|
||||
orderBy: [
|
||||
{ country: { name: "asc" } },
|
||||
{ scopeType: "asc" },
|
||||
@@ -121,32 +120,20 @@ async function readCalendarByIdentifierSnapshot(ctx: HolidayReadContext, identif
|
||||
|
||||
let calendar = await db.holidayCalendar.findUnique({
|
||||
where: { id: trimmedIdentifier },
|
||||
include: {
|
||||
country: { select: { id: true, code: true, name: true } },
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
|
||||
},
|
||||
include: holidayCalendarDetailInclude,
|
||||
});
|
||||
|
||||
if (!calendar) {
|
||||
calendar = await db.holidayCalendar.findFirst({
|
||||
where: { name: { equals: trimmedIdentifier, mode: "insensitive" } },
|
||||
include: {
|
||||
country: { select: { id: true, code: true, name: true } },
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
|
||||
},
|
||||
include: holidayCalendarDetailInclude,
|
||||
});
|
||||
}
|
||||
|
||||
if (!calendar) {
|
||||
calendar = await db.holidayCalendar.findFirst({
|
||||
where: { name: { contains: trimmedIdentifier, mode: "insensitive" } },
|
||||
include: {
|
||||
country: { select: { id: true, code: true, name: true } },
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
|
||||
},
|
||||
include: holidayCalendarDetailInclude,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -191,11 +178,7 @@ export const holidayCalendarCatalogReadProcedures = {
|
||||
return findUniqueOrThrow(
|
||||
db.holidayCalendar.findUnique({
|
||||
where: { id: input.id },
|
||||
include: {
|
||||
country: { select: { id: true, code: true, name: true } },
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
|
||||
},
|
||||
include: holidayCalendarDetailInclude,
|
||||
}),
|
||||
"Holiday calendar",
|
||||
);
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import type { Prisma } from "@capakraken/db";
|
||||
import {
|
||||
CreateHolidayCalendarEntrySchema,
|
||||
CreateHolidayCalendarSchema,
|
||||
UpdateHolidayCalendarEntrySchema,
|
||||
UpdateHolidayCalendarSchema,
|
||||
} from "@capakraken/shared";
|
||||
import { z } from "zod";
|
||||
|
||||
type CreateHolidayCalendarInput = z.infer<typeof CreateHolidayCalendarSchema>;
|
||||
type UpdateHolidayCalendarInput = z.infer<typeof UpdateHolidayCalendarSchema>;
|
||||
type CreateHolidayCalendarEntryInput = z.infer<typeof CreateHolidayCalendarEntrySchema>;
|
||||
type UpdateHolidayCalendarEntryInput = z.infer<typeof UpdateHolidayCalendarEntrySchema>;
|
||||
|
||||
export const holidayCalendarEntryOrderBy = [
|
||||
{ date: "asc" },
|
||||
{ name: "asc" },
|
||||
] as const;
|
||||
|
||||
export const holidayCalendarDetailInclude = {
|
||||
country: { select: { id: true, code: true, name: true } },
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
entries: { orderBy: holidayCalendarEntryOrderBy },
|
||||
} as const;
|
||||
|
||||
export const holidayCalendarListInclude = {
|
||||
...holidayCalendarDetailInclude,
|
||||
_count: { select: { entries: true } },
|
||||
} as const;
|
||||
|
||||
export function buildHolidayCalendarCreateData(
|
||||
input: CreateHolidayCalendarInput & {
|
||||
normalizedScope: {
|
||||
stateCode: string | null;
|
||||
metroCityId: string | null;
|
||||
};
|
||||
},
|
||||
): Prisma.HolidayCalendarUncheckedCreateInput {
|
||||
return {
|
||||
name: input.name,
|
||||
scopeType: input.scopeType,
|
||||
countryId: input.countryId,
|
||||
...(input.normalizedScope.stateCode ? { stateCode: input.normalizedScope.stateCode } : {}),
|
||||
...(input.normalizedScope.metroCityId ? { metroCityId: input.normalizedScope.metroCityId } : {}),
|
||||
isActive: input.isActive ?? true,
|
||||
priority: input.priority ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildHolidayCalendarUpdateData(input: {
|
||||
data: UpdateHolidayCalendarInput;
|
||||
resolvedScope: {
|
||||
stateCode: string | null;
|
||||
metroCityId: string | null;
|
||||
};
|
||||
}): Prisma.HolidayCalendarUncheckedUpdateInput {
|
||||
return {
|
||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
||||
...(input.data.stateCode !== undefined ? { stateCode: input.resolvedScope.stateCode } : {}),
|
||||
...(input.data.metroCityId !== undefined ? { metroCityId: input.resolvedScope.metroCityId } : {}),
|
||||
...(input.data.isActive !== undefined ? { isActive: input.data.isActive } : {}),
|
||||
...(input.data.priority !== undefined ? { priority: input.data.priority } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildHolidayCalendarEntryCreateData(input: {
|
||||
data: CreateHolidayCalendarEntryInput;
|
||||
date: Date;
|
||||
}): Prisma.HolidayCalendarEntryUncheckedCreateInput {
|
||||
return {
|
||||
holidayCalendarId: input.data.holidayCalendarId,
|
||||
date: input.date,
|
||||
name: input.data.name,
|
||||
isRecurringAnnual: input.data.isRecurringAnnual ?? false,
|
||||
...(input.data.source ? { source: input.data.source } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildHolidayCalendarEntryUpdateData(input: {
|
||||
data: UpdateHolidayCalendarEntryInput;
|
||||
date: Date;
|
||||
}): Prisma.HolidayCalendarEntryUncheckedUpdateInput {
|
||||
return {
|
||||
...(input.data.date !== undefined ? { date: input.date } : {}),
|
||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
||||
...(input.data.isRecurringAnnual !== undefined ? { isRecurringAnnual: input.data.isRecurringAnnual } : {}),
|
||||
...(input.data.source !== undefined ? { source: input.data.source ?? null } : {}),
|
||||
};
|
||||
}
|
||||
@@ -11,6 +11,13 @@ 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,
|
||||
@@ -51,20 +58,11 @@ export const holidayCalendarRouter = createTRPCRouter({
|
||||
});
|
||||
|
||||
const created = await db.holidayCalendar.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
scopeType: input.scopeType,
|
||||
countryId: input.countryId,
|
||||
...(normalizedScope.stateCode ? { stateCode: normalizedScope.stateCode } : {}),
|
||||
...(normalizedScope.metroCityId ? { metroCityId: normalizedScope.metroCityId } : {}),
|
||||
isActive: input.isActive ?? true,
|
||||
priority: input.priority ?? 0,
|
||||
},
|
||||
include: {
|
||||
country: { select: { id: true, code: true, name: true } },
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
entries: true,
|
||||
},
|
||||
data: buildHolidayCalendarCreateData({
|
||||
...input,
|
||||
normalizedScope,
|
||||
}),
|
||||
include: holidayCalendarDetailInclude,
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -104,18 +102,14 @@ export const holidayCalendarRouter = createTRPCRouter({
|
||||
|
||||
const updated = await db.holidayCalendar.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
||||
...(input.data.stateCode !== undefined ? { stateCode } : {}),
|
||||
...(input.data.metroCityId !== undefined ? { metroCityId } : {}),
|
||||
...(input.data.isActive !== undefined ? { isActive: input.data.isActive } : {}),
|
||||
...(input.data.priority !== undefined ? { priority: input.data.priority } : {}),
|
||||
},
|
||||
include: {
|
||||
country: { select: { id: true, code: true, name: true } },
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
|
||||
},
|
||||
data: buildHolidayCalendarUpdateData({
|
||||
data: input.data,
|
||||
resolvedScope: {
|
||||
stateCode,
|
||||
metroCityId,
|
||||
},
|
||||
}),
|
||||
include: holidayCalendarDetailInclude,
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -180,13 +174,10 @@ export const holidayCalendarRouter = createTRPCRouter({
|
||||
});
|
||||
|
||||
const created = await db.holidayCalendarEntry.create({
|
||||
data: {
|
||||
holidayCalendarId: input.holidayCalendarId,
|
||||
data: buildHolidayCalendarEntryCreateData({
|
||||
data: input,
|
||||
date: clampHolidayCalendarDate(input.date),
|
||||
name: input.name,
|
||||
isRecurringAnnual: input.isRecurringAnnual ?? false,
|
||||
...(input.source ? { source: input.source } : {}),
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -222,12 +213,10 @@ export const holidayCalendarRouter = createTRPCRouter({
|
||||
|
||||
const updated = await db.holidayCalendarEntry.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
...(input.data.date !== undefined ? { date: nextDate } : {}),
|
||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
||||
...(input.data.isRecurringAnnual !== undefined ? { isRecurringAnnual: input.data.isRecurringAnnual } : {}),
|
||||
...(input.data.source !== undefined ? { source: input.data.source ?? null } : {}),
|
||||
},
|
||||
data: buildHolidayCalendarEntryUpdateData({
|
||||
data: input.data,
|
||||
date: nextDate,
|
||||
}),
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
|
||||
Reference in New Issue
Block a user