diff --git a/package.json b/package.json index 884ba655..e3643e36 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "dev": "next dev", "build": "next build", "gen:license": "ts-node -P scripts/tsconfig.json scripts/license.ts", - "lint": "tsc --noEmit && prettier -w */**/*.{ts,tsx}", + "lint": "tsc --noEmit && prettier -w */**/*.{ts,tsx} && next lint", "start": "next start" }, "dependencies": { diff --git a/src/pages/api/v0/check_email.ts b/src/pages/api/v0/check_email.ts index ee5f4e3d..30221cb6 100644 --- a/src/pages/api/v0/check_email.ts +++ b/src/pages/api/v0/check_email.ts @@ -28,7 +28,7 @@ const POST = async ( return; } - const { userId, sentResponse } = await checkUserInDB(req, res); + const { user, sentResponse } = await checkUserInDB(req, res); if (sentResponse) { return; } @@ -77,7 +77,7 @@ const POST = async ( .from("calls") .insert({ endpoint: "/v0/check_email", - user_id: userId, + user_id: user.id, backend: output.debug?.server_name, domain: output.syntax.domain, verification_id: verificationId, @@ -100,7 +100,10 @@ const POST = async ( // Cleanup await Promise.all([ - updateSendinblue(userId).then(() => { + updateSendinblue( + user.id, + user.sendinblue_contact_id + ).then(() => { const d8 = performance.now() - startTime; console.log( `[🐢] updateSendinblue: ${Math.round(d8)}ms` @@ -115,7 +118,7 @@ const POST = async ( `[🐢] ch1.close: ${Math.round(d7)}ms` ); }), - ]); + ]).catch(sentryException); const d9 = performance.now() - startTime; console.log(`[🐢] Final response: ${Math.round(d9)}ms`); diff --git a/src/util/api.ts b/src/util/api.ts index 1f59fab0..3949a0ab 100644 --- a/src/util/api.ts +++ b/src/util/api.ts @@ -4,7 +4,7 @@ import { NextApiRequest, NextApiResponse } from "next"; import { RateLimiterRes } from "rate-limiter-flexible"; import { subApiMaxCalls } from "./subs"; -import { SupabaseSubscription } from "./supabaseClient"; +import { SupabaseSubscription, SupabaseUser } from "./supabaseClient"; import { supabaseAdmin } from "./supabaseServer"; // Helper method to wait for a middleware to execute before continuing @@ -41,12 +41,12 @@ export const cors = initMiddleware( type CheckUserReturnType = | { - userId?: undefined; + user?: undefined; subAndCalls?: undefined; sentResponse: true; } | { - userId: string; + user: SupabaseUser; subAndCalls: SubAndCalls; sentResponse: false; }; @@ -71,7 +71,7 @@ export async function checkUserInDB( } const { data, error } = await supabaseAdmin - .from("sub_and_calls") + .from("users") .select("*") .eq("api_token", token); if (error) { @@ -81,8 +81,18 @@ export async function checkUserInDB( res.status(401).json({ error: "Invalid API token." }); return { sentResponse: true }; } + const user = data[0]; + + const res2 = await supabaseAdmin + .from("sub_and_calls") + .select("*") + .eq("user_id", user.id) + .single(); + if (res2.error) { + throw res2.error; + } - const subAndCalls = data[0]; + const subAndCalls = res2.data; // Set rate limit headers. const now = new Date(); @@ -118,7 +128,7 @@ export async function checkUserInDB( return { sentResponse: true }; } - return { userId: subAndCalls.user_id, subAndCalls, sentResponse: false }; + return { user, subAndCalls, sentResponse: false }; } interface SubAndCalls { diff --git a/src/util/sendinblue.ts b/src/util/sendinblue.ts index 2f131553..82641d68 100644 --- a/src/util/sendinblue.ts +++ b/src/util/sendinblue.ts @@ -2,8 +2,6 @@ import { ContactsApi, ContactsApiApiKeys } from "@sendinblue/client"; import { format } from "date-fns"; import { sentryException } from "./sentry"; -import { SupabaseUser } from "./supabaseClient"; -import { supabaseAdmin } from "./supabaseServer"; export const sendinblueApi = new ContactsApi(); @@ -26,26 +24,14 @@ export async function updateSendinblue( userId: string, sendinblueContactId?: string ): Promise { - let sibId = sendinblueContactId; - if (!sibId) { - const res = await supabaseAdmin - .from("users") - .select("sendinblue_contact_id") - .eq("id", userId); - if (res.error) { - throw res.error; - } - if (!res.data?.length || !res.data[0].sendinblue_contact_id) { - throw new Error( - `User ${userId} does not have a sendinblue_contact_id.` - ); - } - - sibId = res.data[0].sendinblue_contact_id; + if (!sendinblueContactId) { + throw new Error( + `User ${userId} does not have a sendinblue_contact_id.` + ); } return sendinblueApi - .updateContact(sibId, { + .updateContact(sendinblueContactId, { attributes: { SUPABASE_UUID: userId, // This should be set already, but we re-set it just in case. LAST_API_CALL: sendinblueDateFormat(new Date()),