|
|
import { supabase } from '../../db/supabase'; |
|
|
import { createLogger } from '../../utils/logger'; |
|
|
|
|
|
const logger = createLogger('MessageManager'); |
|
|
|
|
|
interface BotMessage { |
|
|
id: string; |
|
|
bot_id: string; |
|
|
key: string; |
|
|
ar_value: string; |
|
|
en_value: string; |
|
|
description?: string; |
|
|
created_at: string; |
|
|
updated_at: string; |
|
|
} |
|
|
|
|
|
class MessageManager { |
|
|
private static instance: MessageManager; |
|
|
private messages: Map<string, BotMessage> = new Map(); |
|
|
private language: 'ar' | 'en' = 'ar'; |
|
|
|
|
|
private constructor() {} |
|
|
|
|
|
static getInstance(): MessageManager { |
|
|
if (!MessageManager.instance) { |
|
|
MessageManager.instance = new MessageManager(); |
|
|
} |
|
|
return MessageManager.instance; |
|
|
} |
|
|
|
|
|
setLanguage(lang: 'ar' | 'en') { |
|
|
this.language = lang; |
|
|
} |
|
|
|
|
|
async loadMessages() { |
|
|
try { |
|
|
const { data, error } = await supabase |
|
|
.from('bot_messages') |
|
|
.select('*') |
|
|
|
|
|
if (error) throw error; |
|
|
|
|
|
this.messages.clear(); |
|
|
data.forEach((message: BotMessage) => { |
|
|
this.messages.set(message.key, message); |
|
|
}); |
|
|
|
|
|
|
|
|
this.addDefaultMessages(); |
|
|
|
|
|
logger.info(`Messages loaded successfully for bot `); |
|
|
} catch (error) { |
|
|
logger.error('Error loading messages:', error); |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
|
|
|
private addDefaultMessages() { |
|
|
const defaultMessages = { |
|
|
'balance_update_notification': { |
|
|
ar_value: '💰 <b>تحديث الرصيد</b>\n\n' + |
|
|
'تم تحديث رصيدك:\n' + |
|
|
'الرصيد السابق: {old_balance}$\n' + |
|
|
'الرصيد الجديد: {new_balance}$\n' + |
|
|
'نوع التغيير: {change_type}\n' + |
|
|
'قيمة التغيير: {change_amount}$', |
|
|
en_value: '💰 <b>Balance Update</b>\n\n' + |
|
|
'Your balance has been updated:\n' + |
|
|
'Previous balance: ${old_balance}\n' + |
|
|
'New balance: ${new_balance}\n' + |
|
|
'Change type: {change_type}\n' + |
|
|
'Change amount: ${change_amount}' |
|
|
}, |
|
|
'search_product_prompt': { |
|
|
ar_value: 'الرجاء إدخال اسم الخدمة التي تبحث عنها:', |
|
|
en_value: 'Please enter the name of the service you are looking for:' |
|
|
}, |
|
|
'no_search_results': { |
|
|
ar_value: 'عذراً، لم يتم العثور على نتائج لبحثك.', |
|
|
en_value: 'Sorry, no results found for your search.' |
|
|
}, |
|
|
'invalid_search_input': { |
|
|
ar_value: 'الرجاء إدخال ما لا يقل عن حرفين للبحث.', |
|
|
en_value: 'Please enter at least 2 characters to search.' |
|
|
}, |
|
|
'search_country_prompt': { |
|
|
ar_value: 'الرجاء إدخال اسم الدولة أو رمزها أو علمها:', |
|
|
en_value: 'Please enter the country name, code, or flag:' |
|
|
}, |
|
|
'no_country_search_results': { |
|
|
ar_value: 'عذراً، لم يتم العثور على دول مطابقة لبحثك.', |
|
|
en_value: 'Sorry, no matching countries found for your search.' |
|
|
}, |
|
|
'affordable_products_list': { |
|
|
ar_value: 'المنتجات التي يمكنك شراؤها برصيدك الحالي ({balance}):', |
|
|
en_value: 'Products you can buy with your current balance ({balance}):' |
|
|
}, |
|
|
'no_affordable_products': { |
|
|
ar_value: 'عذراً، لا توجد منتجات يمكنك شراؤها برصيدك الحالي ({balance}).', |
|
|
en_value: 'Sorry, no products can be bought with your current balance ({balance}).' |
|
|
} |
|
|
}; |
|
|
|
|
|
Object.entries(defaultMessages).forEach(([key, value]) => { |
|
|
if (!this.messages.has(key)) { |
|
|
this.messages.set(key, { |
|
|
id: key, |
|
|
bot_id: 'default', |
|
|
key, |
|
|
ar_value: value.ar_value, |
|
|
en_value: value.en_value, |
|
|
created_at: new Date().toISOString(), |
|
|
updated_at: new Date().toISOString() |
|
|
}); |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
getMessage(key: string): string { |
|
|
const message = this.messages.get(key); |
|
|
if (!message) { |
|
|
logger.warn(`Message not found for key: ${key}`); |
|
|
return `${key}`; |
|
|
} |
|
|
return this.language === 'ar' ? message.ar_value : message.en_value; |
|
|
} |
|
|
|
|
|
async updateMessage(key: string, arValue: string, enValue: string, description?: string) { |
|
|
try { |
|
|
const { data, error } = await supabase |
|
|
.from('bot_messages') |
|
|
.upsert({ |
|
|
key, |
|
|
ar_value: arValue, |
|
|
en_value: enValue, |
|
|
description |
|
|
}) |
|
|
.select() |
|
|
.single(); |
|
|
|
|
|
if (error) throw error; |
|
|
|
|
|
if (data) { |
|
|
this.messages.set(key, data); |
|
|
} |
|
|
|
|
|
return data; |
|
|
} catch (error) { |
|
|
logger.error('Error updating message:', error); |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
export const messageManager = MessageManager.getInstance(); |