diff --git a/migrations/1719224595366-seedNotificationTypeForSendEmailConfirmation.ts b/migrations/1719224595366-seedNotificationTypeForSendEmailConfirmation.ts new file mode 100644 index 0000000..6d956f2 --- /dev/null +++ b/migrations/1719224595366-seedNotificationTypeForSendEmailConfirmation.ts @@ -0,0 +1,27 @@ +import { MigrationInterface, QueryRunner } from "typeorm" +import { NOTIFICATION_CATEGORY } from '../src/types/general'; +import { MICRO_SERVICES } from '../src/utils/utils'; +import { NotificationType, SCHEMA_VALIDATORS_NAMES } from '../src/entities/notificationType'; +import { NOTIFICATIONS_EVENT_NAMES } from '../src/types/notifications'; + +const EmailConfirmationNotificationType = [ + { + name: NOTIFICATIONS_EVENT_NAMES.SEND_EMAIL_CONFIRMATION, + description: NOTIFICATIONS_EVENT_NAMES.SEND_EMAIL_CONFIRMATION, + microService: MICRO_SERVICES.givethio, + category: NOTIFICATION_CATEGORY.ORTTO, + schemaValidator: SCHEMA_VALIDATORS_NAMES.SEND_EMAIL_CONFIRMATION, + } +] + +export class seedNotificationTypeForSendEmailConfirmation1719224595366 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.manager.save(NotificationType, EmailConfirmationNotificationType); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `DELETE FROM notification_type WHERE "name" = 'Send email confirmation';`, + ); + } +} diff --git a/src/entities/notificationType.ts b/src/entities/notificationType.ts index 888600d..0dc532b 100644 --- a/src/entities/notificationType.ts +++ b/src/entities/notificationType.ts @@ -4,18 +4,15 @@ import { CreateDateColumn, Entity, Index, - JoinTable, - ManyToOne, OneToMany, PrimaryGeneratedColumn, - RelationId, UpdateDateColumn, } from 'typeorm'; -import { NOTIFICATION_CATEGORY } from '../types/general'; import { NotificationSetting } from './notificationSetting'; // Export Object with Schemas to N1 lookup export const SCHEMA_VALIDATORS_NAMES = { + SEND_EMAIL_CONFIRMATION: 'sendEmailConfirmation', CREATE_ORTTO_PROFILE: 'createOrttoProfile', SUPERFLUID: 'userSuperTokensCritical', ADMIN_MESSAGE: 'adminMessage', diff --git a/src/services/notificationService.ts b/src/services/notificationService.ts index 3b159aa..13d0458 100644 --- a/src/services/notificationService.ts +++ b/src/services/notificationService.ts @@ -14,14 +14,14 @@ import { getEmailAdapter } from '../adapters/adapterFactory'; import { NOTIFICATION_CATEGORY } from '../types/general'; const activityCreator = (payload: any, orttoEventName: NOTIFICATIONS_EVENT_NAMES) : any=> { - const fields = { - "str::email": payload.email, - } - if (process.env.ENVIRONMENT === 'production') { - fields['str:cm:user-id'] = payload.userId?.toString() - } let attributes; switch (orttoEventName) { + case NOTIFICATIONS_EVENT_NAMES.SEND_EMAIL_CONFIRMATION: + attributes = { + "str:cm:email": payload.email, + "str:cm:verificationlink": payload.verificationLink, + } + break; case NOTIFICATIONS_EVENT_NAMES.CREATE_ORTTO_PROFILE: attributes = { "str:cm:email": payload.email, @@ -171,8 +171,12 @@ const activityCreator = (payload: any, orttoEventName: NOTIFICATIONS_EVENT_NAMES logger.debug('activityCreator() invalid ORTTO_EVENT_NAMES', orttoEventName) return; } + const fields = { + "str::email": payload.email, + } const merge_by = []; - if (process.env.ENVIRONMENT === 'production') { + if (process.env.ENVIRONMENT === 'production' && orttoEventName !== NOTIFICATIONS_EVENT_NAMES.SEND_EMAIL_CONFIRMATION) { + fields['str:cm:user-id'] = payload.userId?.toString() merge_by.push("str:cm:user-id") } else { merge_by.push("str::email") @@ -204,9 +208,7 @@ export const sendNotification = async ( message: errorMessages.DUPLICATED_TRACK_ID, }; } - const userAddress = await createNewUserAddressIfNotExists( - userWalletAddress as string, - ); + const notificationType = await getNotificationTypeByEventNameAndMicroservice({ eventName: body.eventName, microService, @@ -221,10 +223,14 @@ export const sendNotification = async ( const isOrttoSpecific = notificationType.category === NOTIFICATION_CATEGORY.ORTTO + const userAddress = isOrttoSpecific ? undefined : await createNewUserAddressIfNotExists( + userWalletAddress as string, + ); + const notificationSetting = isOrttoSpecific ? null : await findNotificationSettingByNotificationTypeAndUserAddress({ notificationTypeId: notificationType.id, - userAddressId: userAddress.id, + userAddressId: userAddress?.id as number }); const shouldSendEmail = diff --git a/src/types/notifications.ts b/src/types/notifications.ts index b7c85bd..63da68e 100644 --- a/src/types/notifications.ts +++ b/src/types/notifications.ts @@ -48,6 +48,7 @@ export enum NOTIFICATIONS_EVENT_NAMES { SUPER_TOKENS_BALANCE_MONTH = 'One month left in stream balance', SUPER_TOKENS_BALANCE_DEPLETED = 'Stream balance depleted', CREATE_ORTTO_PROFILE = 'Create Ortto profile', + SEND_EMAIL_CONFIRMATION = 'Send email confirmation', } export const ORTTO_EVENT_NAMES = { @@ -66,5 +67,6 @@ export const ORTTO_EVENT_NAMES = { [NOTIFICATIONS_EVENT_NAMES.VERIFICATION_FORM_REJECTED]: 'project-verification', [NOTIFICATIONS_EVENT_NAMES.PROJECT_BADGE_REVOKE_WARNING]: 'first-update-warning', [NOTIFICATIONS_EVENT_NAMES.PROJECT_BADGE_REVOKE_LAST_WARNING]: 'second-update-warning', - [NOTIFICATIONS_EVENT_NAMES.CREATE_ORTTO_PROFILE]: 'created-profile' + [NOTIFICATIONS_EVENT_NAMES.CREATE_ORTTO_PROFILE]: 'created-profile', + [NOTIFICATIONS_EVENT_NAMES.SEND_EMAIL_CONFIRMATION]: 'verification-form-email-verification', } \ No newline at end of file diff --git a/src/utils/validators/segmentAndMetadataValidators.ts b/src/utils/validators/segmentAndMetadataValidators.ts index c932ca1..8f42340 100644 --- a/src/utils/validators/segmentAndMetadataValidators.ts +++ b/src/utils/validators/segmentAndMetadataValidators.ts @@ -166,12 +166,21 @@ const createOrttoProfileSegmentSchema = Joi.object({ userId: Joi.number().required() }) +const sendEmailConfirmationSchema = Joi.object({ + email: Joi.string().required(), + verificationLink: Joi.string().required(), +}); + export const SEGMENT_METADATA_SCHEMA_VALIDATOR: { [key: string]: { segment: ObjectSchema | null; metadata: ObjectSchema | null; }; } = { + sendEmailConfirmation: { + metadata: null, + segment: sendEmailConfirmationSchema, + }, createOrttoProfile: { segment: createOrttoProfileSegmentSchema, metadata: null @@ -248,10 +257,6 @@ export const SEGMENT_METADATA_SCHEMA_VALIDATOR: { metadata: projectTitleProjectLinkSchema, segment: null, }, - sendEmailConfirmation: { - metadata: null, - segment: projectRelatedTrackerSchema, - }, madeDonation: { metadata: projectTitleProjectLinkSchema, segment: donationTrackerSchema, diff --git a/src/validators/schemaValidators.ts b/src/validators/schemaValidators.ts index 7540433..afa687e 100644 --- a/src/validators/schemaValidators.ts +++ b/src/validators/schemaValidators.ts @@ -60,6 +60,9 @@ export const sendNotificationValidator = Joi.object({ userId: Joi.number(), projectLink: Joi.string().allow(null).allow(''), + // Email confirmation + verificationLink: Joi.string().allow(null).allow(''), + // Donation related attributes amount: Joi.number(), token: Joi.string().allow(null, ''),