lead.js 1.28 KB
"use strict";

/**
 * lead controller
 */

const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController("api::lead.lead", () => ({
  async sendLeads(ctx) {},

  async create(ctx) {
    try {
      const spretoLeadData = await strapi
        .service("api::lead.lead")
        .sendLeadToSperto(ctx.request.body.data);

      ctx.request.body.data.httpRequestHeaders = JSON.stringify(
        spretoLeadData.headers
      );
      ctx.request.body.data.httpRequestMethod = spretoLeadData.config.method;
      ctx.request.body.data.httpRequestUrl = spretoLeadData.config.url;
      ctx.request.body.data.httpsRequestBody = spretoLeadData.config.data;
      ctx.request.body.data.httpResposneBody = JSON.stringify(
        spretoLeadData.data
      );
    } catch (error) {
      ctx.request.body.data.httpRequestHeaders = JSON.stringify(
        error.config.headers
      );
      ctx.request.body.data.httpRequestMethod = error.config.method;
      ctx.request.body.data.httpRequestUrl = error.config.url;
      ctx.request.body.data.httpsRequestBody = error.config.data;
      ctx.request.body.data.httpResposneBody = JSON.stringify(error.message);
    }
    const leadData = await super.create(ctx);

    return ctx.send({
      leadData,
    });
  },
}));