"use strict"; const { getService } = require("@strapi/plugin-users-permissions/server/utils"); /** * vendor controller */ const { createCoreController } = require("@strapi/strapi").factories; module.exports = createCoreController("api::vendor.vendor", () => ({ async importSeedData(ctx) { await strapi.service("api::vendor.vendor").importCountrySeedData(); await strapi.service("api::vendor.vendor").importStateSeedData(); await strapi.service("api::vendor.vendor").importCitySeedData(); await strapi.service("api::vendor.vendor").importPincodeSeedData(); // await strapi.service("api::jeweler.jeweler").importColorSeedData(); // await strapi.service("api::jeweler.jeweler").importKaratSeedData(); // await strapi.service("api::jeweler.jeweler").importOccasionSeedData(); // await strapi.service("api::jeweler.jeweler").importOrnametTypeSeedData(); // await strapi.service("api::jeweler.jeweler").importOrnametTypeMetalSeedData(); // await strapi.service("api::jeweler.jeweler").importJewelerDetailsSeedData(); // await strapi.service("api::jeweler.jeweler").importShopDetailsSeedData(); // await strapi.service("api::jeweler.jeweler").importProductsSeedData(); // await strapi.service("api::jeweler.jeweler").importProductSizeSeedData(); ctx.send({ ok: true }); }, async create(ctx) { // console.log("ctx", ctx.request.body); // Get user from user entity const currentUser = await strapi .query("plugin::users-permissions.user") .findOne({ populate: ["user"], where: { $and: [{ email: ctx.request.body.data.email }], }, }); // Check if its already existing in vendor entity console.log("currentUser", currentUser); console.log( "phoneNumber", ctx.request.body.data.mobileNo, ctx.request.body.data.email ); let existingUser; existingUser = await strapi.entityService.findMany("api::vendor.vendor", { // fields: ["id"], filters: { phone: {$eq: ctx.request.body.data.mobileNo} }, }); console.log("existingUser> 1", existingUser); if (!existingUser) { existingUser = await strapi.entityService.findMany("api::vendor.vendor", { // fields: ["id"], filters: { email: {$eq: ctx.request.body.data.email} }, }); } console.log("existingUser > 2", existingUser); // Generate one time password (otp) const oneTimePassword = Math.floor(1000 + Math.random() * 9000); await strapi.entityService.update( "plugin::users-permissions.user", currentUser.id, { data: { oneTimePassword: `${oneTimePassword}`, }, } ); const date = new Date(); // TODO: Code to send OTP on email // TODO: Code to send OTP on SMS console.log("ctx.request.body.data", ctx.request.body.data); console.log("existingUser", existingUser); if (existingUser && existingUser.length !== 0) { } else { ctx.request.body.data.user = currentUser.id; const response = await strapi.entityService.create("api::vendor.vendor", { data: { ...ctx.request.body.data, phone: ctx.request.body.data.mobileNo, publishedAt: date, }, }); console.log("response", response); return { otpSent: true, data: response }; // ctx.request.body.data.user = currentUser; // console.log("ctx.request.body.data", ctx.request.body.data); // return await super.create(ctx); } ctx.send({ ok: true, message: "Existing vendor found, skipping creation only sent OTP.", }); }, async finishVendorOtpVerification(ctx) { const { email, oneTimePassword } = ctx.request.body; // 1. Identify the end-user record using the above. console.log("email", email, oneTimePassword); const vendorUser = await strapi .query("plugin::users-permissions.user") .findOne({ populate: ["user"], where: { $and: [{ email: email }], }, }); if (!vendorUser) { // throw new ValidationError("Invalid mobile number"); ctx.send({ ok: false, message: "Invalid mobile number" }); } console.log("endUser", vendorUser); // 2. Then identify the user record using step 1. // 3. Verify otp. const user = await strapi.query("plugin::users-permissions.user").findOne({ where: { $and: [{ id: vendorUser.id }, { oneTimePassword: oneTimePassword }], }, }); console.log("USER", user); if (!user || user.blocked) { console.log("invalid otp >>"); ctx.send({ ok: false, message: "OTP is invalid, please enter the correct OTP!", }); } if (user) { await getService("user").edit(user.id, { oneTimePassword: null, confirmed: true, }); } // 4. stamp otp in user to null. if (user) { ctx.send({ ok: true, message: "user registered" }); } else if (!user) { ctx.send({ ok: false, message: "OTP is invalid, please enter the correct OTP!", }); } }, }));