File size: 1,123 Bytes
992a8de
 
 
53e0136
992a8de
 
 
 
 
 
 
 
 
 
 
53e0136
 
 
 
 
 
 
 
 
992a8de
53e0136
 
 
 
 
 
 
992a8de
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { collections } from "$lib/server/database";
import { ObjectId } from "mongodb";
import type { LayoutServerLoad } from "./$types";
import type { Report } from "$lib/types/Report";

export const load = (async ({ locals, parent }) => {
	const { settings } = await parent();

	// find assistants matching the settings assistants
	const assistants = await collections.assistants
		.find({
			_id: { $in: settings.assistants.map((el) => new ObjectId(el)) },
		})
		.toArray();

	let reportsByUser: string[] = [];
	const createdBy = locals.user?._id ?? locals.sessionId;
	if (createdBy) {
		const reports = await collections.reports
			.find<Pick<Report, "assistantId">>({ createdBy }, { projection: { _id: 0, assistantId: 1 } })
			.toArray();
		reportsByUser = reports.map((r) => r.assistantId.toString());
	}

	return {
		assistants: assistants.map((el) => ({
			...el,
			_id: el._id.toString(),
			createdById: undefined,
			createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
			reported: reportsByUser.includes(el._id.toString()),
		})),
	};
}) satisfies LayoutServerLoad;