chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
import { CreateClientSchema, UpdateClientSchema } from "@planarchy/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import { createTRPCRouter, managerProcedure, protectedProcedure } from "../trpc.js";
|
||||
|
||||
import type { ClientTree } from "@planarchy/shared";
|
||||
|
||||
interface FlatClient {
|
||||
id: string;
|
||||
name: string;
|
||||
code: string | null;
|
||||
parentId: string | null;
|
||||
isActive: boolean;
|
||||
sortOrder: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
function buildClientTree(flatItems: FlatClient[], parentId: string | null = null): ClientTree[] {
|
||||
return flatItems
|
||||
.filter((item) => item.parentId === parentId)
|
||||
.sort((a, b) => a.sortOrder - b.sortOrder || a.name.localeCompare(b.name))
|
||||
.map((item) => ({
|
||||
...item,
|
||||
children: buildClientTree(flatItems, item.id),
|
||||
}));
|
||||
}
|
||||
|
||||
export const clientRouter = createTRPCRouter({
|
||||
list: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
parentId: z.string().nullable().optional(),
|
||||
isActive: z.boolean().optional(),
|
||||
search: z.string().optional(),
|
||||
}).optional(),
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.db.client.findMany({
|
||||
where: {
|
||||
...(input?.parentId !== undefined ? { parentId: input.parentId } : {}),
|
||||
...(input?.isActive !== undefined ? { isActive: input.isActive } : {}),
|
||||
...(input?.search
|
||||
? { name: { contains: input.search, mode: "insensitive" as const } }
|
||||
: {}),
|
||||
},
|
||||
include: { _count: { select: { children: true, projects: true } } },
|
||||
orderBy: [{ sortOrder: "asc" }, { name: "asc" }],
|
||||
});
|
||||
}),
|
||||
|
||||
getTree: protectedProcedure
|
||||
.input(z.object({ isActive: z.boolean().optional() }).optional())
|
||||
.query(async ({ ctx, input }) => {
|
||||
const all = await ctx.db.client.findMany({
|
||||
where: {
|
||||
...(input?.isActive !== undefined ? { isActive: input.isActive } : {}),
|
||||
},
|
||||
orderBy: [{ sortOrder: "asc" }, { name: "asc" }],
|
||||
});
|
||||
return buildClientTree(all);
|
||||
}),
|
||||
|
||||
getById: protectedProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const client = await ctx.db.client.findUnique({
|
||||
where: { id: input.id },
|
||||
include: {
|
||||
parent: true,
|
||||
children: { orderBy: { sortOrder: "asc" } },
|
||||
_count: { select: { projects: true, children: true } },
|
||||
},
|
||||
});
|
||||
if (!client) throw new TRPCError({ code: "NOT_FOUND", message: "Client not found" });
|
||||
return client;
|
||||
}),
|
||||
|
||||
create: managerProcedure
|
||||
.input(CreateClientSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
if (input.parentId) {
|
||||
const parent = await ctx.db.client.findUnique({ where: { id: input.parentId } });
|
||||
if (!parent) throw new TRPCError({ code: "NOT_FOUND", message: "Parent client not found" });
|
||||
}
|
||||
|
||||
if (input.code) {
|
||||
const codeConflict = await ctx.db.client.findUnique({ where: { code: input.code } });
|
||||
if (codeConflict) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: `Client code "${input.code}" already exists` });
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.db.client.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
...(input.code ? { code: input.code } : {}),
|
||||
...(input.parentId ? { parentId: input.parentId } : {}),
|
||||
sortOrder: input.sortOrder,
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
||||
update: managerProcedure
|
||||
.input(z.object({ id: z.string(), data: UpdateClientSchema }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await ctx.db.client.findUnique({ where: { id: input.id } });
|
||||
if (!existing) throw new TRPCError({ code: "NOT_FOUND", message: "Client not found" });
|
||||
|
||||
if (input.data.code && input.data.code !== existing.code) {
|
||||
const conflict = await ctx.db.client.findUnique({ where: { code: input.data.code } });
|
||||
if (conflict) {
|
||||
throw new TRPCError({ code: "CONFLICT", message: `Client code "${input.data.code}" already exists` });
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.db.client.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
||||
...(input.data.code !== undefined ? { code: input.data.code } : {}),
|
||||
...(input.data.sortOrder !== undefined ? { sortOrder: input.data.sortOrder } : {}),
|
||||
...(input.data.isActive !== undefined ? { isActive: input.data.isActive } : {}),
|
||||
...(input.data.parentId !== undefined ? { parentId: input.data.parentId } : {}),
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
||||
deactivate: managerProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
return ctx.db.client.update({
|
||||
where: { id: input.id },
|
||||
data: { isActive: false },
|
||||
});
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user