referral.js 2.19 KB
"use strict";

/**
 * referral controller
 */

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

module.exports = createCoreController("api::referral.referral", () => ({
  async create(ctx) {
    console.log("create started");
    try {
      const spertoRefereeData = await strapi
        .service("api::referral.referral")
        .sendReferee(ctx.request.body.data);
        console.log("spertoRefereeData", spertoRefereeData.data.member_id);
        const spertoReferralData = await strapi.service("api::referral.referral").sendReferral({...ctx.request.body.data, parent_id: spertoRefereeData.data.member_id})

      ctx.request.body.data.httpRequestHeaders = JSON.stringify(
        spertoRefereeData.headers
      );
      ctx.request.body.data.httpReferralRequestHeaders = JSON.stringify(
        spertoReferralData.headers
      );

      ctx.request.body.data.httpRequestMethod = spertoRefereeData.config.method;
      ctx.request.body.data.httpReferralRequestMethod = spertoReferralData.config.method;

      ctx.request.body.data.httpReferralRequestUrl = spertoReferralData.config.url;
      ctx.request.body.data.httpRequestUrl = spertoRefereeData.config.url;

      ctx.request.body.data.httpsReferralRequestBody = spertoReferralData.config.data;
      ctx.request.body.data.httpsRequestBody = spertoRefereeData.config.data;

      ctx.request.body.data.httpResposneBody = JSON.stringify(
        spertoRefereeData.data
      ); 
      ctx.request.body.data.httpReferralResposneBody = JSON.stringify(
        spertoReferralData.data
      );

      ctx.request.body.data.thirdPartyApiError = false;
    } catch (error) {
      console.log("error", 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 referralData = await super.create(ctx);

    return ctx.send({
      referralData,
    });


  },
}));