do not use lodash to find notification

This commit is contained in:
Matteo Pagliazzi
2018-02-02 12:24:43 +01:00
parent 4580b33e43
commit 014b367f56

View File

@@ -1,5 +1,4 @@
import { authWithHeaders } from '../../middlewares/auth'; import { authWithHeaders } from '../../middlewares/auth';
import _ from 'lodash';
import { import {
NotFound, NotFound,
} from '../../libs/errors'; } from '../../libs/errors';
@@ -30,8 +29,8 @@ api.readNotification = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors; if (validationErrors) throw validationErrors;
let index = _.findIndex(user.notifications, { const index = user.notifications.findIndex(n => {
id: req.params.notificationId, return n.id === req.params.notificationId;
}); });
if (index === -1) { if (index === -1) {
@@ -72,10 +71,10 @@ api.readNotifications = {
let validationErrors = req.validationErrors(); let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors; if (validationErrors) throw validationErrors;
let notifications = req.body.notificationIds; let notificationsIds = req.body.notificationIds;
for (let notification of notifications) { for (let notificationId of notificationsIds) {
let index = _.findIndex(user.notifications, { const index = user.notifications.findIndex(n => {
id: notification, return n.id === notificationId;
}); });
if (index === -1) { if (index === -1) {
@@ -86,7 +85,7 @@ api.readNotifications = {
} }
await user.update({ await user.update({
$pull: { notifications: { id: { $in: notifications } } }, $pull: { notifications: { id: { $in: notificationsIds } } },
}).exec(); }).exec();
// Update the user version field manually, // Update the user version field manually,
@@ -122,8 +121,8 @@ api.seeNotification = {
const notificationId = req.params.notificationId; const notificationId = req.params.notificationId;
let notification = _.find(user.notifications, { const notification = user.notifications.find(n => {
id: notificationId, return n.id === notificationId;
}); });
if (!notification) { if (!notification) {
@@ -172,8 +171,8 @@ api.seeNotifications = {
let notificationsIds = req.body.notificationIds; let notificationsIds = req.body.notificationIds;
for (let notificationId of notificationsIds) { for (let notificationId of notificationsIds) {
let notification = _.find(user.notifications, { const notification = user.notifications.find(n => {
id: notificationId, return n.id === notificationId;
}); });
if (!notification) { if (!notification) {