From 8b9d81ab2a4ac5deac08a29a9524471ac13de18e Mon Sep 17 00:00:00 2001 From: KMKoushik Date: Sat, 8 Mar 2025 23:04:19 +1100 Subject: [PATCH] fix filter stuff --- .../src/app/(dashboard)/emails/email-list.tsx | 56 +++++++---------- .../public-api/api/emails/send-email.ts | 61 +++++++++++-------- apps/web/src/server/public-api/auth.ts | 2 +- apps/web/src/server/service/email-service.ts | 4 +- 4 files changed, 62 insertions(+), 61 deletions(-) diff --git a/apps/web/src/app/(dashboard)/emails/email-list.tsx b/apps/web/src/app/(dashboard)/emails/email-list.tsx index e9b1c0f..43ec60c 100644 --- a/apps/web/src/app/(dashboard)/emails/email-list.tsx +++ b/apps/web/src/app/(dashboard)/emails/email-list.tsx @@ -45,12 +45,12 @@ import { useState } from "react"; /* Stupid hydrating error. And I so stupid to understand the stupid NextJS docs */ const DynamicSheetWithNoSSR = dynamic( () => import("@unsend/ui/src/sheet").then((mod) => mod.Sheet), - { ssr: false }, + { ssr: false } ); const DynamicSheetContentWithNoSSR = dynamic( () => import("@unsend/ui/src/sheet").then((mod) => mod.SheetContent), - { ssr: false }, + { ssr: false } ); export default function EmailsList() { @@ -58,14 +58,12 @@ export default function EmailsList() { const [page, setPage] = useUrlState("page", "1"); const [status, setStatus] = useUrlState("status"); const [search, setSearch] = useUrlState("search"); - const [domainName, setDomainName] = useUrlState("domain"); - const [apiKeyName, setApiKeyName] = useUrlState("apikey"); - const [domain, setDomain] = useState(null); - const [apikey, setApikey] = useState(null); + const [domain, setDomain] = useUrlState("domain"); + const [apiKey, setApiKey] = useUrlState("apikey"); const pageNumber = Number(page); - const domainId = Number(domain); - const apiId = Number(apikey); + const domainId = domain ? Number(domain) : undefined; + const apiId = apiKey ? Number(apiKey) : undefined; const emailsQuery = api.email.emails.useQuery({ page: pageNumber, @@ -82,20 +80,12 @@ export default function EmailsList() { setSelectedEmail(emailId); }; - const handleDomainName = (val: string) => { + const handleDomain = (val: string) => { setDomain(val === "All Domain" ? null : val); - const nameOfDomain = domainsQuery?.find( - (item) => item.id.toString() === val, - ); - setDomainName(nameOfDomain?.name || "All Domain"); }; - const handleApiKeyName = (val: string) => { - setApikey(val === "All ApiKey" ? null : val); - const nameOfApiKey = apiKeysQuery?.find( - (item) => item.id.toString() === val, - ); - setApiKeyName(nameOfApiKey?.name || "All ApiKey"); + const handleApiKey = (val: string) => { + setApiKey(val === "All ApiKey" ? null : val); }; const handleSheetChange = (isOpen: boolean) => { @@ -119,11 +109,14 @@ export default function EmailsList() { />
@@ -243,7 +229,7 @@ export default function EmailsList() { Scheduled at{" "} {formatDate( email.scheduledAt, - "MMM dd'th', hh:mm a", + "MMM dd'th', hh:mm a" )} @@ -259,7 +245,7 @@ export default function EmailsList() { {email.latestStatus !== "SCHEDULED" ? formatDate( email.scheduledAt ?? email.createdAt, - "MMM do, hh:mm a", + "MMM do, hh:mm a" ) : "--"} diff --git a/apps/web/src/server/public-api/api/emails/send-email.ts b/apps/web/src/server/public-api/api/emails/send-email.ts index 60cfe1e..4d67eaf 100644 --- a/apps/web/src/server/public-api/api/emails/send-email.ts +++ b/apps/web/src/server/public-api/api/emails/send-email.ts @@ -11,30 +11,42 @@ const route = createRoute({ required: true, content: { "application/json": { - schema: z.object({ - to: z.string().or(z.array(z.string())), - from: z.string(), - subject: z.string().optional().openapi({ description: 'Optional when templateId is provided' }), - templateId: z.string().optional().openapi({ description: 'ID of a template from the dashboard' }), - variables: z.record(z.string()).optional(), - replyTo: z.string().or(z.array(z.string())).optional(), - cc: z.string().or(z.array(z.string())).optional(), - bcc: z.string().or(z.array(z.string())).optional(), - text: z.string().optional(), - html: z.string().optional(), - attachments: z - .array( - z.object({ - filename: z.string(), - content: z.string(), - }) - ) - .optional(), - scheduledAt: z.string().datetime().optional(), - }).refine( - data => !!data.subject || !!data.templateId, - 'Either subject or templateId should be passed.', - ), + schema: z + .object({ + to: z.string().or(z.array(z.string())), + from: z.string(), + subject: z + .string() + .optional() + .openapi({ + description: "Optional when templateId is provided", + }), + templateId: z + .string() + .optional() + .openapi({ + description: "ID of a template from the dashboard", + }), + variables: z.record(z.string()).optional(), + replyTo: z.string().or(z.array(z.string())).optional(), + cc: z.string().or(z.array(z.string())).optional(), + bcc: z.string().or(z.array(z.string())).optional(), + text: z.string().optional(), + html: z.string().optional(), + attachments: z + .array( + z.object({ + filename: z.string(), + content: z.string(), + }) + ) + .optional(), + scheduledAt: z.string().datetime().optional(), + }) + .refine( + (data) => !!data.subject || !!data.templateId, + "Either subject or templateId should be passed." + ), }, }, }, @@ -58,6 +70,7 @@ function send(app: PublicAPIApp) { const email = await sendEmail({ ...c.req.valid("json"), teamId: team.id, + apiKeyId: team.apiKeyId, }); return c.json({ emailId: email?.id }); diff --git a/apps/web/src/server/public-api/auth.ts b/apps/web/src/server/public-api/auth.ts index 6e2347d..e86d7e2 100644 --- a/apps/web/src/server/public-api/auth.ts +++ b/apps/web/src/server/public-api/auth.ts @@ -64,7 +64,7 @@ export const getTeamFromToken = async (c: Context) => { }) .catch(console.error); - return team; + return { ...team, apiKeyId: apiKey.id }; }; const checkRateLimit = (token: string) => { diff --git a/apps/web/src/server/service/email-service.ts b/apps/web/src/server/service/email-service.ts index 3cea342..a2d9104 100644 --- a/apps/web/src/server/service/email-service.ts +++ b/apps/web/src/server/service/email-service.ts @@ -46,7 +46,7 @@ export const replaceVariables = ( Send transactional email */ export async function sendEmail( - emailContent: EmailContent & { teamId: number } + emailContent: EmailContent & { teamId: number; apiKeyId?: number } ) { const { to, @@ -62,6 +62,7 @@ export async function sendEmail( cc, bcc, scheduledAt, + apiKeyId, } = emailContent; let subject = subjectFromApiCall; let html = htmlFromApiCall; @@ -122,6 +123,7 @@ export async function sendEmail( attachments: attachments ? JSON.stringify(attachments) : undefined, scheduledAt: scheduledAtDate, latestStatus: scheduledAtDate ? "SCHEDULED" : "QUEUED", + apiId: apiKeyId, }, });