Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor some functions #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/controllers/v1/notificationsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,15 @@ export class NotificationsController {
): Promise<ReadSingleNotificationResponse> {
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);
Expand All @@ -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;
Expand Down
33 changes: 3 additions & 30 deletions src/middlewares/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
38 changes: 19 additions & 19 deletions src/repositories/notificationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
let query = Notification.createQueryBuilder('notification')
.select('notification.id')
.innerJoinAndSelect('notification."notificationType"', 'notificationType')
.update<Notification>(Notification, { isRead: true })
.where('notification."userAddressId" = :userAddressId', {
userAddressId: user.id,
})
Expand All @@ -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<Notification[]> => {
// 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>(Notification, { isRead: true })
.where('notification.id IN (:...ids)');
Expand All @@ -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<CountUnreadNotificationsResponse> => {
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,
Expand Down Expand Up @@ -116,7 +116,7 @@ export const getNotifications = async (

if (isRead) {
query = query.andWhere('notification."isRead" = :isRead', {
isRead: isRead === 'true',
isRead: isRead === 'true',
});
}

Expand Down
6 changes: 1 addition & 5 deletions src/routes/v1/notificationSettingsRouter.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
1 change: 1 addition & 0 deletions src/utils/errorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down