From cf83376b07be528069ee1a57c9dea4488b9e4a8d Mon Sep 17 00:00:00 2001 From: Pooya Raki Date: Sun, 27 Oct 2024 14:32:44 +0100 Subject: [PATCH] refactor notification hooks to use notification v2 --- .../helpers/event-notifications.helper.ts | 6 +- src/routes/hooks/hooks-notifications.spec.ts | 67 +++++++++---------- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/domain/hooks/helpers/event-notifications.helper.ts b/src/domain/hooks/helpers/event-notifications.helper.ts index 5ce7853ebb..e807b35f45 100644 --- a/src/domain/hooks/helpers/event-notifications.helper.ts +++ b/src/domain/hooks/helpers/event-notifications.helper.ts @@ -13,10 +13,7 @@ import { } from '@/routes/hooks/entities/event-type.entity'; import { LoggingService, ILoggingService } from '@/logging/logging.interface'; import { Event } from '@/routes/hooks/entities/event.entity'; -import { - INotificationsRepositoryV2, - NotificationsRepositoryV2Module, -} from '@/domain/notifications/v2/notifications.repository.interface'; +import { INotificationsRepositoryV2 } from '@/domain/notifications/v2/notifications.repository.interface'; import { DeletedMultisigTransactionEvent } from '@/routes/hooks/entities/schemas/deleted-multisig-transaction.schema'; import { ExecutedTransactionEvent } from '@/routes/hooks/entities/schemas/executed-transaction.schema'; import { IncomingEtherEvent } from '@/routes/hooks/entities/schemas/incoming-ether.schema'; @@ -37,6 +34,7 @@ import { IDelegatesV2Repository, } from '@/domain/delegate/v2/delegates.v2.repository.interface'; import { UUID } from 'crypto'; +import { NotificationsRepositoryV2Module } from '@/domain/notifications/v2/notifications.repository.module'; type EventToNotify = | DeletedMultisigTransactionEvent diff --git a/src/routes/hooks/hooks-notifications.spec.ts b/src/routes/hooks/hooks-notifications.spec.ts index dac5c38e75..59f8bdcfd9 100644 --- a/src/routes/hooks/hooks-notifications.spec.ts +++ b/src/routes/hooks/hooks-notifications.spec.ts @@ -28,12 +28,9 @@ import request from 'supertest'; import { IPushNotificationsApi } from '@/domain/interfaces/push-notifications-api.interface'; import { PushNotificationsApiModule } from '@/datasources/push-notifications-api/push-notifications-api.module'; import { TestPushNotificationsApiModule } from '@/datasources/push-notifications-api/__tests__/test.push-notifications-api.module'; -import { NotificationsDatasourceModule } from '@/datasources/notifications/notifications.datasource.module'; -import { TestNotificationsDatasourceModule } from '@/datasources/notifications/__tests__/test.notifications.datasource.module'; import { IConfigurationService } from '@/config/configuration.service.interface'; import type { INetworkService } from '@/datasources/network/network.service.interface'; import { NetworkService } from '@/datasources/network/network.service.interface'; -import { INotificationsDatasource } from '@/domain/interfaces/notifications.datasource.interface'; import { faker } from '@faker-js/faker'; import { getAddress } from 'viem'; import { pageBuilder } from '@/domain/entities/__tests__/page.builder'; @@ -57,12 +54,13 @@ import { import jwtConfiguration from '@/datasources/jwt/configuration/__tests__/jwt.configuration'; import { TestPostgresDatabaseModuleV2 } from '@/datasources/db/v2/test.postgres-database.module'; import { PostgresDatabaseModuleV2 } from '@/datasources/db/v2/postgres-database.module'; +import { INotificationsRepositoryV2 } from '@/domain/notifications/v2/notifications.repository.interface'; // TODO: Migrate to E2E tests as TransactionEventType events are already being received via queue. describe.skip('Post Hook Events for Notifications (Unit)', () => { let app: INestApplication; let pushNotificationsApi: jest.MockedObjectDeep; - let notificationsDatasource: jest.MockedObjectDeep; + let notificationsRepository: jest.MockedObjectDeep; let networkService: jest.MockedObjectDeep; let configurationService: IConfigurationService; let authToken: string; @@ -97,18 +95,18 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { .useModule(TestPostgresDatabaseModuleV2) .overrideModule(PushNotificationsApiModule) .useModule(TestPushNotificationsApiModule) - .overrideModule(NotificationsDatasourceModule) - .useModule(TestNotificationsDatasourceModule) .compile(); app = moduleFixture.createNestApplication(); networkService = moduleFixture.get(NetworkService); pushNotificationsApi = moduleFixture.get(IPushNotificationsApi); - notificationsDatasource = moduleFixture.get(INotificationsDatasource); configurationService = moduleFixture.get(IConfigurationService); authToken = configurationService.getOrThrow('auth.token'); safeConfigUrl = configurationService.getOrThrow('safeConfig.baseUri'); + notificationsRepository = moduleFixture.get(INotificationsRepositoryV2); + notificationsRepository.getSubscribersBySafe = jest.fn(); + await app.init(); } @@ -159,7 +157,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { }), ); const chain = chainBuilder().build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue(subscribers); + notificationsRepository.getSubscribersBySafe.mockResolvedValue(subscribers); networkService.get.mockImplementation(({ url }) => { if (url === `${safeConfigUrl}/api/v1/chains/${event.chainId}`) { return Promise.resolve({ @@ -210,7 +208,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { cloudMessagingToken: faker.string.alphanumeric(), }), ); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -290,7 +288,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { cloudMessagingToken: faker.string.alphanumeric(), }), ); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -367,7 +365,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { ) .with('threshold', faker.number.int({ min: 2 })) .build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -439,7 +437,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { subscribers.map((subscriber) => subscriber.subscriber), ) .build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -491,7 +489,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { subscribers.map((subscriber) => subscriber.subscriber), ) .build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const multisigTransaction = multisigTransactionBuilder() @@ -560,7 +558,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { deviceUuid: faker.string.uuid() as UUID, cloudMessagingToken: faker.string.alphanumeric(), })); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const confirmations = faker.helpers @@ -653,7 +651,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { subscribers.map((subscriber) => subscriber.subscriber), ) .build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -725,7 +723,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { subscribers.map((subscriber) => subscriber.subscriber), ) .build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -777,7 +775,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { subscribers.map((subscriber) => subscriber.subscriber), ) .build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const message = messageBuilder() @@ -847,7 +845,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { deviceUuid: faker.string.uuid() as UUID, cloudMessagingToken: faker.string.alphanumeric(), })); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const confirmations = faker.helpers @@ -946,7 +944,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { .with('safe', event.address) .build(); }); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -1025,7 +1023,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { .with('safe', event.address) .build(); }); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -1078,7 +1076,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { cloudMessagingToken: faker.string.alphanumeric(), }), ); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const delegates = subscribers.map((subscriber) => { @@ -1158,7 +1156,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { deviceUuid: faker.string.uuid() as UUID, cloudMessagingToken: faker.string.alphanumeric(), })); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const delegates = subscribers.map((subscriber) => { @@ -1264,7 +1262,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { .with('safe', event.address) .build(); }); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -1337,7 +1335,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { cloudMessagingToken: faker.string.alphanumeric(), }), ); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const delegates = subscribers.map((subscriber) => { @@ -1396,7 +1394,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { cloudMessagingToken: faker.string.alphanumeric(), }), ); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const delegates = subscribers.map((subscriber) => { @@ -1477,7 +1475,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { deviceUuid: faker.string.uuid() as UUID, cloudMessagingToken: faker.string.alphanumeric(), })); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); const delegates = subscribers.map((subscriber) => { @@ -1582,7 +1580,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { cloudMessagingToken: faker.string.alphanumeric(), }), ); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -1649,7 +1647,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { cloudMessagingToken: faker.string.alphanumeric(), }), ); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue( + notificationsRepository.getSubscribersBySafe.mockResolvedValue( subscribers, ); @@ -1749,7 +1747,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { .build(), ]) .build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue([ + notificationsRepository.getSubscribersBySafe.mockResolvedValue([ ...ownerSubscriptions, ...delegateSubscriptions, ...nonOwnerDelegateSubscriptions, @@ -1890,7 +1888,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { .build(), ]) .build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue([ + notificationsRepository.getSubscribersBySafe.mockResolvedValue([ ...ownerSubscriptions, ...delegateSubscriptions, ...nonOwnerDelegateSubscriptions, @@ -1977,6 +1975,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { }); it('should cleanup unregistered tokens', async () => { + notificationsRepository.deleteDevice = jest.fn(); // Events that are notified "as is" for simplicity const event = faker.helpers.arrayElement([ deletedMultisigTransactionEventBuilder().build(), @@ -1994,7 +1993,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { }), ); const chain = chainBuilder().build(); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue(subscribers); + notificationsRepository.getSubscribersBySafe.mockResolvedValue(subscribers); networkService.get.mockImplementation(({ url }) => { if (url === `${safeConfigUrl}/api/v1/chains/${event.chainId}`) { return Promise.resolve({ @@ -2023,8 +2022,8 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { .send(event) .expect(202); - expect(notificationsDatasource.deleteDevice).toHaveBeenCalledTimes(1); - expect(notificationsDatasource.deleteDevice).toHaveBeenNthCalledWith( + expect(notificationsRepository.deleteDevice).toHaveBeenCalledTimes(1); + expect(notificationsRepository.deleteDevice).toHaveBeenNthCalledWith( 1, subscribers[0].deviceUuid, ); @@ -2086,7 +2085,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => { cloudMessagingToken: faker.string.alphanumeric(), }), ); - notificationsDatasource.getSubscribersBySafe.mockResolvedValue(subscribers); + notificationsRepository.getSubscribersBySafe.mockResolvedValue(subscribers); networkService.get.mockImplementation(({ url }) => { if (url === `${safeConfigUrl}/api/v1/chains/${chain.chainId}`) { return Promise.resolve({