Files
freeplanet_serverside/src/server/router/todos_router.js

186 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-02-05 03:40:22 +01:00
const express = require('express');
const router = express.Router();
const tools = require('../tools/general');
var server_constants = require('../tools/server_constants');
var { Project } = require('../models/project');
2019-02-05 03:40:22 +01:00
var { authenticate } = require('../middleware/authenticate');
2019-02-06 18:48:32 +01:00
var mongoose = require('mongoose');
const Subscription = mongoose.model('subscribers');
2019-02-05 03:40:22 +01:00
var { Todo } = require('../models/todo');
const _ = require('lodash');
const { ObjectID } = require('mongodb');
router.post('/', authenticate, (req, res) => {
2019-02-05 03:40:22 +01:00
var body = _.pick(req.body, tools.allfieldTodoWithId());
tools.mylogshow('INPUT', body);
2019-02-05 03:40:22 +01:00
var todo = new Todo(body);
// todo.expiring_at = new Date(todo.expiring_at);
tools.mylog('ID :', todo._id, todo.descr, todo.userId, req.user._id);
2019-02-13 18:47:58 +01:00
if (!('descr' in req.body)) {
return res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
}
2019-02-08 17:11:33 +01:00
if (String(todo.userId) !== String(req.user._id)) {
// I'm trying to write something not mine!
2019-02-08 17:11:33 +01:00
tools.mylog('todo.userId = ', todo.userId, 'req.user._id', req.user._id)
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
}
tools.mylog('TODO POST :', todo.descr, todo._id);
2019-02-05 03:40:22 +01:00
todo.modified = false;
if (!todo.descr) {
console.log('RECORD NON VALIDO !?', req.body)
}
2019-02-08 17:11:33 +01:00
todo.save().then((writeresult) => {
let idobj = writeresult._id;
Todo.findById(idobj)
.then(record => {
tools.mylog('REC SAVED :', record.descr);
tools.sendNotificationToUser(todo.userId, 'Todo: ' + record.descr, record.descr, '/todo/' + todo.category, 'todo')
2019-02-13 18:47:58 +01:00
.then(ris => {
if (ris) {
res.send({ record });
} else {
// already sent the error on calling sendNotificationToUser
}
})
2019-02-08 17:11:33 +01:00
})
2019-02-05 03:40:22 +01:00
}).catch((e) => {
console.log('ERRORE in TODO POST', e.message);
2019-02-05 03:40:22 +01:00
res.status(400).send(e);
});
});
2019-02-06 18:48:32 +01:00
2019-02-05 03:40:22 +01:00
router.patch('/:id', authenticate, (req, res) => {
var id = req.params.id;
var body = _.pick(req.body, tools.allfieldTodo());
2019-02-05 03:40:22 +01:00
tools.mylogshow('PATCH TODO: ', id);
2019-02-05 03:40:22 +01:00
if (!ObjectID.isValid(id)) {
tools.mylog('ERROR: id not VALID', id);
2019-02-05 03:40:22 +01:00
return res.status(404).send();
}
Todo.findByIdAndUpdate(id, { $set: body }, { new: true })
.then((todo) => {
if (!todo) {
tools.mylogshow(' TODO NOT FOUND !: id:', id, 'body: ', body);
return res.status(404).send();
}
let level = 0;
return Todo.calculateTreeTodo(todo.phase, todo.userId, todo.category, true, todo.category, false)
.then(objdatacalc => {
tools.mylogshow(' TODO TO MODIFY: ', todo.descr, todo.expiring_at);
2019-02-05 03:40:22 +01:00
if (todo.userId !== String(req.user._id)) {
// I'm trying to write something not mine!
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
}
2019-03-28 12:58:58 +01:00
todo.modified = false;
tools.mylog('PATCH ', todo.descr, todo._id);
2019-02-05 03:40:22 +01:00
res.send({ todo, objdatacalc });
});
}).catch((e) => {
2019-02-13 18:47:58 +01:00
tools.mylogserr('Error patch TODO: ', e);
2019-02-05 03:40:22 +01:00
res.status(400).send();
})
});
router.get('/:userId', authenticate, (req, res) => {
var userId = req.params.userId;
// var category = req.params.category;
2019-02-05 03:40:22 +01:00
2019-03-28 12:58:58 +01:00
tools.mylog('GET TODOS : ', req.params);
if (!ObjectID.isValid(userId)) {
return res.status(404).send();
}
if (userId !== String(req.user._id)) {
// I'm trying to write something not mine!
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
}
2019-02-05 03:40:22 +01:00
// Extract all the todos of the userId only
Todo.getAllTodo(userId).then((objtodos) => {
if (!!objtodos.arrtodos) {
tools.mylog('todos', objtodos.arrtodos.length);
tools.mylog('categories', objtodos.arrcategories.length);
}
res.send({ todos: objtodos.arrtodos, categories: objtodos.arrcategories });
}).catch((e) => {
console.log(e);
res.status(400).send(e);
});
});
router.get('/', (req, res) => {
// var category = req.params.category;
tools.mylog('GET ALL TODOS ');
// Extract all the todos of the userId only
Todo.getAllTodo('').then((objtodos) => {
if (!!objtodos.arrtodos) {
tools.mylog('todos', objtodos.arrtodos.length);
tools.mylog('categories', objtodos.arrcategories.length);
}
res.send({ todos: objtodos.arrtodos, categories: objtodos.arrcategories });
2019-02-05 03:40:22 +01:00
}).catch((e) => {
console.log(e);
res.status(400).send(e);
});
});
router.delete('/:id', authenticate, (req, res) => {
2019-02-05 03:40:22 +01:00
var id = req.params.id;
if (!ObjectID.isValid(id)) {
return res.status(404).send();
}
Todo.findByIdAndRemove(id).then((todo) => {
if (!todo) {
return res.status(404).send();
}
tools.mylog('DELETED ', todo.descr, todo._id);
2019-02-13 18:47:58 +01:00
res.send({ todo });
2019-02-05 03:40:22 +01:00
}).catch((e) => {
res.status(400).send();
});
});
2019-02-05 03:40:22 +01:00
module.exports = router;