a tool for shared writing and social publishing
1"use server";
2
3import { drizzle } from "drizzle-orm/node-postgres";
4import {
5 entities,
6 phone_number_auth_tokens,
7 phone_rsvps_to_entity,
8} from "drizzle/schema";
9import { redirect } from "next/navigation";
10import postgres from "postgres";
11import { v7 } from "uuid";
12import { eq, sql } from "drizzle-orm";
13import { Database } from "supabase/database.types";
14import { createServerClient } from "@supabase/ssr";
15import { cookies } from "next/headers";
16import { pool } from "supabase/pool";
17
18export async function submitRSVP(args: {
19 entity: string;
20 status: Database["public"]["Enums"]["rsvp_status"];
21 name: string;
22 plus_ones: number;
23}) {
24 const client = await pool.connect();
25 const db = drizzle(client);
26 let token = (await cookies()).get("phone_auth_token");
27 if (!token) throw new Error("No auth token found");
28
29 let [auth_token] = await db
30 .select()
31 .from(phone_number_auth_tokens)
32 .where(eq(phone_number_auth_tokens.id, token.value));
33 if (!auth_token) throw new Error("Invalid auth token");
34 if (!auth_token.confirmed) throw new Error("Auth token not confirmed");
35
36 await db.transaction(async (tx) => {
37 await tx
38 .insert(phone_rsvps_to_entity)
39 .values([
40 {
41 status: args.status,
42 entity: args.entity,
43 phone_number: auth_token.phone_number,
44 country_code: auth_token.country_code,
45 name: args.name,
46 plus_ones: args.plus_ones,
47 },
48 ])
49 .onConflictDoUpdate({
50 target: [
51 phone_rsvps_to_entity.entity,
52 phone_rsvps_to_entity.phone_number,
53 ],
54 set: {
55 name: args.name,
56 status: args.status,
57 plus_ones: args.plus_ones,
58 },
59 });
60 });
61
62 client.release();
63 return { success: true };
64}