Skip to content

Commit

Permalink
Use authorizedPrismaClient for keycloak and email (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
Macludde authored Aug 7, 2024
1 parent 6ace704 commit 4938ab8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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({
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 11 additions & 8 deletions src/routes/(app)/api/mail/alias/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Map<string, string[]>>();

// Fetch all positions which have an alias (could be multiple aliases for a position)
const posToAlias: Map<string, EmailAlias[]> =
await getAliasToPositions(prisma);
const posToAlias: Map<string, EmailAlias[]> = await getAliasToPositions(
authorizedPrismaClient,
);

const positionIds = new Set<string>(
Array.from(posToAlias.values()).flatMap((alist) =>
Expand All @@ -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));
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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",
},
Expand Down
11 changes: 7 additions & 4 deletions src/routes/(app)/api/mail/alias/senders/+server.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,10 +17,12 @@ import { getCurrentMembersForPosition } from "../utils";
* [email protected] 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)) {
Expand Down

0 comments on commit 4938ab8

Please sign in to comment.