From bd3bd9a041ea74d5e5f9d6f7549f79e7818ae8ae Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Thu, 5 Jun 2025 14:20:30 -0400 Subject: [PATCH] don't use drizzle in the read path of leaflet page --- actions/getRSVPData.ts | 81 +++++++++++++++----------------- actions/pollActions.ts | 104 ++++++++++++++++++++--------------------- 2 files changed, 88 insertions(+), 97 deletions(-) diff --git a/actions/getRSVPData.ts b/actions/getRSVPData.ts index 811cc2cb..a00b3576 100644 --- a/actions/getRSVPData.ts +++ b/actions/getRSVPData.ts @@ -1,21 +1,11 @@ "use server"; -import { drizzle } from "drizzle-orm/postgres-js"; -import { and, eq, inArray } from "drizzle-orm"; -import postgres from "postgres"; -import { - entities, - phone_number_auth_tokens, - phone_rsvps_to_entity, -} from "drizzle/schema"; import { cookies } from "next/headers"; +import { supabaseServerClient } from "supabase/serverClient"; export async function getRSVPData(entity_sets: string[]) { const token = (await cookies()).get("phone_auth_token"); - const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); - const db = drizzle(client); - let authToken: { id: string; created_at: string; @@ -25,42 +15,47 @@ export async function getRSVPData(entity_sets: string[]) { country_code: string; } | null = null; if (token) { - const data = await db - .select() - .from(phone_number_auth_tokens) - .where(eq(phone_number_auth_tokens.id, token.value)); - authToken = data[0]; + let { data } = await supabaseServerClient + .from("phone_number_auth_tokens") + .select("*") + .eq("id", token.value) + .single(); + authToken = data; } - const rsvps = await db - .select() - .from(phone_rsvps_to_entity) - .innerJoin(entities, eq(entities.id, phone_rsvps_to_entity.entity)) - .where(and(inArray(entities.set, entity_sets))); + const { data: rsvps } = await supabaseServerClient + .from("phone_rsvps_to_entity") + .select( + ` + *, + entities!inner(*) + `, + ) + .in("entities.set", entity_sets); - client.end(); return { authToken, - rsvps: rsvps.map((rsvp) => { - if ( - rsvp.phone_rsvps_to_entity.phone_number === authToken?.phone_number && - rsvp.phone_rsvps_to_entity.country_code === authToken.country_code - ) - return { - phone_number: rsvp.phone_rsvps_to_entity.phone_number, - country_code: rsvp.phone_rsvps_to_entity.country_code, - name: rsvp.phone_rsvps_to_entity.name, - entity: rsvp.entities.id, - status: rsvp.phone_rsvps_to_entity.status, - plus_ones: rsvp.phone_rsvps_to_entity.plus_ones, - }; - else - return { - name: rsvp.phone_rsvps_to_entity.name, - entity: rsvp.entities.id, - status: rsvp.phone_rsvps_to_entity.status, - plus_ones: rsvp.phone_rsvps_to_entity.plus_ones, - }; - }), + rsvps: + rsvps?.map((rsvp) => { + if ( + rsvp.phone_number === authToken?.phone_number && + rsvp.country_code === authToken.country_code + ) + return { + phone_number: rsvp.phone_number, + country_code: rsvp.country_code, + name: rsvp.name, + entity: rsvp.entities.id, + status: rsvp.status, + plus_ones: rsvp.plus_ones, + }; + else + return { + name: rsvp.name, + entity: rsvp.entities.id, + status: rsvp.status, + plus_ones: rsvp.plus_ones, + }; + }) || [], }; } diff --git a/actions/pollActions.ts b/actions/pollActions.ts index dc53038e..a3ceb5af 100644 --- a/actions/pollActions.ts +++ b/actions/pollActions.ts @@ -1,28 +1,25 @@ "use server"; -import { drizzle } from "drizzle-orm/postgres-js"; -import { and, eq, inArray } from "drizzle-orm"; -import postgres from "postgres"; -import { entities, poll_votes_on_entity } from "drizzle/schema"; import { cookies } from "next/headers"; import { v7 } from "uuid"; import { getIdentityData } from "./getIdentityData"; +import { supabaseServerClient } from "supabase/serverClient"; export async function getPollData(entity_sets: string[]) { - const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); let voter_token = (await cookies()).get("poll_voter_token")?.value; - const db = drizzle(client); - const polls = await db - .select({ - poll_votes_on_entity: { - poll_entity: poll_votes_on_entity.poll_entity, - voter_token: poll_votes_on_entity.voter_token, - option_entity: poll_votes_on_entity.option_entity, - }, - }) - .from(poll_votes_on_entity) - .innerJoin(entities, eq(entities.id, poll_votes_on_entity.poll_entity)) - .where(and(inArray(entities.set, entity_sets))); + const { data: polls, error } = await supabaseServerClient + .from("poll_votes_on_entity") + .select( + ` + poll_entity, + voter_token, + option_entity, + entities!inner(set) + `, + ) + .in("entities.set", entity_sets); + + if (error) throw error; let pollVotes = polls .reduce< @@ -31,15 +28,13 @@ export async function getPollData(entity_sets: string[]) { votes: { option_entity: string; voter_token: string }[]; }> >((acc, p) => { - let x = acc.find( - (a) => a.poll_entity === p.poll_votes_on_entity.poll_entity, - ); + let x = acc.find((a) => a.poll_entity === p.poll_entity); if (!x) acc.push({ - poll_entity: p.poll_votes_on_entity.poll_entity, - votes: [p.poll_votes_on_entity], + poll_entity: p.poll_entity, + votes: [p], }); - else x.votes.push(p.poll_votes_on_entity); + else x.votes.push(p); return acc; }, []) .map((poll) => { @@ -63,11 +58,8 @@ export async function getPollData(entity_sets: string[]) { pollVotes, polls: polls.map((p) => ({ poll_votes_on_entity: { - ...p.poll_votes_on_entity, - voter_token: - voter_token === p.poll_votes_on_entity.voter_token - ? voter_token - : undefined, + ...p, + voter_token: voter_token === p.voter_token ? voter_token : undefined, }, })), voter_token, @@ -90,34 +82,38 @@ export async function voteOnPoll( sameSite: "lax", }); } - const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); - const db = drizzle(client); + const { data: existingVotes, error: selectError } = await supabaseServerClient + .from("poll_votes_on_entity") + .select("*") + .eq("poll_entity", poll_entity); + + if (selectError) throw selectError; + + const existingVote = existingVotes?.find( + (v) => v.voter_token === voter_token, + ); - const pollVote = await db - .select() - .from(poll_votes_on_entity) - .where(eq(poll_votes_on_entity.poll_entity, poll_entity)); + if (existingVote) { + const { error: deleteError } = await supabaseServerClient + .from("poll_votes_on_entity") + .delete() + .eq("voter_token", voter_token) + .eq("poll_entity", poll_entity); - if ( - pollVote.find((v) => { - return v.voter_token === voter_token; - }) - ) { - await db - .delete(poll_votes_on_entity) - .where( - and( - eq(poll_votes_on_entity.voter_token, voter_token), - eq(poll_votes_on_entity.poll_entity, poll_entity), - ), - ); + if (deleteError) throw deleteError; } - await db.insert(poll_votes_on_entity).values( - option_entities.map((option_entity) => ({ - option_entity, - poll_entity, - voter_token, - })), - ); + const { error: insertError } = await supabaseServerClient + .from("poll_votes_on_entity") + .insert( + option_entities.map((option_entity) => ({ + option_entity, + poll_entity, + voter_token, + })), + ); + + if (insertError) throw insertError; + + return; } -- 2.51.2