// Create appointment to prove reminder system works const { PrismaClient } = require('@prisma/client'); const crypto = require('crypto'); const prisma = new PrismaClient(); async function proveItsWorking() { console.log('šŸŽÆ PROVING THE REMINDER SYSTEM WORKS AGAIN'); console.log('='.repeat(50)); try { // Get your corrected lead info const clinic = await prisma.clinic.findFirst(); let obaidLead = await prisma.lead.findFirst({ where: { phone: '+96178701782' } }); if (!obaidLead) { // Create with correct number obaidLead = await prisma.lead.create({ data: { clinicId: clinic.id, name: 'Obaidalah Saleh', phone: '+96178701782', status: 'new', source: 'system_test' } }); console.log('āœ… Created lead with correct number'); } else { console.log('āœ… Found your lead with correct number'); } const treatment = await prisma.treatment.findFirst({ where: { clinicId: clinic.id, isActive: true } }); console.log('šŸ“‹ Creating real appointment...'); // Create appointment for tomorrow const appointmentTime = new Date(); appointmentTime.setDate(appointmentTime.getDate() + 1); appointmentTime.setHours(16, 30, 0, 0); // Tomorrow 4:30 PM const appointment = await prisma.appointment.create({ data: { clinicId: clinic.id, leadId: obaidLead.id, treatmentId: treatment.id, dateTime: appointmentTime, status: 'scheduled', confirmToken: crypto.randomBytes(32).toString('hex'), notes: 'Post-fix test appointment - proving system works' }, }); console.log(`āœ… Appointment created: ${appointment.id}`); console.log(`šŸ“… Date: ${appointmentTime.toLocaleString()}`); // Schedule booking confirmation for immediate send const template = await prisma.reminderTemplate.findFirst({ where: { clinicId: clinic.id, trigger: 'booking_confirmed', isActive: true } }); if (template) { await prisma.scheduledReminder.create({ data: { appointmentId: appointment.id, templateId: template.id, scheduledFor: new Date(Date.now() + 5000), // 5 seconds }, }); console.log('āœ… Booking confirmation scheduled'); // Wait and trigger send console.log('ā³ Waiting 7 seconds then sending confirmation...'); await new Promise(resolve => setTimeout(resolve, 7000)); const cronResponse = await fetch('http://localhost:3000/api/cron/send-reminders?key=dev-cron-key', { method: 'POST' }); if (cronResponse.ok) { const cronResult = await cronResponse.json(); console.log(`šŸ“¤ Cron result: sent=${cronResult.sent}, failed=${cronResult.failed}`); if (cronResult.sent > 0) { console.log('šŸŽ‰ BOOKING CONFIRMATION SENT!'); // Check message log const messageLog = await prisma.messageLog.findFirst({ where: { appointmentId: appointment.id, phone: obaidLead.phone }, orderBy: { createdAt: 'desc' } }); if (messageLog) { console.log('\nšŸ“± MESSAGE DETAILS:'); console.log(` Status: ${messageLog.status}`); console.log(` Sent: ${messageLog.sentAt}`); console.log(` WAHA ID: ${messageLog.wahaMessageId}`); console.log(` Preview: "${messageLog.message.substr(0, 80)}..."`); } console.log('\nšŸŽ‰ SUCCESS! THE REMINDER SYSTEM IS WORKING!'); console.log('='.repeat(50)); console.log('āœ… Emergency fix completed'); console.log('āœ… WAHA restarted and working'); console.log('āœ… Test appointment created'); console.log('āœ… Booking confirmation sent'); console.log('āœ… Full workflow operational'); console.log(''); console.log('šŸ“± OBAID - CHECK YOUR WHATSAPP!'); console.log(' You should see:'); console.log(' 1. Emergency test message'); console.log(' 2. Booking confirmation for tomorrow 4:30 PM'); console.log(''); console.log('šŸŽÆ THE SYSTEM IS WORKING AGAIN!'); console.log(' No more debugging needed!'); console.log(' Create appointments normally now!'); } else { console.log('āš ļø No messages sent - checking why...'); } } else { console.log('āŒ Cron failed'); } } else { console.log('āŒ No booking confirmation template found'); } } catch (error) { console.error('āŒ Proof failed:', error); } finally { await prisma.$disconnect(); } } proveItsWorking().catch(console.error);