From 1ae6257c11711ed0efb7793932bc7d4eee09b5ad Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Wed, 10 Sep 2025 22:36:39 +1000 Subject: [PATCH] fix --- apps/web/prisma/schema.prisma | 2 +- .../dev-settings/api-keys/add-api-key.tsx | 30 ++++++++++++------- .../server/public-api/api/emails/get-email.ts | 15 ++++------ .../public-api/api/emails/update-email.ts | 6 +++- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma index 7c4925f..a1a29d0 100644 --- a/apps/web/prisma/schema.prisma +++ b/apps/web/prisma/schema.prisma @@ -216,7 +216,7 @@ model ApiKey { lastUsed DateTime? teamId Int team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) - domain Domain? @relation(fields: [domainId], references: [id], onDelete: Cascade) + domain Domain? @relation(fields: [domainId], references: [id], onDelete: SetNull, onUpdate: Cascade) } enum EmailStatus { diff --git a/apps/web/src/app/(dashboard)/dev-settings/api-keys/add-api-key.tsx b/apps/web/src/app/(dashboard)/dev-settings/api-keys/add-api-key.tsx index 098e10d..e3728c2 100644 --- a/apps/web/src/app/(dashboard)/dev-settings/api-keys/add-api-key.tsx +++ b/apps/web/src/app/(dashboard)/dev-settings/api-keys/add-api-key.tsx @@ -33,8 +33,7 @@ import { SelectItem, SelectTrigger, SelectValue, -} from "@unsend/ui/src/select"; - +} from "@usesend/ui/src/select"; const apiKeySchema = z.object({ name: z.string({ required_error: "Name is required" }).min(1, { @@ -49,7 +48,7 @@ export default function AddApiKey() { const createApiKeyMutation = api.apiKey.createToken.useMutation(); const [isCopied, setIsCopied] = useState(false); const [showApiKey, setShowApiKey] = useState(false); - + const domainsQuery = api.domain.domains.useQuery(); const utils = api.useUtils(); @@ -67,7 +66,8 @@ export default function AddApiKey() { { name: values.name, permission: "FULL", - domainId: values.domainId === "all" ? undefined : Number(values.domainId), + domainId: + values.domainId === "all" ? undefined : Number(values.domainId), }, { onSuccess: (data) => { @@ -75,7 +75,7 @@ export default function AddApiKey() { setApiKey(data); apiKeyForm.reset(); }, - }, + } ); } @@ -199,7 +199,10 @@ export default function AddApiKey() { render={({ field }) => ( Domain access - @@ -207,11 +210,16 @@ export default function AddApiKey() { All Domains - {domainsQuery.data?.map((domain: { id: number; name: string }) => ( - - {domain.name} - - ))} + {domainsQuery.data?.map( + (domain: { id: number; name: string }) => ( + + {domain.name} + + ) + )} diff --git a/apps/web/src/server/public-api/api/emails/get-email.ts b/apps/web/src/server/public-api/api/emails/get-email.ts index 0301ed6..a29ebb1 100644 --- a/apps/web/src/server/public-api/api/emails/get-email.ts +++ b/apps/web/src/server/public-api/api/emails/get-email.ts @@ -60,17 +60,12 @@ function send(app: PublicAPIApp) { const team = c.var.team; const emailId = c.req.param("emailId"); - const whereClause: { id: string; teamId: number; domainId?: number } = { - id: emailId, - teamId: team.id, - }; - - if (team.apiKey.domainId !== null) { - whereClause.domainId = team.apiKey.domainId; - } - const email = await db.email.findUnique({ - where: whereClause, + where: { + id: emailId, + teamId: team.id, + domainId: team.apiKey.domainId ?? undefined, + }, select: { id: true, teamId: true, diff --git a/apps/web/src/server/public-api/api/emails/update-email.ts b/apps/web/src/server/public-api/api/emails/update-email.ts index 61afd2f..f2bd532 100644 --- a/apps/web/src/server/public-api/api/emails/update-email.ts +++ b/apps/web/src/server/public-api/api/emails/update-email.ts @@ -48,7 +48,11 @@ function updateEmailScheduledAt(app: PublicAPIApp) { const team = c.var.team; const emailId = c.req.param("emailId"); - await checkIsValidEmailIdWithDomainRestriction(emailId, team.id, team.apiKey.domainId); + await checkIsValidEmailIdWithDomainRestriction( + emailId, + team.id, + team.apiKey.domainId ?? undefined + ); await updateEmail(emailId, { scheduledAt: c.req.valid("json").scheduledAt,