-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(endpoint): Set up User Profile setting
- this allows a user to view and edit his/her profile details only. [delivers #167891580]
- Loading branch information
Showing
7 changed files
with
573 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import db from '../database/models'; | ||
import Response from '../helpers/Response'; | ||
|
||
const { User, Company } = db; | ||
|
||
/** user profile class */ | ||
class Profile { | ||
/** | ||
* | ||
* @param {req} req | ||
* @param {res} res | ||
* @returns {object} object | ||
*/ | ||
static async getProfile(req, res) { | ||
try { | ||
const { payload } = req.payload; | ||
const { email } = payload; | ||
|
||
const user = await User.findOne({ where: { email } }); | ||
const { | ||
firstName, lastName, gender, dob, companyId | ||
} = user; | ||
|
||
const company = await Company.findOne({ where: { id: companyId } }); | ||
const { | ||
name, address | ||
} = company; | ||
|
||
const response = new Response( | ||
true, | ||
200, | ||
'User Profile retrieved successfuly', | ||
{ | ||
profile: { | ||
firstName, | ||
lastName, | ||
gender, | ||
dob, | ||
companyName: name, | ||
address | ||
} | ||
} | ||
); | ||
return res.status(response.code).json(response); | ||
} catch (err) { | ||
const response = new Response( | ||
false, | ||
500, | ||
'Server error, Please try again later', | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {req} req | ||
* @param {res} res | ||
* @returns {object} object | ||
*/ | ||
static async updateProfile(req, res) { | ||
try { | ||
const { payload } = req.payload; | ||
const { email, role, id } = payload; | ||
const { | ||
firstName, lastName, gender, dob, companyName, address | ||
} = req.body; | ||
|
||
await User.update( | ||
{ | ||
firstName, lastName, gender, dob | ||
}, | ||
{ | ||
where: { email }, | ||
returning: true, | ||
} | ||
); | ||
|
||
if (role === 'staff') { | ||
const response = new Response( | ||
true, | ||
200, | ||
'User Profile updated successfuly', | ||
{ | ||
profile: { | ||
firstName, lastName, gender, dob | ||
} | ||
} | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
|
||
if (role === 'owner') { | ||
await Company.update( | ||
{ name: companyName, address }, | ||
{ | ||
where: { owner: id }, | ||
returning: true, | ||
} | ||
); | ||
|
||
const response = new Response( | ||
true, | ||
200, | ||
'Admin Profile updated successfuly', | ||
{ | ||
profile: { | ||
firstName, lastName, gender, dob, companyName, address | ||
} | ||
} | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} catch (err) { | ||
const response = new Response( | ||
false, | ||
500, | ||
'Server error, Please try again later', | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} | ||
} | ||
|
||
export default Profile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { Router } from 'express'; | ||
import authRoute from './user.routes'; | ||
import tripRoute from './trip.routes'; | ||
import profileRoute from './profile.routes'; | ||
|
||
const router = Router(); | ||
|
||
router.use('/auth', authRoute); | ||
router.use('/trips', tripRoute); | ||
router.use(profileRoute); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Router } from 'express'; | ||
import Profile from '../controllers/Profile'; | ||
import profileSchema from '../validation/profileSchema'; | ||
import validator from '../middlewares/validator'; | ||
import middleware from '../middlewares/AuthMiddlewares'; | ||
import tokenHelper from '../helpers/Token'; | ||
|
||
const profileRoute = Router(); | ||
|
||
profileRoute.get( | ||
'/profile', | ||
tokenHelper.verifyToken, | ||
middleware.isUserVerified, | ||
Profile.getProfile, | ||
); | ||
|
||
profileRoute.patch( | ||
'/profile', | ||
validator(profileSchema), | ||
tokenHelper.verifyToken, | ||
middleware.isUserVerified, | ||
Profile.updateProfile, | ||
); | ||
|
||
export default profileRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { check } from 'express-validator'; | ||
import capitalize from '../utils/capitalize'; | ||
|
||
const dateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/; | ||
|
||
/* eslint-disable arrow-parens */ | ||
const profileSchema = [ | ||
check('firstName') | ||
.optional().trim() | ||
.isLength({ min: 2, max: 15 }) | ||
.withMessage('First name should be between 2 to 15 characters') | ||
.isAlpha() | ||
.withMessage('First name should only contain alphabets') | ||
.customSanitizer(value => capitalize(value)), | ||
|
||
check('lastName') | ||
.optional().trim() | ||
.isLength({ min: 2, max: 15 }) | ||
.withMessage('Last name should be between 2 to 15 characters') | ||
.isAlpha() | ||
.withMessage('Last name should only contain alphabets') | ||
.customSanitizer(value => capitalize(value)), | ||
|
||
check('dob') | ||
.optional().matches(dateRegex) | ||
.withMessage("Date must be of the format 'yyyy-mm-dd'") | ||
.custom(value => { | ||
const year = value.substring(0, 4); | ||
const today = new Date().getFullYear(); | ||
if ((today - year) < 18) { | ||
return false; | ||
} | ||
return true; | ||
}) | ||
.withMessage('Users must be 18 and above'), | ||
|
||
check('gender') | ||
.optional() | ||
.matches(/^(male|m|female|f)$/) | ||
.withMessage('Male or Female is the accepted value'), | ||
|
||
check('companyName') | ||
.optional().trim() | ||
.isLength({ min: 1 }) | ||
.customSanitizer(value => capitalize(value)), | ||
|
||
check('address') | ||
.optional().trim() | ||
.isLength({ min: 2, max: 100 }) | ||
.withMessage('Address should be between 2 to 100 characters') | ||
.customSanitizer(value => capitalize(value)) | ||
]; | ||
|
||
export default profileSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.