- add: createPushSubscription :
'Subscribed to FreePlanet.app!', 'You can now receive Notification and Messages.'
This commit is contained in:
117
server/router/todos_router.js
Normal file
117
server/router/todos_router.js
Normal file
@@ -0,0 +1,117 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const webpush = require('web-push');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
var { Todo } = require('../models/todo');
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
const allfieldTodo = ['userId', 'pos', 'category', 'descr', 'priority', 'completed', 'created_at', 'modify_at',
|
||||
'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'id_next', 'progress', 'modified'];
|
||||
|
||||
const allfieldTodoWithId = ['_id', ...allfieldTodo];
|
||||
|
||||
|
||||
router.post('/:id', authenticate, (req, res) => {
|
||||
|
||||
var body = _.pick(req.body, allfieldTodoWithId);
|
||||
var todo = new Todo(body);
|
||||
|
||||
console.log('POST ', todo.descr);
|
||||
|
||||
todo.modified = false;
|
||||
if (!todo.descr) {
|
||||
console.log('RECORD NON VALIDO !?', req.body)
|
||||
}
|
||||
|
||||
sendNotificationToUser('New Post', 'New Post added!', '/' + todo.category);
|
||||
|
||||
todo.save().then((doc) => {
|
||||
res.send(doc);
|
||||
}).catch((e) => {
|
||||
console.log(e.message);
|
||||
res.status(400).send(e);
|
||||
});
|
||||
});
|
||||
|
||||
router.patch('/:id', authenticate, (req, res) => {
|
||||
var id = req.params.id;
|
||||
var body = _.pick(req.body, allfieldTodo);
|
||||
|
||||
if (!ObjectID.isValid(id)) {
|
||||
console.log('ERROR: id not VALID', id);
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
Todo.findByIdAndUpdate(id, {$set: body}, {new: true}).then((todo) => {
|
||||
if (!todo) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
todo.modified = false
|
||||
|
||||
res.send({todo});
|
||||
}).catch((e) => {
|
||||
res.status(400).send();
|
||||
})
|
||||
});
|
||||
|
||||
function sendNotificationToUser(title, content, openUrl) {
|
||||
|
||||
// Create payload
|
||||
const payload = JSON.stringify(
|
||||
{
|
||||
title,
|
||||
content,
|
||||
openUrl
|
||||
}
|
||||
);
|
||||
|
||||
subscriptioncfg = {};
|
||||
|
||||
// Pass object into sendNotification
|
||||
|
||||
webpush.sendNotification(subscriptioncfg, payload).catch(err => console.error(err));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
router.get('/:userId', authenticate, (req, res) => {
|
||||
var userId = req.params.userId;
|
||||
|
||||
console.log('GET : ', req.params);
|
||||
|
||||
// Extract all the todos of the userId only
|
||||
Todo.findAllByUserId(userId).then((todos) => {
|
||||
res.send({ todos });
|
||||
}).catch((e) => {
|
||||
console.log(e);
|
||||
res.status(400).send(e);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
router.delete('/:id', (req, res) => {
|
||||
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();
|
||||
}
|
||||
|
||||
res.send({todo});
|
||||
}).catch((e) => {
|
||||
res.status(400).send();
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user