documenso/packages/lib/server-only/public-api/delete-api-token-by-id.ts

35 lines
841 B
TypeScript
Raw Normal View History

2023-11-24 14:13:09 +00:00
import { prisma } from '@documenso/prisma';
2025-06-10 01:49:52 +00:00
import { TEAM_MEMBER_ROLE_PERMISSIONS_MAP } from '../../constants/teams';
import { AppError, AppErrorCode } from '../../errors/app-error';
import { buildTeamWhereQuery } from '../../utils/teams';
2023-11-24 14:13:09 +00:00
export type DeleteTokenByIdOptions = {
id: number;
userId: number;
2025-06-10 01:49:52 +00:00
teamId: number;
2023-11-24 14:13:09 +00:00
};
2024-02-22 02:39:34 +00:00
export const deleteTokenById = async ({ id, userId, teamId }: DeleteTokenByIdOptions) => {
2025-06-10 01:49:52 +00:00
const team = await prisma.team.findFirst({
where: buildTeamWhereQuery({
teamId,
userId,
roles: TEAM_MEMBER_ROLE_PERMISSIONS_MAP['MANAGE_TEAM'],
}),
});
2024-02-22 02:39:34 +00:00
2025-06-10 01:49:52 +00:00
if (!team) {
throw new AppError(AppErrorCode.UNAUTHORIZED, {
message: 'You do not have permission to delete this token',
});
2024-02-22 02:39:34 +00:00
}
2025-08-24 22:25:01 +00:00
await prisma.apiToken.delete({
2023-11-24 14:13:09 +00:00
where: {
id,
2025-06-10 01:49:52 +00:00
teamId,
2023-11-24 14:13:09 +00:00
},
});
};