import { UpdateNotificationsParams } from '../../model/components/profile'; import { ApiMethods, ApiRoutes } from '../../model/api'; import { Notification, User } from '../../model/user'; import authenticateRequest from './authenticateRequest'; const checkNewNotifications = async (user: User): Promise => { if (user?.notifications?.length) { let needUpdate = false; const updatedNotifications: Notification[] = user.notifications.map( (notification) => { if (notification.newNotificationsInfo?.length) { needUpdate = true; return { date: notification.date, newNotificationsInfo: [], notificationsInfo: (notification.notificationsInfo || []).concat( notification.newNotificationsInfo, ), }; } return notification; }, ); if (needUpdate) { const response = await authenticateRequest< User, UpdateNotificationsParams >(ApiMethods.Put, `${ApiRoutes.UpdateUser}/${user._id}`, { notifications: updatedNotifications, }); return response.data; } return user; } return user; }; export default checkNewNotifications;