From 6bc739791176c80b6dc83307594724f527b34018 Mon Sep 17 00:00:00 2001 From: derGraph Date: Wed, 4 Dec 2024 22:50:37 +0100 Subject: [PATCH] add user filter to trips --- src/routes/api/Trips/+server.ts | 83 +++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/src/routes/api/Trips/+server.ts b/src/routes/api/Trips/+server.ts index 5c10613..0ed422f 100644 --- a/src/routes/api/Trips/+server.ts +++ b/src/routes/api/Trips/+server.ts @@ -25,40 +25,74 @@ export async function GET(event: { locals: { user: { username: any}} }) { let deleted = false; + let username: string|null = null; if(event.url.searchParams.get("deleted")=="true"){ deleted = true; } + username = event.url.searchParams.get("username"); try{ let responseData; if(event.locals.user){ - responseData = await prisma.trip.findMany({ - where:{ - deleted: deleted, - OR: [{ - crew: { - some: { + if(username == null){ + responseData = await prisma.trip.findMany({ + where:{ + deleted: deleted, + OR: [{ + crew: { + some: { + username: event.locals.user.username + } + } + }, + { + skipper: { username: event.locals.user.username } - } - }, - { - skipper: { - username: event.locals.user.username - } + }, + { + visibility: 1 + }, + { + visibility: 2 + }] }, - { - visibility: 1 + include:{ + crew: true, + startPoint: true, + endPoint: true + } + }); + }else{ + responseData = await prisma.trip.findMany({ + where:{ + deleted: deleted, + OR: [ + { + visibility: 1, + ...(username ? { + OR: [ + {crew: {some: {username: username}}}, + {skipper: {username: username}}, + ], + } : {}) + }, + { + visibility: 2, + ...(username ? { + OR: [ + {crew: {some: {username: username}}}, + {skipper: {username: username}}, + ], + } : {}) + }] }, - { - visibility: 2 - }] - }, - include:{ - crew: true, - startPoint: true, - endPoint: true - } - }); + include:{ + crew: true, + startPoint: true, + endPoint: true + } + }); + } }else{ responseData = await prisma.trip.findMany({ where: { @@ -76,6 +110,7 @@ export async function GET(event: { return new Response(JSON.stringify(responseData)); } catch (error_message) { if (error_message instanceof Error) { + console.log(error_message) error(404, { message: error_message.message });