Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New bulk schema #475

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/app/api/v1/bulk/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -38,20 +38,12 @@ export const POST = async (req: NextRequest): Promise<Response> => {
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 });
};
33 changes: 10 additions & 23 deletions src/pages/bulk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ interface BulkProps {
onVerified?(result: CheckEmailOutput): Promise<void>;
}

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<BulkJobWithEmails[]>([]);
const [bulkJobs, setBulkJobs] = useState<Tables<"bulk_jobs_info">[]>([]);

useEffect(() => {
// This is a temporary redirect to the dashboard while I still work
Expand All @@ -45,8 +41,8 @@ export default function Bulk({ onVerified }: BulkProps): React.ReactElement {
setInterval(async () => {
console.log("FETCHING BULK JOBS...");
const res = await supabase
.from<BulkJobWithEmails>("bulk_jobs")
.select(`*,bulk_emails(*)`);
.from<Tables<"bulk_jobs_info">>("bulk_jobs_info")
.select("*");
if (res.error) {
sentryException(res.error);
return;
Expand Down Expand Up @@ -122,23 +118,14 @@ export default function Bulk({ onVerified }: BulkProps): React.ReactElement {
<Spacer />

<Table data={bulkJobs}>
<Table.Column prop="id" label="ID" />
<Table.Column prop="job_id" label="job_id" />
<Table.Column prop="verified" label="Verified" />
<Table.Column
prop="number_of_emails"
label="Total emails"
/>
<Table.Column prop="created_at" label="Created At" />
</Table>

<div>
ALLJOBS:
{bulkJobs.map((job) => (
<div key={job.id}>
{job.id} -{" "}
{
job.bulk_emails.filter(
({ call_id }) => !!call_id
).length
}
/{job.bulk_emails.length}
</div>
))}
</div>
</Page>
</>
);
Expand Down
51 changes: 42 additions & 9 deletions src/supabase/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"];
}
];
};
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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"];
Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion supabase/migrations/20231212135226_bulk.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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())
);
Expand All @@ -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);
18 changes: 18 additions & 0 deletions supabase/migrations/20231218194337_bulk_jobs_info.sql
Original file line number Diff line number Diff line change
@@ -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;

Loading