From 4938ab88374cdd8c74701027f2ce71becaea9e94 Mon Sep 17 00:00:00 2001 From: Ludvig Svedberg Date: Wed, 7 Aug 2024 12:03:18 +0200 Subject: [PATCH] Use authorizedPrismaClient for keycloak and email (#426) --- src/hooks.server.ts | 7 +++++-- src/routes/(app)/api/mail/alias/+server.ts | 19 +++++++++++-------- .../(app)/api/mail/alias/senders/+server.ts | 11 +++++++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index dd9ff8f0f..ddd3e7cf9 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,5 +1,6 @@ import { env } from "$env/dynamic/private"; import keycloak from "$lib/server/keycloak"; +import authorizedPrismaClient from "$lib/server/shop/authorizedPrisma"; import { i18n } from "$lib/utils/i18n"; import { redirect } from "$lib/utils/redirect"; import { themes, type Theme } from "$lib/utils/themes"; @@ -15,8 +16,8 @@ import RPCApiHandler from "@zenstackhq/server/api/rpc"; import zenstack from "@zenstackhq/server/sveltekit"; import { randomBytes } from "crypto"; import schedule from "node-schedule"; -import translatedExtension from "./database/prisma/translationExtension"; import loggingExtension from "./database/prisma/loggingExtension"; +import translatedExtension from "./database/prisma/translationExtension"; import { getAccessPolicies } from "./hooks.server.helpers"; const { handle: authHandle } = SvelteKitAuth({ @@ -206,7 +207,9 @@ const themeHandle: Handle = async ({ event, resolve }) => { }); }; -schedule.scheduleJob("* */24 * * *", () => keycloak.sync(prismaClient)); +schedule.scheduleJob("* */24 * * *", () => + keycloak.sync(authorizedPrismaClient), +); export const handle = sequence( authHandle, diff --git a/src/routes/(app)/api/mail/alias/+server.ts b/src/routes/(app)/api/mail/alias/+server.ts index 7a1d37716..2b74d8970 100644 --- a/src/routes/(app)/api/mail/alias/+server.ts +++ b/src/routes/(app)/api/mail/alias/+server.ts @@ -6,18 +6,18 @@ import { getEmailsForManyMembers, getAliasToPositions, } from "./utils"; +import authorizedPrismaClient from "$lib/server/shop/authorizedPrisma"; -export const GET: RequestHandler = async ({ locals, setHeaders }) => { - const { prisma } = locals; - +export const GET: RequestHandler = async ({ setHeaders }) => { // This is the main data structure that we will use to create the response // It stores all the positions for a given alias, and the user emails for those positions // All code below is to fill this data structure const aliasToPosToUserEmails = new Map>(); // Fetch all positions which have an alias (could be multiple aliases for a position) - const posToAlias: Map = - await getAliasToPositions(prisma); + const posToAlias: Map = await getAliasToPositions( + authorizedPrismaClient, + ); const positionIds = new Set( Array.from(posToAlias.values()).flatMap((alist) => @@ -32,7 +32,10 @@ export const GET: RequestHandler = async ({ locals, setHeaders }) => { >(); for (const posId of positionIds) { // Fetch which members currently have a mandate for the position - const members = await getCurrentMembersForPosition(posId, prisma); + const members = await getCurrentMembersForPosition( + posId, + authorizedPrismaClient, + ); positionIdsToMembers.set(posId, new Set(members)); } @@ -48,7 +51,7 @@ export const GET: RequestHandler = async ({ locals, setHeaders }) => { // Fetches all the emails for the members from Keycloak const userToEmail = await getEmailsForManyMembers( allMembersWithPos.map((m) => m.memberId), - prisma, + authorizedPrismaClient, ); for (const [alias, positions] of posToAlias) { @@ -77,7 +80,7 @@ export const GET: RequestHandler = async ({ locals, setHeaders }) => { // Special receivers are stored in Prisma const specialReceivers = ( - await prisma.specialReceiver.findMany({ + await authorizedPrismaClient.specialReceiver.findMany({ orderBy: { email: "asc", }, diff --git a/src/routes/(app)/api/mail/alias/senders/+server.ts b/src/routes/(app)/api/mail/alias/senders/+server.ts index 6afe9155e..155463ae7 100644 --- a/src/routes/(app)/api/mail/alias/senders/+server.ts +++ b/src/routes/(app)/api/mail/alias/senders/+server.ts @@ -1,6 +1,7 @@ import type { PrismaClient } from "@prisma/client"; import type { RequestHandler } from "./$types"; import { getCurrentMembersForPosition } from "../utils"; +import authorizedPrismaClient from "$lib/server/shop/authorizedPrisma"; /** * Returns a text response where each line contains an email alias @@ -16,10 +17,12 @@ import { getCurrentMembersForPosition } from "../utils"; * styrelsen@dsek.se em5261ha-s, al4070an-s, le6853ha-s * ``` */ -export const GET: RequestHandler = async ({ locals, setHeaders }) => { - const { prisma } = locals; - const emailAddresses = await getAllEmailAddresses(prisma); - const emailToSenders = await getAllAliasSenders(prisma, emailAddresses); +export const GET: RequestHandler = async ({ setHeaders }) => { + const emailAddresses = await getAllEmailAddresses(authorizedPrismaClient); + const emailToSenders = await getAllAliasSenders( + authorizedPrismaClient, + emailAddresses, + ); const output: string[] = []; for (const [emailAddress, senders] of Object.entries(emailToSenders)) {