tasko/actions/update-board/index.ts
2024-02-15 20:49:19 -05:00

49 lines
1.1 KiB
TypeScript

'use server';
import { auth } from '@clerk/nextjs';
import { revalidatePath } from 'next/cache';
import { ACTION, ENTITY_TYPE } from '@prisma/client';
import { db } from '@/lib/db';
import { createAuditLog } from '@/lib/create-audit-log';
import { createSafeAction } from '@/lib/create-safe-action';
import { InputType, ReturnType } from './types';
import { UpdateBoard } from './schema';
const handler = async (data: InputType): Promise<ReturnType> => {
const { userId, orgId } = auth();
if (!userId || !orgId) return { error: 'Unauthorized' };
const { title, id } = data;
let board;
try {
board = await db.board.update({
where: {
id,
orgId,
},
data: {
title,
},
});
await createAuditLog({
entityTitle: board.title,
entityType: ENTITY_TYPE.BOARD,
entityId: board.id,
action: ACTION.UPDATE,
});
} catch (error) {
return {
error: 'Failed to update board',
};
}
revalidatePath(`/board/${id}`);
return { data: board };
};
export const updateBoard = createSafeAction(UpdateBoard, handler);