refactor: move reorder lists to update list
This commit is contained in:
@@ -1,33 +1,4 @@
|
||||
CREATE OR REPLACE FUNCTION reorder_lists(board_id BIGINT, list_id BIGINT, current_index INT, new_index INT)
|
||||
RETURNS BOOLEAN
|
||||
LANGUAGE PLPGSQL
|
||||
AS $$
|
||||
BEGIN
|
||||
UPDATE list
|
||||
SET index =
|
||||
CASE
|
||||
WHEN index = current_index AND id = list_id THEN new_index
|
||||
WHEN current_index < new_index AND index > current_index AND index <= new_index THEN index - 1
|
||||
WHEN current_index > new_index AND index >= new_index AND index < current_index THEN index + 1
|
||||
ELSE index
|
||||
END
|
||||
WHERE "boardId" = board_id;
|
||||
|
||||
-- Check for duplicate indices after the update
|
||||
IF EXISTS (
|
||||
SELECT index, COUNT(*)
|
||||
FROM list
|
||||
WHERE "boardId" = board_id
|
||||
AND "deletedAt" IS NULL
|
||||
GROUP BY index
|
||||
HAVING COUNT(*) > 1
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Duplicate indices found after reordering in board %', board_id;
|
||||
END IF;
|
||||
|
||||
RETURN TRUE;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE OR REPLACE FUNCTION reorder_cards(card_id BIGINT, current_list_id BIGINT, new_list_id BIGINT, current_index INT, new_index INT)
|
||||
RETURNS BOOLEAN
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||
import { and, desc, eq, isNull } from "drizzle-orm";
|
||||
import { and, desc, eq, gt, isNull, sql } from "drizzle-orm";
|
||||
|
||||
import type { dbClient } from "@kan/db/client";
|
||||
import type { Database } from "@kan/db/types/database.types";
|
||||
@@ -68,7 +68,7 @@ export const getWithCardsByPublicId = async (
|
||||
};
|
||||
|
||||
export const update = async (
|
||||
db: SupabaseClient<Database>,
|
||||
db: dbClient,
|
||||
listInput: {
|
||||
name: string;
|
||||
},
|
||||
@@ -76,33 +76,78 @@ export const update = async (
|
||||
listPublicId: string;
|
||||
},
|
||||
) => {
|
||||
const { data } = await db
|
||||
.from("list")
|
||||
.update({ name: listInput.name })
|
||||
.eq("publicId", args.listPublicId)
|
||||
.is("deletedAt", null)
|
||||
.select(`publicId, name`);
|
||||
const [result] = await db
|
||||
.update(lists)
|
||||
.set({ name: listInput.name })
|
||||
.where(and(eq(lists.publicId, args.listPublicId), isNull(lists.deletedAt)))
|
||||
.returning({
|
||||
publicId: lists.publicId,
|
||||
name: lists.name,
|
||||
});
|
||||
|
||||
return data;
|
||||
return result;
|
||||
};
|
||||
|
||||
export const reorder = async (
|
||||
db: SupabaseClient<Database>,
|
||||
db: dbClient,
|
||||
args: {
|
||||
boardPublicId: number;
|
||||
listPublicId: number;
|
||||
currentIndex: number;
|
||||
listPublicId: string;
|
||||
newIndex: number;
|
||||
},
|
||||
) => {
|
||||
const { data } = await db.rpc("reorder_lists", {
|
||||
board_id: args.boardPublicId,
|
||||
list_id: args.listPublicId,
|
||||
current_index: args.currentIndex,
|
||||
new_index: args.newIndex,
|
||||
});
|
||||
return db.transaction(async (tx) => {
|
||||
const list = await tx.query.lists.findFirst({
|
||||
columns: {
|
||||
id: true,
|
||||
boardId: true,
|
||||
index: true,
|
||||
},
|
||||
where: eq(lists.publicId, args.listPublicId),
|
||||
});
|
||||
|
||||
return data;
|
||||
if (!list)
|
||||
throw new Error(`List not found for public ID ${args.listPublicId}`);
|
||||
|
||||
await tx.execute(sql`
|
||||
UPDATE list
|
||||
SET index =
|
||||
CASE
|
||||
WHEN index = ${list.index} AND id = ${list.id} THEN ${args.newIndex}
|
||||
WHEN ${list.index} < ${args.newIndex} AND index > ${list.index} AND index <= ${args.newIndex} THEN index - 1
|
||||
WHEN ${list.index} > ${args.newIndex} AND index >= ${args.newIndex} AND index < ${list.index} THEN index + 1
|
||||
ELSE index
|
||||
END
|
||||
WHERE "boardId" = ${list.boardId};
|
||||
`);
|
||||
|
||||
const countExpr = sql<number>`COUNT(*)`.mapWith(Number);
|
||||
|
||||
const duplicateIndices = await db
|
||||
.select({
|
||||
index: lists.index,
|
||||
count: countExpr,
|
||||
})
|
||||
.from(lists)
|
||||
.where(and(eq(lists.boardId, list.boardId), isNull(lists.deletedAt)))
|
||||
.groupBy(lists.index)
|
||||
.having(gt(countExpr, 1));
|
||||
|
||||
if (duplicateIndices.length > 0) {
|
||||
throw new Error(
|
||||
`Duplicate indices found after reordering in board ${list.boardId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const updatedList = await tx.query.lists.findFirst({
|
||||
columns: {
|
||||
publicId: true,
|
||||
name: true,
|
||||
},
|
||||
where: eq(lists.publicId, args.listPublicId),
|
||||
});
|
||||
|
||||
return updatedList;
|
||||
});
|
||||
};
|
||||
|
||||
export const shiftIndex = async (
|
||||
|
||||
Reference in New Issue
Block a user