feat: monorepo

This commit is contained in:
Henry
2024-12-12 14:34:10 +00:00
parent b8eed7a90c
commit 0c8d17dce5
370 changed files with 10280 additions and 39805 deletions

View File

@@ -0,0 +1,244 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import * as boardRepo from "@kan/db/repository/board.repo";
import * as cardRepo from "@kan/db/repository/card.repo";
import * as activityRepo from "@kan/db/repository/cardActivity.repo";
import * as listRepo from "@kan/db/repository/list.repo";
import * as workspaceRepo from "@kan/db/repository/workspace.repo";
import { createTRPCRouter, protectedProcedure } from "../trpc";
export const boardRouter = createTRPCRouter({
all: protectedProcedure
.meta({
openapi: {
method: "GET",
path: "/workspaces/{workspacePublicId}/boards",
summary: "Get all boards",
description: "Retrieves all boards for a given workspace",
tags: ["Boards"],
protect: true,
},
})
.input(z.object({ workspacePublicId: z.string().min(12) }))
.output(
z.custom<Awaited<ReturnType<typeof boardRepo.getAllByWorkspaceId>>>(),
)
.query(async ({ ctx, input }) => {
const workspace = await workspaceRepo.getByPublicId(
ctx.db,
input.workspacePublicId,
);
if (!workspace)
throw new TRPCError({
message: `Workspace with public ID ${input.workspacePublicId} not found`,
code: "NOT_FOUND",
});
const result = boardRepo.getAllByWorkspaceId(ctx.db, workspace.id);
return result;
}),
byId: protectedProcedure
.meta({
openapi: {
method: "GET",
path: "/boards/{boardPublicId}",
summary: "Get board by public ID",
description: "Retrieves a board by its public ID",
tags: ["Boards"],
protect: true,
},
})
.input(
z.object({
boardPublicId: z.string().min(12),
members: z.array(z.string().min(12)).optional(),
labels: z.array(z.string().min(12)).optional(),
}),
)
.output(z.custom<Awaited<ReturnType<typeof boardRepo.getByPublicId>>>())
.query(async ({ ctx, input }) => {
const result = await boardRepo.getByPublicId(
ctx.db,
input.boardPublicId,
{
members: input.members ?? [],
labels: input.labels ?? [],
},
);
return result;
}),
create: protectedProcedure
.meta({
openapi: {
method: "POST",
path: "/workspaces/{workspacePublicId}/boards",
summary: "Create board",
description: "Creates a new board for a given workspace",
tags: ["Boards"],
protect: true,
},
})
.input(
z.object({
name: z.string().min(1),
workspacePublicId: z.string().min(12),
}),
)
.output(z.custom<Awaited<ReturnType<typeof boardRepo.create>>>())
.mutation(async ({ ctx, input }) => {
const userId = ctx.user?.id;
if (!userId)
throw new TRPCError({
message: `User not authenticated`,
code: "UNAUTHORIZED",
});
const workspace = await workspaceRepo.getByPublicId(
ctx.db,
input.workspacePublicId,
);
if (!workspace)
throw new TRPCError({
message: `Workspace with public ID ${input.workspacePublicId} not found`,
code: "NOT_FOUND",
});
const result = await boardRepo.create(ctx.db, {
name: input.name,
createdBy: userId,
workspaceId: workspace.id,
});
if (!result)
throw new TRPCError({
message: `Failed to create board`,
code: "INTERNAL_SERVER_ERROR",
});
return result;
}),
update: protectedProcedure
.meta({
openapi: {
method: "PUT",
path: "/boards/{boardPublicId}",
summary: "Update board",
description: "Updates a board by its public ID",
tags: ["Boards"],
protect: true,
},
})
.input(
z.object({
boardPublicId: z.string().min(12),
name: z.string().min(1),
}),
)
.output(z.custom<Awaited<ReturnType<typeof boardRepo.update>>>())
.mutation(async ({ ctx, input }) => {
const result = await boardRepo.update(ctx.db, {
name: input.name,
boardPublicId: input.boardPublicId,
});
if (!result)
throw new TRPCError({
message: `Failed to update board`,
code: "INTERNAL_SERVER_ERROR",
});
return result;
}),
delete: protectedProcedure
.meta({
openapi: {
method: "DELETE",
path: "/boards/{boardPublicId}",
summary: "Delete board",
description: "Deletes a board by its public ID",
tags: ["Boards"],
protect: true,
},
})
.input(
z.object({
boardPublicId: z.string().min(12),
}),
)
.output(z.object({ success: z.boolean() }))
.mutation(async ({ ctx, input }) => {
const userId = ctx.user?.id;
if (!userId)
throw new TRPCError({
message: `User not authenticated`,
code: "UNAUTHORIZED",
});
const board = await boardRepo.getWithListIdsByPublicId(
ctx.db,
input.boardPublicId,
);
if (!board)
throw new TRPCError({
message: `Board with public ID ${input.boardPublicId} not found`,
code: "NOT_FOUND",
});
const listIds = board.lists.map((list) => list.id);
const deletedAt = new Date().toISOString();
await boardRepo.softDelete(ctx.db, {
boardId: board.id,
deletedAt,
deletedBy: userId,
});
if (listIds.length) {
const deletedLists = await listRepo.softDeleteAllByBoardId(ctx.db, {
boardId: board.id,
deletedAt,
deletedBy: userId,
});
if (!Array.isArray(deletedLists)) {
throw new TRPCError({
message: `Failed to delete lists`,
code: "INTERNAL_SERVER_ERROR",
});
}
const deletedCards = await cardRepo.softDeleteAllByListIds(ctx.db, {
listIds,
deletedAt,
deletedBy: userId,
});
if (!Array.isArray(deletedCards)) {
throw new TRPCError({
message: `Failed to delete cards`,
code: "INTERNAL_SERVER_ERROR",
});
}
const activities = deletedCards.map((card) => ({
type: "card.archived" as const,
createdBy: userId,
cardId: card.id,
}));
await activityRepo.bulkCreate(ctx.db, activities);
}
return { success: true };
}),
});