mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-02-07 11:42:55 +00:00
50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
|
"use server";
|
||
|
|
||
|
import { auth } from "@clerk/nextjs";
|
||
|
import { revalidatePath } from "next/cache";
|
||
|
|
||
|
import { db } from "@/lib/db";
|
||
|
import { createSafeAction } from "@/lib/create-safe-action";
|
||
|
|
||
|
import { InputType, ReturnType } from "./types";
|
||
|
import { UpdateCardOrder } from "./schema";
|
||
|
|
||
|
const handler = async (data: InputType): Promise<ReturnType> => {
|
||
|
const { userId, orgId } = auth();
|
||
|
|
||
|
if (!userId || !orgId) return { error: "Unauthorized" };
|
||
|
|
||
|
const { items, boardId } = data;
|
||
|
let updatedCards;
|
||
|
|
||
|
try {
|
||
|
const transaction = items.map((card) =>
|
||
|
db.card.update({
|
||
|
where: {
|
||
|
id: card.id,
|
||
|
list: {
|
||
|
board: {
|
||
|
orgId,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
data: {
|
||
|
order: card.order,
|
||
|
listId: card.listId,
|
||
|
},
|
||
|
})
|
||
|
);
|
||
|
|
||
|
updatedCards = await db.$transaction(transaction);
|
||
|
} catch (error) {
|
||
|
return {
|
||
|
error: "Failed to reorder list",
|
||
|
};
|
||
|
}
|
||
|
|
||
|
revalidatePath(`/board/${boardId}`);
|
||
|
return { data: updatedCards };
|
||
|
};
|
||
|
|
||
|
export const updateCardOrder = createSafeAction(UpdateCardOrder, handler);
|