Skip to content

Commit

Permalink
refactor notification hooks to use notification v2
Browse files Browse the repository at this point in the history
  • Loading branch information
PooyaRaki committed Oct 27, 2024
1 parent c6ba9bb commit cf83376
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
6 changes: 2 additions & 4 deletions src/domain/hooks/helpers/event-notifications.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down
67 changes: 33 additions & 34 deletions src/routes/hooks/hooks-notifications.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<Server>;
let pushNotificationsApi: jest.MockedObjectDeep<IPushNotificationsApi>;
let notificationsDatasource: jest.MockedObjectDeep<INotificationsDatasource>;
let notificationsRepository: jest.MockedObjectDeep<INotificationsRepositoryV2>;
let networkService: jest.MockedObjectDeep<INetworkService>;
let configurationService: IConfigurationService;
let authToken: string;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -210,7 +208,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
cloudMessagingToken: faker.string.alphanumeric(),
}),
);
notificationsDatasource.getSubscribersBySafe.mockResolvedValue(
notificationsRepository.getSubscribersBySafe.mockResolvedValue(
subscribers,
);

Expand Down Expand Up @@ -290,7 +288,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
cloudMessagingToken: faker.string.alphanumeric(),
}),
);
notificationsDatasource.getSubscribersBySafe.mockResolvedValue(
notificationsRepository.getSubscribersBySafe.mockResolvedValue(
subscribers,
);

Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -946,7 +944,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
.with('safe', event.address)
.build();
});
notificationsDatasource.getSubscribersBySafe.mockResolvedValue(
notificationsRepository.getSubscribersBySafe.mockResolvedValue(
subscribers,
);

Expand Down Expand Up @@ -1025,7 +1023,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
.with('safe', event.address)
.build();
});
notificationsDatasource.getSubscribersBySafe.mockResolvedValue(
notificationsRepository.getSubscribersBySafe.mockResolvedValue(
subscribers,
);

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -1264,7 +1262,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
.with('safe', event.address)
.build();
});
notificationsDatasource.getSubscribersBySafe.mockResolvedValue(
notificationsRepository.getSubscribersBySafe.mockResolvedValue(
subscribers,
);

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -1582,7 +1580,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
cloudMessagingToken: faker.string.alphanumeric(),
}),
);
notificationsDatasource.getSubscribersBySafe.mockResolvedValue(
notificationsRepository.getSubscribersBySafe.mockResolvedValue(
subscribers,
);

Expand Down Expand Up @@ -1649,7 +1647,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
cloudMessagingToken: faker.string.alphanumeric(),
}),
);
notificationsDatasource.getSubscribersBySafe.mockResolvedValue(
notificationsRepository.getSubscribersBySafe.mockResolvedValue(
subscribers,
);

Expand Down Expand Up @@ -1749,7 +1747,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
.build(),
])
.build();
notificationsDatasource.getSubscribersBySafe.mockResolvedValue([
notificationsRepository.getSubscribersBySafe.mockResolvedValue([
...ownerSubscriptions,
...delegateSubscriptions,
...nonOwnerDelegateSubscriptions,
Expand Down Expand Up @@ -1890,7 +1888,7 @@ describe.skip('Post Hook Events for Notifications (Unit)', () => {
.build(),
])
.build();
notificationsDatasource.getSubscribersBySafe.mockResolvedValue([
notificationsRepository.getSubscribersBySafe.mockResolvedValue([
...ownerSubscriptions,
...delegateSubscriptions,
...nonOwnerDelegateSubscriptions,
Expand Down Expand Up @@ -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(),
Expand All @@ -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({
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit cf83376

Please sign in to comment.