mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-01-31 17:03:38 +00:00
82 lines
1.9 KiB
TypeScript
82 lines
1.9 KiB
TypeScript
'use server';
|
|
|
|
import { auth } from '@clerk/nextjs/server';
|
|
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 { CopyList } from './schema';
|
|
|
|
const handler = async (data: InputType): Promise<ReturnType> => {
|
|
const { userId, orgId } = auth();
|
|
|
|
if (!userId || !orgId) return { error: 'Unauthorized' };
|
|
|
|
const { id, boardId } = data;
|
|
let list;
|
|
|
|
try {
|
|
const listToCopy = await db.list.findUnique({
|
|
where: {
|
|
id,
|
|
boardId,
|
|
board: {
|
|
orgId,
|
|
},
|
|
},
|
|
include: {
|
|
cards: true,
|
|
},
|
|
});
|
|
|
|
if (!listToCopy) return { error: 'List not found' };
|
|
|
|
const lastList = await db.list.findFirst({
|
|
where: { boardId },
|
|
orderBy: { order: 'desc' },
|
|
select: { order: true },
|
|
});
|
|
|
|
const newOrder = lastList ? lastList.order + 1 : 1;
|
|
|
|
list = await db.list.create({
|
|
data: {
|
|
boardId: listToCopy.boardId,
|
|
title: `${listToCopy.title} - Copy`,
|
|
order: newOrder,
|
|
cards: {
|
|
createMany: {
|
|
data: listToCopy.cards.map((card) => ({
|
|
title: card.title,
|
|
description: card.description,
|
|
order: card.order,
|
|
})),
|
|
},
|
|
},
|
|
},
|
|
include: {
|
|
cards: true,
|
|
},
|
|
});
|
|
|
|
await createAuditLog({
|
|
entityTitle: list.title,
|
|
entityType: ENTITY_TYPE.LIST,
|
|
entityId: list.id,
|
|
action: ACTION.CREATE,
|
|
});
|
|
} catch (error) {
|
|
return {
|
|
error: 'Failed to copy list',
|
|
};
|
|
}
|
|
|
|
revalidatePath(`/board/${boardId}`);
|
|
return { data: list };
|
|
};
|
|
|
|
export const copyList = createSafeAction(CopyList, handler);
|