From f5d4194c4e4916331199a3f6d8cfee5b1f9fdf93 Mon Sep 17 00:00:00 2001 From: "Josepablo C." Date: Thu, 9 Nov 2023 20:17:08 -0600 Subject: [PATCH] EN-80: Fixing public endpoints EN-83: Removing company_type from register process --- README.md | 3 +- src/apps/public/account/services.js | 7 ++--- src/lib/Handlers/Account/index.js | 11 ++----- src/lib/Misc.js | 49 ++++++++++++++++++++++++++++- src/lib/Models/users.model.js | 2 +- 5 files changed, 55 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 59698cb..27c08a6 100644 --- a/README.md +++ b/README.md @@ -119,14 +119,13 @@ There is no timeout to confirm the email, but it is expected that the Fron End r If the checksum matches but the user is already registered, then this request will be rejected. -Expects a body with the same data as the POST request, but adding the OTP received in the email, the company type decided by the user and the checksum generated by the POST request. It is important to noticed that company_type is case sensitive, the only options are either `Shipper` or `Carrier`. Here is an example: +Expects a body with the same data as the POST request, but adding the OTP received in the email, the company type decided by the user and the checksum generated by the POST request. Here is an example: ```{.json} { "email":"testing@etaviaporte.com", "password":"PasswordExample", "otp":"OTP string", - "company_type":"Shipper or Carrier", "checksum":"Checksum generated in the POST request" } ``` diff --git a/src/apps/public/account/services.js b/src/apps/public/account/services.js index 55b5b8f..ee17708 100644 --- a/src/apps/public/account/services.js +++ b/src/apps/public/account/services.js @@ -19,7 +19,6 @@ const create_account_schema = { email : { type : 'string' , maxLength : 256 }, password : { type : 'string', maxLength : 256}, otp : { type : 'string', maxLength : 6 }, - company_type : { type : 'string', enum : ["Shipper" , "Carrier"] }, checksum : { type : 'string', maxLength : 32 } }, required : [ 'email', 'password' ] @@ -28,7 +27,7 @@ const create_account_schema = { const confirm_account_schema = { type : 'object', properties : create_account_schema.properties,//Same properties - required : [ 'email', 'password', 'otp', 'company_type', 'checksum' ]//Different requirements + required : [ 'email', 'password', 'otp', 'checksum' ]//Different requirements }; const login_account_schema = { @@ -168,7 +167,7 @@ const TryCreateAccount = async(req, res) => { const ConfirmAccount = async(req, res) => { try{ if( validator.validate( req.body , confirm_account_schema ).valid ){ - const { email, password, otp, company_type, checksum } = req.body; + const { email, password, otp, checksum } = req.body; const it_exists = await already_exists( email ); if( it_exists ){ @@ -181,7 +180,7 @@ const ConfirmAccount = async(req, res) => { return res.status(400).send({ error : "Wrong OTP" }); } - await create_account( email, password, company_type ); + await create_account( email, password ); const content = { user_name : email }; const receiver = email; diff --git a/src/lib/Handlers/Account/index.js b/src/lib/Handlers/Account/index.js index b4c9170..6127173 100644 --- a/src/lib/Handlers/Account/index.js +++ b/src/lib/Handlers/Account/index.js @@ -6,18 +6,11 @@ const UserModel = require( `${ROOT_PATH}/${MODELS_PATH}/users.model.js` ); const pwd_secret = apiConfig.authentication.pwdSecret; -async function create_account( email, password, company_type ){ - let permissions; - if( company_type === "Shipper"){ - permissions = "role_shipper"; - }else{ - permissions = "role_carrier"; - } +async function create_account( email, password ){ let safe_password = toSha256( password + pwd_secret ); const user = new UserModel({ email, - password : safe_password, - permissions + password : safe_password }); await user.save(); } diff --git a/src/lib/Misc.js b/src/lib/Misc.js index 46c2be9..39f7afa 100644 --- a/src/lib/Misc.js +++ b/src/lib/Misc.js @@ -27,4 +27,51 @@ function genKey( len = 6 , key="" ){ return complete_string.substr(0 , len ); } -module.exports = { genKey , toSha256 }; \ No newline at end of file +function getPagination( query ){ + let limit = { + page : 0, + elements : 10 + }; + + if( query.page ){ + limit.page = parseInt( query.page ) || 0; + if( limit.page < 0 ){ + limit.page = 0; + } + } + if( query.elements ){ + limit.elements = parseInt( query.elements ) || 10; + /** Safe pagination limit */ + if( limit.elements > 1000 ){ + limit.elements = 1000; + } + else if( limit.elements < 0 ){ + limit.elements = 10; + } + } + return limit; +} + +async function getPage( page, elements, model, filter=null, projection=null){ + const skip = elements * page; + const total = await model.count( filter ); + const list = await model.find( filter , projection, { skip : skip , limit : elements } ); + return { + total : total, + limit : elements, + skip : skip, + data : list + } +} + +async function queryPage(page, elements, model, filter=null, projection=null){ + const skip = elements * page; + const total = await model.count( filter ); + return { + query : model.find( filter , projection, { skip : skip , limit : elements } ), + total : total, + skip : skip + }; +} + +module.exports = { genKey , toSha256, getPagination, getPage, queryPage }; \ No newline at end of file diff --git a/src/lib/Models/users.model.js b/src/lib/Models/users.model.js index 17a12ad..93af359 100644 --- a/src/lib/Models/users.model.js +++ b/src/lib/Models/users.model.js @@ -21,7 +21,7 @@ const schema = new Schema({ password: { type: String , maxLength : 256 }, phone: { type: String }, phone2: { type: String }, - permissions: [{ type: String, default: 'role_admin', enum : ['role_admin', 'role_shipper', 'role_carrier', 'role_driver' ] }], + permissions: [{ type: String, default: 'role_admin', enum : [ 'admin', 'role_admin', 'role_shipper', 'role_carrier', 'role_driver' ] }], gender: { type: String }, address: { type: String }, dob: { type: String },