- Created all Tests with Mocha: User + Todo tables
This commit is contained in:
@@ -2,6 +2,10 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const webpush = require('web-push');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
var server_constants = require('../tools/server_constants');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
var mongoose = require('mongoose');
|
||||
@@ -13,18 +17,27 @@ 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) => {
|
||||
router.post('/', authenticate, (req, res) => {
|
||||
|
||||
var body = _.pick(req.body, allfieldTodoWithId);
|
||||
var body = _.pick(req.body, tools.allfieldTodoWithId());
|
||||
var todo = new Todo(body);
|
||||
|
||||
console.log('POST ', todo.descr);
|
||||
// tools.mylogshow('ID :', todo._id)
|
||||
|
||||
// tools.mylogshow('userid', todo.userId, req.user._id)
|
||||
|
||||
if (!('descr' in req.body)){
|
||||
return res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
tools.mylog('POST ', todo.descr);
|
||||
|
||||
todo.modified = false;
|
||||
if (!todo.descr) {
|
||||
@@ -34,6 +47,7 @@ router.post('/:id', authenticate, (req, res) => {
|
||||
sendNotificationToUser(todo.userId, 'New Todo', 'New Todo added!', '/' + todo.category, 'todo');
|
||||
|
||||
todo.save().then((doc) => {
|
||||
// tools.mylogshow('ID SAVED :', doc._id)
|
||||
res.send(doc);
|
||||
}).catch((e) => {
|
||||
console.log(e.message);
|
||||
@@ -115,18 +129,24 @@ function sendNotificationToUser(userId, title, content, openUrl, tag) {
|
||||
|
||||
router.patch('/:id', authenticate, (req, res) => {
|
||||
var id = req.params.id;
|
||||
var body = _.pick(req.body, allfieldTodo);
|
||||
var body = _.pick(req.body, tools.allfieldTodo());
|
||||
|
||||
if (!ObjectID.isValid(id)) {
|
||||
console.log('ERROR: id not VALID', id);
|
||||
tools.mylog('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();
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
todo.modified = false;
|
||||
|
||||
res.send({todo});
|
||||
@@ -140,10 +160,20 @@ router.patch('/:id', authenticate, (req, res) => {
|
||||
router.get('/:userId', authenticate, (req, res) => {
|
||||
var userId = req.params.userId;
|
||||
|
||||
console.log('GET : ', req.params);
|
||||
// tools.mylogshow('GET : ', 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 });
|
||||
}
|
||||
|
||||
// Extract all the todos of the userId only
|
||||
Todo.findAllByUserId(userId).then((todos) => {
|
||||
// tools.mylogshow('todos', todos)
|
||||
res.send({ todos });
|
||||
}).catch((e) => {
|
||||
console.log(e);
|
||||
|
||||
Reference in New Issue
Block a user