feat(platform): harden access scoping and delivery baseline
This commit is contained in:
@@ -78,6 +78,98 @@ export const orgUnitRouter = createTRPCRouter({
|
||||
return unit;
|
||||
}),
|
||||
|
||||
resolveByIdentifier: protectedProcedure
|
||||
.input(z.object({ identifier: z.string().trim().min(1) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const identifier = input.identifier.trim();
|
||||
const select = {
|
||||
id: true,
|
||||
name: true,
|
||||
shortName: true,
|
||||
level: true,
|
||||
isActive: true,
|
||||
} as const;
|
||||
|
||||
let unit = await ctx.db.orgUnit.findUnique({
|
||||
where: { id: identifier },
|
||||
select,
|
||||
});
|
||||
|
||||
if (!unit) {
|
||||
unit = await ctx.db.orgUnit.findFirst({
|
||||
where: { name: { equals: identifier, mode: "insensitive" } },
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!unit) {
|
||||
unit = await ctx.db.orgUnit.findFirst({
|
||||
where: { shortName: { equals: identifier, mode: "insensitive" } },
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!unit) {
|
||||
unit = await ctx.db.orgUnit.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ name: { contains: identifier, mode: "insensitive" } },
|
||||
{ shortName: { contains: identifier, mode: "insensitive" } },
|
||||
],
|
||||
},
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!unit) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: `Org unit not found: ${identifier}` });
|
||||
}
|
||||
|
||||
return unit;
|
||||
}),
|
||||
|
||||
getByIdentifier: protectedProcedure
|
||||
.input(z.object({ identifier: z.string().trim().min(1) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const identifier = input.identifier.trim();
|
||||
let unit = await ctx.db.orgUnit.findUnique({
|
||||
where: { id: identifier },
|
||||
include: { _count: { select: { resources: true } } },
|
||||
});
|
||||
|
||||
if (!unit) {
|
||||
unit = await ctx.db.orgUnit.findFirst({
|
||||
where: { name: { equals: identifier, mode: "insensitive" } },
|
||||
include: { _count: { select: { resources: true } } },
|
||||
});
|
||||
}
|
||||
|
||||
if (!unit) {
|
||||
unit = await ctx.db.orgUnit.findFirst({
|
||||
where: { shortName: { equals: identifier, mode: "insensitive" } },
|
||||
include: { _count: { select: { resources: true } } },
|
||||
});
|
||||
}
|
||||
|
||||
if (!unit) {
|
||||
unit = await ctx.db.orgUnit.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ name: { contains: identifier, mode: "insensitive" } },
|
||||
{ shortName: { contains: identifier, mode: "insensitive" } },
|
||||
],
|
||||
},
|
||||
include: { _count: { select: { resources: true } } },
|
||||
});
|
||||
}
|
||||
|
||||
if (!unit) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: `Org unit not found: ${identifier}` });
|
||||
}
|
||||
|
||||
return unit;
|
||||
}),
|
||||
|
||||
create: adminProcedure
|
||||
.input(CreateOrgUnitSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
|
||||
Reference in New Issue
Block a user