Skip to content

Commit

Permalink
feat: added single workday api
Browse files Browse the repository at this point in the history
  • Loading branch information
varijkapil13 committed Jan 17, 2019
1 parent c93b41c commit 6459dad
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
37 changes: 35 additions & 2 deletions server/controllers/workday.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as XLSX from 'xlsx';
import moment from 'moment';
import model from '../models';

const {WorkDay} = model;
const {Avatar} = model;
const {WorkDay, Avatar} = model;

class Workdays {
/**
*
Expand Down Expand Up @@ -76,6 +76,39 @@ class Workdays {
})
.catch(error => res.status(400).send(error));
}

static addWorkday(req, res) {
const avatarId = req.params.avatarId;
const {date, tags, notes, from, to, logged_hours} = req.body;
Avatar.findByPk(avatarId)
.then(avatar => {
if (avatar) {
WorkDay.create({
date,
tags,
notes,
from,
to,
logged_hours,
avatarId
})
.then(workday => {
return res.status(201).send({
status: true,
message: 'Workday successfully added for ' + avatar.first_name + ' ' + avatar.last_name,
workday
});
})
.catch(error => res.status(400).send(error));
} else {
return res.status(400).send({
status: false,
message: 'Avatar with id ' + avatarId + ' not found'
});
}
})
.catch(error => res.status(400).send(error));
}
}

export default Workdays;
2 changes: 1 addition & 1 deletion server/migrations/20190116075822-create-work-day.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
primaryKey: true,
type: Sequelize.INTEGER
},
user_id: {
avatarId: {
allowNull: false,
type: Sequelize.INTEGER
},
Expand Down
6 changes: 3 additions & 3 deletions server/models/workday.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ export default (sequelize, DataTypes) => {
const WorkDay = sequelize.define(
'WorkDay',
{
user_id: {
avatarId: {
type: DataTypes.INTEGER,
allowNull: {
args: false,
msg: "Please enter user's name"
msg: 'Please provide avatar id'
}
},
date: {
Expand All @@ -20,7 +20,7 @@ export default (sequelize, DataTypes) => {
notes: DataTypes.STRING,
from: DataTypes.TIME,
to: DataTypes.TIME,
logged_hours: {type: DataTypes.DOUBLE}
logged_hours: DataTypes.DOUBLE
},
{}
);
Expand Down
1 change: 1 addition & 0 deletions server/routes/workday.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const routes = express.Router();
const upload = multer({storage: multer.memoryStorage()});

routes.post('/:avatarId/upload', upload.single('reportFile'), Workday.importTimelyFile);
routes.post('/:avatarId', Workday.addWorkday);

export default routes;

0 comments on commit 6459dad

Please sign in to comment.