Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating from Autopilot to Ortto #69

Merged
merged 20 commits into from
Feb 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
RamRamez marked this conversation as resolved.
Show resolved Hide resolved
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',
RamRamez marked this conversation as resolved.
Show resolved Hide resolved
headers: {
'X-Api-Key': process.env.ORTTO_API_KEY as string,
'Content-Type': 'application/json'
},
data: activityCreator(segmentData, EmailNotificationId.donationReceived)
};
await axios.request(config);
RamRamez marked this conversation as resolved.
Show resolved Hide resolved
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
Loading