'use strict';

import express from 'express';
import models  from '../models';
import { GENERIC, USER }          from '../utils/errorTypes.js';
import { verifyJWT, resolveUser } from '../middleware';
import { sendNotification }       from '../utils/pushNotifications';

let User = models.User;

let router = express.Router();

// Route for registering device notification tokens
router.post('/notifications/register', verifyJWT, resolveUser, (req, res, next) => {
    const deviceToken = req.body.deviceToken;

    if (deviceToken) {
        let tokens = req.user.notificationTokens || [];
        if (!tokens.includes(deviceToken)) {
            tokens.push(deviceToken);
        }

        req.user.update({ notificationTokens: tokens })
        .then(results => {
            return res.status(200).json({ message: 'Registered device token succesfully' });
        })
        .catch(err => {
            return res.err(400, USER.NOTIFICATIONS.SAVE_FAILED, 'Saving tokens failed!');
        });

    } else {
        res.err(400, GENERIC.MISSING_PARAMS, 'Token is missing!');
    }
});

export default router;


@@ -35,24 +35,54 @@
  'express';%0A
+import _       from 'lodash';%0A
 import model
@@ -578,24 +578,52 @@
 okens = 
-req.user
+%5B%5D;%0A%0A        if (req.user.dataValues
 .notific
@@ -637,15 +637,11 @@
 kens
- %7C%7C %5B%5D;
+) %7B
 %0A   
@@ -649,19 +649,87 @@
     
-if (!tokens
+    tokens = req.user.dataValues.notificationTokens;%0A        %7D%0A%0A        if (! _
 .inc
@@ -745,16 +745,24 @@
 iceToken
+, tokens
 )) %7B%0A   

Fix notification tokens