From a700f0dc3594c461372f56bf01eddf6fccd9d446 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Thu, 22 Aug 2024 03:03:06 +0330 Subject: [PATCH 1/3] Add email verification for Qacc --- src/entities/notificationType.ts | 1 + src/services/notificationService.ts | 6 ++++++ src/types/general.ts | 1 + src/types/notifications.ts | 5 +++++ src/utils/validators/segmentAndMetadataValidators.ts | 9 +++++++++ src/validators/schemaValidators.ts | 3 +++ 6 files changed, 25 insertions(+) diff --git a/src/entities/notificationType.ts b/src/entities/notificationType.ts index a26fad5..8004a9e 100644 --- a/src/entities/notificationType.ts +++ b/src/entities/notificationType.ts @@ -68,6 +68,7 @@ export const SCHEMA_VALIDATORS_NAMES = { YOUR_PROJECT_GOT_A_RANK: 'yourProjectGotARank', NOTIFY_REWARD_AMOUNT: 'notifyRewardAmount', + SEND_EMAIL_VERIFICATION_CODE_FOR_QACC: 'sendEmailVerificationCodeForQacc', }; export type HtmlTemplate = { type: string; content: string; href?: string }[]; diff --git a/src/services/notificationService.ts b/src/services/notificationService.ts index 2640ed9..cca9eb8 100644 --- a/src/services/notificationService.ts +++ b/src/services/notificationService.ts @@ -200,6 +200,12 @@ export const activityCreator = ( 'str:cm:transactionhash': payload.transactionHash, }; break; + case NOTIFICATIONS_EVENT_NAMES.SEND_EMAIL_VERIFICATION_CODE_FOR_QACC: + attributes = { + 'int:cm:verificationcode': payload.verificationCode, + 'str:cm:email': payload.email, + }; + break; default: logger.debug('activityCreator() invalid event name', orttoEventName); return; diff --git a/src/types/general.ts b/src/types/general.ts index c0e3059..f7611b4 100644 --- a/src/types/general.ts +++ b/src/types/general.ts @@ -55,4 +55,5 @@ export enum NOTIFICATION_TYPE_NAMES { CREATE_ORTTO_PROFILE = 'Create Ortto profile', NOTIFY_REWARD_AMOUNT = 'Notify reward amount', + SEND_EMAIL_VERIFICATION_CODE_FOR_QACC = 'Send email verification code for Qacc', } diff --git a/src/types/notifications.ts b/src/types/notifications.ts index 68d1d35..a35a801 100644 --- a/src/types/notifications.ts +++ b/src/types/notifications.ts @@ -52,6 +52,9 @@ export enum NOTIFICATIONS_EVENT_NAMES { SEND_EMAIL_CONFIRMATION = 'Send email confirmation', SUBSCRIBE_ONBOARDING = 'Subscribe onboarding', NOTIFY_REWARD_AMOUNT = 'Notify reward amount', + + // Qacc Notification + SEND_EMAIL_VERIFICATION_CODE_FOR_QACC = 'Send email verification code for Qacc', } export const ORTTO_EVENT_NAMES = { @@ -81,4 +84,6 @@ export const ORTTO_EVENT_NAMES = { 'verification-form-email-verification', [NOTIFICATIONS_EVENT_NAMES.NOTIFY_REWARD_AMOUNT]: 'notify-reward', [NOTIFICATIONS_EVENT_NAMES.SUBSCRIBE_ONBOARDING]: 'onboarding-form', + [NOTIFICATIONS_EVENT_NAMES.SEND_EMAIL_VERIFICATION_CODE_FOR_QACC]: + 'qacc-email-verification', }; diff --git a/src/utils/validators/segmentAndMetadataValidators.ts b/src/utils/validators/segmentAndMetadataValidators.ts index 915d5bf..adcc634 100644 --- a/src/utils/validators/segmentAndMetadataValidators.ts +++ b/src/utils/validators/segmentAndMetadataValidators.ts @@ -175,6 +175,11 @@ const notifyRewardAmountSegmentSchema = Joi.object({ email: Joi.string().required(), }); +const sendEmailVerificationCodeForQaccSegmentSchema = Joi.object({ + verificationCode: Joi.number().required(), + email: Joi.string().required(), +}); + export const SEGMENT_METADATA_SCHEMA_VALIDATOR: { [key: string]: { segment: ObjectSchema | null; @@ -352,4 +357,8 @@ export const SEGMENT_METADATA_SCHEMA_VALIDATOR: { metadata: null, segment: notifyRewardAmountSegmentSchema, }, + sendEmailVerificationCodeForQacc: { + metadata: null, + segment: sendEmailVerificationCodeForQaccSegmentSchema, + }, }; diff --git a/src/validators/schemaValidators.ts b/src/validators/schemaValidators.ts index ccd99ff..306b7df 100644 --- a/src/validators/schemaValidators.ts +++ b/src/validators/schemaValidators.ts @@ -101,6 +101,9 @@ export const sendNotificationValidator = Joi.object({ network: Joi.string(), script: Joi.string(), transactionHash: Joi.string(), + + // Qacc email verification + verificationCode: Joi.number(), }), }), }); From 7f81dfdbd958df564999f0a6b99c51d4a3afa921 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Thu, 22 Aug 2024 03:03:40 +0330 Subject: [PATCH 2/3] Add migration for seed notification type for email verification for Qacc --- ...ificationTypeForEmailVerificationOfQacc.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 migrations/1724281179980-seedNotificationTypeForEmailVerificationOfQacc.ts diff --git a/migrations/1724281179980-seedNotificationTypeForEmailVerificationOfQacc.ts b/migrations/1724281179980-seedNotificationTypeForEmailVerificationOfQacc.ts new file mode 100644 index 0000000..0c51144 --- /dev/null +++ b/migrations/1724281179980-seedNotificationTypeForEmailVerificationOfQacc.ts @@ -0,0 +1,39 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; +import { + NOTIFICATION_CATEGORY, + NOTIFICATION_TYPE_NAMES, +} from '../src/types/general'; +import { MICRO_SERVICES } from '../src/utils/utils'; +import { + NotificationType, + SCHEMA_VALIDATORS_NAMES, +} from '../src/entities/notificationType'; + +const QaccEmailVerificationNotificationType = [ + { + name: NOTIFICATION_TYPE_NAMES.SEND_EMAIL_VERIFICATION_CODE_FOR_QACC, + description: 'sending email verification code for the Qacc project.', + microService: MICRO_SERVICES.qacc, + category: NOTIFICATION_CATEGORY.ORTTO, + schemaValidator: + SCHEMA_VALIDATORS_NAMES.SEND_EMAIL_VERIFICATION_CODE_FOR_QACC, + title: 'Qacc email verification', + }, +]; + +export class SeedNotificationTypeForEmailVerificationOfQacc1724281179980 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.manager.save( + NotificationType, + QaccEmailVerificationNotificationType, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `DELETE FROM notification_type WHERE "name" = ${NOTIFICATION_TYPE_NAMES.SEND_EMAIL_VERIFICATION_CODE_FOR_QACC};`, + ); + } +} From b0ffe2cdea0a7ff08e11ef34971d580092dabce7 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Thu, 22 Aug 2024 03:04:10 +0330 Subject: [PATCH 3/3] Add unit tests for email verification for Qacc --- config/test.env | 3 ++ src/routes/v1/notificationRouter.test.ts | 53 ++++++++++++++++++++++++ src/services/notificationService.test.ts | 29 +++++++++++++ test/testUtils.ts | 7 ++++ 4 files changed, 92 insertions(+) diff --git a/config/test.env b/config/test.env index ac7cf6d..6804ddf 100644 --- a/config/test.env +++ b/config/test.env @@ -26,6 +26,9 @@ GIV_ECONOMY_THIRD_PARTY_MICRO_SERVICE=giveconomy-notification-service NOTIFY_REWARD_THIRD_PARTY_SECRET=secret NOTIFY_REWARD_THIRD_PARTY_MICRO_SERVICE=notifyreward +QACC_THIRD_PARTY_SECRET=secret +QACC_THIRD_PARTY_MICRO_SERVICE=qacc + # 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 6f6fa7a..7f50503 100644 --- a/src/routes/v1/notificationRouter.test.ts +++ b/src/routes/v1/notificationRouter.test.ts @@ -8,6 +8,7 @@ import { getGivEconomyBasicAuth, getGivethIoBasicAuth, getNotifyRewardBasicAuth, + getQaccBasicAuth, serverUrl, sleep, } from '../../../test/testUtils'; @@ -2167,6 +2168,58 @@ function sendNotificationTestCases() { ); } }); + + it('should create *Send email verification code for Qacc* notification, success', async () => { + const data = { + eventName: 'Send email verification code for Qacc', + segment: { + payload: { + verificationCode: 654321, + email: 'aliebrahimi2079@gmail.com', + }, + }, + }; + + const result = await axios.post(sendNotificationUrl, data, { + headers: { + authorization: getQaccBasicAuth(), + }, + }); + + assert.equal(result.status, 200); + assert.isOk(result.data); + assert.isTrue(result.data.success); + }); + it('should create *Send email verification code for Qacc* notification, failed invalid payload', async () => { + try { + const data = { + eventName: 'Send email verification code for Qacc', + segment: { + payload: { + verificationCode: 654321, + email: 'aliebrahimi2079@gmail.com', + invalidField: 'invalid data', + }, + }, + }; + await axios.post(sendNotificationUrl, data, { + headers: { + authorization: getQaccBasicAuth(), + }, + }); + // 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/src/services/notificationService.test.ts b/src/services/notificationService.test.ts index a054d6c..f34ee5c 100644 --- a/src/services/notificationService.test.ts +++ b/src/services/notificationService.test.ts @@ -52,4 +52,33 @@ describe('activityCreator', () => { }), ); }); + + it('should create attributes for SEND_EMAIL_VERIFICATION_CODE_FOR_QACC', () => { + const payload = { + verificationCode: 123456, + email: 'ali@generalmagic.io', + }; + const result = activityCreator( + payload, + NOTIFICATIONS_EVENT_NAMES.SEND_EMAIL_VERIFICATION_CODE_FOR_QACC, + MICRO_SERVICES.qacc, + ); + expect(JSON.stringify(result)).equal( + JSON.stringify({ + activities: [ + { + activity_id: 'act:cm:qacc-email-verification', + attributes: { + 'int:cm:verificationcode': 123456, + 'str:cm:email': 'ali@generalmagic.io', + }, + fields: { + 'str::email': payload.email, + }, + }, + ], + merge_by: ['str::email'], + }), + ); + }); }); diff --git a/test/testUtils.ts b/test/testUtils.ts index 29b5fd2..e0843e3 100644 --- a/test/testUtils.ts +++ b/test/testUtils.ts @@ -119,6 +119,13 @@ export const getNotifyRewardBasicAuth = () => { }); }; +export const getQaccBasicAuth = () => { + return createBasicAuthentication({ + secret: process.env.QACC_THIRD_PARTY_SECRET as string, + username: process.env.QACC_THIRD_PARTY_MICRO_SERVICE as string, + }); +}; + export const getAccessTokenForMockAuthMicroService = ( walletAddress: string, ) => {