Skip to content

Commit 72f6e78

Browse files
authored
Merge pull request #1625 from mfts/fix/emails
feat: add brand logo to cache for faster retrieval
2 parents 14b1106 + d9bfec8 commit 72f6e78

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

lib/emails/send-email-otp-verification.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getCustomEmail } from "@/lib/edge-config/custom-email";
22
import prisma from "@/lib/prisma";
3+
import { redis } from "@/lib/redis";
34
import { sendEmail } from "@/lib/resend";
45

56
import OtpEmailVerification from "@/components/emails/otp-verification";
@@ -10,26 +11,21 @@ export const sendOtpVerificationEmail = async (
1011
isDataroom: boolean = false,
1112
teamId: string,
1213
) => {
13-
let brand: { logo: string | null } | null = null;
14+
let logo: string | null = null;
1415
let from: string | undefined;
1516

1617
const customEmail = await getCustomEmail(teamId);
1718

1819
if (customEmail && teamId) {
1920
from = customEmail;
20-
21-
// Get brand logo if we have a custom email
22-
brand = await prisma.brand.findUnique({
23-
where: { id: teamId },
24-
select: { logo: true },
25-
});
21+
logo = await redis.get(`brand:logo:${teamId}`);
2622
}
2723

2824
const emailTemplate = OtpEmailVerification({
2925
email,
3026
code,
3127
isDataroom,
32-
logo: brand?.logo ?? undefined,
28+
logo: logo ?? undefined,
3329
});
3430

3531
try {

pages/api/teams/[teamId]/branding.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getServerSession } from "next-auth";
55

66
import { errorhandler } from "@/lib/errorHandler";
77
import prisma from "@/lib/prisma";
8+
import { redis } from "@/lib/redis";
89
import { CustomUser } from "@/lib/types";
910

1011
import { authOptions } from "../../auth/[...nextauth]";
@@ -74,6 +75,11 @@ export default async function handle(
7475
},
7576
});
7677

78+
// Cache the logo URL in Redis if logo exists
79+
if (logo) {
80+
await redis.set(`brand:logo:${teamId}`, logo);
81+
}
82+
7783
return res.status(200).json(brand);
7884
} else if (req.method === "PUT") {
7985
// PUT /api/teams/:teamId/branding
@@ -94,6 +100,14 @@ export default async function handle(
94100
},
95101
});
96102

103+
// Update logo in Redis cache
104+
if (logo) {
105+
await redis.set(`brand:logo:${teamId}`, logo);
106+
} else {
107+
// If logo is null or undefined, delete the cache
108+
await redis.del(`brand:logo:${teamId}`);
109+
}
110+
97111
return res.status(200).json(brand);
98112
} else if (req.method === "DELETE") {
99113
// DELETE /api/teams/:teamId/branding
@@ -116,6 +130,9 @@ export default async function handle(
116130
},
117131
});
118132

133+
// Remove logo from Redis cache
134+
await redis.del(`brand:logo:${teamId}`);
135+
119136
return res.status(204).end();
120137
} else {
121138
// We only allow GET and DELETE requests

pages/api/teams/[teamId]/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { removeDomainFromVercelProject } from "@/lib/domains";
99
import { errorhandler } from "@/lib/errorHandler";
1010
import { deleteFiles } from "@/lib/files/delete-team-files-server";
1111
import prisma from "@/lib/prisma";
12+
import { redis } from "@/lib/redis";
1213
import { CustomUser } from "@/lib/types";
1314
import { unsubscribe } from "@/lib/unsend";
1415

@@ -225,6 +226,8 @@ export default async function handle(
225226
id: (session.user as CustomUser).id,
226227
},
227228
}),
229+
// delete team branding from redis
230+
redis.del(`brand:logo:${teamId}`),
228231
// delete team
229232
prisma.team.delete({
230233
where: {

0 commit comments

Comments
 (0)