tasko/actions/update-board/index.ts

83 lines
2 KiB
TypeScript
Raw Permalink Normal View History

2024-02-16 01:49:19 +00:00
'use server';
2024-02-15 02:30:10 +00:00
2024-03-21 21:15:27 +00:00
import { auth } from '@clerk/nextjs/server';
2025-01-19 06:36:59 +00:00
import { revalidatePath, revalidateTag } from 'next/cache';
2024-02-16 01:49:19 +00:00
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';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { InputType, ReturnType } from './types';
import { UpdateBoard } from './schema';
2024-02-15 02:30:10 +00:00
const handler = async (data: InputType): Promise<ReturnType> => {
const { userId, orgId } = await auth();
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
if (!userId || !orgId) return { error: 'Unauthorized' };
2024-02-15 02:30:10 +00:00
const { title, id, image } = data;
2024-02-15 02:30:10 +00:00
let board;
try {
const currentBoard = await db.board.findUnique({
where: {
id,
orgId,
},
select: {
imageId: true,
imageThumbUrl: true,
imageFullUrl: true,
imageUserName: true,
imageLinkHTML: true,
imageDownloadUrl: true,
},
});
const currentImageString = `${currentBoard?.imageId}|${currentBoard?.imageThumbUrl}|${currentBoard?.imageFullUrl}|${currentBoard?.imageUserName}|${currentBoard?.imageLinkHTML}|${currentBoard?.imageDownloadUrl}`;
const [
imageId,
imageThumbUrl,
imageFullUrl,
imageLinkHTML,
imageUserName,
imageDownloadUrl,
] = image?.split('|') || currentImageString.split('|');
2024-02-15 02:30:10 +00:00
board = await db.board.update({
where: {
id,
orgId,
},
data: {
title,
imageId,
imageThumbUrl,
imageFullUrl,
imageLinkHTML,
imageUserName,
imageDownloadUrl,
2024-02-15 02:30:10 +00:00
},
});
await createAuditLog({
entityTitle: board.title,
entityType: ENTITY_TYPE.BOARD,
entityId: board.id,
action: ACTION.UPDATE,
});
} catch (error) {
return {
2024-02-16 01:49:19 +00:00
error: 'Failed to update board',
2024-02-15 02:30:10 +00:00
};
}
2025-01-19 06:36:59 +00:00
revalidateTag(`board-${id}`);
2024-02-15 02:30:10 +00:00
revalidatePath(`/board/${id}`);
return { data: board };
};
2024-02-16 01:49:19 +00:00
export const updateBoard = createSafeAction(UpdateBoard, handler);