'use strict';

const helpers = require('../../../helpers');
const logger = require('../../../logger');
const UnprocessibleEntityError = require('../../../../app/exceptions/UnprocessibleEntityError');

module.exports = function params() {
  return (req, res, next) => {
    const body = req.body;
    const userId = helpers.request.getUserIdParamFromReq(req);
    logger.debug('origin=subscriptionStatusActive', { body }, req);
    let error;

    helpers.request.setUserId(req, userId);
    if (!req.userId) {
      error = new UnprocessibleEntityError('Missing required userId.');
      return helpers.sendErrorResponse(res, error);
    }

    helpers.request.setPlatform(req, body.platform);

    // Set subscriptionStatusActive template properties
    const template = helpers.template.getSubscriptionStatusActive();

    logger.debug('template', template);
    helpers.request.setOutboundMessageTemplate(req, template.name);
    helpers.request.setOutboundMessageText(req, template.text);

    return next();
  };
};

'use strict';

const helpers = require('../../../helpers');
const logger = require('../../../logger');
const UnprocessibleEntityError = require('../../../../app/exceptions/UnprocessibleEntityError');

module.exports = function params() {
  return (req, res, next) => {
    const body = req.body;
    const userId = helpers.request.getUserIdParamFromReq(req);
    logger.debug('origin=subscriptionStatusActive', { body }, req);
    let error;

    helpers.request.setUserId(req, userId);
    if (!req.userId) {
      error = new UnprocessibleEntityError('Missing required userId.');
      return helpers.sendErrorResponse(res, error);
    }

    helpers.request.setPlatform(req, body.platform);

    // Set subscriptionStatusActive template properties
    const template = helpers.template.getSubscriptionStatusActive();

    logger.debug('template', { template }, req);
    helpers.request.setOutboundMessageTemplate(req, template.name);
    helpers.request.setOutboundMessageText(req, template.text);

    return next();
  };
};

add request_id to template debug log in subscription-status-active/params middleware