|
1 | | -/** |
2 | | - * Revoke License Command (Admin only) |
3 | | - */ |
4 | | - |
5 | | -const Validator = require('../utils/Validator'); |
6 | | -const PermissionManager = require('../utils/PermissionManager'); |
7 | | - |
8 | | -module.exports = { |
9 | | - name: 'revoke', |
10 | | - description: 'Revoke a license (Admin only)', |
11 | | - |
12 | | - async execute(msg, bot, licenseClient, dbManager) { |
13 | | - const chatId = msg.chat.id; |
14 | | - const userId = msg.from.id; |
15 | | - const args = msg.text.split(' ').slice(1); |
16 | | - |
17 | | - const permissionManager = new PermissionManager(); |
18 | | - try { |
19 | | - permissionManager.requirePermission(userId, 'admin'); |
20 | | - } catch (err) { |
21 | | - await bot.sendMessage(chatId, '❌ ' + (err.message || 'Access denied. Administrators only.'), { parse_mode: 'Markdown' }); |
22 | | - return; |
23 | | - } |
24 | | - |
25 | | - if (args.length === 0) { |
26 | | - await bot.sendMessage(chatId, |
27 | | - '❌ *Usage:* `/revoke <license_key>`\n\n' + |
28 | | - 'Example: `/revoke LC-ABC123-DEF456-GHI789`\n\n' + |
29 | | - '⚠️ *Warning:* This will permanently revoke the license.', |
30 | | - { parse_mode: 'Markdown' } |
31 | | - ); |
32 | | - return; |
33 | | - } |
34 | | - |
35 | | - let licenseKey; |
36 | | - try { |
37 | | - licenseKey = Validator.validateLicenseKey(args[0]); |
38 | | - } catch (err) { |
39 | | - await bot.sendMessage(chatId, '❌ ' + Validator.sanitizeForDisplay(err.message), { parse_mode: 'HTML' }); |
40 | | - return; |
41 | | - } |
42 | | - |
43 | | - // Show loading message (defined outside try to be accessible in catch) |
44 | | - let loadingMsg; |
45 | | - try { |
46 | | - loadingMsg = await bot.sendMessage(chatId, '🔄 Revoking license...'); |
47 | | - |
48 | | - // First, validate the license exists |
49 | | - const validationResult = await licenseClient.validateLicense(licenseKey); |
50 | | - |
51 | | - if (!validationResult.valid && validationResult.reason !== 'License has expired') { |
52 | | - await bot.editMessageText( |
53 | | - `❌ *License Not Found*\n\n` + |
54 | | - `License key \`${licenseKey}\` was not found.\n` + |
55 | | - `Reason: ${validationResult.reason || 'License key not found'}`, |
56 | | - { |
57 | | - chat_id: chatId, |
58 | | - message_id: loadingMsg.message_id, |
59 | | - parse_mode: 'Markdown' |
60 | | - } |
61 | | - ); |
62 | | - return; |
63 | | - } |
64 | | - |
65 | | - // API supports finding licenses by licenseKey, so we can use the key directly |
66 | | - const licenseId = licenseKey; |
67 | | - |
68 | | - try { |
69 | | - const result = await licenseClient.revokeLicense(licenseId); |
70 | | - |
71 | | - let message = `✅ *License Revoked*\n\n` + |
72 | | - `*License Key:* \`${licenseKey}\`\n` + |
73 | | - `*Status:* Revoked\n` + |
74 | | - `*Action:* License has been permanently revoked.\n\n` + |
75 | | - `⚠️ This license can no longer be used for validation.`; |
76 | | - |
77 | | - await bot.editMessageText(message, { |
78 | | - chat_id: chatId, |
79 | | - message_id: loadingMsg.message_id, |
80 | | - parse_mode: 'Markdown' |
81 | | - }); |
82 | | - } catch (revokeError) { |
83 | | - // If API revoke fails, try updating status to REVOKED |
84 | | - console.warn('Direct revoke failed, trying status update:', revokeError.message); |
85 | | - |
86 | | - try { |
87 | | - const updateResult = await licenseClient.updateLicense(licenseId, { |
88 | | - status: 'REVOKED' |
89 | | - }); |
90 | | - |
91 | | - let message = `✅ *License Revoked*\n\n` + |
92 | | - `*License Key:* \`${licenseKey}\`\n` + |
93 | | - `*Status:* Revoked\n` + |
94 | | - `*Action:* License status has been updated to REVOKED.\n\n` + |
95 | | - `⚠️ This license can no longer be used for validation.`; |
96 | | - |
97 | | - await bot.editMessageText(message, { |
98 | | - chat_id: chatId, |
99 | | - message_id: loadingMsg.message_id, |
100 | | - parse_mode: 'Markdown' |
101 | | - }); |
102 | | - } catch (updateError) { |
103 | | - throw new Error(`Failed to revoke license: ${updateError.message}`); |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - } catch (error) { |
108 | | - console.error('Error revoking license:', error); |
109 | | - |
110 | | - const errorMessage = `❌ *Revocation Failed*\n\n` + |
111 | | - `An error occurred while revoking the license:\n` + |
112 | | - `\`${error.message}\`\n\n` + |
113 | | - `Please check the license key and try again.`; |
114 | | - |
115 | | - // Check if loadingMsg exists before trying to edit it |
116 | | - if (loadingMsg && loadingMsg.message_id) { |
117 | | - await bot.editMessageText(errorMessage, { |
118 | | - chat_id: chatId, |
119 | | - message_id: loadingMsg.message_id, |
120 | | - parse_mode: 'Markdown' |
121 | | - }).catch(() => { |
122 | | - bot.sendMessage(chatId, errorMessage, { parse_mode: 'Markdown' }); |
123 | | - }); |
124 | | - } else { |
125 | | - await bot.sendMessage(chatId, errorMessage, { parse_mode: 'Markdown' }); |
126 | | - } |
127 | | - } |
128 | | - } |
129 | | -}; |
| 1 | +/** |
| 2 | + * Revoke License Command (Admin only) |
| 3 | + */ |
| 4 | + |
| 5 | +const Validator = require('../utils/Validator'); |
| 6 | +const PermissionManager = require('../utils/PermissionManager'); |
| 7 | + |
| 8 | +module.exports = { |
| 9 | + name: 'revoke', |
| 10 | + description: 'Revoke a license (Admin only)', |
| 11 | + |
| 12 | + async execute(msg, bot, licenseClient, dbManager) { |
| 13 | + const chatId = msg.chat.id; |
| 14 | + const userId = msg.from.id; |
| 15 | + const args = msg.text.split(' ').slice(1); |
| 16 | + |
| 17 | + const permissionManager = new PermissionManager(); |
| 18 | + try { |
| 19 | + permissionManager.requirePermission(userId, 'admin'); |
| 20 | + } catch (err) { |
| 21 | + await bot.sendMessage(chatId, '❌ ' + (err.message || 'Access denied. Administrators only.'), { parse_mode: 'Markdown' }); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + if (args.length === 0) { |
| 26 | + await bot.sendMessage(chatId, |
| 27 | + '❌ *Usage:* `/revoke <license_key>`\n\n' + |
| 28 | + 'Example: `/revoke LC-ABC123-DEF456-GHI789`\n\n' + |
| 29 | + '⚠️ *Warning:* This will permanently revoke the license.', |
| 30 | + { parse_mode: 'Markdown' } |
| 31 | + ); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + let licenseKey; |
| 36 | + try { |
| 37 | + licenseKey = Validator.validateLicenseKey(args[0]); |
| 38 | + } catch (err) { |
| 39 | + await bot.sendMessage(chatId, '❌ ' + Validator.sanitizeForDisplay(err.message), { parse_mode: 'HTML' }); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Show loading message (defined outside try to be accessible in catch) |
| 44 | + let loadingMsg; |
| 45 | + try { |
| 46 | + loadingMsg = await bot.sendMessage(chatId, '🔄 Revoking license...'); |
| 47 | + |
| 48 | + // First, validate the license exists |
| 49 | + const validationResult = await licenseClient.validateLicense(licenseKey); |
| 50 | + |
| 51 | + if (!validationResult.valid && validationResult.reason !== 'License has expired') { |
| 52 | + await bot.editMessageText( |
| 53 | + `❌ *License Not Found*\n\n` + |
| 54 | + `License key \`${licenseKey}\` was not found.\n` + |
| 55 | + `Reason: ${validationResult.reason || 'License key not found'}`, |
| 56 | + { |
| 57 | + chat_id: chatId, |
| 58 | + message_id: loadingMsg.message_id, |
| 59 | + parse_mode: 'Markdown' |
| 60 | + } |
| 61 | + ); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // API supports finding licenses by licenseKey, so we can use the key directly |
| 66 | + const licenseId = licenseKey; |
| 67 | + |
| 68 | + try { |
| 69 | + const result = await licenseClient.revokeLicense(licenseId); |
| 70 | + |
| 71 | + let message = `✅ *License Revoked*\n\n` + |
| 72 | + `*License Key:* \`${licenseKey}\`\n` + |
| 73 | + `*Status:* Revoked\n` + |
| 74 | + `*Action:* License has been permanently revoked.\n\n` + |
| 75 | + `⚠️ This license can no longer be used for validation.`; |
| 76 | + |
| 77 | + await bot.editMessageText(message, { |
| 78 | + chat_id: chatId, |
| 79 | + message_id: loadingMsg.message_id, |
| 80 | + parse_mode: 'Markdown' |
| 81 | + }); |
| 82 | + } catch (revokeError) { |
| 83 | + // If API revoke fails, try updating status to REVOKED |
| 84 | + console.warn('Direct revoke failed, trying status update:', revokeError.message); |
| 85 | + |
| 86 | + try { |
| 87 | + const updateResult = await licenseClient.updateLicense(licenseId, { |
| 88 | + status: 'REVOKED' |
| 89 | + }); |
| 90 | + |
| 91 | + let message = `✅ *License Revoked*\n\n` + |
| 92 | + `*License Key:* \`${licenseKey}\`\n` + |
| 93 | + `*Status:* Revoked\n` + |
| 94 | + `*Action:* License status has been updated to REVOKED.\n\n` + |
| 95 | + `⚠️ This license can no longer be used for validation.`; |
| 96 | + |
| 97 | + await bot.editMessageText(message, { |
| 98 | + chat_id: chatId, |
| 99 | + message_id: loadingMsg.message_id, |
| 100 | + parse_mode: 'Markdown' |
| 101 | + }); |
| 102 | + } catch (updateError) { |
| 103 | + throw new Error(`Failed to revoke license: ${updateError.message}`); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + } catch (error) { |
| 108 | + console.error('Error revoking license:', error); |
| 109 | + |
| 110 | + const errorMessage = `❌ *Revocation Failed*\n\n` + |
| 111 | + `An error occurred while revoking the license:\n` + |
| 112 | + `\`${error.message}\`\n\n` + |
| 113 | + `Please check the license key and try again.`; |
| 114 | + |
| 115 | + // Check if loadingMsg exists before trying to edit it |
| 116 | + if (loadingMsg && loadingMsg.message_id) { |
| 117 | + await bot.editMessageText(errorMessage, { |
| 118 | + chat_id: chatId, |
| 119 | + message_id: loadingMsg.message_id, |
| 120 | + parse_mode: 'Markdown' |
| 121 | + }).catch(() => { |
| 122 | + bot.sendMessage(chatId, errorMessage, { parse_mode: 'Markdown' }); |
| 123 | + }); |
| 124 | + } else { |
| 125 | + await bot.sendMessage(chatId, errorMessage, { parse_mode: 'Markdown' }); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +}; |
0 commit comments