Commit 94c6186b by Harish Patel

End user register

1 parent 10c6d8ae
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
"info": { "info": {
"singularName": "end-user", "singularName": "end-user",
"pluralName": "end-users", "pluralName": "end-users",
"displayName": "End User" "displayName": "End User",
"description": ""
}, },
"options": { "options": {
"draftAndPublish": true "draftAndPublish": true
...@@ -14,14 +15,17 @@ ...@@ -14,14 +15,17 @@
"fullName": { "fullName": {
"type": "string" "type": "string"
}, },
"emailAddress": {
"type": "email"
},
"mobileNo": { "mobileNo": {
"type": "string" "type": "string",
"unique": true
}, },
"oneTimePassword": { "whatsappAccepted": {
"type": "string" "type": "boolean"
},
"user": {
"type": "relation",
"relation": "oneToOne",
"target": "plugin::users-permissions.user"
} }
} }
} }
'use strict'; "use strict";
/** /**
* end-user controller * end-user controller
*/ */
const { createCoreController } = require('@strapi/strapi').factories; const { factories } = require("@strapi/strapi");
module.exports = createCoreController('api::end-user.end-user'); module.exports = factories.createCoreController(
"api::end-user.end-user",
({ strapi: Strapi }) => ({
// Wrapping a core action (leaves core logic in place)
async create(ctx) {
console.log(ctx.request.body);
const currentUser = ctx.state.user;
// Find the linked user next.
// const existingUser = await strapi
// .query("plugin::users-permissions.user")
// .findOne({ username: ctx.request.body.data.mobileNo });
// const existingUserEntries = await strapi.entityService.findMany(
// "plugin::users-permissions.user",
// {
// // fields: ['title', 'description'],
// filters: { username: ctx.request.body.data.mobileNo },
// }
// );
// console.log(`Inside end-user create, found existingUserEntries: `);
// console.log(existingUserEntries);
// if (!existingUserEntries || existingUserEntries.length === 0) {
// throw new ValidationError(
// `Unable to resolve existing user with mobileNo: ${ctx.request.body.data.mobileNo}.`
// );
// }
// const existingUser = existingUserEntries[0];
// 2. check if the current user already has an existing business listing (existingEndUser) against their name.
const existingEndUser = await strapi.entityService.findMany(
"api::end-user.end-user",
{
fields: ["id"],
filters: { mobileNo: ctx.request.body.data.mobileNo },
}
);
const oneTimePassword = Math.floor(100000 + Math.random() * 900000);
const emailToSend = {
to: ctx.request.body.data.email,
from: undefined,
replyTo: undefined,
subject: `Your one time password is: ${oneTimePassword}`,
text: `Hello ${"Jay Mehta"}, Your one time password to login to your partner portal is ${oneTimePassword}`,
html: `<p>Hello ${"Jay Mehta"}, <br></br>Your one time password to login to your partner portal is ${oneTimePassword}</p><br /> Best Regards, <br /> Team Hiranandani.`,
};
// NOTE: Update the user before sending the email so an Admin can generate the link if the email fails
await strapi.entityService.update(
"plugin::users-permissions.user",
currentUser.id,
{
data: {
oneTimePassword: `${oneTimePassword}`,
},
}
);
// Send an email to the user.
await strapi.plugin("email").service("email").send(emailToSend);
// TODO: Send SMS.
if (existingEndUser && existingEndUser.length !== 0) {
console.log(`Found existing end user: `);
console.log(existingEndUser);
// This makes sure that we are updating the existing business listing only.
ctx.params.id = existingEndUser[0].id;
return super.update(ctx);
} else {
// We make sure that the newly created listing is created against the current business owner.
ctx.request.body.data["user"] = currentUser.id;
console.log(`About to insert end user: `);
console.log(ctx.request.body.data);
// Now go ahead and create the listing.
return await super.create(ctx);
}
},
})
);
{ {
"kind": "collectionType", "kind": "collectionType",
"collectionName": "up_users", "collectionName": "up_users",
"info": { "info": {
"name": "user", "name": "user",
"description": "", "description": "",
"singularName": "user", "singularName": "user",
"pluralName": "users", "pluralName": "users",
"displayName": "User" "displayName": "User"
},
"options": {
"draftAndPublish": false,
"timestamps": true
},
"attributes": {
"username": {
"type": "string",
"minLength": 3,
"unique": true,
"configurable": false,
"required": true
}, },
"options": { "email": {
"draftAndPublish": false, "type": "email",
"timestamps": true "minLength": 6,
"configurable": false,
"required": true
}, },
"attributes": { "provider": {
"username": { "type": "string",
"type": "string", "configurable": false
"minLength": 3, },
"unique": true, "password": {
"configurable": false, "type": "password",
"required": true "minLength": 6,
}, "configurable": false,
"email": { "private": true
"type": "email", },
"minLength": 6, "resetPasswordToken": {
"configurable": false, "type": "string",
"required": true "configurable": false,
}, "private": true
"provider": { },
"type": "string", "confirmationToken": {
"configurable": false "type": "string",
}, "configurable": false,
"password": { "private": true
"type": "password", },
"minLength": 6, "confirmed": {
"configurable": false, "type": "boolean",
"private": true "default": false,
}, "configurable": false
"resetPasswordToken": { },
"type": "string", "blocked": {
"configurable": false, "type": "boolean",
"private": true "default": false,
}, "configurable": false
"confirmationToken": { },
"type": "string", "role": {
"configurable": false, "type": "relation",
"private": true "relation": "manyToOne",
}, "target": "plugin::users-permissions.role",
"confirmed": { "inversedBy": "users",
"type": "boolean", "configurable": false
"default": false, },
"configurable": false "oneTimePassword": {
}, "type": "string",
"blocked": { "configurable": false,
"type": "boolean", "private": true
"default": false,
"configurable": false
},
"role": {
"type": "relation",
"relation": "manyToOne",
"target": "plugin::users-permissions.role",
"inversedBy": "users",
"configurable": false
},
"profileImage": {
"type": "media",
"multiple": false,
"required": false,
"allowedTypes": [
"images"
]
},
"aboutMe": {
"type": "text"
},
"fullName": {
"type": "string",
"required": true
},
"oneTimePassword": {
"type": "string",
"configurable": false,
"private": true
}
} }
}
\ No newline at end of file \ No newline at end of file
}
}
...@@ -22,7 +22,7 @@ const userPermissionExtension = (plugin) => { ...@@ -22,7 +22,7 @@ const userPermissionExtension = (plugin) => {
return plugin.controllers.user.update(ctx); return plugin.controllers.user.update(ctx);
}; };
plugin.controllers.user.startOtpLogin = async (ctx) => { plugin.controllers.user.startChannelPartnerOtpLogin = async (ctx) => {
const { mahareraNumber, mobileNumber } = ctx.request.body; const { mahareraNumber, mobileNumber } = ctx.request.body;
if (!mahareraNumber || !mobileNumber) { if (!mahareraNumber || !mobileNumber) {
throw new ValidationError( throw new ValidationError(
...@@ -98,7 +98,7 @@ const userPermissionExtension = (plugin) => { ...@@ -98,7 +98,7 @@ const userPermissionExtension = (plugin) => {
ctx.send({ ok: true, message: "otp sent" }); ctx.send({ ok: true, message: "otp sent" });
}; };
plugin.controllers.user.finishOtpLogin = async (ctx) => { plugin.controllers.user.finishChannelPartnerOtpLogin = async (ctx) => {
const { oneTimePassword, mahareraNumber, mobileNumber } = ctx.request.body; const { oneTimePassword, mahareraNumber, mobileNumber } = ctx.request.body;
if (!oneTimePassword || !mobileNumber || !mahareraNumber) { if (!oneTimePassword || !mobileNumber || !mahareraNumber) {
throw new ValidationError( throw new ValidationError(
...@@ -267,13 +267,13 @@ const userPermissionExtension = (plugin) => { ...@@ -267,13 +267,13 @@ const userPermissionExtension = (plugin) => {
}); });
plugin.routes["content-api"].routes.push({ plugin.routes["content-api"].routes.push({
method: "POST", method: "POST",
path: "/users/start-otp-login", path: "/users/channel-partner/start-otp-login",
handler: "user.startOtpLogin", handler: "user.startChannelPartnerOtpLogin",
}); });
plugin.routes["content-api"].routes.push({ plugin.routes["content-api"].routes.push({
method: "POST", method: "POST",
path: "/users/finish-otp-login", path: "/users/channel-partner/finish-otp-login",
handler: "user.finishOtpLogin", handler: "user.finishChannelPartnerOtpLogin",
}); });
return plugin; return plugin;
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!