Files
kan/packages/api/src/routers/list.ts
Henry 7f5a1ab513 feat: customisable workspace role permissions (#345)
* feat: setup schema for workspace roles

* chore: regen migration

* feat: add publicId to workspace roles

* feat: setup default permissions

* feat: add repo funcs

* feat: setup basic router interactions

* feat: add card permissions

* feat: assert permissions for lists

* feat: assert board permissions

* feat: assert permission for remaining routes

* feat: add permissions page to settings

* feat: enable updating member roles

* feat: order members by role and createdAt

* feat: allow editing individual permissions

* feat: reset role defaults

* feat: clear all permission overrides

* feat: allow users to delete entities they have created

* feat: set roleId when inviting new members

* feat: disable UI elements if user does not have permissions

* feat: allow admins to assign the admin role to other users

* feat: allow delete:list as default

* refactor: centre permissions modal

* chore: translations
2026-02-01 21:17:18 +00:00

226 lines
5.6 KiB
TypeScript

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 { createTRPCRouter, protectedProcedure } from "../trpc";
import { assertCanDelete, assertCanEdit, assertPermission } from "../utils/permissions";
export const listRouter = createTRPCRouter({
create: protectedProcedure
.meta({
openapi: {
summary: "Create a list",
method: "POST",
path: "/lists",
description: "Creates a new list for a given board",
tags: ["Lists"],
protect: true,
},
})
.input(
z.object({
name: z.string().min(1),
boardPublicId: z.string().min(12),
}),
)
.output(z.custom<Awaited<ReturnType<typeof listRepo.create>>>())
.mutation(async ({ ctx, input }) => {
const userId = ctx.user?.id;
if (!userId)
throw new TRPCError({
message: `User not authenticated`,
code: "UNAUTHORIZED",
});
const board = await boardRepo.getWorkspaceAndBoardIdByBoardPublicId(
ctx.db,
input.boardPublicId,
);
if (!board)
throw new TRPCError({
message: `Board with public ID ${input.boardPublicId} not found`,
code: "NOT_FOUND",
});
await assertPermission(ctx.db, userId, board.workspaceId, "list:create");
const result = await listRepo.create(ctx.db, {
name: input.name,
createdBy: userId,
boardId: board.id,
});
if (!result)
throw new TRPCError({
message: `Failed to create list`,
code: "INTERNAL_SERVER_ERROR",
});
return result;
}),
delete: protectedProcedure
.meta({
openapi: {
summary: "Delete a list",
method: "DELETE",
path: "/lists/{listPublicId}",
description: "Deletes a list by its public ID",
tags: ["Lists"],
protect: true,
},
})
.input(
z.object({
listPublicId: 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 list = await listRepo.getWorkspaceAndListIdByListPublicId(
ctx.db,
input.listPublicId,
);
if (!list)
throw new TRPCError({
message: `List with public ID ${input.listPublicId} not found`,
code: "NOT_FOUND",
});
await assertCanDelete(
ctx.db,
userId,
list.workspaceId,
"list:delete",
list.createdBy,
);
const deletedAt = new Date();
const deletedList = await listRepo.softDeleteById(ctx.db, {
listId: list.id,
deletedAt,
deletedBy: userId,
});
if (!deletedList)
throw new TRPCError({
message: `Failed to delete list`,
code: "INTERNAL_SERVER_ERROR",
});
const deletedCards = await cardRepo.softDeleteAllByListIds(ctx.db, {
listIds: [list.id],
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,
}));
if (activities.length) await activityRepo.bulkCreate(ctx.db, activities);
return { success: true };
}),
update: protectedProcedure
.meta({
openapi: {
summary: "Update a list",
method: "PUT",
path: "/lists/{listPublicId}",
description: "Updates a list by its public ID",
tags: ["Lists"],
protect: true,
},
})
.input(
z.object({
listPublicId: z.string().min(12),
name: z.string().min(1).optional(),
index: z.number().optional(),
}),
)
.output(
z.custom<
| Awaited<ReturnType<typeof listRepo.update>>
| Awaited<ReturnType<typeof listRepo.reorder>>
>(),
)
.mutation(async ({ ctx, input }) => {
const userId = ctx.user?.id;
if (!userId)
throw new TRPCError({
message: `User not authenticated`,
code: "UNAUTHORIZED",
});
const list = await listRepo.getWorkspaceAndListIdByListPublicId(
ctx.db,
input.listPublicId,
);
if (!list)
throw new TRPCError({
message: `List with public ID ${input.listPublicId} not found`,
code: "NOT_FOUND",
});
await assertCanEdit(
ctx.db,
userId,
list.workspaceId,
"list:edit",
list.createdBy,
);
let result: { name: string; publicId: string } | undefined;
if (input.name) {
result = await listRepo.update(
ctx.db,
{ name: input.name },
{ listPublicId: input.listPublicId },
);
}
if (input.index !== undefined) {
result = await listRepo.reorder(ctx.db, {
listPublicId: input.listPublicId,
newIndex: input.index,
});
}
if (!result)
throw new TRPCError({
message: `Failed to update list`,
code: "INTERNAL_SERVER_ERROR",
});
return result;
}),
});