Spaces:
Building
Building
File size: 7,528 Bytes
14960b8 |
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 |
// src/handlers/registrationHandler.js (previously selfReg.js)
const logger = require('../logger');
const whatsapp = require('../services/whatsappService');
const { User } = require('../database'); // Import User model directly
const config = require('../config'); // Import config for delays
const { sleep } = require('../utils'); // Import sleep directly if needed
const userState = {};
const REGISTRATION_DELAY_MIN = 2000; // 2 seconds
const REGISTRATION_DELAY_MAX = 3000; // 3 seconds
/**
* Handles the multi-step registration process. Uses whatsappService for communication.
* /restart command functionality has been removed.
*/
async function handleRegistration(jid, messageContent) { // Renamed remoteJid to jid for consistency
logger.debug({ jid }, `[RegistrationHandler] Handling step.`);
const currentState = userState[jid] || { step: 'START', data: {} };
const delayOptions = { minDelay: config.registrationMinDelay, maxDelay: config.registrationMaxDelay };
// REMOVED: Global /restart check is removed from here.
try { // Wrap steps in try-catch
switch (currentState.step) {
case 'START':
userState[jid] = { step: 'ASKING_NAME', data: {} };
// REMOVED: Mention of /restart removed from the prompt
await whatsapp.sendMessageWithTyping(jid, { text: "Hi there! Let's get you registered.\n\nWhat is your full Name?" }, delayOptions);
break;
case 'ASKING_NAME':
if (!messageContent.trim()) {
await whatsapp.sendMessageWithTyping(jid, { text: "Please enter a valid name." }, delayOptions);
break;
}
currentState.data.name = messageContent.trim();
currentState.step = 'ASKING_REGNO';
userState[jid] = currentState;
logger.debug({ jid, data: currentState.data }, `[RegistrationHandler] Name received.`);
await whatsapp.sendMessageWithTyping(jid, { text: `Got it, ${currentState.data.name}.\n\nWhat is your Registration Number?` }, delayOptions);
break;
case 'ASKING_REGNO':
if (!messageContent.trim()) {
await whatsapp.sendMessageWithTyping(jid, { text: "Please enter a valid registration number." }, delayOptions);
break;
}
currentState.data.regNo = messageContent.trim();
currentState.step = 'ASKING_SEMESTER';
userState[jid] = currentState;
logger.debug({ jid, data: currentState.data }, `[RegistrationHandler] RegNo received.`);
await whatsapp.sendMessageWithTyping(jid, { text: "Great!\n\nWhich Semester are you in? (e.g., 1st, 2nd, 3rd, 4th)" }, delayOptions);
break;
case 'ASKING_SEMESTER':
if (!messageContent.trim()) {
await whatsapp.sendMessageWithTyping(jid, { text: "Please enter a valid semester." }, delayOptions);
break;
}
currentState.data.semester = messageContent.trim();
currentState.step = 'CONFIRMING';
userState[jid] = currentState;
logger.debug({ jid, data: currentState.data }, `[RegistrationHandler] Semester received.`);
const confirmationText = `?? *Please confirm your details:* ??\n\n*Name:* ${currentState.data.name}\n*Reg No:* ${currentState.data.regNo}\n*Semester:* ${currentState.data.semester}\n\nIs this correct? Reply with *yes* or *no*.`;
await whatsapp.sendMessageWithTyping(jid, { text: confirmationText }, delayOptions);
break;
case 'CONFIRMING':
const confirmation = messageContent.trim().toLowerCase();
if (confirmation === 'yes') {
try {
logger.info({ jid, data: currentState.data }, `[RegistrationHandler] User confirmed. Saving data.`);
await sleep(300); // Small pause before DB write
const updateResult = await User.updateOne(
{ remoteJid: jid },
{
$set: {
name: currentState.data.name,
regNo: currentState.data.regNo,
semester: currentState.data.semester,
isRegistered: true,
registrationTimestamp: new Date()
}
},
{ upsert: true }
);
logger.info({ jid, result: updateResult }, `[RegistrationHandler] MongoDB update result.`);
if (updateResult.acknowledged) {
await whatsapp.sendMessageWithTyping(jid, { text: "✅ Registration successful! Your details have been saved." }, delayOptions);
delete userState[jid]; // Clear state
} else { throw new Error("Database update not acknowledged."); }
} catch (dbError) {
logger.error({ jid, err: dbError }, `[RegistrationHandler] Error saving data during confirmation.`);
// REMOVED: Mention of /restart in error message
await whatsapp.sendMessageWithTyping(jid, { text: "❌ Sorry, there was an error saving your details. Please reply 'yes' again or 'no' to start over." }, delayOptions);
}
} else if (confirmation === 'no') { // Only 'no' triggers restart now
logger.info({ jid }, `[RegistrationHandler] User replied 'no' at confirmation.`);
delete userState[jid];
userState[jid] = { step: 'ASKING_NAME', data: {} };
await whatsapp.sendMessageWithTyping(jid, { text: "Okay, let's start over.\n\nWhat is your full Name?" }, delayOptions);
} else {
await whatsapp.sendMessageWithTyping(jid, { text: "Please reply with *yes* or *no*." }, delayOptions);
}
break;
default: // Unhandled state
logger.warn({ jid, state: currentState.step }, `[RegistrationHandler] Unhandled state step.`);
delete userState[jid];
await whatsapp.sendMessageWithTyping(jid, { text: "Something went wrong during registration. Please send any message to try starting again." }, delayOptions); // Updated generic error
break;
}
} catch (stepError) { // Catch errors in the switch/case logic itself
logger.error({ jid, err: stepError, step: currentState.step }, "[RegistrationHandler] Error during registration step execution.");
try {
// REMOVED: Mention of /restart in generic error message
await whatsapp.sendMessageWithTyping(jid, { text: "An unexpected error occurred during registration. Please try starting again by sending any message." });
delete userState[jid]; // Reset state on error
} catch (sendErr) {
logger.error({ jid, err: sendErr }, "[RegistrationHandler] Failed to send error message during step error handling.");
}
}
}
module.exports = { handleRegistration };
|