File size: 5,829 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
// src/handlers/adminHandler.js
const logger = require('../logger');
const whatsapp = require('../services/whatsappService');
const { User } = require('../database');
const config = require('../config');

// State to track pending admin actions requiring confirmation
// Key: adminJid, Value: { action: 'confirm_clear', semester: '...', timeoutId: ... }
const adminState = {};
const CONFIRMATION_TIMEOUT = 60000; // 60 seconds

/**
 * Clears the pending state for an admin.
 * @param {string} jid - Admin JID.
 */
function clearAdminState(jid) {
    if (adminState[jid]) {
        clearTimeout(adminState[jid].timeoutId); // Clear the timeout
        delete adminState[jid];
        logger.info({ adminJid: jid }, `[AdminHandler] Cleared pending admin state.`);
    }
}

/**
 * Handles commands sent by recognized admins, including confirmation steps.
 * Returns true if an admin command (or confirmation) was processed, false otherwise.
 */
async function handleAdminCommand(jid, messageContent) {
    logger.debug({ adminJid: jid, command: messageContent }, `[AdminHandler] Processing potential admin command.`);

    const pendingAction = adminState[jid];

    // --- Step 1: Check if awaiting confirmation ---
    if (pendingAction) {
        const confirmation = messageContent.trim().toLowerCase();
        const semesterToDelete = pendingAction.semester; // Get semester from stored state

        if (pendingAction.action === 'confirm_clear') {
            if (confirmation === 'yes') {
                logger.warn({ adminJid: jid, semester: semesterToDelete }, `[AdminHandler] Confirmation 'yes' received for .clear`);
                clearAdminState(jid); // Clear state before performing action
                try {
                    const deleteResult = await User.deleteMany({
                        semester: { $regex: new RegExp(`^${semesterToDelete}$`, 'i') }
                    });
                    const replyText = `βœ… Admin: Successfully deleted ${deleteResult.deletedCount} records matching semester "${semesterToDelete}".`;
                    logger.warn({ result: deleteResult, semester: semesterToDelete }, `[AdminHandler] .clear command executed successfully.`);
                    await whatsapp.sendMessageWithTyping(jid, { text: replyText });
                } catch (cmdError) {
                    logger.error({ err: cmdError, semester: semesterToDelete }, `[AdminHandler] .clear command failed during execution.`);
                    await whatsapp.sendMessageWithTyping(jid, { text: `❌ Admin: Error executing .clear command for semester "${semesterToDelete}" after confirmation. Check logs.` });
                }
            } else { // Includes 'no' or any other reply
                logger.info({ adminJid: jid, semester: pendingAction.semester, reply: confirmation }, `[AdminHandler] .clear command aborted by admin or invalid confirmation.`);
                clearAdminState(jid);
                await whatsapp.sendMessageWithTyping(jid, { text: `❌ Admin: Aborted deletion for semester "${semesterToDelete}".` });
            }
        } else {
            // Handle other potential pending actions here if added later
            logger.warn({ adminJid: jid, pendingAction }, `[AdminHandler] Unknown pending action found.`);
            clearAdminState(jid); // Clear unknown state
        }
        return true; // Confirmation message was processed
    }

    // --- Step 2: Check for new commands if not awaiting confirmation ---

    // ** .clear command **
    if (messageContent.startsWith('.clear ')) {
        const parts = messageContent.split(' ');
        if (parts.length === 2 && parts[1]) {
            const semesterToDelete = parts[1].trim();
            logger.warn({ adminJid: jid, semester: semesterToDelete }, `[AdminHandler] .clear command received. Requesting confirmation.`);

            // Set pending state with timeout
            adminState[jid] = {
                action: 'confirm_clear',
                semester: semesterToDelete,
                timeoutId: setTimeout(() => {
                    if (adminState[jid]?.action === 'confirm_clear') { // Check if still pending this action
                         logger.warn({ adminJid: jid, semester: semesterToDelete }, `[AdminHandler] Confirmation for .clear timed out.`);
                         delete adminState[jid]; // Clear state on timeout
                         whatsapp.sendMessageWithTyping(jid, { text: `⏰ Admin: Confirmation request for deleting semester "${semesterToDelete}" timed out.` }).catch(err => logger.error({err}, "Failed to send timeout message"));
                    }
                }, CONFIRMATION_TIMEOUT)
            };

            // Ask for confirmation
            const promptText = `❓ *Confirmation Needed* ❓\n\nAre you sure you want to delete ALL student data for semester *${semesterToDelete}*? This cannot be undone.\n\nReply with *Yes* or *No*. (Expires in 60 seconds)`;
            await whatsapp.sendMessageWithTyping(jid, { text: promptText });

        } else { // Invalid format
            await whatsapp.sendMessageWithTyping(jid, { text: `❌ Admin: Invalid .clear command format. Use: \`.clear <semester>\` (e.g., \`.clear 3rd\`)` });
        }
        return true; // .clear command attempt was handled
    }

    // --- Add other admin commands here ---
    // else if (messageContent.startsWith('.some_other_command')) {
    //    logger.info({ adminJid: jid }, `[AdminHandler] Handling other admin command.`);
    //    ... handle command ...
    //    return true;
    // }

    // If no known admin command matched
    logger.debug({ adminJid: jid, command: messageContent }, `[AdminHandler] Not a recognized admin command.`);
    return false; // No admin command was processed
}

module.exports = { handleAdminCommand };