Skip to content

Commit

Permalink
merge backend/dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Nov 26, 2024
2 parents e02c2bb + 4f0649f commit 7f94688
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 55 deletions.
6 changes: 2 additions & 4 deletions src/lib/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ export interface Session {
hostName: string;
title: string;
createdAt: string;
status: 'draft' | 'waiting' | 'active' | 'ended';
stage: 'grouping' | 'individual' | 'group' | 'ended';
tempIdExpiry: string | null;
goal: string;
subQuestions: string[];
status: 'draft' | 'waiting' | 'active' | 'ended' | 'individual' | 'group';
tempIdExpiry: string | null;
resourceIds: string[];
participants: {
[userId: string]: {
Expand Down Expand Up @@ -77,7 +76,6 @@ export function convertFirestoreSession(data: FirestoreSession): Session {
title: data.title,
createdAt: data.createdAt.toDate().toISOString(),
status: data.status,
stage: data.stage,
tempIdExpiry: data.tempIdExpiry ? data.tempIdExpiry.toDate().toISOString() : null,
goal: data.goal,
subQuestions: data.subQuestions,
Expand Down
25 changes: 25 additions & 0 deletions src/routes/api/session/[id]/save/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { adminDb } from '$lib/server/firebase';
import { json, type RequestHandler } from '@sveltejs/kit';

export const POST: RequestHandler = async ({ request, params, locals }) => {
if (!locals.user) {
return json({ error: 'Unauthorized' }, { status: 401 });
}

const { id } = params;
const { goal, subQuestions } = await request.json();

if (!id) {
return json({ error: 'Missing parameters' }, { status: 400 });
}

try {
const sessionRef = adminDb.collection('sessions').doc(id);
await sessionRef.update({ goal, subQuestions });

return json({ success: true });
} catch (error) {
console.error('Error saving session:', error);
return json({ error: 'Failed to save session' }, { status: 500 });
}
};
75 changes: 73 additions & 2 deletions src/routes/session/[id]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { adminDb } from '$lib/server/firebase';
import type { FirestoreSession } from '$lib/types/session';
import { convertFirestoreSession } from '$lib/types/session';
import { error, redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { error, fail, redirect } from '@sveltejs/kit';
import { FieldValue } from 'firebase-admin/firestore';
import type { Actions, PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ params, locals }) => {
if (!locals.user) {
Expand All @@ -26,3 +27,73 @@ export const load: PageServerLoad = async ({ params, locals }) => {
user: locals.user
};
};

export const actions = {
deleteParticipant: async ({ request, locals }) => {
if (!locals.user) {
throw redirect(303, '/login');
}

const data = await request.formData();
const participantId = data.get('participantId')?.toString();
const sessionId = data.get('sessionId')?.toString();

if (!participantId || !sessionId) {
return fail(400, { missing: true });
}

try {
// Delete participant from session
const sessionRef = adminDb.collection('sessions').doc(sessionId);
await sessionRef.update({
[`participants.${participantId}`]: FieldValue.delete()
});

// Delete participant from group
const groupSnapshot = await adminDb
.collection('groups')
.where('sessionId', '==', sessionId)
.where('participants', 'array-contains', participantId)
.get();

if (!groupSnapshot.empty) {
const groupDoc = groupSnapshot.docs[0];
const groupRef = adminDb.collection('groups').doc(groupDoc.id);

// remove participant from group
await groupRef.update({
participants: FieldValue.arrayRemove(participantId)
});
}
return { success: true };
} catch (error) {
console.error('Error updating profile:', error);
return fail(500, { error: true });
}
},
updateTitle: async ({ request, locals }) => {
if (!locals.user) {
throw redirect(303, '/login');
}

const data = await request.formData();
const sessionId = data.get('sessionId')?.toString();
const title = data.get('title')?.toString();

if (!sessionId || !title) {
return fail(400, { missing: true });
}

try {
const sessionRef = adminDb.collection('sessions').doc(sessionId);
await sessionRef.update({
title
});

return { success: true };
} catch (error) {
console.error('Error updating session title:', error);
return fail(500, { error: true });
}
}
} satisfies Actions;
Loading

0 comments on commit 7f94688

Please sign in to comment.