tasko/actions/update-list-order/index.ts

47 lines
1 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-02-16 01:49:19 +00:00
import { auth } from '@clerk/nextjs';
import { revalidatePath } from 'next/cache';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { db } from '@/lib/db';
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 { UpdateListOrder } from './schema';
2024-02-15 02:30:10 +00:00
const handler = async (data: InputType): Promise<ReturnType> => {
const { userId, orgId } = auth();
2024-02-16 01:49:19 +00:00
if (!userId || !orgId) return { error: 'Unauthorized' };
2024-02-15 02:30:10 +00:00
const { items, boardId } = data;
let lists;
try {
const transaction = items.map((list) =>
db.list.update({
where: {
id: list.id,
board: {
orgId,
},
},
data: {
order: list.order,
},
})
);
lists = await db.$transaction(transaction);
} catch (error) {
return {
2024-02-16 01:49:19 +00:00
error: 'Failed to reorder list',
2024-02-15 02:30:10 +00:00
};
}
revalidatePath(`/board/${boardId}`);
return { data: lists };
};
export const updateListOrder = createSafeAction(UpdateListOrder, handler);