Skip to content

Commit

Permalink
Merge pull request #87 from alkem-io/develop
Browse files Browse the repository at this point in the history
Release: Notifications Flag
  • Loading branch information
valentinyanakiev authored Jan 17, 2022
2 parents 9087c83 + bf6fc8e commit 9de235c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "alkemio-notifications",
"version": "0.4.5",
"version": "0.4.6",
"description": "Alkemio notifications service",
"author": "Cherrytwist Foundation",
"author": "Alkemio Foundation",
"private": false,
"license": "EUPL-1.2",
"main": "src/main.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class AppController {
@Payload() eventPayload: any,
@Ctx() context: RmqContext,
// notificationBuilder: any,
sendNotifications: any,
sendNotificationsImpl: any,
eventName: string
) {
this.logger.verbose?.(
Expand All @@ -109,7 +109,7 @@ export class AppController {
}

// https://www.squaremobius.net/amqp.node/channel_api.html#channel_nack
sendNotifications
sendNotificationsImpl
.then((x: any[]) => {
const nacked = x.filter(
(y: { status: string }) => y.status === 'rejected'
Expand Down
3 changes: 2 additions & 1 deletion src/core/contracts/notified.users.provider.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { User } from '../models';
import { CredentialCriteria } from '../models/credential.criteria';
import { IFeatureFlagProvider } from './feature.flag.provider.interface';

export interface INotifiedUsersProvider {
export interface INotifiedUsersProvider extends IFeatureFlagProvider {
getUser(userID: string): Promise<User>;
getUniqueUsersMatchingCredentialCriteria(
credentialCriterias: CredentialCriteria[]
Expand Down
17 changes: 17 additions & 0 deletions src/services/domain/notification/notification.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('NotificationService', () => {
{
provide: ALKEMIO_CLIENT_ADAPTER,
useValue: {
areNotificationsEnabled: jest.fn(),
getUser: jest.fn(),
getApplicationCreator: jest.fn(),
getUsersMatchingCredentialCriteria: jest.fn(),
Expand Down Expand Up @@ -108,6 +109,10 @@ describe('NotificationService', () => {
...testData.opportunityAdmins,
];

jest
.spyOn(alkemioAdapter, 'areNotificationsEnabled')
.mockResolvedValue(true);

jest
.spyOn(alkemioAdapter, 'getUniqueUsersMatchingCredentialCriteria')
.mockResolvedValue(admins);
Expand Down Expand Up @@ -140,5 +145,17 @@ describe('NotificationService', () => {
)
).rejects.toThrow();
});

it('Should not send notifications when notifications are disabled', async () => {
jest
.spyOn(alkemioAdapter, 'areNotificationsEnabled')
.mockResolvedValue(false);

const res = await notificationService.sendApplicationCreatedNotifications(
testData.data as ApplicationCreatedEventPayload
);

expect(res.length).toBe(0); //shouldn't have any notifications sent
});
});
});
20 changes: 19 additions & 1 deletion src/services/domain/notification/notification.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Injectable, Inject, LoggerService } from '@nestjs/common';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import NotifmeSdk, { NotificationStatus } from 'notifme-sdk';
import { LogContext, NOTIFICATIONS_PROVIDER } from '@src/common';
import {
ALKEMIO_CLIENT_ADAPTER,
LogContext,
NOTIFICATIONS_PROVIDER,
} from '@src/common';
import { ApplicationCreatedEventPayload } from '@src/types/application.created.event.payload';
import { ApplicationCreatedNotificationBuilder } from '@src/services';
import { CommunicationDiscussionCreatedNotificationBuilder } from '../builders/communication-discussion-created/communication.discussion.created.notification.builder';
Expand All @@ -10,12 +14,15 @@ import { UserRegisteredNotificationBuilder } from '../builders/user-registered/u
import { UserRegistrationEventPayload } from '@src/types';
import { CommunicationUpdateEventPayload } from '@src/types/communication.update.event.payload';
import { CommunicationDiscussionCreatedEventPayload } from '@src/types/communication.discussion.created.event.payload';
import { AlkemioClientAdapter } from '@src/services/application/alkemio-client-adapter';

@Injectable()
export class NotificationService {
constructor(
@Inject(WINSTON_MODULE_NEST_PROVIDER)
private readonly logger: LoggerService,
@Inject(ALKEMIO_CLIENT_ADAPTER)
private readonly alkemioClientAdapter: AlkemioClientAdapter,
@Inject(NOTIFICATIONS_PROVIDER)
private readonly notifmeService: NotifmeSdk,
private applicationCreatedNotificationBuilder: ApplicationCreatedNotificationBuilder,
Expand All @@ -28,6 +35,17 @@ export class NotificationService {
payload: any,
notificationBuilder: any
): Promise<PromiseSettledResult<NotificationStatus>[]> {
const notificationsEnabled =
await this.alkemioClientAdapter.areNotificationsEnabled();
if (!notificationsEnabled) {
this.logger.verbose?.(
'Notification disabled. No notifications are going to be built.',
LogContext.NOTIFICATIONS
);

return [];
}

return notificationBuilder
.buildNotifications(payload)
.then((x: any[]) => x.map((x: any) => this.sendNotification(x)))
Expand Down

0 comments on commit 9de235c

Please sign in to comment.