parent
1ab979b5b4
commit
76cf79158e
2 changed files with 16 additions and 6 deletions
|
@ -3,7 +3,7 @@ import debug from "debug";
|
||||||
|
|
||||||
const log = debug('survey:admin:edit');
|
const log = debug('survey:admin:edit');
|
||||||
|
|
||||||
import { eq, and, inArray, gte, sql } from "drizzle-orm";
|
import { eq, or, and, inArray, gte, sql, isNull } from "drizzle-orm";
|
||||||
import { db } from "../db";
|
import { db } from "../db";
|
||||||
import { surveyAccessTable, surveyAnswersTable, surveyPermissionsTable, surveySkillsTable, surveysTable, usersTable } from "../db/schema";
|
import { surveyAccessTable, surveyAnswersTable, surveyPermissionsTable, surveySkillsTable, surveysTable, usersTable } from "../db/schema";
|
||||||
import { AccessLevel, type AccessToken, type Email, type ParticipantId, type SkillId, type SurveyData, type SurveyId, type SurveyMetaData, type UserId } from "$lib/types";
|
import { AccessLevel, type AccessToken, type Email, type ParticipantId, type SkillId, type SurveyData, type SurveyId, type SurveyMetaData, type UserId } from "$lib/types";
|
||||||
|
@ -54,17 +54,27 @@ export async function loadSurveyPermissions(surveyId: SurveyId) {
|
||||||
|
|
||||||
export async function loadMySurveys(userId: UserId): Promise<SurveyMetaData[]> {
|
export async function loadMySurveys(userId: UserId): Promise<SurveyMetaData[]> {
|
||||||
/// Get all surveys I have access to
|
/// Get all surveys I have access to
|
||||||
const mySurves = await db.select()
|
const mySurveys = await db.select()
|
||||||
.from(surveyPermissionsTable)
|
.from(surveyPermissionsTable)
|
||||||
.innerJoin(surveysTable, eq(surveysTable.id, surveyPermissionsTable.surveyId))
|
.innerJoin(surveysTable, eq(surveysTable.id, surveyPermissionsTable.surveyId))
|
||||||
.where(eq(surveyPermissionsTable.user, userId));
|
.where(or(eq(surveyPermissionsTable.user, userId), isNull(surveyPermissionsTable.user)));
|
||||||
|
|
||||||
return mySurves.map(survey => ({
|
// deduplicate the results, always preferring the entry with the direct mention of the given user ID
|
||||||
|
// Note: without the deduplications, surveys shared with "Anyone" would show up twice, if they were also shared directly with a user
|
||||||
|
const deduplicatedResults = new Map<number, (typeof mySurveys)[0]>();
|
||||||
|
for (const result of mySurveys) {
|
||||||
|
const survey = deduplicatedResults.get(result.surveys_table.id);
|
||||||
|
if (!survey?.survey_permissions_table.user) {
|
||||||
|
deduplicatedResults.set(result.surveys_table.id, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [...deduplicatedResults.values().map(survey => ({
|
||||||
id: survey.surveys_table.id as SurveyId,
|
id: survey.surveys_table.id as SurveyId,
|
||||||
title: survey.surveys_table.title,
|
title: survey.surveys_table.title,
|
||||||
description: survey.surveys_table.description,
|
description: survey.surveys_table.description,
|
||||||
permissions: survey.survey_permissions_table.access,
|
permissions: survey.survey_permissions_table.access,
|
||||||
}))
|
}))]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
<div class="p-4">
|
<div class="p-4">
|
||||||
<h2 class="text-2xl">Surveys you own</h2>
|
<h2 class="text-2xl">Surveys you own</h2>
|
||||||
<ul class="ml-8 list-disc">
|
<ul class="ml-8 list-disc">
|
||||||
{#each data.surveys as survey}
|
{#each data.surveys.sort((a, b) => a.title.localeCompare(b.title)) as survey}
|
||||||
<li class="grid grid-cols-2">
|
<li class="grid grid-cols-2">
|
||||||
<div>
|
<div>
|
||||||
<Link href="survey/{survey.id}">{survey.title}</Link>
|
<Link href="survey/{survey.id}">{survey.title}</Link>
|
||||||
|
|
Loading…
Add table
Reference in a new issue