Skip to content

Commit

Permalink
Update: added user activity in database
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsinht committed May 11, 2020
1 parent 7bc7301 commit b3f3869
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 22 deletions.
8 changes: 8 additions & 0 deletions admin/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const router = express.Router();
const auth = require('../../middleware/auth');
const { check, validationResult } = require('express-validator');
const request = require('request');
const config = require('config');

module.exports = router;
32 changes: 20 additions & 12 deletions client/src/components/Work/Work.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,32 @@ class Work extends Component {
*/
refreshData = (id) => {
this.props.fetchWork(id).then((fetched_work) => {
const isValid = fetched_work
? fetched_work.work_data.length
? fetched_work.work_data[0].taskDetails[0]
? true
: false
: false
: false;
this.setState(
{
work: fetched_work,
loading: false,
task: fetched_work.work_data.length
? fetched_work.work_data[0].taskDetails[0]
: [],
task: isValid ? fetched_work.work_data[0].taskDetails[0] : [],
isValid: isValid,
},
() => {
let status = this.getLatestStatus();
if (status === 3) {
this.props.getCurrentProfile();
}
this.setState({
last_status: status,
});
if (this.state.isValid) {
let status = this.getLatestStatus();
if (status === 3) {
this.props.getCurrentProfile();
}
this.setState({
last_status: status,
});

this.checkIfFeedbackPending();
this.checkIfFeedbackPending();
}
}
);
});
Expand Down Expand Up @@ -252,7 +260,7 @@ class Work extends Component {
</div>
);
}
if (this.state.task.length === 0) {
if (!this.state.task || this.state.task.length === 0) {
return (
<div className='taskv-loader error-msg-center'>
You are not allowed to view this page.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions config/keys.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
// mongoURI: 'mongodb://localhost/taskbarter', //For local server localhost
mongoURI: process.env.MONGO_URI, //For live Server taskbarter.com
mongoURI: 'mongodb://localhost/taskbarter', //For local server localhost
//mongoURI: process.env.MONGO_URI, //For live Server taskbarter.com
secretOrKey: 'secret', //TODO: Change this before release
jwtSecret: 'mysecret', //TODO: Change this before release
taskBarterGmail: process.env.EMAIL_ADDRESS,
Expand Down
31 changes: 31 additions & 0 deletions models/UserActivity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//TODO: Add more relevant fields
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserActivitySchema = new Schema(
{
user_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users',
default: null,
},
activity: {
type: String,
default: '',
},
//Boolean representing whether the receiver has seen this update or not
payload: {
type: String,
default: '',
},
type: {
type: Number,
default: 0,
},
},
{ timestamps: true }
);

module.exports = UserActivity = mongoose.model(
'useractivity',
UserActivitySchema
);
17 changes: 16 additions & 1 deletion routes/api/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const User = require('../../models/User');
const Conversation = require('../../models/Conversation');
const Message = require('../../models/Message');
const PersonalDetails = require('../../models/PersonalDetails');

const UserActivity = require('../../models/UserActivity');
var ObjectId = require('mongodb').ObjectId;

// @route POST api/messages/send
Expand All @@ -35,6 +35,11 @@ router.post('/send', auth, (req, res) => {
res.status(500).send('Server Error');
});
} catch (e) {
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while sending a message.`,
payload: JSON.stringify(e.message),
}).save();
console.log(e);
}
});
Expand All @@ -53,6 +58,11 @@ router.post('/conversation', auth, (req, res) => {
.then((conv) => res.json(conv))
.catch((err) => {
console.error(err);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while creating a convo`,
payload: JSON.stringify(err),
}).save();
res.status(500).send('Server Error');
});
});
Expand Down Expand Up @@ -165,6 +175,11 @@ router.get('/conversations', async (req, res) => {
.status(400)
.json({ msg: 'Could not find conversations for this user.' });
}
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while fetching convo list`,
payload: JSON.stringify(err),
}).save();
console.log(err);
}
});
Expand Down
84 changes: 82 additions & 2 deletions routes/api/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Proposal = require('../../models/Proposal');
const Work = require('../../models/Work');
const Conversation = require('../../models/Conversation');
const PersonalDetails = require('../../models/PersonalDetails');
const UserActivity = require('../../models/UserActivity');
const { check, validationResult } = require('express-validator');
const { MESSAGETYPE_PROPOSAL } = require('../../constants/types');
const {
Expand Down Expand Up @@ -67,11 +68,19 @@ router.post('/add', auth, async (req, res) => {
profile.user,
`/t/${task._id}`
);

new UserActivity({
user_id: req.user.id,
activity: `User added a new task. '${task.headline}'`,
}).save();
return res.json(task);
})
.catch((err) => {
console.error(err);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while adding task`,
payload: JSON.stringify(err.message),
}).save();
return res.status(500).send('Server Error');
});
});
Expand Down Expand Up @@ -179,6 +188,10 @@ router.get('/explore', async (req, res) => {
res.json(tasks);
} catch (err) {
console.error(err.message);
new UserActivity({
activity: `User ran into a problem while searching task`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error');
}
});
Expand Down Expand Up @@ -242,6 +255,11 @@ router.get('/fetch', auth, async (req, res) => {
res.json({ taskData });
} catch (err) {
console.error(err.message);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while fetching task`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error');
}
});
Expand Down Expand Up @@ -326,6 +344,11 @@ router.get('/edit', auth, async (req, res) => {
res.json({ taskData });
} catch (err) {
console.error(err.message);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while getting edit data of a task`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error');
}
});
Expand Down Expand Up @@ -377,9 +400,20 @@ router.post('/editstatus', auth, async (req, res) => {
);
}

new UserActivity({
user_id: req.user.id,
activity: `User changed status of the task ${taskData.headline}`,
payload: JSON.stringify(req.body),
}).save();

res.json({ taskData: taskData[0] });
} catch (err) {
console.error(err.message);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while getting status.`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error');
}
});
Expand Down Expand Up @@ -415,8 +449,18 @@ router.post('/edit', auth, async (req, res) => {

await taskData.save();
res.json({ taskData: taskData[0] });
new UserActivity({
user_id: req.user.id,
activity: `User edited a task ${taskData.headline}`,
payload: JSON.stringify(req.body),
}).save();
} catch (err) {
console.error(err.message);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while editing a task`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error');
}
});
Expand All @@ -435,8 +479,18 @@ router.delete('/:task_id', auth, async (req, res) => {
await Task.findOneAndRemove({ _id: req.params.task_id });

res.json({ msg: 'Task deleted' });
new UserActivity({
user_id: req.user.id,
activity: `User just deleted the task.`,
payload: JSON.stringify(err.message),
}).save();
} catch (err) {
console.error(err.message);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while deleting a task`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error');
}
});
Expand Down Expand Up @@ -566,6 +620,11 @@ router.post(
res.json(task.proposals);
} catch (err) {
console.error(err.message);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while sending a proposal`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error .');
}
}
Expand Down Expand Up @@ -691,8 +750,19 @@ router.post('/sendproposal', auth, async (req, res) => {
);

res.json(prop);

new UserActivity({
user_id: req.user.id,
activity: `User sent a new proposal`,
payload: JSON.stringify(req.body),
}).save();
} catch (err) {
console.error(err);
new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while sending a proposal`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error');
}
});
Expand Down Expand Up @@ -785,9 +855,19 @@ router.post(

await pros.save();
res.json(pros);

new UserActivity({
user_id: req.user.id,
activity: `User responded with changed proposal status`,
payload: JSON.stringify(req.body),
}).save();
} catch (err) {
console.error(err.message);

new UserActivity({
user_id: req.user.id,
activity: `User ran into a problem while changing state of proposal task`,
payload: JSON.stringify(err.message),
}).save();
res.status(500).send('Server Error .');
}
}
Expand Down
Loading

0 comments on commit b3f3869

Please sign in to comment.