a tool for shared writing and social publishing
1"use server";
2
3import { getIdentityData } from "./getIdentityData";
4import { getStripe } from "stripe/client";
5import { supabaseServerClient } from "supabase/serverClient";
6import { Ok, Err, type Result } from "src/result";
7
8export async function reactivateSubscription(): Promise<
9 Result<{ renewsAt: string }, string>
10> {
11 const identity = await getIdentityData();
12 if (!identity) {
13 return Err("Not authenticated");
14 }
15
16 const { data: sub } = await supabaseServerClient
17 .from("user_subscriptions")
18 .select("stripe_subscription_id, current_period_end")
19 .eq("identity_id", identity.id)
20 .single();
21
22 if (!sub?.stripe_subscription_id) {
23 return Err("No active subscription found");
24 }
25
26 await getStripe().subscriptions.update(sub.stripe_subscription_id, {
27 cancel_at_period_end: false,
28 });
29
30 // Optimistic update
31 await supabaseServerClient
32 .from("user_subscriptions")
33 .update({
34 status: "active",
35 updated_at: new Date().toISOString(),
36 })
37 .eq("identity_id", identity.id);
38
39 return Ok({ renewsAt: sub.current_period_end || "" });
40}