Skip to content

Commit 5888c82

Browse files
committed
[Assistants settings] Update data backend
1 parent 105d8aa commit 5888c82

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

src/routes/settings/+layout.server.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
import { collections } from "$lib/server/database";
21
import type { LayoutServerLoad } from "./$types";
3-
import type { Report } from "$lib/types/Report";
42

5-
export const load = (async ({ locals, parent }) => {
3+
export const load = (async ({ parent }) => {
64
const { assistants } = await parent();
7-
8-
let reportsByUser: string[] = [];
9-
const createdBy = locals.user?._id ?? locals.sessionId;
10-
if (createdBy) {
11-
const reports = await collections.reports
12-
.find<Pick<Report, "assistantId">>({ createdBy }, { projection: { _id: 0, assistantId: 1 } })
13-
.toArray();
14-
reportsByUser = reports.map((r) => r.assistantId.toString());
15-
}
16-
17-
return {
18-
assistants: assistants.map((el) => ({
19-
...el,
20-
reported: reportsByUser.includes(el._id),
21-
})),
22-
};
5+
return { assistants };
236
}) satisfies LayoutServerLoad;

src/routes/settings/assistants/[assistantId]/+page.server.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { PUBLIC_ORIGIN, PUBLIC_SHARE_PREFIX } from "$env/static/public";
77
import { WEBHOOK_URL_REPORT_ASSISTANT } from "$env/static/private";
88
import { z } from "zod";
99
import type { Assistant } from "$lib/types/Assistant";
10+
1011
async function assistantOnlyIfAuthor(locals: App.Locals, assistantId?: string) {
1112
const assistant = await collections.assistants.findOne({ _id: new ObjectId(assistantId) });
1213

@@ -21,6 +22,31 @@ async function assistantOnlyIfAuthor(locals: App.Locals, assistantId?: string) {
2122
return assistant;
2223
}
2324

25+
export async function load({ parent, params, locals }) {
26+
const data = await parent();
27+
28+
const assistantId = params.assistantId as string;
29+
30+
const assistant = (data.settings.assistants as string[]).find((id) => id === assistantId);
31+
32+
let isReported = false;
33+
34+
const createdBy = locals.user?._id ?? locals.sessionId;
35+
if (createdBy) {
36+
const report = await collections.reports.findOne({
37+
createdBy,
38+
assistantId: new ObjectId(assistantId),
39+
});
40+
isReported = !!report;
41+
}
42+
43+
if (!assistant) {
44+
throw redirect(302, `${base}/assistant/${params.assistantId}`);
45+
}
46+
47+
return { ...data, isReported };
48+
}
49+
2450
export const actions: Actions = {
2551
delete: async ({ params, locals }) => {
2652
let assistant;

src/routes/settings/assistants/[assistantId]/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<CarbonCopy class="mr-1.5 inline text-xs" />Duplicate</button
108108
>
109109
</form>
110-
{#if !assistant?.reported}
110+
{#if !data.isReported}
111111
<button
112112
type="button"
113113
on:click={() => {

src/routes/settings/assistants/[assistantId]/+page.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)