Skip to content

Commit

Permalink
updates #3 | adds: adds: support for consumer using template, transac…
Browse files Browse the repository at this point in the history
…tion schemas for push
  • Loading branch information
imsheth committed Aug 11, 2020
1 parent e845491 commit 2911a7f
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 4 deletions.
4 changes: 3 additions & 1 deletion consumer/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const createNotifications = async (notificationConfig) => {
Promise.all(fetchToBeSentNotificationsPromises).then(async (toBeSentNotificationsResult) => {
// console.log('createNotifications.toBeSentNotificationsResult ', toBeSentNotificationsResult);
await processNotifications(toBeSentNotificationsResult.flat());
readNotifications(notificationConfig);
// client.close();
}).catch((toBeSentNotificationsError) => {
console.log('createNotifications.toBeSentNotificationsError ', toBeSentNotificationsError);
Expand All @@ -151,12 +152,13 @@ const createNotifications = async (notificationConfig) => {

// Entry point
const startNotifEngine = async () => {
console.log('\nengine booting up at ', new Date().toString());
// Execute after every TIMEOUT ms
setInterval(() => {
console.log('\nengine iteration invoked at ', new Date().toString());
console.time('iteration_time');
createNotifications(NotificationConfig);
readNotifications(NotificationConfig);
// readNotifications(NotificationConfig);
console.log('\nengine iteration returned at ', new Date().toString());
console.timeEnd('iteration_time');
}, TIMEOUT);
Expand Down
147 changes: 144 additions & 3 deletions consumer/classes/notificationPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const NOTIFICATION_STATUSES = NotificationConfig.statuses;
notificationPush = () => {
};

const replacementHandler = (input, transformationMapping) => {
transformationMapping["{{"] = '';
transformationMapping["}}"] = '';
// console.log(transformationMapping);
return input.replace(new RegExp(Object.keys(transformationMapping).join('|'), 'gi'), (matched) => {
return transformationMapping[matched];
});
};

notificationPush.prototype = {
// Send notification by PROCESSING => sendMail => SENDING_SUCCEEDED or SENDING_FAILED
sendNotification: (notification) => {
Expand All @@ -20,7 +29,7 @@ notificationPush.prototype = {

// Verify
if (OS_APP_ID && OS_API_KEY && MONGOURL) {
console.log('notificationMail.sendNotification.onesignal.notification._id ', notification._id);
console.log('notificationPush.sendNotification.onesignal.notification._id ', notification._id);

// Connect
const mongoClient = await MongoClient.connect(MONGOURL, {
Expand All @@ -47,7 +56,7 @@ notificationPush.prototype = {

resolve(createNotificationResponse);
} catch (sendNotificationError) {
console.log('notificationMail.sendNotification.onesignal.sendNotificationError for ', notification._id);
console.log('notificationPush.sendNotification.onesignal.sendNotificationError for ', notification._id);
reject(sendNotificationError);
} finally {
// Disconnect
Expand All @@ -66,7 +75,139 @@ notificationPush.prototype = {
// TODO : Create notifications for sending
createNotification: (notification) => {
return new Promise(async (resolve, reject) => {
resolve({});
const MONGOURL = process.env.MONGOURL || '';

// Verify
if (MONGOURL) {

const generatedPushes = [];

// Process only if template and push_template exist
if (notification['template'] && notification['template']['push_template'] && notification['template']['push_template']['headings'] && notification['template']['push_template']['subtitle'] && notification['template']['push_template']['contents']) {

// Generate mails to be stored
notification.data.forEach((toBeGeneratedNotificationsData) => {
let newGeneratedPush = {};
newGeneratedPush.trigger_event = 'send';
newGeneratedPush.type = notification.type;
newGeneratedPush.provider = notification.provider;
newGeneratedPush.notification_transaction_id = notification._id;
newGeneratedPush.user_id = toBeGeneratedNotificationsData.user_id;
newGeneratedPush.user_email = toBeGeneratedNotificationsData.user_email;

newGeneratedPush.data = {
headings: {},
subtitle: {},
contents: {},
include_player_ids: toBeGeneratedNotificationsData.include_player_ids,
};
Object.keys(notification['template']['push_template']['headings']).forEach((templateHeadingForConfiguredLanguage) => {
newGeneratedPush['data']['headings'][templateHeadingForConfiguredLanguage] = replacementHandler(notification['template']['push_template']['headings'][templateHeadingForConfiguredLanguage], toBeGeneratedNotificationsData);

// Helps identify sending server in the heading
if (notification.server && notification.server !== 'production') {
newGeneratedPush['data']['headings'][templateHeadingForConfiguredLanguage] = newGeneratedPush['data']['headings'][templateHeadingForConfiguredLanguage] + ' ( ' + notification.server + ' ) ';
}
});
Object.keys(notification['template']['push_template']['subtitle']).forEach((templateHeadingForConfiguredLanguage) => {
newGeneratedPush['data']['subtitle'][templateHeadingForConfiguredLanguage] = replacementHandler(notification['template']['push_template']['subtitle'][templateHeadingForConfiguredLanguage], toBeGeneratedNotificationsData);
});
Object.keys(notification['template']['push_template']['contents']).forEach((templateHeadingForConfiguredLanguage) => {
newGeneratedPush['data']['contents'][templateHeadingForConfiguredLanguage] = replacementHandler(notification['template']['push_template']['contents'][templateHeadingForConfiguredLanguage], toBeGeneratedNotificationsData);
});

// Optional attributes
if (notification['template']['push_template']['url']) {
newGeneratedPush['data']['url'] = notification['template']['push_template']['url'];
}
if (notification['template']['push_template']['big_picture']) {
newGeneratedPush['data']['big_picture'] = notification['template']['push_template']['big_picture'];
}
if (notification['template']['push_template']['buttons'] && notification['template']['push_template']['buttons'].length > 0) {
newGeneratedPush['data']['buttons'] = notification['template']['push_template']['buttons'];
}
if (notification['template']['push_template']['android_accent_color']) {
newGeneratedPush['data']['android_accent_color'] = notification['template']['push_template']['android_accent_color'];
}
if (notification['template']['push_template']['huawei_accent_color']) {
newGeneratedPush['data']['huawei_accent_color'] = notification['template']['push_template']['huawei_accent_color'];
}
if (notification['template']['push_template']['android_led_color']) {
newGeneratedPush['data']['android_led_color'] = notification['template']['push_template']['android_led_color'];
}
if (notification['template']['push_template']['huawei_led_color']) {
newGeneratedPush['data']['huawei_led_color'] = notification['template']['push_template']['huawei_led_color'];
}

newGeneratedPush.status = NOTIFICATION_STATUSES['NOT_PROCESSED'];
newGeneratedPush.service_response = null;
newGeneratedPush.try_count = 0;

const currentTimestamp = new Date();
newGeneratedPush.db_created_at = currentTimestamp.getTime();
newGeneratedPush.db_updated_at = currentTimestamp.getTime();

generatedPushes.push(newGeneratedPush);
});

// Connect
const mongoClient = await MongoClient.connect(MONGOURL, {
useUnifiedTopology: true
});

try {
// Insert pushes
const pushesDB = mongoClient.db(NotificationConfig['dynamic']['push']['database_name']);
const createNotificationResult = generatedPushes.length > 1 ? await pushesDB.collection(NotificationConfig['dynamic']['push']['schema_name']).insertMany(generatedPushes) : await pushesDB.collection(NotificationConfig['dynamic']['push']['schema_name']).insertOne(generatedPushes[0]);
// console.log('createNotificationResult ', createNotificationResult);

const notificationTransactionDB = mongoClient.db(NotificationConfig['base']['transaction']['database_name']);
const notificationTransactionCollection = notificationTransactionDB.collection(NotificationConfig['base']['transaction']['schema_name']);

if (!createNotificationResult) {
// Update flag and reject
const failedResult = await notificationTransactionCollection.updateOne({ _id: notification._id }, { $set: { status: NOTIFICATION_STATUSES['SENDING_FAILED'] } });
console.log('notif-engine.notificationPush.createNotification.!createNotificationResult ');
reject({ message: 'notif-engine.notificationPush.createNotification.!createNotificationResult ' });
} else {
// Update flag and resolve
const succeededResult = await notificationTransactionCollection.updateOne({ _id: notification._id }, { $set: { status: NOTIFICATION_STATUSES['SENDING_SUCCEEDED'] } });
console.log('notif-engine.notificationPush.createNotification.createNotificationResult notification_transaction_id', notification._id);
resolve(createNotificationResult);
}

} catch (error) {
// Catch all errors
console.log('notif-engine.notificationPush.createNotification.mongo.error ');
reject({ message: 'notif-engine.notificationPush.createNotification.error ' });
} finally {
// Disconnect
mongoClient.close();
}
} else {
// Update status to SENDING_FAILED
// Connect
const mongoClientForNotificationTransactionNoTemplate = await MongoClient.connect(MONGOURL, {
useUnifiedTopology: true
});

try {
const notificationTransactionDBNoTemplate = mongoClientForNotificationTransactionNoTemplate.db(NotificationConfig['base']['transaction']['database_name']);
const notificationTransactionCollectionNoTemplate = notificationTransactionDBNoTemplate.collection(NotificationConfig['base']['transaction']['schema_name']);
const failedResultNoTemplate = await notificationTransactionCollectionNoTemplate.updateOne({ _id: notification._id }, { $set: { status: NOTIFICATION_STATUSES['SENDING_FAILED'] } });
resolve(failedResultNoTemplate);
} catch (noTemplateError) {
console.log('notif-engine.notificationPush.createNotification.!notification[\'template\'] ');
reject({ message: 'notif-engine.notificationPush.createNotification.!notification[\'template\'] ', data: noTemplateError });
} finally {
// Disconnect
mongoClientForNotificationTransactionNoTemplate.close();
}
}

} else {
reject({ message: 'Improper credentials, notificationPush.createNotification failed' });
}
});
}
}
Expand Down

0 comments on commit 2911a7f

Please sign in to comment.