-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(create and update profile): User can create new profile or up…
…date profile [starts #167727632]
- Loading branch information
1 parent
9e84f5e
commit 36f8e0d
Showing
10 changed files
with
193 additions
and
13 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
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,3 +1,7 @@ | ||
import Users from './users'; | ||
import Profile from './profile_update'; | ||
|
||
export default Users; | ||
module.exports = { | ||
Users, | ||
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* eslint-disable linebreak-style */ | ||
import {sequelize, Sequelize } from '../models/index'; | ||
import userModel from '../models/User'; | ||
import { returnDate } from '../utils/helper'; | ||
|
||
|
||
const Users = userModel(sequelize, Sequelize.DataTypes); | ||
|
||
module.exports = { | ||
create: async (req, res) => { | ||
const timeStamp = Date.now().toString(); | ||
const id = Math.round(parseInt(timeStamp, 10) / 1000); | ||
const { firstName, lastName, email, password } = req.body; | ||
try { | ||
const user = await Users.create({ | ||
firstName, | ||
lastName, | ||
email, | ||
password, | ||
id, | ||
|
||
|
||
}); | ||
return res.status(201).send(user); | ||
|
||
} | ||
catch(error) { | ||
return res.status(400).send(error); | ||
} | ||
|
||
}, | ||
updateProfile: async (req, res) => { | ||
|
||
const userId = parseInt(req.params.id, 10); | ||
try { | ||
const user = await Users.findOne({where: {id:userId}}); | ||
try { | ||
const oldDate = user.birthdate | ||
await user.update({ | ||
gender: req.body.gender || user.gender, | ||
birthdate: returnDate(req.body.birthdate, oldDate), | ||
preferredLanguage: req.body.preferredLanguage || user.preferredLanguage, | ||
preferredCurrency: req.body.preferredCurrency || user.preferredCurrency, | ||
city: req.body.city || user.city, | ||
state: req.body.state || user.state, | ||
zip: req.body.zip || user.zip, | ||
country: req.body.country || user.country, | ||
role: req.body.role || user.role, | ||
department: req.body.department || user.department, | ||
lineManager: req.body.lineManager || user.lineManager, | ||
}) | ||
} | ||
catch (error) { | ||
return res.status(400).json({ | ||
status: 'error', | ||
error, | ||
}) | ||
} | ||
|
||
|
||
return res.status(200).json(user) | ||
} | ||
catch(err) { | ||
return res.status(404).json({ | ||
status: 'error', | ||
error: 'no user found' | ||
}) | ||
} | ||
}, | ||
getProfile: async (req, res) => { | ||
const userId = parseInt(req.params.id, 10); | ||
|
||
try { | ||
const user = await Users.findOne({where: {id:userId}}); | ||
return res.status(200).json({ | ||
status: 'success', | ||
user, | ||
|
||
}) | ||
} | ||
catch(error) { | ||
return res.status(404).json({ | ||
status: 'error', | ||
error: 'No user with that id was found' | ||
}) | ||
} | ||
} | ||
|
||
} |
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
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
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
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
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
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,5 +1,10 @@ | ||
const router = require("express").Router(); | ||
import express from 'express'; | ||
import Profile from '../controllers/profile_update'; | ||
const router = express.Router(); | ||
|
||
router.use("/api", require("./api")); | ||
router.post('/auth/signup', Profile.create); | ||
router.patch('/user/:id', Profile.updateProfile); | ||
router.get('/user/:id', Profile.getProfile) | ||
|
||
module.exports = 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,39 @@ | ||
import {sequelize, Sequelize } from '../models/index'; | ||
import userModel from '../models/User'; | ||
|
||
const Users = userModel(sequelize, Sequelize.DataTypes); | ||
|
||
|
||
const returnDate = (newdate, oldDate) => { | ||
if(newdate) { | ||
const date = new Date(newdate); | ||
return date.toLocaleDateString(); | ||
} | ||
return oldDate | ||
} | ||
const isProfileUpdated = async (req, res, next) => { | ||
let isUpdated = false; | ||
const userId = parseInt(req.params.id, 10); | ||
try { | ||
const user = await Users.findOne({where: {id:userId}}); | ||
if(!user.gender) { | ||
return res.status(404).json({ | ||
status: 'error', | ||
error: 'User is yet to set up profile' | ||
}) | ||
//Or redirect to profile settings page | ||
} | ||
return next(); | ||
|
||
} | ||
catch (error) { | ||
res.status(404).json({ | ||
status: 'error', | ||
error: 'No user with that id was found' | ||
}) | ||
} | ||
return isUpdated; | ||
|
||
} | ||
export {returnDate, isProfileUpdated }; | ||
|