-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
const users = {}; // Simple in-memory storage for demonstration | ||
const { createClient } = require('@supabase/supabase-js'); | ||
const supabaseUrl = 'https://gaexwodpzfivlcycxzgf.supabase.co'; | ||
const supabaseKey = process.env.SUPABASE_KEY; | ||
const supabase = createClient(supabaseUrl, supabaseKey); | ||
|
||
async function saveUser(chatId, username) { | ||
users[chatId] = { username }; | ||
console.log(`User saved: ${chatId}`); | ||
const { data, error } = await supabase | ||
.from('users') | ||
.insert([{ user_id: chatId, start_date: new Date(), total_messages: 0 }]); | ||
if (error) console.error('Error saving user:', error); | ||
} | ||
|
||
module.exports = { saveUser }; | ||
async function incrementMessageCount(chatId) { | ||
const { data, error } = await supabase | ||
.from('users') | ||
.select('total_messages, last_message_date') | ||
.eq('user_id', chatId) | ||
.single(); | ||
|
||
const today = new Date().toISOString().split('T')[0]; | ||
|
||
if (!data) return { canSend: false, totalMessages: 0 }; | ||
|
||
let newMessageCount = data.total_messages; | ||
|
||
if (data.last_message_date.split('T')[0] !== today) { | ||
await supabase.from('users') | ||
.update({ total_messages: 1, last_message_date: new Date() }) | ||
.eq('user_id', chatId); | ||
newMessageCount = 1; | ||
} else if (data.total_messages < 10) { | ||
await supabase.from('users') | ||
.update({ total_messages: data.total_messages + 1, last_message_date: new Date() }) | ||
.eq('user_id', chatId); | ||
newMessageCount = data.total_messages + 1; | ||
} else { | ||
return { canSend: false, totalMessages: 10 }; | ||
} | ||
|
||
return { canSend: true, totalMessages: newMessageCount }; | ||
} | ||
|
||
module.exports = { saveUser, incrementMessageCount }; |