# All files / controllers WhatsAppCloudFlowController.js

0% Statements 0/30
0% Branches 0/21
0% Functions 0/3
0% Lines 0/30

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

     const WhatsAppCloudFlow = require('../models/WhatsAppCloudFlow');
    const { logger } = require('../config/logger');

    class WhatsAppCloudFlowController {
      static async list(req, res) {
        try {
          const tenantId = req.tenantId || req.user?.tenantId || 1;
          const { accountId } = req.query;
          const flows = await WhatsAppCloudFlow.listByTenant(tenantId, accountId ? String(accountId) : null);
          res.json({ success: true, data: flows });
        } catch (error) {
          logger.error('Error listing flows', { error: error.message });
          res.status(500).json({ success: false, error: 'Failed to load flows' });
        }
      }

      static async save(req, res) {
        try {
          const tenantId = req.tenantId || req.user?.tenantId || 1;
          const flow = req.body || {};
          if (!flow.id || !flow.name) {
            return res.status(400).json({ success: false, error: 'Flow id and name are required' });
          }
          const saved = await WhatsAppCloudFlow.upsert(tenantId, flow);
          res.json({ success: true, data: saved });
        } catch (error) {
          logger.error('Error saving flow', { error: error.message });
          res.status(500).json({ success: false, error: 'Failed to save flow' });
        }
      }

      static async delete(req, res) {
        try {
          const tenantId = req.tenantId || req.user?.tenantId || 1;
          const { flowId } = req.params;
          if (!flowId) {
            return res.status(400).json({ success: false, error: 'Flow id is required' });
          }
          const removed = await WhatsAppCloudFlow.deleteByFlowId(tenantId, flowId);
          if (!removed) {
            return res.status(404).json({ success: false, error: 'Flow not found' });
          }
          res.json({ success: true });
        } catch (error) {
          logger.error('Error deleting flow', { error: error.message });
          res.status(500).json({ success: false, error: 'Failed to delete flow' });
        }
      }
    }

    module.exports = WhatsAppCloudFlowController;