feat(platform): harden access scoping and delivery baseline
This commit is contained in:
@@ -30,6 +30,100 @@ 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,
|
||||
});
|
||||
|
||||
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: protectedProcedure
|
||||
.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 },
|
||||
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: protectedProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
@@ -46,6 +140,19 @@ export const countryRouter = createTRPCRouter({
|
||||
return country;
|
||||
}),
|
||||
|
||||
getCityById: protectedProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const city = await findUniqueOrThrow(
|
||||
ctx.db.metroCity.findUnique({
|
||||
where: { id: input.id },
|
||||
select: { id: true, name: true, countryId: true },
|
||||
}),
|
||||
"Metro city",
|
||||
);
|
||||
return city;
|
||||
}),
|
||||
|
||||
create: adminProcedure
|
||||
.input(CreateCountrySchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
@@ -207,6 +314,6 @@ export const countryRouter = createTRPCRouter({
|
||||
source: "ui",
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
return { success: true, id: city.id, name: city.name };
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user