Skip to content

Commit

Permalink
feat(admin): recover dropped songs when insufficient unused songs is …
Browse files Browse the repository at this point in the history
…present

Signed-off-by: ZTL-UwU <[email protected]>
  • Loading branch information
ZTL-UwU committed Dec 19, 2024
1 parent 462851e commit c38f61b
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions server/trpc/routers/arrangements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ export const arrangementsRouter = router({
if (!(await reviewAll()))
throw new TRPCError({ code: 'FORBIDDEN', message: '请审核全部歌曲' });

if (!(await fitsInTime(new Date())))
if (await fitsInTime(new Date()))
throw new TRPCError({ code: 'FORBIDDEN', message: '请在投稿截止后排歌' });

const start = parseDate(input.start);
const end = parseDate(input.end);

await db.transaction(async (tx) => {
// Get unused songs
// get unused songs
const approvedSongs = await tx.query.songs.findMany({
where: eq(songs.state, 'approved'),
orderBy: sql`RANDOM()`,
Expand All @@ -96,7 +96,21 @@ export const arrangementsRouter = router({
},
});

// get dropped songs
let droppedSongs: typeof approvedSongs = [];
// (only when insufficient unused songs is present)
if ((end.compare(start) + 1) * input.songCount > approvedSongs.length) {
droppedSongs = await tx.query.songs.findMany({
where: eq(songs.state, 'dropped'),
orderBy: sql`RANDOM()`,
columns: {
id: true,
},
});
}

let songIndex = 0;
let droppedSongIndex = 0;
for (let date = start; date.compare(end) <= 0; date = date.add({ days: 1 })) {
const dateString = date.toString();
await tx.insert(arrangements).values({ date: dateString });
Expand All @@ -110,9 +124,19 @@ export const arrangementsRouter = router({
state: 'used',
})
.where(eq(songs.id, approvedSongs[songIndex]!.id));
}

songIndex++;
songIndex++;
} else if (droppedSongIndex < droppedSongs.length) { // recover dropped songs
await tx
.update(songs)
.set({
arrangementDate: dateString,
state: 'used',
})
.where(eq(songs.id, droppedSongs[droppedSongIndex]!.id));

droppedSongIndex++;
}
}
}

Expand Down

0 comments on commit c38f61b

Please sign in to comment.