feat(platform): harden access scoping and delivery baseline
This commit is contained in:
@@ -44,7 +44,12 @@ export const clientRouter = createTRPCRouter({
|
||||
...(input?.parentId !== undefined ? { parentId: input.parentId } : {}),
|
||||
...(input?.isActive !== undefined ? { isActive: input.isActive } : {}),
|
||||
...(input?.search
|
||||
? { name: { contains: input.search, mode: "insensitive" as const } }
|
||||
? {
|
||||
OR: [
|
||||
{ name: { contains: input.search, mode: "insensitive" as const } },
|
||||
{ code: { contains: input.search, mode: "insensitive" as const } },
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
include: { _count: { select: { children: true, projects: true } } },
|
||||
@@ -81,6 +86,98 @@ export const clientRouter = createTRPCRouter({
|
||||
return client;
|
||||
}),
|
||||
|
||||
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,
|
||||
code: true,
|
||||
parentId: true,
|
||||
isActive: true,
|
||||
} as const;
|
||||
|
||||
let client = await ctx.db.client.findUnique({
|
||||
where: { id: identifier },
|
||||
select,
|
||||
});
|
||||
|
||||
if (!client) {
|
||||
client = await ctx.db.client.findUnique({
|
||||
where: { code: identifier },
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!client) {
|
||||
client = await ctx.db.client.findFirst({
|
||||
where: { name: { equals: identifier, mode: "insensitive" } },
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!client) {
|
||||
client = await ctx.db.client.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ name: { contains: identifier, mode: "insensitive" } },
|
||||
{ code: { contains: identifier, mode: "insensitive" } },
|
||||
],
|
||||
},
|
||||
select,
|
||||
});
|
||||
}
|
||||
|
||||
if (!client) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: `Client not found: ${identifier}` });
|
||||
}
|
||||
|
||||
return client;
|
||||
}),
|
||||
|
||||
getByIdentifier: protectedProcedure
|
||||
.input(z.object({ identifier: z.string().trim().min(1) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const identifier = input.identifier.trim();
|
||||
let client = await ctx.db.client.findUnique({
|
||||
where: { id: identifier },
|
||||
include: { _count: { select: { projects: true, children: true } } },
|
||||
});
|
||||
|
||||
if (!client) {
|
||||
client = await ctx.db.client.findUnique({
|
||||
where: { code: identifier },
|
||||
include: { _count: { select: { projects: true, children: true } } },
|
||||
});
|
||||
}
|
||||
|
||||
if (!client) {
|
||||
client = await ctx.db.client.findFirst({
|
||||
where: { name: { equals: identifier, mode: "insensitive" } },
|
||||
include: { _count: { select: { projects: true, children: true } } },
|
||||
});
|
||||
}
|
||||
|
||||
if (!client) {
|
||||
client = await ctx.db.client.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ name: { contains: identifier, mode: "insensitive" } },
|
||||
{ code: { contains: identifier, mode: "insensitive" } },
|
||||
],
|
||||
},
|
||||
include: { _count: { select: { projects: true, children: true } } },
|
||||
});
|
||||
}
|
||||
|
||||
if (!client) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: `Client not found: ${identifier}` });
|
||||
}
|
||||
|
||||
return client;
|
||||
}),
|
||||
|
||||
create: managerProcedure
|
||||
.input(CreateClientSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
|
||||
Reference in New Issue
Block a user