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>
 | 
										<Link href="survey/{survey.id}">{survey.title}</Link>
 | 
				
			||||||
					<span class="mr-5 inline-block">
 | 
										<span class="mr-5 inline-block">
 | 
				
			||||||
						({survey.fillRate.filled}/{survey.fillRate.expected})
 | 
											({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 />
 | 
												<CheckIcon />
 | 
				
			||||||
						{/if}
 | 
											{/if}
 | 
				
			||||||
					</span>
 | 
										</span>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,13 +17,22 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let { data }: { data: PageData } = $props();
 | 
						let { data }: { data: PageData } = $props();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const diagramData = data.skills.flatMap((skill) =>
 | 
						const diagramData = data.skills.flatMap((skill) => {
 | 
				
			||||||
		data.participants.map((participant) => ({
 | 
							if (data.participants.length > 0) {
 | 
				
			||||||
 | 
								return data.participants.map((participant) => ({
 | 
				
			||||||
				skill: skill.title,
 | 
									skill: skill.title,
 | 
				
			||||||
				participant: participant.id,
 | 
									participant: participant.id,
 | 
				
			||||||
				rating: participant.answers.find((answer) => answer.skillId === skill.id)?.rating
 | 
									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) {
 | 
						function copyLinkToClipboard(link: string) {
 | 
				
			||||||
		navigator.clipboard.writeText(link);
 | 
							navigator.clipboard.writeText(link);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,7 +5,7 @@ import debug from 'debug';
 | 
				
			||||||
import { fromFormData } from '$lib/survey';
 | 
					import { fromFormData } from '$lib/survey';
 | 
				
			||||||
import { db } from '../../../../../db';
 | 
					import { db } from '../../../../../db';
 | 
				
			||||||
import { surveyAccessTable, surveyAnswersTable, surveySkillsTable, surveysTable } from '../../../../../db/schema';
 | 
					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';
 | 
					import { addParticipant, addSkill, loadSurveyData } from '../../../../../db/survey';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const log = debug('survey:admin:edit');
 | 
					const log = debug('survey:admin:edit');
 | 
				
			||||||
| 
						 | 
					@ -58,10 +58,13 @@ export const actions = {
 | 
				
			||||||
        // update the participants where applicable (unchanged participants are left untouched)
 | 
					        // update the participants where applicable (unchanged participants are left untouched)
 | 
				
			||||||
        const deletedParticipants = survey.participants.filter(participant => !participants.includes(participant.email));
 | 
					        const deletedParticipants = survey.participants.filter(participant => !participants.includes(participant.email));
 | 
				
			||||||
        const newParticipants = participants.filter(email => !survey.participants.some(candidate => candidate.email === email));
 | 
					        const newParticipants = participants.filter(email => !survey.participants.some(candidate => candidate.email === email));
 | 
				
			||||||
        // delete all participants no longer part of the survey
 | 
					        // delete all participants no longer part of the survey (this should also delete their answers via cascade delete)
 | 
				
			||||||
        await db.delete(surveyAccessTable).where(inArray(surveyAccessTable.recepientEmail, deletedParticipants.map(participant => participant.email)));
 | 
					        await db.delete(surveyAccessTable).where(
 | 
				
			||||||
        // delete answers from deleted participants
 | 
					            and(
 | 
				
			||||||
        await db.delete(surveyAnswersTable).where(inArray(surveyAnswersTable.participantId, deletedParticipants.map(participant => participant.id)));
 | 
					                eq(surveyAccessTable.surveyId, survey.id),
 | 
				
			||||||
 | 
					                inArray(surveyAccessTable.recepientEmail, deletedParticipants.map(participant => participant.email))
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
        // add any new participants
 | 
					        // add any new participants
 | 
				
			||||||
        for (const newParticipant of newParticipants) {
 | 
					        for (const newParticipant of newParticipants) {
 | 
				
			||||||
            await addParticipant(survey.id, newParticipant);
 | 
					            await addParticipant(survey.id, newParticipant);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue