diff --git a/src/api/end-user/content-types/end-user/schema.json b/src/api/end-user/content-types/end-user/schema.json index f68c8a6..48597d3 100644 --- a/src/api/end-user/content-types/end-user/schema.json +++ b/src/api/end-user/content-types/end-user/schema.json @@ -62,6 +62,36 @@ }, "httpResposneEmailBody": { "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" } } } diff --git a/src/api/end-user/controllers/end-user.js b/src/api/end-user/controllers/end-user.js index 39142dd..dadc83d 100644 --- a/src/api/end-user/controllers/end-user.js +++ b/src/api/end-user/controllers/end-user.js @@ -25,7 +25,7 @@ module.exports = factories.createCoreController( // Method 1: Creating an entirely custom action async finishEndUserOtpVerification(ctx) { const { mobileNo, oneTimePassword } = ctx.request.body; - + // console.log(">>>>>> One", ctx.request.body); // 1. Identify the end-user record using the above. const endUser = await strapi.query("api::end-user.end-user").findOne({ populate: ["user"], @@ -37,7 +37,7 @@ module.exports = factories.createCoreController( if (!endUser) { throw new ValidationError("Invalid mobile number."); } - + // console.log(" >>>> two ", endUser); // 2. Then identify the user record using step 1. // 3. Verify otp. const user = await strapi @@ -54,6 +54,35 @@ module.exports = factories.createCoreController( 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. await getService("user").edit(user.id, { oneTimePassword: null, @@ -64,20 +93,25 @@ module.exports = factories.createCoreController( await strapi.entityService.update("api::end-user.end-user", endUser.id, { data: { 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 }, }); - + // TODO: at this point we might have to invoke a Hiranandani API to send the newly registered user there. ctx.send({ ok: true, message: "user registered" }); }, // Wrapping a core action (leaves core logic in place) async create(ctx) { - console.log("ctx.request.body", ctx.request.body); + // console.log("ctx.request.body", ctx.request.body); try { const spretoLeadData = await strapi .service("api::end-user.end-user") - .sendLeadToSperto(ctx.request.body.data); + .sendLeadToSperto(ctx.request.body.data, 'N'); // console.log("spretoLeadData.data", spretoLeadData.data); ctx.request.body.data.httpRequestHeaders = JSON.stringify( spretoLeadData.headers @@ -112,6 +146,7 @@ module.exports = factories.createCoreController( const oneTimePassword = Math.floor(100000 + Math.random() * 900000); const emailToSend = { + oneTimePassword: oneTimePassword, to: ctx.request.body.data.email, from: `contact@hiranandani.net`, // replyTo: undefined, diff --git a/src/api/end-user/services/end-user.js b/src/api/end-user/services/end-user.js index 787fba1..2ac8e75 100644 --- a/src/api/end-user/services/end-user.js +++ b/src/api/end-user/services/end-user.js @@ -7,80 +7,82 @@ const { default: axios } = require("axios"); */ const { createCoreService } = require("@strapi/strapi").factories; -module.exports = createCoreService("api::end-user.end-user", -({ strapi: Strapi }) => ({ - async sendLeadToSperto(data) { - // console.log(">>>",data); - const headers = { "Content-Type": "application/json" }; - const leadData = { - api_key: process.env.SPERTO_API_KEY, - lead_category: "W", - campaign_key: process.env.CAMPAIGN_KEY, - customer_name: data.fullName, - mobile1_isd: "", - mobile_no1: data.mobileNo, - email_id1: data.email, - otp_verified_yn: "N", - }; - // console.log("lead data", leadData); - // if (data.comments) { - // leadData.comments = data.comments; - // } - if (data?.project) { - const projectData = await strapi.db - .query("api::project.project") - .findOne({ - where: { - id: data?.project, - }, - }); - leadData.project_name = projectData.projectName; - } +module.exports = createCoreService( + "api::end-user.end-user", + ({ strapi: Strapi }) => ({ + async sendLeadToSperto(data, isVerified) { + console.log(">>>>>>>",data); + const headers = { "Content-Type": "application/json" }; + const leadData = { + api_key: process.env.SPERTO_API_KEY, + lead_category: "W", + campaign_key: process.env.CAMPAIGN_KEY, + customer_name: data.fullName, + mobile1_isd: "", + mobile_no1: data.mobileNo, + email_id1: data.email, + otp_verified_yn: isVerified, + }; + console.log("lead data", leadData); + // if (data.comments) { + // leadData.comments = data.comments; + // } + if (data?.project) { + const projectData = await strapi.db + .query("api::project.project") + .findOne({ + where: { + id: data?.project, + }, + }); + leadData.project_name = projectData.projectName; + } - const spertoLeadResponse = await axios.post( - "https://net4hgc.sperto.co.in/_api/api_auth_post_lead_json.php", - leadData, - { headers: headers } - ); - console.log("spertoLeadResponse",spertoLeadResponse); - return spertoLeadResponse; - }, + const spertoLeadResponse = await axios.post( + "https://net4hgc.sperto.co.in/_api/api_auth_post_lead_json.php", + leadData, + { headers: headers } + ); + console.log("spertoLeadResponse", spertoLeadResponse); + return spertoLeadResponse; + }, - async sendOTPToSpreto(data) { - console.log("OTP error", data); - const headers = { "Content-Type": "application/json" }; - const otpDetails = { - api_key: process.env.SPERTO_API_KEY, - from_name: "Hiranandani", - from_mail: data.emailToSend.from, - to: data.emailToSend.to, - subject: data.emailToSend.subject, - body: data.emailToSend.html, - // lead_category: "W", - // campaign_key: process.env.CAMPAIGN_KEY, - // customer_name: data.fullName, - // mobile1_isd: "", - // mobile_no1: data.mobileNumber, - // email_id1: data.email, - // otp_verified_yn: "N", - }; - console.log("OTPDETAILS", otpDetails); - const spertoResponse = await axios.post( - "https://net4hgc.sperto.co.in/_api/api_auth_send_mail.php", - otpDetails, - { headers: headers } - ); - console.log("spertoResponse",spertoResponse); - return spertoResponse; - // return ; - - }, -})) + async sendOTPToSpreto(data) { + // console.log("OTP data", data); + const headers = { "Content-Type": "application/json" }; + const otpDetails = { + api_key: process.env.SPERTO_API_KEY, + from_name: "Hiranandani", + from_mail: data.emailToSend.from, + to: data.emailToSend.to, + subject: data.emailToSend.subject, + body: data.emailToSend.html, + // lead_category: "W", + // campaign_key: process.env.CAMPAIGN_KEY, + // customer_name: data.fullName, + // mobile1_isd: "", + // mobile_no1: data.mobileNumber, + // email_id1: data.email, + // otp_verified_yn: "N", + }; + // 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( + "https://net4hgc.sperto.co.in/_api/api_auth_send_mail.php", + otpDetails, + { headers: headers } + ); + // console.log("spertoResponse", spertoResponse); + return spertoResponse; + // return ; + }, + }) +); // module.exports = createCoreService( // "api::end-user.end-user", // ({ strapi: Strapi }) => ({ - + // // ({ strapi: Strapi }) => ({async sendLeadToSperto(data) { // // console.log(data); // // const headers = { "Content-Type": "application/json" }; @@ -117,4 +119,3 @@ module.exports = createCoreService("api::end-user.end-user", // // }}) // }) // ); - diff --git a/src/api/lead/services/lead.js b/src/api/lead/services/lead.js index d458096..6ff4a86 100644 --- a/src/api/lead/services/lead.js +++ b/src/api/lead/services/lead.js @@ -41,6 +41,7 @@ module.exports = createCoreService("api::lead.lead", ({ strapi: Strapi }) => ({ leadData, { headers: headers } ); + return spertoResponse; }, })); diff --git a/src/extensions/users-permissions/strapi-server.js b/src/extensions/users-permissions/strapi-server.js index 38a23c0..6d87dac 100644 --- a/src/extensions/users-permissions/strapi-server.js +++ b/src/extensions/users-permissions/strapi-server.js @@ -7,6 +7,7 @@ const { validateRegisterBody, } = require("@strapi/plugin-users-permissions/server/controllers/validation/auth"); const { getService } = require("@strapi/plugin-users-permissions/server/utils"); +const { default: axios } = require("axios"); const sanitizeUser = (user, ctx) => { const { auth } = ctx.state; @@ -30,7 +31,6 @@ const userPermissionExtension = (plugin) => { ); } - const pluginStore = await strapi.store({ type: "plugin", name: "users-permissions", @@ -48,19 +48,12 @@ const userPermissionExtension = (plugin) => { // }, // }); - const endUser = await strapi - .query("api::end-user.end-user") - .findOne({ - populate: ["user"], - where: { - $and: [ - { publishedAt: { $notNull: true } }, - { mobileNo: mobileNumber }, - - ], - }, - }); - + const endUser = await strapi.query("api::end-user.end-user").findOne({ + populate: ["user"], + where: { + $and: [{ publishedAt: { $notNull: true } }, { mobileNo: mobileNumber }], + }, + }); if (!endUser) { throw new ValidationError( @@ -71,7 +64,7 @@ const userPermissionExtension = (plugin) => { // Find the linked user next. const user = await strapi .query("plugin::users-permissions.user") - .findOne({where:{ id: endUser.user.id }}); + .findOne({ where: { id: endUser.user.id } }); if (!user || user.blocked) { throw new ValidationError("Unable to resolve user linked to end user."); } @@ -82,32 +75,77 @@ const userPermissionExtension = (plugin) => { {} ); const oneTimePassword = Math.floor(100000 + Math.random() * 900000); - + const emailToSend = { to: user.email, - from: - resetPasswordSettings.from.email || resetPasswordSettings.from.name - ? `${resetPasswordSettings.from.name} <${resetPasswordSettings.from.email}>` - : undefined, + from: `contact@hiranandani.net`, replyTo: resetPasswordSettings.response_email, subject: `Your one time password 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 - const updateUser=await getService("user").edit(user.id, { + const updateUser = await getService("user").edit(user.id, { 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. - await strapi.plugin("email").service("email").send(emailToSend); // await getService("user").sendOTPOnEmail(emailToSend); // TODO: Send SMS. - - ctx.send({ ok: true, message: "otp sent" }); }; plugin.controllers.user.finishEndUserOtpLogin = async (ctx) => { @@ -136,8 +174,6 @@ const userPermissionExtension = (plugin) => { ); } - - // Find the linked user next. const user = await strapi.query("plugin::users-permissions.user").findOne({ where: { @@ -152,7 +188,7 @@ const userPermissionExtension = (plugin) => { oneTimePassword: null, password: oneTimePassword, }); - + ctx.send({ ok: true, message: "otp updated" }); }; @@ -190,10 +226,10 @@ const userPermissionExtension = (plugin) => { ); } - // Find the linked user next. + // Find the linked user next. const user = await strapi .query("plugin::users-permissions.user") - .findOne({where:{ id: channelPartner.user.id }}); + .findOne({ where: { id: channelPartner.user.id } }); if (!user || user.blocked) { throw new ValidationError(