lead.js 1.47 KB
"use strict";

/**
 * lead controller
 */

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

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

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

      console.log("spretoLeadData", spretoLeadData);
      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
      );
      ctx.request.body.data.thirdPartyApiError = false;
    } 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);
      ctx.request.body.data.thirdPartyApiError = true;
    }
    const leadData = await super.create(ctx);

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