channel-partner.js 5.57 KB
"use strict";

/**
 * channel-partner controller
 */

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

module.exports = createCoreController(
  "api::channel-partner.channel-partner",
  ({ strapi: Strapi }) => ({
    async create(ctx) {
      console.log(ctx);
      //   const currentChannelPartner = ctx.state.user;
      const reraNumber = ctx.request.body?.data?.reraNumber;
      console.log(reraNumber);
      if (!reraNumber) {
        return ctx.bad(`Please specify your rera number.`);
      }

      // 1. check if the channel  partner already  exist
      const existingChannelPartner = await strapi.entityService.findMany(
        "api::channel-partner.channel-partner",
        {
          filters: { reraNumber: reraNumber },
        }
      );
      // if (existingChannelPartner && existingChannelPartner.length !== 0) {
      //   return ctx.badRequest(
      //     `Channel Partner Already Exist with the Given rera number ${reraNumber}`
      //   );
      // }
      // try {
      //   const spertoRegisterCP = await strapi
      //     .service("api::channel-partner.channel-partner")
      //     .sendCPToSperto(ctx.request.body.data);
      //   console.log("Helllo >>>>>>>>", spertoRegisterCP);

      //   ctx.request.body.data.httpRegisterCPRequestHeaders = JSON.stringify(
      //     spertoRegisterCP.headers
      //   );
      //   ctx.request.body.data.httpRegisterCPRequestMethod =
      //     spertoRegisterCP.config.method;
      //   ctx.request.body.data.httpRegisterCPRequestUrl =
      //     spertoRegisterCP.config.url;
      //   ctx.request.body.data.httpsRegisterCPRequestBody =
      //     spertoRegisterCP.config.data;
      //   ctx.request.body.data.httpRegisterCPResposneBody = JSON.stringify(
      //     spertoRegisterCP.data
      //   );
      //   ctx.request.body.data.thirdPartyApiRegisterCPError = false;
      // } catch (error) {
      //   ctx.request.body.data.httpRegisterCPRequestHeaders = JSON.stringify(
      //         error.config.headers
      //       );
      //       ctx.request.body.data.httpRegisterCPRequestMethod = error.config.method;

      //       ctx.request.body.data.httpRegisterCPRequestUrl = error.config.url;
      //       ctx.request.body.data.httpsRegisterCPRequestBody = error.config.data;
      //       ctx.request.body.data.httpRegisterCPResposneBody = JSON.stringify(error.message);
      //       ctx.request.body.data.thirdPartyApiRegisterCPError=true;
      // }

      return await super.create(ctx);
    },
    async regsiterCPToSperto(ctx) {
      // console.log(ctx);
      //   const currentChannelPartner = ctx.state.user;
      const reraNumber = ctx.request.body.reraNumber;
      // console.log(reraNumber);

      // if (existingChannelPartner && existingChannelPartner.length !== 0) {
      //   return ctx.badRequest(
      //     `Channel Partner Already Exist with the Given rera number ${reraNumber}`
      //   );
      // }
      try {
        const spertoRegisterCP = await strapi
          .service("api::channel-partner.channel-partner")
          .sendCPToSperto(ctx.request.body);
        // console.log("Helllo >>>>>>>>", spertoRegisterCP);

        ctx.request.body.httpRegisterCPRequestHeaders = JSON.stringify(
          spertoRegisterCP.headers
        );
        ctx.request.body.httpRegisterCPRequestMethod =
          spertoRegisterCP.config.method;
        ctx.request.body.httpRegisterCPRequestUrl = spertoRegisterCP.config.url;
        ctx.request.body.httpsRegisterCPRequestBody =
          spertoRegisterCP.config.data;
        ctx.request.body.httpRegisterCPResposneBody = JSON.stringify(
          spertoRegisterCP.data
        );
        ctx.request.body.thirdPartyApiRegisterCPError = false;
      } catch (error) {
        ctx.request.body.httpRegisterCPRequestHeaders = JSON.stringify(
          error.config.headers
        );
        ctx.request.body.httpRegisterCPRequestMethod = error.config.method;

        ctx.request.body.httpRegisterCPRequestUrl = error.config.url;
        ctx.request.body.httpsRegisterCPRequestBody = error.config.data;
        ctx.request.body.httpRegisterCPResposneBody = JSON.stringify(
          error.message
        );
        ctx.request.body.thirdPartyApiRegisterCPError = true;
      }
      // const existingChannelPartner = await strapi.entityService.findOne(
      //   "api::channel-partner.channel-partner",
      //   where: {
      //      reraNumber: reraNumber
      //   }
      // );
      const existingChannelPartner = await strapi
        .query("api::channel-partner.channel-partner")
        .findOne({
          // populate: ["id"],
          where: {
            $and: [{ reraNumber: reraNumber }],
          },
        });
      console.log("existingChannelPartner >>>>>>>>>>", existingChannelPartner);
      const res = await strapi.entityService.update(
        "api::channel-partner.channel-partner",
        existingChannelPartner.id,
        {
          data: {
            httpRegisterCPRequestHeaders:
              ctx.request.body.httpRegisterCPRequestHeaders,
            httpRegisterCPRequestMethod:
              ctx.request.body.httpRegisterCPRequestMethod,

            httpRegisterCPRequestUrl: ctx.request.body.httpRegisterCPRequestUrl,

            httpsRegisterCPRequestBody:
              ctx.request.body.httpsRegisterCPRequestBody,

            httpRegisterCPResposneBody:
              ctx.request.body.httpRegisterCPResposneBody,
            thirdPartyApiRegisterCPError:
              ctx.request.body.thirdPartyApiRegisterCPError,
          },
        }
      );
    console.log("res >>>>>",res);
    ctx.send({ ok: true, data: res});

    },
  })
);