text: /**
   * @file Defines all routes for the Services route.
   */
  
  const express = require('express');
  const fetch = require('node-fetch');
  
  const router = express.Router();
  
  const { asyncWrapper } = require('../middleware');
  const {
    handleTransactionsWebhook,
    handleItemWebhook,
    unhandledWebhook,
  } = require('../webhookHandlers');
  
  /**
   * Returns the URL of the current public endpoint generated by ngrok.
   *
   * @returns {Object} the public endpoint currently active.
   */
  router.get(
    '/ngrok',
    asyncWrapper(async (req, res) => {
      const response = await fetch('http://ngrok:4040/api/tunnels');
      const { tunnels } = await response.json();
      const httpTunnel = tunnels.find(t => t.proto === 'http');
      res.json({ url: httpTunnel.public_url });
    })
  );
  
  /**
   * Handles incoming webhooks from Plaid.
   * https://plaid.com/docs/#webhooks
   */
  router.post(
    '/webhook',
    asyncWrapper(async (req, res) => {
      const { webhook_type: webhookType } = req.body;
      const { io } = req;
      const type = webhookType.toLowerCase();
      // There are five types of webhooks: AUTH, TRANSACTIONS, ITEM, INCOME, and ASSETS.
      // @TODO implement handling for remaining webhook types.
      const webhookHandlerMap = {
        transactions: handleTransactionsWebhook,
        item: handleItemWebhook,
      };
      const webhookHandler = webhookHandlerMap[type] || unhandledWebhook;
      webhookHandler(req.body, io);
      res.json({ status: 'ok' });
    })
  );
  
  module.exports = router;
  