tasko/actions/update-card/index.ts

57 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-03-21 21:15:27 +00:00
import { auth } from '@clerk/nextjs/server';
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 { UpdateCard } 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 { id, boardId, startedAt, dueDate, ...values } = data;
2024-02-15 02:30:10 +00:00
let card;
try {
card = await db.card.update({
where: {
id,
list: {
board: {
orgId,
},
},
},
data: {
...values,
dueDate: dueDate,
2024-11-08 01:29:32 +00:00
startedAt: startedAt,
2024-02-15 02:30:10 +00:00
},
});
await createAuditLog({
entityTitle: card.title,
entityType: ENTITY_TYPE.CARD,
entityId: card.id,
action: ACTION.UPDATE,
});
} catch (error) {
return {
2024-02-16 01:49:19 +00:00
error: 'Failed to update card',
2024-02-15 02:30:10 +00:00
};
}
revalidateTag(`card-${id}`);
2024-02-15 02:30:10 +00:00
revalidatePath(`/board/${boardId}`);
return { data: card };
};
export const updateCard = createSafeAction(UpdateCard, handler);