pues yo qué sé se ha cargado un montón de archivos
							parent
							
								
									a7bac11d77
								
							
						
					
					
						commit
						dcfd184a6b
					
				| @ -1,49 +0,0 @@ | |||||||
| import { query } from '../database/db'; |  | ||||||
| import { sendMessage } from '../bot/utils/messaging'; |  | ||||||
| import { Task } from '../models/task'; |  | ||||||
| import { getUserPreferences } from './userService'; |  | ||||||
| 
 |  | ||||||
| export function getTasksNeedingReminders(): Task[] { |  | ||||||
|   const now = new Date().toISOString(); |  | ||||||
|   const tasks = query(` |  | ||||||
|     SELECT t.*  |  | ||||||
|     FROM tasks t |  | ||||||
|     JOIN user_preferences up ON t.assigned_to = up.phone_number |  | ||||||
|     WHERE t.completed = FALSE  |  | ||||||
|       AND (t.due_date IS NULL OR t.due_date > ?) |  | ||||||
|       AND (t.last_reminder IS NULL OR t.last_reminder < datetime(?, '-1 day')) |  | ||||||
|       AND up.reminders_enabled = TRUE |  | ||||||
|   `, [now, now]);
 |  | ||||||
| 
 |  | ||||||
|   // Also include tasks for users who haven't set preferences yet
 |  | ||||||
|   const newUsersTasks = query(` |  | ||||||
|     SELECT t.*  |  | ||||||
|     FROM tasks t |  | ||||||
|     LEFT JOIN user_preferences up ON t.assigned_to = up.phone_number |  | ||||||
|     WHERE t.completed = FALSE  |  | ||||||
|       AND (t.due_date IS NULL OR t.due_date > ?) |  | ||||||
|       AND (t.last_reminder IS NULL OR t.last_reminder < datetime(?, '-1 day')) |  | ||||||
|       AND up.phone_number IS NULL |  | ||||||
|   `, [now, now]);
 |  | ||||||
| 
 |  | ||||||
|   return [...tasks, ...newUsersTasks]; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| export async function sendTaskReminder(task: Task) { |  | ||||||
|   if (!task.assignedTo) return; |  | ||||||
| 
 |  | ||||||
|   const message = `🔔 Recordatorio de tarea:\n` + |  | ||||||
|     `ID: ${task.id}\n` + |  | ||||||
|     `Descripción: ${task.description}\n` + |  | ||||||
|     (task.dueDate ? `Fecha límite: ${task.dueDate}\n` : '') + |  | ||||||
|     `Creada: ${task.createdAt}\n\n` + |  | ||||||
|     `Usa /tarea completar ${task.id} cuando la termines.`; |  | ||||||
| 
 |  | ||||||
|   try { |  | ||||||
|     await sendMessage(task.assignedTo, message); |  | ||||||
|     // Update last reminder timestamp
 |  | ||||||
|     query('UPDATE tasks SET last_reminder = CURRENT_TIMESTAMP WHERE id = ?', [task.id]); |  | ||||||
|   } catch (error) { |  | ||||||
|     console.error('Error sending reminder:', error); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,45 +0,0 @@ | |||||||
| import axios from 'axios'; |  | ||||||
| import dotenv from 'dotenv'; |  | ||||||
| 
 |  | ||||||
| dotenv.config(); |  | ||||||
| 
 |  | ||||||
| const API_URL = process.env.API_URL; |  | ||||||
| const INSTANCE_NAME = process.env.INSTANCE_NAME; |  | ||||||
| const API_KEY = process.env.API_KEY; |  | ||||||
| 
 |  | ||||||
| export async function isUserInCommunity(userJid: string, groupJids: Set<string>): Promise<boolean> { |  | ||||||
|   try { |  | ||||||
|     // Check each group for the user
 |  | ||||||
|     for (const groupJid of groupJids) { |  | ||||||
|       const response = await axios.get( |  | ||||||
|         `${API_URL}/group/participants/${INSTANCE_NAME}`, |  | ||||||
|         { |  | ||||||
|           params: { |  | ||||||
|             groupJid: groupJid |  | ||||||
|           }, |  | ||||||
|           headers: { |  | ||||||
|             apikey: API_KEY |  | ||||||
|           } |  | ||||||
|         } |  | ||||||
|       ); |  | ||||||
|        |  | ||||||
|       // Check if user is in this group's participants
 |  | ||||||
|       const participants = response.data?.participants || []; |  | ||||||
|       console.log('Participants:', participants); |  | ||||||
|        |  | ||||||
|       // Check both JID and phone number formats
 |  | ||||||
|       const userPhone = userJid.split('@')[0]; |  | ||||||
|       if (participants.some(p => { |  | ||||||
|         // Handle both string and object participant formats
 |  | ||||||
|         const participantJid = typeof p === 'string' ? p : p?.id; |  | ||||||
|         return participantJid === userJid || participantJid?.startsWith?.(userPhone); |  | ||||||
|       })) { |  | ||||||
|         return true; |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|     return false; |  | ||||||
|   } catch (error) { |  | ||||||
|     console.error('Error checking community membership:', error); |  | ||||||
|     return false; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,53 +1,56 @@ | |||||||
| export function normalizeUserIdentifier(input: string): string { | export function normalizeUserIdentifier(input: string): string { | ||||||
|   console.log('Normalizing user identifier:', input); | 	console.log('Normalizing user identifier:', input); | ||||||
|    | 
 | ||||||
|   // Handle group JID format (120363418564126395@g.us)
 | 	// Handle group JID format (120363418564126395@g.us)
 | ||||||
|   if (input.endsWith('@g.us')) { | 	if (input.endsWith('@g.us')) { | ||||||
|     return input; // Return group JID as-is
 | 		return input; // Return group JID as-is
 | ||||||
|   } | 	} | ||||||
|    | 
 | ||||||
|   // Handle user JID format (12345678@s.whatsapp.net)
 | 	// Handle user JID format (12345678@s.whatsapp.net)
 | ||||||
|   if (input.includes('@s.whatsapp.net')) { | 	// if (input.includes('@s.whatsapp.net')) {
 | ||||||
|     const [phone, domain] = input.split('@'); | 	//   const [phone, domain] = input.split('@');
 | ||||||
|     const cleanPhone = phone.replace(/\D/g, ''); | 	//   const cleanPhone = phone.replace(/\D/g, '');
 | ||||||
|     return `${cleanPhone}@${domain}`; | 	//   return `${cleanPhone}@${domain}`;
 | ||||||
|   } | 	// }
 | ||||||
| 
 | 	if (input.includes('@s.whatsapp.net')) { | ||||||
|   // Handle raw input (phone number or @mention)
 | 		return input.split('@')[0]; | ||||||
|   const cleanInput = input.startsWith('@') ? input.slice(1) : input; | 	} | ||||||
|   const phoneNumber = cleanInput.replace(/\D/g, ''); | 
 | ||||||
|    | 	// Handle raw input (phone number or @mention)
 | ||||||
|   // Validate phone number length
 | 	const cleanInput = input.startsWith('@') ? input.slice(1) : input; | ||||||
|   if (phoneNumber.length < 8 || phoneNumber.length > 15) { | 	const phoneNumber = cleanInput.replace(/\D/g, ''); | ||||||
|     throw new Error('Número de teléfono inválido'); | 
 | ||||||
|   } | 	// Validate phone number length
 | ||||||
|    | 	if (phoneNumber.length < 8 || phoneNumber.length > 15) { | ||||||
|   const jid = `${phoneNumber}@s.whatsapp.net`; | 		throw new Error('Número de teléfono inválido'); | ||||||
|   console.log('Normalized JID:', jid); | 	} | ||||||
|   return jid; | 
 | ||||||
|  | 	const jid = `${phoneNumber}@s.whatsapp.net`; | ||||||
|  | 	console.log('Normalized JID:', jid); | ||||||
|  | 	return jid; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function extractUserFromJid(jid: string): string { | export function extractUserFromJid(jid: string): string { | ||||||
|   // Extract phone number from JID (format: 12345678@s.whatsapp.net)
 | 	// Extract phone number from JID (format: 12345678@s.whatsapp.net)
 | ||||||
|   const phoneNumber = jid.split('@')[0]; | 	const phoneNumber = jid.split('@')[0]; | ||||||
|   return normalizeUserIdentifier(phoneNumber); | 	return normalizeUserIdentifier(phoneNumber); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function formatUserMention(phoneNumber?: string): string { | export function formatUserMention(phoneNumber?: string): string { | ||||||
|   if (!phoneNumber) { | 	if (!phoneNumber) { | ||||||
|     return '@unknown'; | 		return '@unknown'; | ||||||
|   } | 	} | ||||||
|    | 
 | ||||||
|   // Handle full JID format
 | 	// Handle full JID format
 | ||||||
|   if (phoneNumber.includes('@s.whatsapp.net')) { | 	if (phoneNumber.includes('@s.whatsapp.net')) { | ||||||
|     return `@${phoneNumber.split('@')[0]}`; | 		return `@${phoneNumber.split('@')[0]}`; | ||||||
|   } | 	} | ||||||
|    | 
 | ||||||
|   // Handle already formatted mentions
 | 	// Handle already formatted mentions
 | ||||||
|   if (phoneNumber.startsWith('@')) { | 	if (phoneNumber.startsWith('@')) { | ||||||
|     return phoneNumber; | 		return phoneNumber; | ||||||
|   } | 	} | ||||||
|    | 
 | ||||||
|   // Handle raw numbers
 | 	// Handle raw numbers
 | ||||||
|   return `@${phoneNumber.replace(/\D/g, '')}`; | 	return `@${phoneNumber.replace(/\D/g, '')}`; | ||||||
| } | } | ||||||
|  | |||||||
					Loading…
					
					
				
		Reference in New Issue