Validates and improves schedule reordering
Ensures schedule reordering requests are valid by checking for duplicate and non-existent IDs. Improves performance by using a transaction for batch updates.
This commit is contained in:
parent
c3f5cd6b4e
commit
8e49b10042
1 changed files with 29 additions and 6 deletions
|
|
@ -639,13 +639,36 @@ const getMirrorCompatibility = async (scheduleId: number) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const reorderSchedules = async (scheduleIds: number[]) => {
|
const reorderSchedules = async (scheduleIds: number[]) => {
|
||||||
// Update each schedule's sortOrder based on its position in the array
|
// Validate input - check for duplicates
|
||||||
for (let i = 0; i < scheduleIds.length; i++) {
|
const uniqueIds = new Set(scheduleIds);
|
||||||
await db
|
if (uniqueIds.size !== scheduleIds.length) {
|
||||||
.update(backupSchedulesTable)
|
throw new BadRequestError("Duplicate schedule IDs in reorder request");
|
||||||
.set({ sortOrder: i, updatedAt: Date.now() })
|
|
||||||
.where(eq(backupSchedulesTable.id, scheduleIds[i]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify all schedules exist
|
||||||
|
const existingSchedules = await db.query.backupSchedulesTable.findMany({
|
||||||
|
columns: { id: true },
|
||||||
|
});
|
||||||
|
const existingIds = new Set(existingSchedules.map((s) => s.id));
|
||||||
|
|
||||||
|
for (const id of scheduleIds) {
|
||||||
|
if (!existingIds.has(id)) {
|
||||||
|
throw new NotFoundError(`Backup schedule with ID ${id} not found`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Batch update in a transaction
|
||||||
|
await db.transaction(async (tx) => {
|
||||||
|
const now = Date.now();
|
||||||
|
await Promise.all(
|
||||||
|
scheduleIds.map((scheduleId, index) =>
|
||||||
|
tx
|
||||||
|
.update(backupSchedulesTable)
|
||||||
|
.set({ sortOrder: index, updatedAt: now })
|
||||||
|
.where(eq(backupSchedulesTable.id, scheduleId)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const backupsService = {
|
export const backupsService = {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue