a tool for shared writing and social publishing
1"use server"; 2import { drizzle } from "drizzle-orm/node-postgres"; 3import { eq } from "drizzle-orm"; 4import { 5 entities, 6 permission_token_rights, 7 phone_rsvps_to_entity, 8} from "drizzle/schema"; 9import twilio from "twilio"; 10import { pool } from "supabase/pool"; 11 12export async function sendUpdateToRSVPS( 13 token: { id: string }, 14 { 15 entity, 16 message, 17 eventName, 18 sendto, 19 publicLeafletID, 20 }: { 21 entity: string; 22 message: string; 23 eventName: string; 24 publicLeafletID: string; 25 sendto: { GOING: boolean; MAYBE: boolean; NOT_GOING: boolean }; 26 }, 27) { 28 let dbclient = await pool.connect(); 29 const db = drizzle(dbclient); 30 let token_rights = await db 31 .select() 32 .from(permission_token_rights) 33 .where(eq(permission_token_rights.token, token.id)); 34 35 let RSVPS = db 36 .select() 37 .from(phone_rsvps_to_entity) 38 .innerJoin(entities, eq(phone_rsvps_to_entity.entity, entities.id)) 39 .where(eq(phone_rsvps_to_entity.entity, entity)); 40 41 dbclient.release(); 42 43 if (!token_rights[0]?.write) return; 44 let rsvps = await RSVPS; 45 let entity_set = rsvps[0]?.entities.set; 46 if (!token_rights.find((r) => r.entity_set === entity_set)) { 47 return; 48 } 49 50 const accountSid = process.env.TWILIO_ACCOUNT_SID; 51 const authToken = process.env.TWILIO_AUTH_TOKEN; 52 const client = twilio(accountSid, authToken); 53 54 for (let rsvp of rsvps) { 55 if (sendto[rsvp.phone_rsvps_to_entity.status]) { 56 let { country_code, phone_number } = rsvp.phone_rsvps_to_entity; 57 let number = `+${country_code}${phone_number}`; 58 await client.messages.create({ 59 contentSid: "HX8e1217f791d38fa4cf7b7b24a02fe10c", 60 contentVariables: JSON.stringify({ 61 1: eventName, 62 2: message, 63 3: `https://leaflet.pub/${publicLeafletID}`, 64 }), 65 from: `${country_code === "1" ? "" : "whatsapp:"}+18449523391`, 66 messagingServiceSid: "MGffbf9a66770350b25caf3b80b9aac481", 67 to: country_code === "1" ? number : `whatsapp:${number}`, 68 }); 69 } 70 } 71}