From 8f1ee2a7da8330bf3a9ce1c8ddf06e12f4c5cff5 Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Wed, 27 Feb 2019 02:59:02 +0100 Subject: [PATCH] - Update the way to use the data records on Vuex with Getters! - Fix: mongodb call passing array todos and categiroes already splitted --- server/models/todo.js | 59 +++++++++++++++++++++++++++++--- server/router/todos_router.js | 9 +++-- server/tests/seed/seed.js | 4 --- server/tools/general.js | 47 ++++++++++++++++++++++--- server/tools/server_constants.js | 4 +++ 5 files changed, 107 insertions(+), 16 deletions(-) diff --git a/server/models/todo.js b/server/models/todo.js index 9bf1a87..f9b7292 100644 --- a/server/models/todo.js +++ b/server/models/todo.js @@ -53,9 +53,6 @@ var TodoSchema = new mongoose.Schema({ id_prev: { type: String, }, - id_next: { - type: String, - }, progress: { type: Number, }, @@ -74,14 +71,68 @@ TodoSchema.methods.toJSON = function () { }; -TodoSchema.statics.findAllByUserId = function (userId) { +TodoSchema.statics.findByUserIdAndCat = function (userId, category) { var Todo = this; return Todo.find({ 'userId': userId, + 'category': category, }); }; +TodoSchema.statics.findAllByUserIdAndCat = function (userId, category = '') { + var Todo = this; + + if (category === '') { + return Todo.find({ + 'userId': userId, + }).then(ris => { + return tools.mapSort(ris) + }) + + } else { + return Todo.find({ + 'userId': userId, + 'category': category, + }); + } +}; + +TodoSchema.statics.getArrCategoryInTable = function (userId) { + var Todo = this; + + return Todo.find({ 'userId': userId }).distinct("category") + .then(arrcategory => { + return arrcategory + }) + +}; + +TodoSchema.statics.getAllTodo = async function (userId) { + var Todo = this; + + const arralltodo = await Todo.findAllByUserIdAndCat(userId); + // console.log('arralltodo', ); + + let obj = []; + + obj.arrcategories = await Todo.getArrCategoryInTable(userId); + + const arrtodos = []; + if (obj.arrcategories.length > 0) { + for (let mycat in obj.arrcategories) { + arrtodos.push(arralltodo.filter(item => item.category === obj.arrcategories[mycat])) + } + } + + obj.arrtodos = arrtodos; + + // console.log('obj', obj); + + return obj; + +}; + TodoSchema.pre('save', function (next) { // var todo = this; diff --git a/server/router/todos_router.js b/server/router/todos_router.js index 4e9abe9..5233d74 100644 --- a/server/router/todos_router.js +++ b/server/router/todos_router.js @@ -105,6 +105,7 @@ router.patch('/:id', authenticate, (req, res) => { router.get('/:userId', authenticate, (req, res) => { var userId = req.params.userId; + // var category = req.params.category; tools.mylog('GET : ', req.params); @@ -118,11 +119,13 @@ router.get('/:userId', authenticate, (req, res) => { } // Extract all the todos of the userId only - Todo.findAllByUserId(userId).then((todos) => { + // Todo.findAllByUserIdAndCat(userId, category).then((todos) => { + Todo.getAllTodo(userId).then((objtodos) => { - tools.mylog('todos', todos.length); + tools.mylog('todos', objtodos.arrtodos.length); + tools.mylog('categories', objtodos.arrcategories.length); - res.send({ todos: todos }); + res.send({ todos: objtodos.arrtodos, categories: objtodos.arrcategories }); }).catch((e) => { console.log(e); res.status(400).send(e); diff --git a/server/tests/seed/seed.js b/server/tests/seed/seed.js index fbf6aa1..a69285f 100644 --- a/server/tests/seed/seed.js +++ b/server/tests/seed/seed.js @@ -81,7 +81,6 @@ const todos = [{ descr: "Primo Task Esempio", enableExpiring: false, expiring_at: new Date(), - id_next: "10000000", id_prev: "0", modified: false, modify_at: new Date(), @@ -98,7 +97,6 @@ const todos = [{ descr: "Secondo Task Esempio", enableExpiring: false, expiring_at: new Date(), - id_next: "10000000", id_prev: "1", modified: false, modify_at: new Date(), @@ -115,7 +113,6 @@ const todos = [{ descr: "Terzo Task Esempio", enableExpiring: false, expiring_at: new Date(), - id_next: "10000000", id_prev: "1", modified: false, modify_at: new Date(), @@ -132,7 +129,6 @@ const todos = [{ descr: "Nuovo Quarto Task Esempio da Inserire", enableExpiring: false, expiring_at: new Date(), - id_next: "10000000", id_prev: "2", modified: false, modify_at: new Date(), diff --git a/server/tools/general.js b/server/tools/general.js index 567e6e6..cfb966c 100644 --- a/server/tools/general.js +++ b/server/tools/general.js @@ -7,6 +7,8 @@ var Url = require('url-parse'); const mongoose = require('mongoose'); const Subscription = mongoose.model('subscribers'); +const server_constants = require('../tools/server_constants'); + // SETTINGS WebPush Configuration const webpush = require('web-push'); @@ -48,7 +50,7 @@ module.exports = { allfieldTodo: function () { return ['userId', 'pos', 'category', 'descr', 'priority', 'completed', 'created_at', 'modify_at', - 'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'id_next', 'progress', 'modified'] + 'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'progress', 'modified'] }, allfieldTodoWithId: function () { @@ -60,10 +62,10 @@ module.exports = { console.log('sendBackNotif:', subscription, payload); // Pass object into sendNotification webpush.sendNotification(subscription, JSON.stringify(payload)).catch(err => console.error(err)) - .catch(err => { + .catch(err => { if (err.statusCode === 410) { // Gone: is not valid anymore (Expired probably!), so I have to delete from my db - return Subscription.findOneAndRemove( { _id:subscription._id } ) + return Subscription.findOneAndRemove({ _id: subscription._id }) } else { console.log('Subscription is no longer valid: ', err); } @@ -132,7 +134,7 @@ module.exports = { console.log('************ INVIO WEBPUSH.SENDNOTIFICATION N° ', conta, '/', trovati, 'A', subscription.browser); console.log('vapidDetails', pushOptions.vapidDetails); - payload.title = process.env.URLBASE_APP1 + ' Msg n° ' + conta +'/' + trovati; + payload.title = process.env.URLBASE_APP1 + ' Msg n° ' + conta + '/' + trovati; // payload.message += subscription.browser ; const pushPayload = JSON.stringify(payload); @@ -172,8 +174,43 @@ module.exports = { } }); + }, + // ********************** + // SORT WITH PREV_ID + // ********************** + mapSort: function (linkedList) { + var sortedList = []; + var map = new Map(); + var currentId = null; + + // console.log('linkedList', linkedList); + + // index the linked list by previous_item_id + for (var i = 0; i < linkedList.length; i++) { + var item = linkedList[i]; + if (item.id_prev === server_constants.LIST_START) { + // first item + currentId = String(item._id); + // console.log('currentId', currentId); + sortedList.push(item); + } else { + map.set(item.id_prev, i); + } + } + + while (sortedList.length < linkedList.length) { + // get the item with a previous item ID referencing the current item + var nextItem = linkedList[map.get(currentId)]; + if (nextItem === undefined) + break; + sortedList.push(nextItem); + currentId = String(nextItem._id); + } + + // console.log('DOPO sortedList', sortedList); + + return sortedList; } - // Test }; diff --git a/server/tools/server_constants.js b/server/tools/server_constants.js index 636e38b..a50f465 100644 --- a/server/tools/server_constants.js +++ b/server/tools/server_constants.js @@ -13,4 +13,8 @@ module.exports = Object.freeze({ RIS_CODE_HTTP_INVALID_TOKEN: 403, + + LIST_END: '10000000', + LIST_START: '0', + });