diff --git a/src/controllers/v1/notificationsController.ts b/src/controllers/v1/notificationsController.ts index ed7a662..2d8486c 100644 --- a/src/controllers/v1/notificationsController.ts +++ b/src/controllers/v1/notificationsController.ts @@ -152,12 +152,15 @@ export class NotificationsController { ): Promise { try { const user = params.user; - const notification = await markNotificationsAsRead( + const [notification] = await markNotificationsAsRead( [Number(notificationId)], user.id, ); + if (!notification) { + throw new Error(errorMessages.NOTIFICATION_NOT_FOUND); + } return { - notification: notification.raw[0], + notification, }; } catch (e) { logger.error('readNotification() error', e); @@ -178,9 +181,8 @@ export class NotificationsController { try { // in case mark as read all is limited per category await markNotificationGroupAsRead(user, params.category); - const notificationCounts = await countUnreadNotifications(user); - return notificationCounts; + return countUnreadNotifications(user); } catch (e) { logger.error('readNotification() error', e); throw e; diff --git a/src/middlewares/authentication.ts b/src/middlewares/authentication.ts index ee32cdd..08f51eb 100644 --- a/src/middlewares/authentication.ts +++ b/src/middlewares/authentication.ts @@ -73,28 +73,6 @@ export const authenticateThirdPartyServiceToken = async ( } }; -export const authenticateUser = async ( - req: Request, - res: Response, - next: NextFunction, -) => { - try { - const authorization = req.headers.authorization as string; - // TODO check with Authentication micro service - res.locals.user = { - userIdInGiveth: '', - userIdInTrace: '', - walletAddress: '', - email: '', - }; - res.locals.microservice = ''; - next(); - } catch (e) { - console.log('authenticateThirdPartyBasicAuth error', e); - next(e); - } -}; - export const validateAuthMicroserviceJwt = async ( req: Request, res: Response, @@ -118,14 +96,9 @@ export const validateAuthMicroserviceJwt = async ( const userAddress = result.data.publicAddress.toLowerCase(); - let user = await findUserByWalletAddress(userAddress); - - if (!user) { - user = await createNewUserAddress(userAddress); - } - - res.locals.user = user; - + res.locals.user = + (await findUserByWalletAddress(userAddress)) || + (await createNewUserAddress(userAddress)); next(); } catch (e) { console.log('authenticateThirdPartyBasicAuth error', e); diff --git a/src/repositories/notificationRepository.ts b/src/repositories/notificationRepository.ts index 1d8ce12..75e1871 100644 --- a/src/repositories/notificationRepository.ts +++ b/src/repositories/notificationRepository.ts @@ -5,14 +5,15 @@ import { Notification } from '../entities/notification'; import { errorMessages } from '../utils/errorMessages'; import { query } from 'express'; import { NOTIFICATION_CATEGORY } from '../types/general'; +import { CountUnreadNotificationsResponse } from '../types/requestResponses'; export const markNotificationGroupAsRead = async ( user: UserAddress, category?: string, -) => { +): Promise => { let query = Notification.createQueryBuilder('notification') - .select('notification.id') .innerJoinAndSelect('notification."notificationType"', 'notificationType') + .update(Notification, { isRead: true }) .where('notification."userAddressId" = :userAddressId', { userAddressId: user.id, }) @@ -23,19 +24,15 @@ export const markNotificationGroupAsRead = async ( category: category, }); } - const notifications = await query.getMany(); - - const notificationsIds = notifications.map(notification => notification.id); - const updateQuery = await markNotificationsAsRead(notificationsIds); - - return updateQuery.raw; + await query.execute(); }; // returns raw data as array always export const markNotificationsAsRead = async ( ids: number[], userAddressId?: number, -) => { +): Promise => { + // TODO as I changed markNotificationGroupAsRead, we should change this function to just get one id and update that notificaiton let updateQuery = Notification.createQueryBuilder('notification') .update(Notification, { isRead: true }) .where('notification.id IN (:...ids)'); @@ -47,30 +44,33 @@ export const markNotificationsAsRead = async ( ); } - return updateQuery + const result = await updateQuery .setParameter('ids', ids) .returning('*') .updateEntity(true) .execute(); + return result.raw; }; -export const countUnreadNotifications = async (user: UserAddress) => { - const [, total] = await baseNotificationQuery(user).getManyAndCount(); - const [, projectsRelated] = await baseNotificationQuery(user) +export const countUnreadNotifications = async ( + user: UserAddress, +): Promise => { + const total = await baseNotificationQuery(user).getCount(); + const projectsRelated = await baseNotificationQuery(user) .andWhere('notificationType.category = :category', { category: NOTIFICATION_CATEGORY.PROJECT_RELATED, }) - .getManyAndCount(); - const [, givEconomyRelated] = await baseNotificationQuery(user) + .getCount(); + const givEconomyRelated = await baseNotificationQuery(user) .andWhere('notificationType.category = :category', { category: NOTIFICATION_CATEGORY.GENERAL, }) - .getManyAndCount(); - const [, general] = await baseNotificationQuery(user) + .getCount(); + const general = await baseNotificationQuery(user) .andWhere('notificationType.category = :category', { category: NOTIFICATION_CATEGORY.GIV_ECONOMY, }) - .getManyAndCount(); + .getCount(); return { total, @@ -116,7 +116,7 @@ export const getNotifications = async ( if (isRead) { query = query.andWhere('notification."isRead" = :isRead', { - isRead: isRead === 'true', + isRead: isRead === 'true', }); } diff --git a/src/routes/v1/notificationSettingsRouter.ts b/src/routes/v1/notificationSettingsRouter.ts index 72773e7..982cf10 100644 --- a/src/routes/v1/notificationSettingsRouter.ts +++ b/src/routes/v1/notificationSettingsRouter.ts @@ -1,9 +1,5 @@ import express, { Request, Response } from 'express'; -import { - authenticateThirdPartyBasicAuth, - authenticateUser, - validateAuthMicroserviceJwt, -} from '../../middlewares/authentication'; +import { validateAuthMicroserviceJwt } from '../../middlewares/authentication'; import { NotificationSettingsController } from '../../controllers/v1/notificationSettingsController'; import { sendStandardResponse } from '../../utils/responseUtils'; diff --git a/src/utils/errorMessages.ts b/src/utils/errorMessages.ts index a15e42a..d9f4b59 100644 --- a/src/utils/errorMessages.ts +++ b/src/utils/errorMessages.ts @@ -52,6 +52,7 @@ export const errorMessages = { CHANGE_API_INVALID_TITLE_OR_EIN: 'ChangeAPI title or EIN not found or invalid', INVALID_SOCIAL_NETWORK: 'Invalid social network', + NOTIFICATION_NOT_FOUND: 'Notification not found', IT_SHOULD_HAVE_ONE_OR_TWO_ADDRESSES_FOR_RECIPIENT: 'It should have one or two wallet recipient addresses', NOT_IMPLEMENTED: 'Not implemented',