channel-partner.js
5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"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});
},
})
);