fixed migrations
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { db } from '$lib/server/db/index.js';
|
||||
import { inviteLink, participant, audioFile, rating } from '$lib/server/db/schema.js';
|
||||
import { eq, isNull, and } from 'drizzle-orm';
|
||||
import { eq, isNull, and, inArray } from 'drizzle-orm';
|
||||
import { parseStoredTags, matchesInviteTags } from '$lib/server/tag-utils.js';
|
||||
|
||||
export async function load({ url, cookies }) {
|
||||
const token = url.searchParams.get('token');
|
||||
@@ -21,7 +22,8 @@ export async function load({ url, cookies }) {
|
||||
throw error(404, 'Invite link not found or has been deleted');
|
||||
}
|
||||
|
||||
const invite = invites[0];
|
||||
const { tags: inviteTagString, ...safeInvite } = invites[0];
|
||||
const inviteTags = parseStoredTags(inviteTagString);
|
||||
|
||||
let participantId = cookies.get(`participant-${token}`);
|
||||
let isExistingParticipant = false;
|
||||
@@ -66,49 +68,71 @@ export async function load({ url, cookies }) {
|
||||
});
|
||||
}
|
||||
|
||||
const audioFiles = await db.select({
|
||||
const audioRows = await db.select({
|
||||
id: audioFile.id,
|
||||
filename: audioFile.filename,
|
||||
contentType: audioFile.contentType,
|
||||
duration: audioFile.duration,
|
||||
fileSize: audioFile.fileSize,
|
||||
createdAt: audioFile.createdAt
|
||||
createdAt: audioFile.createdAt,
|
||||
tags: audioFile.tags
|
||||
})
|
||||
.from(audioFile)
|
||||
.where(isNull(audioFile.deletedAt)); // Only show active audio files
|
||||
|
||||
const filteredAudio = audioRows
|
||||
.map((audio) => ({
|
||||
data: {
|
||||
id: audio.id,
|
||||
filename: audio.filename,
|
||||
contentType: audio.contentType,
|
||||
duration: audio.duration,
|
||||
fileSize: audio.fileSize,
|
||||
createdAt: audio.createdAt
|
||||
},
|
||||
tags: parseStoredTags(audio.tags)
|
||||
}))
|
||||
.filter(({ tags }) => matchesInviteTags(tags, inviteTags))
|
||||
.map(({ data }) => data);
|
||||
|
||||
const allowedAudioIds = filteredAudio.map((file) => file.id);
|
||||
|
||||
// Get completed ratings for this participant (only active, non-deleted ratings for active audio files)
|
||||
const completedRatings = await db
|
||||
.select({
|
||||
audioFileId: rating.audioFileId
|
||||
})
|
||||
.from(rating)
|
||||
.innerJoin(audioFile, and(
|
||||
eq(rating.audioFileId, audioFile.id),
|
||||
isNull(audioFile.deletedAt) // Only count ratings for active audio files
|
||||
))
|
||||
.where(
|
||||
and(
|
||||
eq(rating.participantId, participantId),
|
||||
eq(rating.isCompleted, true),
|
||||
isNull(rating.deletedAt) // Only count active ratings
|
||||
)
|
||||
);
|
||||
let completedRatings = [];
|
||||
if (allowedAudioIds.length > 0) {
|
||||
completedRatings = await db
|
||||
.select({
|
||||
audioFileId: rating.audioFileId
|
||||
})
|
||||
.from(rating)
|
||||
.innerJoin(audioFile, and(
|
||||
eq(rating.audioFileId, audioFile.id),
|
||||
isNull(audioFile.deletedAt) // Only count ratings for active audio files
|
||||
))
|
||||
.where(
|
||||
and(
|
||||
eq(rating.participantId, participantId),
|
||||
eq(rating.isCompleted, true),
|
||||
isNull(rating.deletedAt), // Only count active ratings
|
||||
inArray(rating.audioFileId, allowedAudioIds)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const completedAudioIds = new Set(completedRatings.map(r => r.audioFileId));
|
||||
|
||||
// Add completion status to audio files
|
||||
const audioFilesWithStatus = audioFiles.map(file => ({
|
||||
const audioFilesWithStatus = filteredAudio.map(file => ({
|
||||
...file,
|
||||
isCompleted: completedAudioIds.has(file.id)
|
||||
}));
|
||||
|
||||
return {
|
||||
invite,
|
||||
invite: safeInvite,
|
||||
participantId,
|
||||
audioFiles: audioFilesWithStatus,
|
||||
token,
|
||||
completedCount: completedRatings.length,
|
||||
totalCount: audioFiles.length
|
||||
totalCount: filteredAudio.length
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user