a tool for shared writing and social publishing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

don't use drizzle in the read path of leaflet page

+88 -97
+38 -43
actions/getRSVPData.ts
··· 1 1 "use server"; 2 2 3 - import { drizzle } from "drizzle-orm/postgres-js"; 4 - import { and, eq, inArray } from "drizzle-orm"; 5 - import postgres from "postgres"; 6 - import { 7 - entities, 8 - phone_number_auth_tokens, 9 - phone_rsvps_to_entity, 10 - } from "drizzle/schema"; 11 3 import { cookies } from "next/headers"; 4 + import { supabaseServerClient } from "supabase/serverClient"; 12 5 13 6 export async function getRSVPData(entity_sets: string[]) { 14 7 const token = (await cookies()).get("phone_auth_token"); 15 8 16 - const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); 17 - const db = drizzle(client); 18 - 19 9 let authToken: { 20 10 id: string; 21 11 created_at: string; ··· 25 15 country_code: string; 26 16 } | null = null; 27 17 if (token) { 28 - const data = await db 29 - .select() 30 - .from(phone_number_auth_tokens) 31 - .where(eq(phone_number_auth_tokens.id, token.value)); 32 - authToken = data[0]; 18 + let { data } = await supabaseServerClient 19 + .from("phone_number_auth_tokens") 20 + .select("*") 21 + .eq("id", token.value) 22 + .single(); 23 + authToken = data; 33 24 } 34 25 35 - const rsvps = await db 36 - .select() 37 - .from(phone_rsvps_to_entity) 38 - .innerJoin(entities, eq(entities.id, phone_rsvps_to_entity.entity)) 39 - .where(and(inArray(entities.set, entity_sets))); 26 + const { data: rsvps } = await supabaseServerClient 27 + .from("phone_rsvps_to_entity") 28 + .select( 29 + ` 30 + *, 31 + entities!inner(*) 32 + `, 33 + ) 34 + .in("entities.set", entity_sets); 40 35 41 - client.end(); 42 36 return { 43 37 authToken, 44 - rsvps: rsvps.map((rsvp) => { 45 - if ( 46 - rsvp.phone_rsvps_to_entity.phone_number === authToken?.phone_number && 47 - rsvp.phone_rsvps_to_entity.country_code === authToken.country_code 48 - ) 49 - return { 50 - phone_number: rsvp.phone_rsvps_to_entity.phone_number, 51 - country_code: rsvp.phone_rsvps_to_entity.country_code, 52 - name: rsvp.phone_rsvps_to_entity.name, 53 - entity: rsvp.entities.id, 54 - status: rsvp.phone_rsvps_to_entity.status, 55 - plus_ones: rsvp.phone_rsvps_to_entity.plus_ones, 56 - }; 57 - else 58 - return { 59 - name: rsvp.phone_rsvps_to_entity.name, 60 - entity: rsvp.entities.id, 61 - status: rsvp.phone_rsvps_to_entity.status, 62 - plus_ones: rsvp.phone_rsvps_to_entity.plus_ones, 63 - }; 64 - }), 38 + rsvps: 39 + rsvps?.map((rsvp) => { 40 + if ( 41 + rsvp.phone_number === authToken?.phone_number && 42 + rsvp.country_code === authToken.country_code 43 + ) 44 + return { 45 + phone_number: rsvp.phone_number, 46 + country_code: rsvp.country_code, 47 + name: rsvp.name, 48 + entity: rsvp.entities.id, 49 + status: rsvp.status, 50 + plus_ones: rsvp.plus_ones, 51 + }; 52 + else 53 + return { 54 + name: rsvp.name, 55 + entity: rsvp.entities.id, 56 + status: rsvp.status, 57 + plus_ones: rsvp.plus_ones, 58 + }; 59 + }) || [], 65 60 }; 66 61 }
+50 -54
actions/pollActions.ts
··· 1 1 "use server"; 2 - import { drizzle } from "drizzle-orm/postgres-js"; 3 - import { and, eq, inArray } from "drizzle-orm"; 4 - import postgres from "postgres"; 5 - import { entities, poll_votes_on_entity } from "drizzle/schema"; 6 2 import { cookies } from "next/headers"; 7 3 import { v7 } from "uuid"; 8 4 import { getIdentityData } from "./getIdentityData"; 5 + import { supabaseServerClient } from "supabase/serverClient"; 9 6 10 7 export async function getPollData(entity_sets: string[]) { 11 - const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); 12 8 let voter_token = (await cookies()).get("poll_voter_token")?.value; 13 9 14 - const db = drizzle(client); 15 - const polls = await db 16 - .select({ 17 - poll_votes_on_entity: { 18 - poll_entity: poll_votes_on_entity.poll_entity, 19 - voter_token: poll_votes_on_entity.voter_token, 20 - option_entity: poll_votes_on_entity.option_entity, 21 - }, 22 - }) 23 - .from(poll_votes_on_entity) 24 - .innerJoin(entities, eq(entities.id, poll_votes_on_entity.poll_entity)) 25 - .where(and(inArray(entities.set, entity_sets))); 10 + const { data: polls, error } = await supabaseServerClient 11 + .from("poll_votes_on_entity") 12 + .select( 13 + ` 14 + poll_entity, 15 + voter_token, 16 + option_entity, 17 + entities!inner(set) 18 + `, 19 + ) 20 + .in("entities.set", entity_sets); 21 + 22 + if (error) throw error; 26 23 27 24 let pollVotes = polls 28 25 .reduce< ··· 31 28 votes: { option_entity: string; voter_token: string }[]; 32 29 }> 33 30 >((acc, p) => { 34 - let x = acc.find( 35 - (a) => a.poll_entity === p.poll_votes_on_entity.poll_entity, 36 - ); 31 + let x = acc.find((a) => a.poll_entity === p.poll_entity); 37 32 if (!x) 38 33 acc.push({ 39 - poll_entity: p.poll_votes_on_entity.poll_entity, 40 - votes: [p.poll_votes_on_entity], 34 + poll_entity: p.poll_entity, 35 + votes: [p], 41 36 }); 42 - else x.votes.push(p.poll_votes_on_entity); 37 + else x.votes.push(p); 43 38 return acc; 44 39 }, []) 45 40 .map((poll) => { ··· 63 58 pollVotes, 64 59 polls: polls.map((p) => ({ 65 60 poll_votes_on_entity: { 66 - ...p.poll_votes_on_entity, 67 - voter_token: 68 - voter_token === p.poll_votes_on_entity.voter_token 69 - ? voter_token 70 - : undefined, 61 + ...p, 62 + voter_token: voter_token === p.voter_token ? voter_token : undefined, 71 63 }, 72 64 })), 73 65 voter_token, ··· 90 82 sameSite: "lax", 91 83 }); 92 84 } 93 - const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); 94 - const db = drizzle(client); 85 + const { data: existingVotes, error: selectError } = await supabaseServerClient 86 + .from("poll_votes_on_entity") 87 + .select("*") 88 + .eq("poll_entity", poll_entity); 95 89 96 - const pollVote = await db 97 - .select() 98 - .from(poll_votes_on_entity) 99 - .where(eq(poll_votes_on_entity.poll_entity, poll_entity)); 90 + if (selectError) throw selectError; 100 91 101 - if ( 102 - pollVote.find((v) => { 103 - return v.voter_token === voter_token; 104 - }) 105 - ) { 106 - await db 107 - .delete(poll_votes_on_entity) 108 - .where( 109 - and( 110 - eq(poll_votes_on_entity.voter_token, voter_token), 111 - eq(poll_votes_on_entity.poll_entity, poll_entity), 112 - ), 113 - ); 92 + const existingVote = existingVotes?.find( 93 + (v) => v.voter_token === voter_token, 94 + ); 95 + 96 + if (existingVote) { 97 + const { error: deleteError } = await supabaseServerClient 98 + .from("poll_votes_on_entity") 99 + .delete() 100 + .eq("voter_token", voter_token) 101 + .eq("poll_entity", poll_entity); 102 + 103 + if (deleteError) throw deleteError; 114 104 } 115 105 116 - await db.insert(poll_votes_on_entity).values( 117 - option_entities.map((option_entity) => ({ 118 - option_entity, 119 - poll_entity, 120 - voter_token, 121 - })), 122 - ); 106 + const { error: insertError } = await supabaseServerClient 107 + .from("poll_votes_on_entity") 108 + .insert( 109 + option_entities.map((option_entity) => ({ 110 + option_entity, 111 + poll_entity, 112 + voter_token, 113 + })), 114 + ); 115 + 116 + if (insertError) throw insertError; 117 + 118 + return; 123 119 }