refactor(api): extract holiday calendar support
This commit is contained in:
@@ -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