tasko/actions/delete-board/index.ts

56 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-02-16 01:49:19 +00:00
'use server';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { auth } from '@clerk/nextjs';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { ACTION, ENTITY_TYPE } from '@prisma/client';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { db } from '@/lib/db';
import { createAuditLog } from '@/lib/create-audit-log';
import { createSafeAction } from '@/lib/create-safe-action';
import { decreaseAvailableCount } from '@/lib/org-limit';
import { checkSubscription } from '@/lib/subscription';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { InputType, ReturnType } from './types';
import { DeleteBoard } from './schema';
2024-02-15 02:30:10 +00:00
const handler = async (data: InputType): Promise<ReturnType> => {
const { userId, orgId } = auth();
2024-02-16 01:49:19 +00:00
if (!userId || !orgId) return { error: 'Unauthorized' };
2024-02-15 02:30:10 +00:00
const isPro = await checkSubscription();
const { id } = data;
let board;
try {
board = await db.board.delete({
where: {
id,
orgId,
},
});
if (!isPro) {
await decreaseAvailableCount();
}
await createAuditLog({
entityTitle: board.title,
entityType: ENTITY_TYPE.BOARD,
entityId: board.id,
action: ACTION.DELETE,
});
} catch (error) {
return {
2024-02-16 01:49:19 +00:00
error: 'Failed to delete board',
2024-02-15 02:30:10 +00:00
};
}
revalidatePath(`/organization/${orgId}`);
redirect(`/organization/${orgId}`);
};
export const deleteBoard = createSafeAction(DeleteBoard, handler);