Blame view

src/api/lead/controllers/lead.js 1.47 KB
kashif ansari committed
1
"use strict";
jay committed
2 3 4 5 6

/**
 * lead controller
 */

kashif ansari committed
7
const { createCoreController } = require("@strapi/strapi").factories;
jay committed
8

kashif ansari committed
9 10 11 12
module.exports = createCoreController("api::lead.lead", () => ({
  async sendLeads(ctx) {},

  async create(ctx) {
.  
jay committed
13
    // console.log(">>>>", ctx);
kashif ansari committed
14 15 16 17 18
    try {
      const spretoLeadData = await strapi
        .service("api::lead.lead")
        .sendLeadToSperto(ctx.request.body.data);

.  
jay committed
19
      // console.log("spretoLeadData", spretoLeadData);
kashif ansari committed
20 21 22 23 24 25 26 27 28
      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
      );
jay committed
29
      ctx.request.body.data.thirdPartyApiError = false;
kashif ansari committed
30 31 32 33 34 35 36 37
    } 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);
jay committed
38
      ctx.request.body.data.thirdPartyApiError = true;
kashif ansari committed
39 40 41 42 43 44 45 46
    }
    const leadData = await super.create(ctx);

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