From 03bb4036a0227eea622c3e098fa2759219096a26 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Tue, 9 Jul 2024 03:43:31 +0330 Subject: [PATCH 1/6] Add a unit test for activity creator for notify reward amount notification type --- src/services/notificationService.test.ts | 43 ++++++++++++++++++++++++ src/services/notificationService.ts | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/services/notificationService.test.ts diff --git a/src/services/notificationService.test.ts b/src/services/notificationService.test.ts new file mode 100644 index 0000000..9f5a5ce --- /dev/null +++ b/src/services/notificationService.test.ts @@ -0,0 +1,43 @@ +import { activityCreator } from './notificationService'; +import { NOTIFICATIONS_EVENT_NAMES } from '../types/notifications'; +import { expect } from 'chai'; + +describe('activityCreator', () => { + it('should create attributes for NOTIFY_REWARD_AMOUNT', () => { + const payload = { + round: 1, + date: '2024-06-01', + amount: '1000', + contractAddress: '0x123', + farm: 'Test Farm', + message: 'Test Message', + network: 'Test Network', + script: 'Test Script', + transactionHash: '0xabc', + email: 'test@example.com' + }; + const result = activityCreator(payload, NOTIFICATIONS_EVENT_NAMES.NOTIFY_REWARD_AMOUNT); + expect(JSON.stringify(result)).equal(JSON.stringify({ + activities: [ + { + activity_id: "act:cm:notify-reward-amount", + attributes: { + 'int:cm:round': payload.round, + 'str:cm:date': payload.date, + 'str:cm:amount': payload.amount, + 'str:cm:contractaddress': payload.contractAddress, + 'str:cm:farm': payload.farm, + 'str:cm:message': payload.message, + 'str:cm:network': payload.network, + 'str:cm:script': payload.script, + 'str:cm:transactionhash': payload.transactionHash, + }, + fields: { + 'str::email': payload.email, + }, + }, + ], + merge_by: ['str::email'], + })); + }) +}); diff --git a/src/services/notificationService.ts b/src/services/notificationService.ts index 2ae1d0a..09d0bf1 100644 --- a/src/services/notificationService.ts +++ b/src/services/notificationService.ts @@ -13,7 +13,7 @@ import { NOTIFICATIONS_EVENT_NAMES, ORTTO_EVENT_NAMES } from '../types/notificat import { getEmailAdapter } from '../adapters/adapterFactory'; import { NOTIFICATION_CATEGORY } from '../types/general'; -const activityCreator = (payload: any, orttoEventName: NOTIFICATIONS_EVENT_NAMES) : any=> { +export const activityCreator = (payload: any, orttoEventName: NOTIFICATIONS_EVENT_NAMES) : any=> { let attributes; switch (orttoEventName) { case NOTIFICATIONS_EVENT_NAMES.SUBSCRIBE_ONBOARDING: From 8cdaf7f2e9ce96e5a41c0f0cfe4f389956214a62 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Sat, 13 Jul 2024 01:54:30 +0330 Subject: [PATCH 2/6] Add a migration for change microService name of notify reward amount notification type --- ...roserviceOfNotifyRewardNotificationType.ts | 20 +++++++++++++++++++ src/utils/utils.ts | 1 + 2 files changed, 21 insertions(+) create mode 100644 migrations/1720553769343-changeMicroserviceOfNotifyRewardNotificationType.ts diff --git a/migrations/1720553769343-changeMicroserviceOfNotifyRewardNotificationType.ts b/migrations/1720553769343-changeMicroserviceOfNotifyRewardNotificationType.ts new file mode 100644 index 0000000..39a99a2 --- /dev/null +++ b/migrations/1720553769343-changeMicroserviceOfNotifyRewardNotificationType.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; +import { MICRO_SERVICES } from '../src/utils/utils'; + +export class changeMicroserviceOfNotifyRewardNotificationType1720553769343 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + UPDATE notification_type + SET "microService" = '${MICRO_SERVICES.notifyReward}' + WHERE name = 'Notify reward amount'; + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + UPDATE notification_type + SET "microService" = '${MICRO_SERVICES.givethio}' + WHERE name = 'Notify reward amount'; + `); + } +} diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 674ee1a..1d8c328 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -19,6 +19,7 @@ export const MICRO_SERVICES = { givethio: 'givethio', givEconomyNotificationMicroService: 'giveconomy-notification-service', trace: 'trace', + notifyReward: 'notifyreward', }; // Need to define trace, blockchain and miscellaneos events From 78f0d14b2ecc9bcc2bb3c583d514d1732b9ed517 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Sat, 13 Jul 2024 01:55:23 +0330 Subject: [PATCH 3/6] Add missing fields to schema validator --- src/validators/schemaValidators.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/validators/schemaValidators.ts b/src/validators/schemaValidators.ts index 179f86b..34b07a4 100644 --- a/src/validators/schemaValidators.ts +++ b/src/validators/schemaValidators.ts @@ -94,8 +94,14 @@ export const sendNotificationValidator = Joi.object({ update: Joi.string(), // Notify reward attributes + round: Joi.number(), + date: Joi.string(), contractAddress: Joi.string(), farm: Joi.string(), + message: Joi.string(), + network: Joi.string(), + script: Joi.string(), + transactionHash: Joi.string(), }), }), }); From c918439d383b5245a6bd3e41feac9a1e9b81ad8a Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Sat, 13 Jul 2024 02:43:54 +0330 Subject: [PATCH 4/6] change notification category to orrto --- ...88344202-seedNotificationTypeForNotifyRewardAmount.ts | 2 +- ...oserviceAndCategoryOfNotifyRewardNotificationType.ts} | 9 ++++++--- src/types/general.ts | 1 - 3 files changed, 7 insertions(+), 5 deletions(-) rename migrations/{1720553769343-changeMicroserviceOfNotifyRewardNotificationType.ts => 1720553769343-changeMicroserviceAndCategoryOfNotifyRewardNotificationType.ts} (61%) diff --git a/migrations/1718888344202-seedNotificationTypeForNotifyRewardAmount.ts b/migrations/1718888344202-seedNotificationTypeForNotifyRewardAmount.ts index 0cb4861..5e4a341 100644 --- a/migrations/1718888344202-seedNotificationTypeForNotifyRewardAmount.ts +++ b/migrations/1718888344202-seedNotificationTypeForNotifyRewardAmount.ts @@ -8,7 +8,7 @@ const NotifyRewardAmountNotificationType = [ name: NOTIFICATION_TYPE_NAMES.NOTIFY_REWARD_AMOUNT, description: NOTIFICATION_TYPE_NAMES.NOTIFY_REWARD_AMOUNT, microService: MICRO_SERVICES.givethio, - category: NOTIFICATION_CATEGORY.NOTIFY_REWARD_AMOUNT, + category: NOTIFICATION_CATEGORY.GENERAL, schemaValidator: SCHEMA_VALIDATORS_NAMES.NOTIFY_REWARD_AMOUNT, title: "Notify reward report", } diff --git a/migrations/1720553769343-changeMicroserviceOfNotifyRewardNotificationType.ts b/migrations/1720553769343-changeMicroserviceAndCategoryOfNotifyRewardNotificationType.ts similarity index 61% rename from migrations/1720553769343-changeMicroserviceOfNotifyRewardNotificationType.ts rename to migrations/1720553769343-changeMicroserviceAndCategoryOfNotifyRewardNotificationType.ts index 39a99a2..d83a425 100644 --- a/migrations/1720553769343-changeMicroserviceOfNotifyRewardNotificationType.ts +++ b/migrations/1720553769343-changeMicroserviceAndCategoryOfNotifyRewardNotificationType.ts @@ -1,11 +1,13 @@ import { MigrationInterface, QueryRunner } from "typeorm"; import { MICRO_SERVICES } from '../src/utils/utils'; +import { NOTIFICATION_CATEGORY } from '../src/types/general'; -export class changeMicroserviceOfNotifyRewardNotificationType1720553769343 implements MigrationInterface { +export class changeMicroserviceAndCategoryOfNotifyRewardNotificationType1720553769343 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { await queryRunner.query(` UPDATE notification_type - SET "microService" = '${MICRO_SERVICES.notifyReward}' + SET "microService" = '${MICRO_SERVICES.notifyReward}', + category = '${NOTIFICATION_CATEGORY.ORTTO}' WHERE name = 'Notify reward amount'; `); } @@ -13,7 +15,8 @@ export class changeMicroserviceOfNotifyRewardNotificationType1720553769343 imple public async down(queryRunner: QueryRunner): Promise { await queryRunner.query(` UPDATE notification_type - SET "microService" = '${MICRO_SERVICES.givethio}' + SET "microService" = '${MICRO_SERVICES.givethio}', + categoty = '${NOTIFICATION_CATEGORY.GENERAL}' WHERE name = 'Notify reward amount'; `); } diff --git a/src/types/general.ts b/src/types/general.ts index a267f31..c0e3059 100644 --- a/src/types/general.ts +++ b/src/types/general.ts @@ -12,7 +12,6 @@ export enum NOTIFICATION_CATEGORY { SUPPORTED_PROJECTS = 'supportedProjects', GIV_POWER = 'givPower', ORTTO = 'ortto', - NOTIFY_REWARD_AMOUNT = 'notifyRewardAmount', } export enum NOTIFICATION_TYPE_NAMES { From f3ff7acd0039c911f930774fcd2ef737e852522b Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Sat, 13 Jul 2024 03:28:49 +0330 Subject: [PATCH 5/6] Add migration for adding new third party for notify reward --- ...828190666-seedThirdPartyForNotifyReward.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 migrations/1720828190666-seedThirdPartyForNotifyReward.ts diff --git a/migrations/1720828190666-seedThirdPartyForNotifyReward.ts b/migrations/1720828190666-seedThirdPartyForNotifyReward.ts new file mode 100644 index 0000000..14b8d51 --- /dev/null +++ b/migrations/1720828190666-seedThirdPartyForNotifyReward.ts @@ -0,0 +1,27 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class seedThirdPartyForNotifyReward1720828190666 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + if ( + process.env.NODE_ENV === 'test' || + process.env.NODE_ENV === 'development' + ) { + // Create third part record for notifyreward in development and test ENVs + await queryRunner.query(` + INSERT INTO third_party( + "microService", secret, "isActive") + VALUES + ('notifyreward', 'secret', true) + ; + `); + } + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + DELETE FROM third_party + WHERE "microService" = 'notifyreward'; + `); + } + +} From c3f34ed1004c7f67fd4e5c33e18d425ff9894c8d Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Sat, 13 Jul 2024 03:30:01 +0330 Subject: [PATCH 6/6] Add test cases for notify reward amount notification type to the notificationRouter.test.ts --- config/test.env | 3 + src/routes/v1/notificationRouter.test.ts | 72 ++++++++++++++++++++++++ test/testUtils.ts | 7 +++ 3 files changed, 82 insertions(+) diff --git a/config/test.env b/config/test.env index a699c35..ac7cf6d 100644 --- a/config/test.env +++ b/config/test.env @@ -23,6 +23,9 @@ GIVETH_IO_THIRD_PARTY_MICRO_SERVICE=givethio GIV_ECONOMY_THIRD_PARTY_SECRET=secret GIV_ECONOMY_THIRD_PARTY_MICRO_SERVICE=giveconomy-notification-service +NOTIFY_REWARD_THIRD_PARTY_SECRET=secret +NOTIFY_REWARD_THIRD_PARTY_MICRO_SERVICE=notifyreward + # OPTIONAL - force logging to stdout when the value is true LOG_STDOUT=false diff --git a/src/routes/v1/notificationRouter.test.ts b/src/routes/v1/notificationRouter.test.ts index 387c745..6e15a0a 100644 --- a/src/routes/v1/notificationRouter.test.ts +++ b/src/routes/v1/notificationRouter.test.ts @@ -5,6 +5,7 @@ import { getAccessTokenForMockAuthMicroService, getGivEconomyBasicAuth, getGivethIoBasicAuth, + getNotifyRewardBasicAuth, serverUrl, sleep, } from '../../../test/testUtils'; @@ -2092,6 +2093,77 @@ function sendNotificationTestCases() { const createdNotification = await findNotificationByTrackId(trackId); assert.equal(createdNotification?.createdAt.getTime(), creationTime); }); + + it('should create *Notify reward amount* notification, success', async () => { + const data = { + eventName: "Notify reward amount", + sendEmail: true, + sendSegment: true, + creationTime: 1667992708000, + email: "aliebrahimi2079@gmail.com", + segment: { + payload: { + round: 10, + date: "1667992708000", + amount: "12134", + contractAddress: "0xsfglsjfdflk", + farm: "test farm", + message: "test message", + network: "ethereum", + script: "test script", + transactionHash: "test txhash" + } + } + }; + + const result = await axios.post(sendNotificationUrl, data, { + headers: { + authorization: getNotifyRewardBasicAuth(), + }, + }); + + assert.equal(result.status, 200); + assert.isOk(result.data); + assert.isTrue(result.data.success); + }); + it('should create *Notify reward amount* notification, failed invalid payload', async () => { + try { + const data = { + eventName: "Notify reward amount", + sendEmail: true, + sendSegment: true, + creationTime: 1667992708000, + email: "aliebrahimi2079@gmail.com", + segment: { + payload: { + round: 10, + date: "1667992708000", + amount: "12134", + contractAddress: "0xsfglsjfdflk", + farm: "test farm", + message: "test message", + network: "ethereum", + script: "test script", + transactionHash: "test txhash", + invalidField: "invalid data" + } + } + }; + await axios.post(sendNotificationUrl, data, { + headers: { + authorization: getNotifyRewardBasicAuth(), + }, + }); + // If request doesn't fail, it means this test failed + assert.isTrue(false); + } catch (e: any) { + assert.equal( + e.response.data.message, + errorMessagesEnum.IMPACT_GRAPH_VALIDATION_ERROR.message, + ); + assert.equal(e.response.data.description, '"segment.payload.invalidField" is not allowed'); + } + }); } function sendBulkNotificationsTestCases() { diff --git a/test/testUtils.ts b/test/testUtils.ts index 9890c5d..96b744b 100644 --- a/test/testUtils.ts +++ b/test/testUtils.ts @@ -112,6 +112,13 @@ export const getGivEconomyBasicAuth = () => { }); }; +export const getNotifyRewardBasicAuth = () => { + return createBasicAuthentication({ + secret: process.env.NOTIFY_REWARD_THIRD_PARTY_SECRET as string, + username: process.env.NOTIFY_REWARD_THIRD_PARTY_MICRO_SERVICE as string, + }); +}; + export const getAccessTokenForMockAuthMicroService = ( walletAddress: string, ) => {