diff --git a/src/app/api/v1/bulk/webhook/route.ts b/src/app/api/v1/bulk/webhook/route.ts index c90a4812..e94ac9f5 100644 --- a/src/app/api/v1/bulk/webhook/route.ts +++ b/src/app/api/v1/bulk/webhook/route.ts @@ -5,7 +5,7 @@ import { removeSensitiveData } from "@/util/api"; import { Tables } from "@/supabase/database.types"; export interface WebhookExtra { - bulkEmailId: string; + bulkEmailId: number; userId: string; endpoint: string; } @@ -38,20 +38,12 @@ export const POST = async (req: NextRequest): Promise => { is_reachable: output.is_reachable, verif_method: output.debug?.smtp?.verif_method?.type, result: removeSensitiveData(output), + bulk_email_id: extra.bulkEmailId, }) .select("*"); if (res1.error) { return Response.json(res1.error, res1); } - // Update bulk_emails table - const res2 = await supabaseAdmin - .from("bulk_emails") - .update({ call_id: res1.data[0].id }) - .eq("id", extra.bulkEmailId); - if (res2.error) { - return Response.json(res2.error, res2); - } - return Response.json({ message: "ok" }, { status: 200 }); }; diff --git a/src/pages/bulk.tsx b/src/pages/bulk.tsx index 0160ba67..117053ed 100644 --- a/src/pages/bulk.tsx +++ b/src/pages/bulk.tsx @@ -22,16 +22,12 @@ interface BulkProps { onVerified?(result: CheckEmailOutput): Promise; } -interface BulkJobWithEmails extends Tables<"bulk_jobs"> { - bulk_emails: Tables<"bulk_emails">[]; -} - export default function Bulk({ onVerified }: BulkProps): React.ReactElement { const { user, userDetails } = useUser(); const [emails, setEmails] = useState(""); const [loading, setLoading] = useState(false); - const [bulkJobs, setBulkJobs] = useState([]); + const [bulkJobs, setBulkJobs] = useState[]>([]); useEffect(() => { // This is a temporary redirect to the dashboard while I still work @@ -45,8 +41,8 @@ export default function Bulk({ onVerified }: BulkProps): React.ReactElement { setInterval(async () => { console.log("FETCHING BULK JOBS..."); const res = await supabase - .from("bulk_jobs") - .select(`*,bulk_emails(*)`); + .from>("bulk_jobs_info") + .select("*"); if (res.error) { sentryException(res.error); return; @@ -122,23 +118,14 @@ export default function Bulk({ onVerified }: BulkProps): React.ReactElement { - + + + +
- -
- ALLJOBS: - {bulkJobs.map((job) => ( -
- {job.id} -{" "} - { - job.bulk_emails.filter( - ({ call_id }) => !!call_id - ).length - } - /{job.bulk_emails.length} -
- ))} -
); diff --git a/src/supabase/database.types.ts b/src/supabase/database.types.ts index 2ecf5e8b..29326eae 100644 --- a/src/supabase/database.types.ts +++ b/src/supabase/database.types.ts @@ -12,21 +12,18 @@ export interface Database { bulk_emails: { Row: { bulk_job_id: number; - call_id: number | null; created_at: string | null; email: string; id: number; }; Insert: { bulk_job_id: number; - call_id?: number | null; created_at?: string | null; email: string; id?: number; }; Update: { bulk_job_id?: number; - call_id?: number | null; created_at?: string | null; email?: string; id?: number; @@ -40,11 +37,11 @@ export interface Database { referencedColumns: ["id"]; }, { - foreignKeyName: "bulk_emails_call_id_fkey"; - columns: ["call_id"]; + foreignKeyName: "bulk_emails_bulk_job_id_fkey"; + columns: ["bulk_job_id"]; isOneToOne: false; - referencedRelation: "calls"; - referencedColumns: ["id"]; + referencedRelation: "bulk_jobs_info"; + referencedColumns: ["job_id"]; } ]; }; @@ -81,6 +78,7 @@ export interface Database { Row: { backend: string | null; backend_ip: string | null; + bulk_email_id: number | null; created_at: string | null; domain: string | null; duration: number | null; @@ -97,6 +95,7 @@ export interface Database { Insert: { backend?: string | null; backend_ip?: string | null; + bulk_email_id?: number | null; created_at?: string | null; domain?: string | null; duration?: number | null; @@ -113,6 +112,7 @@ export interface Database { Update: { backend?: string | null; backend_ip?: string | null; + bulk_email_id?: number | null; created_at?: string | null; domain?: string | null; duration?: number | null; @@ -127,6 +127,13 @@ export interface Database { verification_id?: string; }; Relationships: [ + { + foreignKeyName: "calls_bulk_email_id_fkey"; + columns: ["bulk_email_id"]; + isOneToOne: false; + referencedRelation: "bulk_emails"; + referencedColumns: ["id"]; + }, { foreignKeyName: "calls_user_id_fkey"; columns: ["user_id"]; @@ -364,9 +371,26 @@ export interface Database { }; }; Views: { + bulk_jobs_info: { + Row: { + created_at: string | null; + job_id: number | null; + number_of_emails: number | null; + user_id: string | null; + verified: number | null; + }; + Relationships: [ + { + foreignKeyName: "bulk_jobs_user_id_fkey"; + columns: ["user_id"]; + isOneToOne: false; + referencedRelation: "users"; + referencedColumns: ["id"]; + } + ]; + }; sub_and_calls: { Row: { - api_token: string | null; current_period_end: string | null; current_period_start: string | null; number_of_calls: number | null; @@ -386,7 +410,16 @@ export interface Database { }; }; Functions: { - [_ in never]: never; + bulk_job_info: { + Args: { + job_id: number; + }; + Returns: { + number_of_email: number; + verified: number; + created_at: string; + }[]; + }; }; Enums: { is_reachable_type: "safe" | "invalid" | "risky" | "unknown"; diff --git a/supabase/migrations/20231212135226_bulk.sql b/supabase/migrations/20231212135226_bulk.sql index 232fed4d..d773b2b8 100644 --- a/supabase/migrations/20231212135226_bulk.sql +++ b/supabase/migrations/20231212135226_bulk.sql @@ -14,7 +14,6 @@ CREATE POLICY "Can only view own bulk jobs." CREATE TABLE bulk_emails ( id SERIAL NOT NULL PRIMARY KEY, bulk_job_id INTEGER NOT NULL REFERENCES bulk_jobs (id), - call_id INTEGER REFERENCES calls (id), email TEXT NOT NULL, created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) ); @@ -32,3 +31,5 @@ CREATE POLICY "Can only view own bulk emails." AND auth.uid() = bulk_jobs.user_id ) ); + +ALTER TABLE calls ADD COLUMN bulk_email_id INTEGER REFERENCES bulk_emails (id); \ No newline at end of file diff --git a/supabase/migrations/20231218194337_bulk_jobs_info.sql b/supabase/migrations/20231218194337_bulk_jobs_info.sql new file mode 100644 index 00000000..9d073e60 --- /dev/null +++ b/supabase/migrations/20231218194337_bulk_jobs_info.sql @@ -0,0 +1,18 @@ +CREATE VIEW bulk_jobs_info AS + SELECT + bj.id AS job_id, + bj.user_id, + bj.created_at, + COUNT(DISTINCT be.id) AS number_of_emails, + COUNT(DISTINCT CASE WHEN c.id IS NOT NULL THEN be.id ELSE NULL END) AS verified + FROM + bulk_jobs bj + LEFT JOIN + bulk_emails be ON bj.id = be.bulk_job_id + LEFT JOIN + calls c ON be.id = c.bulk_email_id + GROUP BY + bj.id + ORDER BY + bj.created_at DESC; +