# All files / controllers SystemAddonController.js

0% Statements 0/159
0% Branches 0/94
0% Functions 0/14
0% Lines 0/156

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433

     /**
     * System Addon Controller
     * Manages system add-ons (plugins) that can be uploaded and activated
     *
     * @class SystemAddonController
     */

    const BaseController = require('./BaseController');
    const { ValidationError, NotFoundError } = require('../middleware/errorHandler');
    const { logger } = require('../config/logger');
    const { clearAddonCache } = require('../middleware/addonCheck');
    const path = require('path');
    const fs = require('fs').promises;
    const fsSync = require('fs');
    const AdmZip = require('adm-zip');

    // Directory where addons are stored
    const ADDONS_DIR = path.join(__dirname, '..', 'addons');
    const UPLOADS_TEMP_DIR = path.join(__dirname, '..', 'uploads', 'temp');

    class SystemAddonController extends BaseController {
      /**
       * Get all system addons
       * GET /api/superadmin/system-addons
       */
      static async getAddons(req, res) {
        try {
          const addons = await BaseController.executeQuery(`
            SELECT * FROM system_addons ORDER BY created_at DESC
          `);

          // Check for icon images in each addon
          const addonsWithIcons = addons.map(addon => {
            const hasIconImage = SystemAddonController.checkAddonHasIconImage(addon.directory);
            return { ...addon, has_icon_image: hasIconImage };
          });

          return BaseController.sendSuccess(res, { addons: addonsWithIcons });
        } catch (error) {
          logger.error('Error getting system addons', { error: error.message });
          return BaseController.sendError(res, error.message, 500);
        }
      }

      /**
       * Check if addon has an icon image file
       */
      static checkAddonHasIconImage(directory) {
        if (!directory) return false;

        const iconExtensions = ['.png', '.jpg', '.jpeg', '.svg', '.ico', '.webp'];
        const addonDir = path.join(ADDONS_DIR, directory);

        for (const ext of iconExtensions) {
          const iconPath = path.join(addonDir, `icon${ext}`);
          if (fsSync.existsSync(iconPath)) {
            return true;
          }
        }
        return false;
      }

      /**
       * Get single addon by ID
       * GET /api/superadmin/system-addons/:id
       */
      static async getAddon(req, res) {
        try {
          const { id } = req.params;

          const addons = await BaseController.executeQuery(
            'SELECT * FROM system_addons WHERE id = ?',
            [id]
          );

          if (addons.length === 0) {
            throw new NotFoundError('Addon not found');
          }

          return BaseController.sendSuccess(res, { addon: addons[0] });
        } catch (error) {
          logger.error('Error getting addon', { error: error.message });
          return BaseController.sendError(res, error.message, error.statusCode || 500);
        }
      }

      /**
       * Upload and install a new addon
       * POST /api/superadmin/system-addons/upload
       */
      static async uploadAddon(req, res) {
        try {
          if (!req.file) {
            throw new ValidationError('No file uploaded');
          }

          const zipPath = req.file.path;
          const originalName = req.file.originalname;

          // Validate file extension
          if (!originalName.endsWith('.zip')) {
            await fs.unlink(zipPath);
            throw new ValidationError('Only ZIP files are allowed');
          }

          // Create addons directory if it doesn't exist
          await fs.mkdir(ADDONS_DIR, { recursive: true });

          // Extract and validate the addon
          const addonInfo = await SystemAddonController.extractAndValidateAddon(zipPath);

          // Check if addon already exists
          const existing = await BaseController.executeQuery(
            'SELECT id FROM system_addons WHERE slug = ?',
            [addonInfo.slug]
          );

          if (existing.length > 0) {
            // Update existing addon
            await BaseController.executeQuery(`
              UPDATE system_addons SET
                name = ?,
                description = ?,
                version = ?,
                author = ?,
                icon = ?,
                config = ?,
                updated_at = NOW()
              WHERE slug = ?
            `, [
              addonInfo.name,
              addonInfo.description || '',
              addonInfo.version || '1.0.0',
              addonInfo.author || '',
              addonInfo.icon || 'puzzle-piece',
              JSON.stringify(addonInfo.config || {}),
              addonInfo.slug
            ]);

            logger.info(`Addon updated: ${addonInfo.name}`);

            return BaseController.sendSuccess(res, {
              addon: { ...addonInfo, id: existing[0].id },
              updated: true
            }, 200, 'Addon updated successfully');
          }

          // Insert new addon
          const result = await BaseController.executeQuery(`
            INSERT INTO system_addons (
              slug, name, description, version, author, icon,
              directory, config, active, created_at
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, FALSE, NOW())
          `, [
            addonInfo.slug,
            addonInfo.name,
            addonInfo.description || '',
            addonInfo.version || '1.0.0',
            addonInfo.author || '',
            addonInfo.icon || 'puzzle-piece',
            addonInfo.directory,
            JSON.stringify(addonInfo.config || {})
          ]);

          // Clean up temp file
          await fs.unlink(zipPath).catch(() => {});

          logger.info(`Addon installed: ${addonInfo.name}`);

          return BaseController.sendSuccess(res, {
            addon: { id: result.insertId, ...addonInfo }
          }, 201, 'Addon installed successfully');
        } catch (error) {
          // Clean up temp file on error
          if (req.file && req.file.path) {
            await fs.unlink(req.file.path).catch(() => {});
          }
          logger.error('Error uploading addon', { error: error.message });
          return BaseController.sendError(res, error.message, error.statusCode || 500);
        }
      }

      /**
       * Extract and validate addon from ZIP file
       */
      static async extractAndValidateAddon(zipPath) {
        try {
          const zip = new AdmZip(zipPath);
          const zipEntries = zip.getEntries();

          // Find addon.json in the ZIP
          let addonJsonEntry = null;
          let rootDir = '';

          for (const entry of zipEntries) {
            if (entry.entryName.endsWith('addon.json') && !entry.isDirectory) {
              addonJsonEntry = entry;
              // Get the root directory (if any)
              const parts = entry.entryName.split('/');
              if (parts.length > 1) {
                rootDir = parts[0];
              }
              break;
            }
          }

          if (!addonJsonEntry) {
            throw new ValidationError('Invalid addon: addon.json not found');
          }

          // Parse addon.json
          const addonJsonContent = addonJsonEntry.getData().toString('utf8');
          let addonInfo;

          try {
            addonInfo = JSON.parse(addonJsonContent);
          } catch (e) {
            throw new ValidationError('Invalid addon.json format');
          }

          // Validate required fields
          if (!addonInfo.slug || !addonInfo.name) {
            throw new ValidationError('addon.json must contain slug and name');
          }

          // Validate slug format (alphanumeric and hyphens only)
          if (!/^[a-z0-9-]+$/.test(addonInfo.slug)) {
            throw new ValidationError('Addon slug must contain only lowercase letters, numbers, and hyphens');
          }

          // Create addon directory
          const addonDir = path.join(ADDONS_DIR, addonInfo.slug);

          // Remove existing directory if it exists
          await fs.rm(addonDir, { recursive: true, force: true }).catch(() => {});
          await fs.mkdir(addonDir, { recursive: true });

          // Extract files to addon directory
          for (const entry of zipEntries) {
            if (entry.isDirectory) continue;

            let targetPath = entry.entryName;

            // Remove root directory prefix if present
            if (rootDir && targetPath.startsWith(rootDir + '/')) {
              targetPath = targetPath.substring(rootDir.length + 1);
            }

            if (!targetPath) continue;

            const fullPath = path.join(addonDir, targetPath);
            const dirPath = path.dirname(fullPath);

            // Create directory structure
            await fs.mkdir(dirPath, { recursive: true });

            // Write file
            await fs.writeFile(fullPath, entry.getData());
          }

          addonInfo.directory = addonInfo.slug;

          return addonInfo;
        } catch (error) {
          if (error instanceof ValidationError) {
            throw error;
          }
          throw new ValidationError(`Failed to extract addon: ${error.message}`);
        }
      }

      /**
       * Toggle addon active status
       * PUT /api/superadmin/system-addons/:id/toggle
       */
      static async toggleAddon(req, res) {
        try {
          const { id } = req.params;

          // Get current addon
          const addons = await BaseController.executeQuery(
            'SELECT * FROM system_addons WHERE id = ?',
            [id]
          );

          if (addons.length === 0) {
            throw new NotFoundError('Addon not found');
          }

          const addon = addons[0];
          const newStatus = !addon.active;

          // If activating, run activation hook
          if (newStatus) {
            await SystemAddonController.runAddonHook(addon, 'activate');
          } else {
            // If deactivating, run deactivation hook
            await SystemAddonController.runAddonHook(addon, 'deactivate');
          }

          // Update status
          await BaseController.executeQuery(
            'UPDATE system_addons SET active = ?, updated_at = NOW() WHERE id = ?',
            [newStatus ? 1 : 0, id]
          );

          // Clear addon cache so changes take effect immediately
          clearAddonCache(addon.slug);

          logger.info(`Addon ${newStatus ? 'activated' : 'deactivated'}: ${addon.name}`);

          return BaseController.sendSuccess(res, {
            addon: { ...addon, active: newStatus }
          }, 200, `Addon ${newStatus ? 'activated' : 'deactivated'} successfully`);
        } catch (error) {
          logger.error('Error toggling addon', { error: error.message });
          return BaseController.sendError(res, error.message, error.statusCode || 500);
        }
      }

      /**
       * Run addon lifecycle hook
       */
      static async runAddonHook(addon, hookName) {
        try {
          const hookPath = path.join(ADDONS_DIR, addon.directory, 'hooks', `${hookName}.js`);

          if (fsSync.existsSync(hookPath)) {
            const hook = require(hookPath);
            if (typeof hook === 'function') {
              await hook();
            } else if (typeof hook[hookName] === 'function') {
              await hook[hookName]();
            }
            logger.info(`Addon hook executed: ${addon.slug}/${hookName}`);
          }
        } catch (error) {
          logger.error(`Error running addon hook: ${addon.slug}/${hookName}`, { error: error.message });
          // Don't throw - hooks are optional
        }
      }

      /**
       * Delete addon
       * DELETE /api/superadmin/system-addons/:id
       */
      static async deleteAddon(req, res) {
        try {
          const { id } = req.params;

          // Get addon
          const addons = await BaseController.executeQuery(
            'SELECT * FROM system_addons WHERE id = ?',
            [id]
          );

          if (addons.length === 0) {
            throw new NotFoundError('Addon not found');
          }

          const addon = addons[0];

          // Run uninstall hook if exists
          await SystemAddonController.runAddonHook(addon, 'uninstall');

          // Delete addon directory
          const addonDir = path.join(ADDONS_DIR, addon.directory);
          await fs.rm(addonDir, { recursive: true, force: true }).catch(() => {});

          // Delete from database
          await BaseController.executeQuery('DELETE FROM system_addons WHERE id = ?', [id]);

          logger.info(`Addon deleted: ${addon.name}`);

          return BaseController.sendSuccess(res, null, 200, 'Addon deleted successfully');
        } catch (error) {
          logger.error('Error deleting addon', { error: error.message });
          return BaseController.sendError(res, error.message, error.statusCode || 500);
        }
      }

      /**
       * Get addon icon image
       * GET /api/superadmin/system-addons/:id/icon
       */
      static async getAddonIcon(req, res) {
        try {
          const { id } = req.params;

          const addons = await BaseController.executeQuery(
            'SELECT directory, icon FROM system_addons WHERE id = ?',
            [id]
          );

          if (addons.length === 0) {
            throw new NotFoundError('Addon not found');
          }

          const addon = addons[0];

          // Check for icon file in addon directory
          const iconExtensions = ['.png', '.jpg', '.jpeg', '.svg', '.ico', '.webp'];
          const addonDir = path.join(ADDONS_DIR, addon.directory);

          for (const ext of iconExtensions) {
            const iconPath = path.join(addonDir, `icon${ext}`);
            if (fsSync.existsSync(iconPath)) {
              // Set appropriate content type
              const contentTypes = {
                '.png': 'image/png',
                '.jpg': 'image/jpeg',
                '.jpeg': 'image/jpeg',
                '.svg': 'image/svg+xml',
                '.ico': 'image/x-icon',
                '.webp': 'image/webp'
              };
              res.setHeader('Content-Type', contentTypes[ext] || 'image/png');
              res.setHeader('Cache-Control', 'public, max-age=86400'); // Cache for 24h
              return res.sendFile(iconPath);
            }
          }

          // No icon image found - return 404
          return res.status(404).json({ error: 'Icon not found' });
        } catch (error) {
          logger.error('Error getting addon icon', { error: error.message });
          return BaseController.sendError(res, error.message, error.statusCode || 500);
        }
      }
    }

    module.exports = SystemAddonController;