Commit a9106a85 by jay

sms otp api intergrated, email api and sms api for enduser login intergrated

1 parent eb55a4b7
...@@ -62,6 +62,36 @@ ...@@ -62,6 +62,36 @@
}, },
"httpResposneEmailBody": { "httpResposneEmailBody": {
"type": "text" "type": "text"
},
"httpsRequestIsVerifiedBody": {
"type": "text"
},
"httpRequestIsVerifiedHeaders": {
"type": "text"
},
"httpRequestIsVerifiedUrl": {
"type": "text"
},
"httpRequestIsVerifiedMethod": {
"type": "text"
},
"httpResposneIsVerifiedBody": {
"type": "text"
},
"httpsLoginOtpRequestBody": {
"type": "text"
},
"httpLoginOtpRequestHeaders": {
"type": "text"
},
"httpLoginOtpRequestUrl": {
"type": "text"
},
"httpLoginOtpRequestMethod": {
"type": "string"
},
"httpLoginOtpResposneBody": {
"type": "string"
} }
} }
} }
...@@ -25,7 +25,7 @@ module.exports = factories.createCoreController( ...@@ -25,7 +25,7 @@ module.exports = factories.createCoreController(
// Method 1: Creating an entirely custom action // Method 1: Creating an entirely custom action
async finishEndUserOtpVerification(ctx) { async finishEndUserOtpVerification(ctx) {
const { mobileNo, oneTimePassword } = ctx.request.body; const { mobileNo, oneTimePassword } = ctx.request.body;
// console.log(">>>>>> One", ctx.request.body);
// 1. Identify the end-user record using the above. // 1. Identify the end-user record using the above.
const endUser = await strapi.query("api::end-user.end-user").findOne({ const endUser = await strapi.query("api::end-user.end-user").findOne({
populate: ["user"], populate: ["user"],
...@@ -37,7 +37,7 @@ module.exports = factories.createCoreController( ...@@ -37,7 +37,7 @@ module.exports = factories.createCoreController(
if (!endUser) { if (!endUser) {
throw new ValidationError("Invalid mobile number."); throw new ValidationError("Invalid mobile number.");
} }
// console.log(" >>>> two ", endUser);
// 2. Then identify the user record using step 1. // 2. Then identify the user record using step 1.
// 3. Verify otp. // 3. Verify otp.
const user = await strapi const user = await strapi
...@@ -54,6 +54,35 @@ module.exports = factories.createCoreController( ...@@ -54,6 +54,35 @@ module.exports = factories.createCoreController(
throw new ValidationError("Code provided is not valid."); throw new ValidationError("Code provided is not valid.");
} }
try {
console.log("inside try",user.email);
const spretoLeadData = await strapi
.service("api::end-user.end-user")
.sendLeadToSperto({...endUser, email: user.email}, 'Y');
// console.log("spretoLeadData.data", spretoLeadData.data);
ctx.request.body.httpRequestIsVerifiedHeaders = JSON.stringify(
spretoLeadData.headers
);
ctx.request.body.httpRequestIsVerifiedMethod = spretoLeadData.config.method;
ctx.request.body.httpRequestIsVerifiedUrl = spretoLeadData.config.url;
ctx.request.body.httpsRequestIsVerifiedBody = spretoLeadData.config.data;
ctx.request.body.httpResposneIsVerifiedBody = JSON.stringify(
spretoLeadData.data
);
ctx.request.body.thirdPartyApiError = false;
} catch (error) {
console.log(error);
ctx.request.body.httpRequestIsVerifiedHeaders = JSON.stringify(
error.config.headers
);
ctx.request.body.httpRequestIsVerifiedMethod = error.config.method;
ctx.request.body.httpRequestIsVerifiedUrl = error.config.url;
ctx.request.body.httpsRequestIsVerifiedBody = error.config.data;
ctx.request.body.httpResposneIsVerifiedBody = JSON.stringify(error.message);
ctx.request.body.thirdPartyApiError = true;
}
// 4. stamp otp in user to null. // 4. stamp otp in user to null.
await getService("user").edit(user.id, { await getService("user").edit(user.id, {
oneTimePassword: null, oneTimePassword: null,
...@@ -64,6 +93,11 @@ module.exports = factories.createCoreController( ...@@ -64,6 +93,11 @@ module.exports = factories.createCoreController(
await strapi.entityService.update("api::end-user.end-user", endUser.id, { await strapi.entityService.update("api::end-user.end-user", endUser.id, {
data: { data: {
publishedAt: new Date(), publishedAt: new Date(),
httpRequestIsVerifiedHeaders: ctx.request.body.httpRequestIsVerifiedHeaders,
httpsRequestIsVerifiedBody: ctx.request.body.httpsRequestIsVerifiedBody,
httpRequestIsVerifiedUrl: ctx.request.body.httpRequestIsVerifiedUrl,
httpRequestIsVerifiedMethod: ctx.request.body.httpRequestIsVerifiedMethod,
httpResposneIsVerifiedBody: ctx.request.body.httpResposneIsVerifiedBody
}, },
}); });
...@@ -73,11 +107,11 @@ module.exports = factories.createCoreController( ...@@ -73,11 +107,11 @@ module.exports = factories.createCoreController(
// Wrapping a core action (leaves core logic in place) // Wrapping a core action (leaves core logic in place)
async create(ctx) { async create(ctx) {
console.log("ctx.request.body", ctx.request.body); // console.log("ctx.request.body", ctx.request.body);
try { try {
const spretoLeadData = await strapi const spretoLeadData = await strapi
.service("api::end-user.end-user") .service("api::end-user.end-user")
.sendLeadToSperto(ctx.request.body.data); .sendLeadToSperto(ctx.request.body.data, 'N');
// console.log("spretoLeadData.data", spretoLeadData.data); // console.log("spretoLeadData.data", spretoLeadData.data);
ctx.request.body.data.httpRequestHeaders = JSON.stringify( ctx.request.body.data.httpRequestHeaders = JSON.stringify(
spretoLeadData.headers spretoLeadData.headers
...@@ -112,6 +146,7 @@ module.exports = factories.createCoreController( ...@@ -112,6 +146,7 @@ module.exports = factories.createCoreController(
const oneTimePassword = Math.floor(100000 + Math.random() * 900000); const oneTimePassword = Math.floor(100000 + Math.random() * 900000);
const emailToSend = { const emailToSend = {
oneTimePassword: oneTimePassword,
to: ctx.request.body.data.email, to: ctx.request.body.data.email,
from: `contact@hiranandani.net`, from: `contact@hiranandani.net`,
// replyTo: undefined, // replyTo: undefined,
......
...@@ -7,10 +7,11 @@ const { default: axios } = require("axios"); ...@@ -7,10 +7,11 @@ const { default: axios } = require("axios");
*/ */
const { createCoreService } = require("@strapi/strapi").factories; const { createCoreService } = require("@strapi/strapi").factories;
module.exports = createCoreService("api::end-user.end-user", module.exports = createCoreService(
({ strapi: Strapi }) => ({ "api::end-user.end-user",
async sendLeadToSperto(data) { ({ strapi: Strapi }) => ({
// console.log(">>>",data); async sendLeadToSperto(data, isVerified) {
console.log(">>>>>>>",data);
const headers = { "Content-Type": "application/json" }; const headers = { "Content-Type": "application/json" };
const leadData = { const leadData = {
api_key: process.env.SPERTO_API_KEY, api_key: process.env.SPERTO_API_KEY,
...@@ -20,9 +21,9 @@ module.exports = createCoreService("api::end-user.end-user", ...@@ -20,9 +21,9 @@ module.exports = createCoreService("api::end-user.end-user",
mobile1_isd: "", mobile1_isd: "",
mobile_no1: data.mobileNo, mobile_no1: data.mobileNo,
email_id1: data.email, email_id1: data.email,
otp_verified_yn: "N", otp_verified_yn: isVerified,
}; };
// console.log("lead data", leadData); console.log("lead data", leadData);
// if (data.comments) { // if (data.comments) {
// leadData.comments = data.comments; // leadData.comments = data.comments;
// } // }
...@@ -42,12 +43,12 @@ module.exports = createCoreService("api::end-user.end-user", ...@@ -42,12 +43,12 @@ module.exports = createCoreService("api::end-user.end-user",
leadData, leadData,
{ headers: headers } { headers: headers }
); );
console.log("spertoLeadResponse",spertoLeadResponse); console.log("spertoLeadResponse", spertoLeadResponse);
return spertoLeadResponse; return spertoLeadResponse;
}, },
async sendOTPToSpreto(data) { async sendOTPToSpreto(data) {
console.log("OTP error", data); // console.log("OTP data", data);
const headers = { "Content-Type": "application/json" }; const headers = { "Content-Type": "application/json" };
const otpDetails = { const otpDetails = {
api_key: process.env.SPERTO_API_KEY, api_key: process.env.SPERTO_API_KEY,
...@@ -64,18 +65,19 @@ module.exports = createCoreService("api::end-user.end-user", ...@@ -64,18 +65,19 @@ module.exports = createCoreService("api::end-user.end-user",
// email_id1: data.email, // email_id1: data.email,
// otp_verified_yn: "N", // otp_verified_yn: "N",
}; };
console.log("OTPDETAILS", otpDetails); // console.log("OTPDETAILS", otpDetails);
await axios.get(`http://vas.themultimedia.in/domestic/sendsms/bulksms.php?username=OSAPI&password=os123456&type=TEXT&sender=HROTPs&entityId=1101407690000029629&templateId=1507166789848358346&mobile=${data.mobileNo}&message=Dear%20${data.fullName}%0AYour%20OTP%20for%20Hiranandani%20Exclusive%20website%20login%20is%20${data.emailToSend.oneTimePassword}%0AValid%20for%2010%20minute%20Please%20do%20not%20share%20this%20OTP.%0ARegards%2C%0AHiranandani%20Team.`)
const spertoResponse = await axios.post( const spertoResponse = await axios.post(
"https://net4hgc.sperto.co.in/_api/api_auth_send_mail.php", "https://net4hgc.sperto.co.in/_api/api_auth_send_mail.php",
otpDetails, otpDetails,
{ headers: headers } { headers: headers }
); );
console.log("spertoResponse",spertoResponse); // console.log("spertoResponse", spertoResponse);
return spertoResponse; return spertoResponse;
// return ; // return ;
}, },
})) })
);
// module.exports = createCoreService( // module.exports = createCoreService(
// "api::end-user.end-user", // "api::end-user.end-user",
...@@ -117,4 +119,3 @@ module.exports = createCoreService("api::end-user.end-user", ...@@ -117,4 +119,3 @@ module.exports = createCoreService("api::end-user.end-user",
// // }}) // // }})
// }) // })
// ); // );
...@@ -41,6 +41,7 @@ module.exports = createCoreService("api::lead.lead", ({ strapi: Strapi }) => ({ ...@@ -41,6 +41,7 @@ module.exports = createCoreService("api::lead.lead", ({ strapi: Strapi }) => ({
leadData, leadData,
{ headers: headers } { headers: headers }
); );
return spertoResponse; return spertoResponse;
}, },
})); }));
...@@ -7,6 +7,7 @@ const { ...@@ -7,6 +7,7 @@ const {
validateRegisterBody, validateRegisterBody,
} = require("@strapi/plugin-users-permissions/server/controllers/validation/auth"); } = require("@strapi/plugin-users-permissions/server/controllers/validation/auth");
const { getService } = require("@strapi/plugin-users-permissions/server/utils"); const { getService } = require("@strapi/plugin-users-permissions/server/utils");
const { default: axios } = require("axios");
const sanitizeUser = (user, ctx) => { const sanitizeUser = (user, ctx) => {
const { auth } = ctx.state; const { auth } = ctx.state;
...@@ -30,7 +31,6 @@ const userPermissionExtension = (plugin) => { ...@@ -30,7 +31,6 @@ const userPermissionExtension = (plugin) => {
); );
} }
const pluginStore = await strapi.store({ const pluginStore = await strapi.store({
type: "plugin", type: "plugin",
name: "users-permissions", name: "users-permissions",
...@@ -48,20 +48,13 @@ const userPermissionExtension = (plugin) => { ...@@ -48,20 +48,13 @@ const userPermissionExtension = (plugin) => {
// }, // },
// }); // });
const endUser = await strapi const endUser = await strapi.query("api::end-user.end-user").findOne({
.query("api::end-user.end-user")
.findOne({
populate: ["user"], populate: ["user"],
where: { where: {
$and: [ $and: [{ publishedAt: { $notNull: true } }, { mobileNo: mobileNumber }],
{ publishedAt: { $notNull: true } },
{ mobileNo: mobileNumber },
],
}, },
}); });
if (!endUser) { if (!endUser) {
throw new ValidationError( throw new ValidationError(
"No end user registered with specified email address, mobile number combination." "No end user registered with specified email address, mobile number combination."
...@@ -71,7 +64,7 @@ const userPermissionExtension = (plugin) => { ...@@ -71,7 +64,7 @@ const userPermissionExtension = (plugin) => {
// Find the linked user next. // Find the linked user next.
const user = await strapi const user = await strapi
.query("plugin::users-permissions.user") .query("plugin::users-permissions.user")
.findOne({where:{ id: endUser.user.id }}); .findOne({ where: { id: endUser.user.id } });
if (!user || user.blocked) { if (!user || user.blocked) {
throw new ValidationError("Unable to resolve user linked to end user."); throw new ValidationError("Unable to resolve user linked to end user.");
} }
...@@ -85,29 +78,74 @@ const userPermissionExtension = (plugin) => { ...@@ -85,29 +78,74 @@ const userPermissionExtension = (plugin) => {
const emailToSend = { const emailToSend = {
to: user.email, to: user.email,
from: from: `contact@hiranandani.net`,
resetPasswordSettings.from.email || resetPasswordSettings.from.name
? `${resetPasswordSettings.from.name} <${resetPasswordSettings.from.email}>`
: undefined,
replyTo: resetPasswordSettings.response_email, replyTo: resetPasswordSettings.response_email,
subject: `Your one time password is: ${oneTimePassword}`, subject: `Your one time password is: ${oneTimePassword}`,
text: `Hello ${endUser.fullName}, Your one time password to login to your partner portal is ${oneTimePassword}`, text: `Hello ${endUser.fullName}, Your one time password to login to your partner portal is ${oneTimePassword}`,
html: `<p>Hello ${endUser.fullName}, <br></br>Your one time password to login to the hiranandani offers portal is ${oneTimePassword}</p><br /> Best Regards, <br /> Team Hiranandani.`, html: `<p>Dear ${endUser.fullName},
Your OTP for Hiranandani Exclusive website login is <strong>${oneTimePassword}</strong> . Valid for 10 minutes. Please do not
share this OTP.
Regards,
Hiranandani Team.`,
}; };
// NOTE: Update the user before sending the email so an Admin can generate the link if the email fails // NOTE: Update the user before sending the email so an Admin can generate the link if the email fails
const updateUser=await getService("user").edit(user.id, { const updateUser = await getService("user").edit(user.id, {
oneTimePassword: `${oneTimePassword}`, oneTimePassword: `${oneTimePassword}`,
}); });
const headers = { "Content-Type": "application/json" };
const otpDetails = {
api_key: process.env.SPERTO_API_KEY,
from_name: "Hiranandani",
from_mail: emailToSend.from,
to: emailToSend.to,
subject: emailToSend.subject,
body: emailToSend.html,
};
await strapi.plugin("email").service("email").send(emailToSend);
await axios.get(
`http://vas.themultimedia.in/domestic/sendsms/bulksms.php?username=OSAPI&password=os123456&type=TEXT&sender=HROTPs&entityId=1101407690000029629&templateId=1507166789848358346&mobile=${mobileNumber}&message=Dear%20${endUser.fullName}%0AYour%20OTP%20for%20Hiranandani%20Exclusive%20website%20login%20is%20${oneTimePassword}%0AValid%20for%2010%20minute%20Please%20do%20not%20share%20this%20OTP.%0ARegards%2C%0AHiranandani%20Team.`
);
ctx.send({ ok: true, message: "otp sent" });
console.log("ctx.request.body",ctx.request.body);
try {
const spertoResponse = await axios.post(
"https://net4hgc.sperto.co.in/_api/api_auth_send_mail.php",
otpDetails,
{ headers: headers }
);
ctx.request.body.httpLoginOtpRequestHeaders = JSON.stringify(
spertoResponse.headers
);
ctx.request.body.httpLoginOtpRequestMethod = spertoResponse.config.method;
ctx.request.body.httpLoginOtpRequestlUrl = spertoResponse.config.url;
ctx.request.body.httpsLoginOtpRequestBody = spertoResponse.config.data;
ctx.request.body.httpLoginOtpResposneBody = JSON.stringify(
spertoResponse.data
);
ctx.request.body.thirdPartyApiError = false;
console.log("spertoResponse", spertoResponse);
return spertoResponse;
} catch (error) {
ctx.request.body.httpLoginOtpRequestHeaders = JSON.stringify(
error.config.headers
);
ctx.request.body.httpLoginOtpRequestMethod = error.config.method;
ctx.request.body.httpLoginOtpRequestlUrl = error.config.url;
ctx.request.body.httpsLoginOtpRequestBody = error.config.data;
ctx.request.body.httpLoginOtpResposneBody = JSON.stringify(
error.message
);
ctx.request.body.thirdPartyApiError = true;
}
// Send an email to the user. // Send an email to the user.
await strapi.plugin("email").service("email").send(emailToSend);
// await getService("user").sendOTPOnEmail(emailToSend); // await getService("user").sendOTPOnEmail(emailToSend);
// TODO: Send SMS. // TODO: Send SMS.
ctx.send({ ok: true, message: "otp sent" });
}; };
plugin.controllers.user.finishEndUserOtpLogin = async (ctx) => { plugin.controllers.user.finishEndUserOtpLogin = async (ctx) => {
...@@ -136,8 +174,6 @@ const userPermissionExtension = (plugin) => { ...@@ -136,8 +174,6 @@ const userPermissionExtension = (plugin) => {
); );
} }
// Find the linked user next. // Find the linked user next.
const user = await strapi.query("plugin::users-permissions.user").findOne({ const user = await strapi.query("plugin::users-permissions.user").findOne({
where: { where: {
...@@ -193,7 +229,7 @@ const userPermissionExtension = (plugin) => { ...@@ -193,7 +229,7 @@ const userPermissionExtension = (plugin) => {
// Find the linked user next. // Find the linked user next.
const user = await strapi const user = await strapi
.query("plugin::users-permissions.user") .query("plugin::users-permissions.user")
.findOne({where:{ id: channelPartner.user.id }}); .findOne({ where: { id: channelPartner.user.id } });
if (!user || user.blocked) { if (!user || user.blocked) {
throw new ValidationError( throw new ValidationError(
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!