Skip to content

Commit

Permalink
replace Segment with Ortto API
Browse files Browse the repository at this point in the history
  • Loading branch information
RamRamez committed Jan 30, 2024
1 parent 5578c54 commit 0adf73b
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions src/services/notificationService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from 'axios';
import {
createNotification,
findNotificationByTrackId,
Expand All @@ -10,10 +11,37 @@ import { logger } from '../utils/logger';
import { EMAIL_STATUSES, Notification } from '../entities/notification';
import { SEGMENT_METADATA_SCHEMA_VALIDATOR } from '../utils/validators/segmentAndMetadataValidators';
import { validateWithJoiSchema } from '../validators/schemaValidators';
import { SegmentAnalyticsSingleton } from './segment/segmentAnalyticsSingleton';
import { SendNotificationRequest } from '../types/requestResponses';
import { StandardError } from '../types/StandardError';

enum EmailNotificationId {
donationReceived = 'donationReceived',
}

const activityCreator = (payload: any, emailNotificationId: EmailNotificationId) => {
if (emailNotificationId === EmailNotificationId.donationReceived) {
return {
"activities": [
{
"activity_id": `act:cm:${emailNotificationId}`,
"attributes": {
"str:cm:projecttitle": payload.title,
"int:cm:donationamount": payload.amount,
"str:cm:donationtoken": payload.token,
"str:cm:email": payload.email,
"str:cm:projectlink": payload.slug,
"bol:cm:verified": payload.verified,
"str:cm:transactionlink": payload.transactionId
},
"fields": {
"str::email": payload.email
}
}
]
};
}
}

export const sendNotification = async (
body: SendNotificationRequest,
microService: string,
Expand Down Expand Up @@ -80,12 +108,17 @@ export const sendNotification = async (
// And it's not good, we should find another solution to separate sending segment and email
const segmentData = body.segment?.payload;
validateWithJoiSchema(segmentData, segmentValidator);
await SegmentAnalyticsSingleton.getInstance().track({
eventName: notificationType.emailNotificationId as string,
anonymousId: body?.segment?.anonymousId,
properties: segmentData,
analyticsUserId: body?.segment?.analyticsUserId,
});
const config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://api-us.ortto.app/v1/activities/create',
headers: {
'X-Api-Key': process.env.ORTTO_API_KEY as string,
'Content-Type': 'application/json'
},
data: activityCreator(segmentData, EmailNotificationId.donationReceived)
};
await axios.request(config);
emailStatus = EMAIL_STATUSES.SENT;
}

Expand All @@ -99,7 +132,7 @@ export const sendNotification = async (
}

if (!notificationSetting?.allowDappPushNotification) {
//TODO In future we can add a create notification but with disabledNotification:true
//TODO In future we can add a create notification but with disabledNotification:true
// So we can exclude them in list of notifications
return {
success: true,
Expand Down

0 comments on commit 0adf73b

Please sign in to comment.