fix serious issue where deleting a participant from one survey would delete them from all
This commit is contained in:
		
							parent
							
								
									f45deb8680
								
							
						
					
					
						commit
						edb8c7b723
					
				
					 3 changed files with 25 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -40,7 +40,7 @@
 | 
			
		|||
					<Link href="survey/{survey.id}">{survey.title}</Link>
 | 
			
		||||
					<span class="mr-5 inline-block">
 | 
			
		||||
						({survey.fillRate.filled}/{survey.fillRate.expected})
 | 
			
		||||
						{#if survey.fillRate.filled === survey.fillRate.expected}
 | 
			
		||||
						{#if survey.fillRate.filled === survey.fillRate.expected && survey.fillRate.expected > 0}
 | 
			
		||||
							<CheckIcon />
 | 
			
		||||
						{/if}
 | 
			
		||||
					</span>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,13 +17,22 @@
 | 
			
		|||
 | 
			
		||||
	let { data }: { data: PageData } = $props();
 | 
			
		||||
 | 
			
		||||
	const diagramData = data.skills.flatMap((skill) =>
 | 
			
		||||
		data.participants.map((participant) => ({
 | 
			
		||||
			skill: skill.title,
 | 
			
		||||
			participant: participant.id,
 | 
			
		||||
			rating: participant.answers.find((answer) => answer.skillId === skill.id)?.rating
 | 
			
		||||
		}))
 | 
			
		||||
	);
 | 
			
		||||
	const diagramData = data.skills.flatMap((skill) => {
 | 
			
		||||
		if (data.participants.length > 0) {
 | 
			
		||||
			return data.participants.map((participant) => ({
 | 
			
		||||
				skill: skill.title,
 | 
			
		||||
				participant: participant.id,
 | 
			
		||||
				rating: participant.answers.find((answer) => answer.skillId === skill.id)?.rating
 | 
			
		||||
			}));
 | 
			
		||||
		} else {
 | 
			
		||||
			// fallback pseudo participant because empty diagram data will break the diagram and make the survey page unaccessible
 | 
			
		||||
			return {
 | 
			
		||||
				skill: skill.title,
 | 
			
		||||
				participant: -1,
 | 
			
		||||
				rating: undefined
 | 
			
		||||
			};
 | 
			
		||||
		}
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
	function copyLinkToClipboard(link: string) {
 | 
			
		||||
		navigator.clipboard.writeText(link);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,7 @@ import debug from 'debug';
 | 
			
		|||
import { fromFormData } from '$lib/survey';
 | 
			
		||||
import { db } from '../../../../../db';
 | 
			
		||||
import { surveyAccessTable, surveyAnswersTable, surveySkillsTable, surveysTable } from '../../../../../db/schema';
 | 
			
		||||
import { eq, inArray } from 'drizzle-orm';
 | 
			
		||||
import { eq, inArray, and } from 'drizzle-orm';
 | 
			
		||||
import { addParticipant, addSkill, loadSurveyData } from '../../../../../db/survey';
 | 
			
		||||
 | 
			
		||||
const log = debug('survey:admin:edit');
 | 
			
		||||
| 
						 | 
				
			
			@ -58,10 +58,13 @@ export const actions = {
 | 
			
		|||
        // update the participants where applicable (unchanged participants are left untouched)
 | 
			
		||||
        const deletedParticipants = survey.participants.filter(participant => !participants.includes(participant.email));
 | 
			
		||||
        const newParticipants = participants.filter(email => !survey.participants.some(candidate => candidate.email === email));
 | 
			
		||||
        // delete all participants no longer part of the survey
 | 
			
		||||
        await db.delete(surveyAccessTable).where(inArray(surveyAccessTable.recepientEmail, deletedParticipants.map(participant => participant.email)));
 | 
			
		||||
        // delete answers from deleted participants
 | 
			
		||||
        await db.delete(surveyAnswersTable).where(inArray(surveyAnswersTable.participantId, deletedParticipants.map(participant => participant.id)));
 | 
			
		||||
        // delete all participants no longer part of the survey (this should also delete their answers via cascade delete)
 | 
			
		||||
        await db.delete(surveyAccessTable).where(
 | 
			
		||||
            and(
 | 
			
		||||
                eq(surveyAccessTable.surveyId, survey.id),
 | 
			
		||||
                inArray(surveyAccessTable.recepientEmail, deletedParticipants.map(participant => participant.email))
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
        // add any new participants
 | 
			
		||||
        for (const newParticipant of newParticipants) {
 | 
			
		||||
            await addParticipant(survey.id, newParticipant);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue