Skip to content

Commit

Permalink
feature(create and update profile): User can create new profile or up…
Browse files Browse the repository at this point in the history
…date profile [starts #167727632]
  • Loading branch information
chrismeeky committed Aug 22, 2019
1 parent 9e84f5e commit 36f8e0d
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/config/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import dotenv from 'dotenv';

dotenv.config();
require ('dotenv').config();
const databaseEnvDetails = {
username: process.env.DB_CONFIG_USERNAME,
password: process.env.DB_CONFIG_PASSWORD,
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/index.js
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
};
89 changes: 89 additions & 0 deletions src/controllers/profile_update.js
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'
})
}
}

}
6 changes: 3 additions & 3 deletions src/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Sequelize, sequelize } from '../models/index';

const Users = require('../models/User')(sequelize, Sequelize.DataTypes);

const timeStamp = Date.now().toString();
const id = Math.round(parseInt(timeStamp, 10) / 1000);
console.log(id, typeof id)


module.exports = {
create(req, res) {
const timeStamp = Date.now().toString();
const id = Math.round(parseInt(timeStamp, 10) / 1000);
const { name, email, password } = req.body;
return Users.create({
name,
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import bodyParser from 'body-parser';
import cors from 'cors';
import swaggerUI from 'swagger-ui-express';
import doc from '../doc.json';
import routes from './routes/index'

const port = process.env.PORT || 3000;
const app = express();
Expand All @@ -13,12 +14,13 @@ app.use(cors());

// api doc
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(doc));
app.use('/api/v1', routes);

app.get('/', (req, res) => res.status(200).send({ message: 'Welcome to Barefoot Nomad' }));
app.all('*', (req, res) => res.send({ message: 'route not found' }));

app.listen(port, () => {
console.info(`Server is up and listening on port ${port}`);
console.log(`Server is up and listening on port ${port}`);
});

export default app;
33 changes: 33 additions & 0 deletions src/migrations/20190814125500-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@ module.exports = {
password: {
type: Sequelize.STRING
},
gender: {
type:Sequelize.STRING
},
birthdate: {
type:Sequelize.DATE
},
preferredLanguage: {
type:Sequelize.STRING
},
preferredCurrency: {
type:Sequelize.STRING
},
city: {
type:Sequelize.STRING
},
state: {
type:Sequelize.STRING
},
zip: {
type:Sequelize.STRING
},
country: {
type:Sequelize.STRING
},
role: {
type:Sequelize.STRING
},
department: {
type:Sequelize.STRING
},
lineManager: {
type:Sequelize.STRING
},

createdAt: {
allowNull: false,
Expand Down
11 changes: 11 additions & 0 deletions src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ export default (sequelize, DataTypes) => {
lastName: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
gender: DataTypes.STRING,
birthdate: DataTypes.DATE,
preferredLanguage: DataTypes.STRING,
preferredCurrency: DataTypes.STRING,
city: DataTypes.STRING,
state: DataTypes.STRING,
zip: DataTypes.STRING,
country: DataTypes.STRING,
role: DataTypes.STRING,
department: DataTypes.STRING,
lineManager: DataTypes.STRING
}, {
classMethods: {
associate: (models) => {
Expand Down
5 changes: 2 additions & 3 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Object.keys(db).forEach((modelName) => {
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

export default db;

export {sequelize, Sequelize}
9 changes: 7 additions & 2 deletions src/routes/index.js
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;

39 changes: 39 additions & 0 deletions src/utils/helper.js
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 };

0 comments on commit 36f8e0d

Please sign in to comment.