wsb-bot / WSW /src /lib /db.ts
APRK01
Configure cloud database and production pages
c35213b
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;
}