refactor(api): extract country router support
This commit is contained in:
@@ -4,8 +4,7 @@ import {
|
||||
UpdateCountrySchema,
|
||||
UpdateMetroCitySchema,
|
||||
} from "@capakraken/shared";
|
||||
import { Prisma } from "@capakraken/db";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import type { Prisma } from "@capakraken/db";
|
||||
import { z } from "zod";
|
||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||
import { createAuditEntry } from "../lib/audit.js";
|
||||
@@ -15,21 +14,37 @@ import {
|
||||
protectedProcedure,
|
||||
resourceOverviewProcedure,
|
||||
} from "../trpc.js";
|
||||
import {
|
||||
assertCountryCodeAvailable,
|
||||
assertMetroCityDeletable,
|
||||
buildCountryCreateData,
|
||||
buildCountryListWhere,
|
||||
buildCountryUpdateData,
|
||||
buildMetroCityCreateData,
|
||||
buildMetroCityUpdateData,
|
||||
findCountryByIdentifier,
|
||||
} from "./country-support.js";
|
||||
|
||||
/** Convert nullable JSON to Prisma-compatible value (null → Prisma.JsonNull). */
|
||||
function jsonOrNull(val: unknown): Prisma.InputJsonValue | typeof Prisma.JsonNull {
|
||||
if (val === null || val === undefined) return Prisma.JsonNull;
|
||||
return val as Prisma.InputJsonValue;
|
||||
}
|
||||
type CountryIdentifierReadModel = {
|
||||
id: string;
|
||||
code: string;
|
||||
name: string;
|
||||
isActive: boolean;
|
||||
dailyWorkingHours: number;
|
||||
};
|
||||
|
||||
type CountryDetailReadModel = CountryIdentifierReadModel & {
|
||||
scheduleRules?: Prisma.JsonValue | null;
|
||||
metroCities: Array<{ id: string; name: string; countryId: string }>;
|
||||
_count: { resources: number };
|
||||
};
|
||||
|
||||
export const countryRouter = createTRPCRouter({
|
||||
list: protectedProcedure
|
||||
.input(z.object({ isActive: z.boolean().optional() }).optional())
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.db.country.findMany({
|
||||
where: {
|
||||
...(input?.isActive !== undefined ? { isActive: input.isActive } : {}),
|
||||
},
|
||||
where: buildCountryListWhere(input ?? {}),
|
||||
include: { metroCities: { orderBy: { name: "asc" } } },
|
||||
orderBy: { name: "asc" },
|
||||
});
|
||||
@@ -38,95 +53,26 @@ export const countryRouter = createTRPCRouter({
|
||||
resolveByIdentifier: protectedProcedure
|
||||
.input(z.object({ identifier: z.string().trim().min(1) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const identifier = input.identifier.trim();
|
||||
const select = {
|
||||
id: true,
|
||||
code: true,
|
||||
name: true,
|
||||
isActive: true,
|
||||
dailyWorkingHours: true,
|
||||
} as const;
|
||||
|
||||
let country = await ctx.db.country.findUnique({
|
||||
where: { id: identifier },
|
||||
select,
|
||||
return findCountryByIdentifier<CountryIdentifierReadModel>(ctx.db, input.identifier, {
|
||||
select: {
|
||||
id: true,
|
||||
code: true,
|
||||
name: true,
|
||||
isActive: true,
|
||||
dailyWorkingHours: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!country) {
|
||||
country = await ctx.db.country.findFirst({
|
||||
where: { code: { equals: identifier.toUpperCase(), mode: "insensitive" } },
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!country) {
|
||||
country = await ctx.db.country.findFirst({
|
||||
where: { name: { equals: identifier, mode: "insensitive" } },
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!country) {
|
||||
country = await ctx.db.country.findFirst({
|
||||
where: { name: { contains: identifier, mode: "insensitive" } },
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!country) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: `Country not found: ${identifier}` });
|
||||
}
|
||||
|
||||
return country;
|
||||
}),
|
||||
|
||||
getByIdentifier: resourceOverviewProcedure
|
||||
.input(z.object({ identifier: z.string().trim().min(1) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const identifier = input.identifier.trim();
|
||||
let country = await ctx.db.country.findUnique({
|
||||
where: { id: identifier },
|
||||
return findCountryByIdentifier<CountryDetailReadModel>(ctx.db, input.identifier, {
|
||||
include: {
|
||||
metroCities: { orderBy: { name: "asc" } },
|
||||
_count: { select: { resources: true } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!country) {
|
||||
country = await ctx.db.country.findFirst({
|
||||
where: { code: { equals: identifier.toUpperCase(), mode: "insensitive" } },
|
||||
include: {
|
||||
metroCities: { orderBy: { name: "asc" } },
|
||||
_count: { select: { resources: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!country) {
|
||||
country = await ctx.db.country.findFirst({
|
||||
where: { name: { equals: identifier, mode: "insensitive" } },
|
||||
include: {
|
||||
metroCities: { orderBy: { name: "asc" } },
|
||||
_count: { select: { resources: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!country) {
|
||||
country = await ctx.db.country.findFirst({
|
||||
where: { name: { contains: identifier, mode: "insensitive" } },
|
||||
include: {
|
||||
metroCities: { orderBy: { name: "asc" } },
|
||||
_count: { select: { resources: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!country) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: `Country not found: ${identifier}` });
|
||||
}
|
||||
|
||||
return country;
|
||||
}),
|
||||
|
||||
getById: resourceOverviewProcedure
|
||||
@@ -161,17 +107,9 @@ export const countryRouter = createTRPCRouter({
|
||||
create: adminProcedure
|
||||
.input(CreateCountrySchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await ctx.db.country.findUnique({ where: { code: input.code } });
|
||||
if (existing) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: `Country code "${input.code}" already exists` });
|
||||
}
|
||||
await assertCountryCodeAvailable(ctx.db, input.code);
|
||||
const created = await ctx.db.country.create({
|
||||
data: {
|
||||
code: input.code,
|
||||
name: input.name,
|
||||
dailyWorkingHours: input.dailyWorkingHours,
|
||||
...(input.scheduleRules !== undefined ? { scheduleRules: jsonOrNull(input.scheduleRules) } : {}),
|
||||
},
|
||||
data: buildCountryCreateData(input),
|
||||
include: { metroCities: true },
|
||||
});
|
||||
|
||||
@@ -198,23 +136,14 @@ export const countryRouter = createTRPCRouter({
|
||||
);
|
||||
|
||||
if (input.data.code && input.data.code !== existing.code) {
|
||||
const conflict = await ctx.db.country.findUnique({ where: { code: input.data.code } });
|
||||
if (conflict) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: `Country code "${input.data.code}" already exists` });
|
||||
}
|
||||
await assertCountryCodeAvailable(ctx.db, input.data.code, existing.id);
|
||||
}
|
||||
|
||||
const before = existing as unknown as Record<string, unknown>;
|
||||
|
||||
const updated = await ctx.db.country.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
...(input.data.code !== undefined ? { code: input.data.code } : {}),
|
||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
||||
...(input.data.dailyWorkingHours !== undefined ? { dailyWorkingHours: input.data.dailyWorkingHours } : {}),
|
||||
...(input.data.scheduleRules !== undefined ? { scheduleRules: jsonOrNull(input.data.scheduleRules) } : {}),
|
||||
...(input.data.isActive !== undefined ? { isActive: input.data.isActive } : {}),
|
||||
},
|
||||
data: buildCountryUpdateData(input.data),
|
||||
include: { metroCities: true },
|
||||
});
|
||||
|
||||
@@ -244,7 +173,7 @@ export const countryRouter = createTRPCRouter({
|
||||
);
|
||||
|
||||
const created = await ctx.db.metroCity.create({
|
||||
data: { name: input.name, countryId: input.countryId },
|
||||
data: buildMetroCityCreateData(input),
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -272,7 +201,7 @@ export const countryRouter = createTRPCRouter({
|
||||
|
||||
const updated = await ctx.db.metroCity.update({
|
||||
where: { id: input.id },
|
||||
data: { ...(input.data.name !== undefined ? { name: input.data.name } : {}) },
|
||||
data: buildMetroCityUpdateData(input.data),
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -300,12 +229,7 @@ export const countryRouter = createTRPCRouter({
|
||||
}),
|
||||
"Metro city",
|
||||
);
|
||||
if (city._count.resources > 0) {
|
||||
throw new TRPCError({
|
||||
code: "PRECONDITION_FAILED",
|
||||
message: `Cannot delete metro city assigned to ${city._count.resources} resource(s)`,
|
||||
});
|
||||
}
|
||||
assertMetroCityDeletable(city);
|
||||
await ctx.db.metroCity.delete({ where: { id: input.id } });
|
||||
|
||||
void createAuditEntry({
|
||||
|
||||
Reference in New Issue
Block a user