File size: 5,828 Bytes
e5c9966 | 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | const { createClient } = require('@supabase/supabase-js');
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY
);
// ββ Prepared Statements (Wrappers for Supabase) βββββββββββββββββ
const stmts = {
// Tickets
createTicket: async (userId, username, channelId) => {
return await supabase
.from('tickets')
.insert({ user_id: userId, username, channel_id: channelId });
},
closeTicket: async (status, channelId) => {
return await supabase
.from('tickets')
.update({ status, closed_at: new Date().toISOString() })
.eq('channel_id', channelId);
},
getTicket: async (channelId) => {
const { data } = await supabase
.from('tickets')
.select('*')
.eq('channel_id', channelId)
.single();
return data;
},
getOpenTickets: async (status) => {
const { data } = await supabase
.from('tickets')
.select('*')
.eq('status', status);
return data || [];
},
getUserTicket: async (userId, status) => {
const { data } = await supabase
.from('tickets')
.select('*')
.eq('user_id', userId)
.eq('status', status)
.single();
return data;
},
ticketStats: async () => {
const { data, error } = await supabase.rpc('get_ticket_stats');
if (data) return data;
// Manual fallback if RPC not setup
const { count: total } = await supabase.from('tickets').select('*', { count: 'exact', head: true });
const { count: open } = await supabase.from('tickets').select('*', { count: 'exact', head: true }).eq('status', 'open');
const { count: closed } = await supabase.from('tickets').select('*', { count: 'exact', head: true }).eq('status', 'closed');
const { count: deleted } = await supabase.from('tickets').select('*', { count: 'exact', head: true }).eq('status', 'deleted');
return { total, open_count: open, closed_count: closed, deleted_count: deleted };
},
// Verification log
logVerification: async (userId, username, action) => {
return await supabase
.from('verification_log')
.insert({ user_id: userId, username, action });
},
// Bot state (key-value)
setState: async (key, value) => {
return await supabase
.from('bot_state')
.upsert({ key, value });
},
getState: async (key) => {
const { data } = await supabase
.from('bot_state')
.select('value')
.eq('key', key)
.single();
return data ? data.value : null;
},
delState: async (key) => {
return await supabase
.from('bot_state')
.delete()
.eq('key', key);
},
// Whitelist
addWhitelist: async (userId, maxDrops, addedBy) => {
return await supabase
.from('whitelist')
.upsert({ user_id: userId, max_drops: maxDrops, added_by: addedBy });
},
removeWhitelist: async (userId) => {
return await supabase
.from('whitelist')
.delete()
.eq('user_id', userId);
},
getWhitelist: async (userId) => {
const { data } = await supabase
.from('whitelist')
.select('*')
.eq('user_id', userId)
.single();
return data;
},
getAllWhitelist: async () => {
const { data } = await supabase
.from('whitelist')
.select('*');
return data || [];
},
// Drops array
logDrop: async (userId, title, channelId) => {
return await supabase
.from('drop_log')
.insert({ user_id: userId, title, channel_id: channelId });
},
getDropCount24h: async (userId) => {
const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
const { count } = await supabase
.from('drop_log')
.select('*', { count: 'exact', head: true })
.eq('user_id', userId)
.gt('dropped_at', twentyFourHoursAgo);
return { count: count || 0 };
},
getLastDrop: async (userId) => {
const { data } = await supabase
.from('drop_log')
.select('dropped_at')
.eq('user_id', userId)
.order('dropped_at', { ascending: false })
.limit(1)
.single();
return data;
},
addWebDrop: async (userId, category, title, description, status, isExternal, assetId, fileUrl, imageUrl) => {
return await supabase
.from('web_drops')
.insert({
user_id: userId,
category,
title,
description,
status,
is_external: isExternal,
asset_id: assetId,
file_url: fileUrl,
image_url: imageUrl
});
},
getWebDrop: async (id) => {
const { data } = await supabase
.from('web_drops')
.select('*')
.eq('id', id)
.single();
return data;
},
deleteWebDrop: async (id) => {
return await supabase
.from('web_drops')
.delete()
.eq('id', id);
},
getAllWebDrops: async () => {
const { data } = await supabase
.from('web_drops')
.select('*')
.order('published_at', { ascending: false })
.limit(50);
return data || [];
},
};
module.exports = { db: supabase, stmts };
|