File size: 2,160 Bytes
c35213b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { createClient } from '@supabase/supabase-js';

const supabaseUrl = process.env.SUPABASE_URL || '';
const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || '';

export const supabase = createClient(supabaseUrl, supabaseKey);

export interface WebDrop {
  id: number;
  user_id: string;
  title: string;
  description: string;
  status: string;
  is_external: number; // 0 or 1
  asset_id: string | null;
  file_url: string | null;
  image_url: string | null;
  published_at: string;
  category: string;
}

// Function to fetch all web drops from Supabase, sorted by newest first
export async function getAllWebDrops(): Promise<WebDrop[]> {
  try {
    const { data, error } = await supabase
      .from('web_drops')
      .select('*')
      .order('published_at', { ascending: false })
      .limit(50);

    if (error) throw error;
    return (data || []) as WebDrop[];
  } catch (e) {
    console.error("Supabase Fetch Error:", e);
    return [];
  }
}

export async function getWebDropsByCategory(category: string): Promise<WebDrop[]> {
  try {
    const { data, error } = await supabase
      .from('web_drops')
      .select('*')
      .eq('category', category)
      .order('published_at', { ascending: false })
      .limit(50);

    if (error) throw error;
    return (data || []) as WebDrop[];
  } catch (e) {
    console.error(`Supabase Category Fetch Error (${category}):`, e);
    return [];
  }
}

export async function checkVipStatus(discordId: string): Promise<boolean> {
  try {
    const { data: user, error } = await supabase
      .from('vip_users')
      .select('*')
      .eq('discord_id', discordId)
      .single();

    if (error || !user) return false;

    // Check if expired
    if (user.expires_at) {
      const expiresAt = new Date(user.expires_at).getTime();
      if (Date.now() > expiresAt) return false;
    }

    return true; // Has lifetime or active VIP
  } catch (e) {
    // single() throws error if no rows found
    return false;
  }
}

// Legacy helper for webhook (which might still want a "db" object feel)
export function getDb() {
    return supabase;
}