File size: 1,136 Bytes
61f39da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { env } from "./config.js";
import { supabaseAdmin } from "./supabase.js";

export async function deleteStoredSession(sessionId: string, actorId: string, isAdmin: boolean) {
  const response = await fetch(new URL(`/api/sessions/${sessionId}`, env.ANALYSIS_API_URL), {
    method: "DELETE",
    headers: {
      "x-sporalize-proxy-secret": env.ANALYSIS_PROXY_SECRET,
      "x-sporalize-user-id": actorId,
      ...(isAdmin ? { "x-sporalize-admin": "true" } : {}),
    },
  });
  if (!response.ok) {
    const payload = await response.json().catch(() => ({})) as { detail?: string; error?: string };
    throw new Error(payload.detail || payload.error || `Unable to delete session ${sessionId} (${response.status}).`);
  }
}

export async function refundAdminUsage(userId: string | null, players: number, sessions: number) {
  if (!userId || (players <= 0 && sessions <= 0)) return;
  const { error } = await supabaseAdmin.rpc("adjust_subscription_usage", {
    p_user_id: userId,
    p_players: players,
    p_sessions: sessions,
  });
  if (error) throw new Error(`Unable to restore deleted resource quota: ${error.message}`);
}