Skip to content

Commit

Permalink
updates #3 | adds: createNotificationTransaction for maintaining back…
Browse files Browse the repository at this point in the history
…ward compatibility for now
  • Loading branch information
imsheth committed Jul 29, 2020
1 parent ee71754 commit 1c93984
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,37 @@ createMail = async (notification) => {
});
};

createNotificationTransaction = async (notificationTransaction) => {
return new Promise(async (resolve, reject) => {
// Verify
if (notificationTransaction.mongo.MONGOURL && notificationTransaction.mongo.DATABASE_NAME && notificationTransaction.mongo.SCHEMA_NAME) {
// Connect
const mongoClient = await MongoClient.connect(notificationTransaction.mongo.MONGOURL, {
useUnifiedTopology: true
});

try {
// Insert
const db = mongoClient.db(notificationTransaction.mongo.DATABASE_NAME);
const createNotificationTransactionResult = await db.collection(notificationTransaction.mongo.SCHEMA_NAME).insertOne(notificationTransaction.data);
// console.log('notif-engine.createNotificationTransaction.mongo.result ', notificationTransaction.data.id, ' ', notificationTransaction.data.email);
resolve(createNotificationTransactionResult);
} catch (error) {
// Catch all errors
console.log('notif-engine.createNotificationTransaction.mongo.error ', error);
reject({ message: 'notif-engine.createNotificationTransaction.error ', data: error });
} finally {
// Disconnect
mongoClient.close();
}
} else {
console.log('notif-engine.createNotificationTransaction.badRequest.error ', error);
reject({ message: 'Improper credentials, notif-engine.createMail.badRequest.error' });
}
});
};

module.exports = {
createNotificationTransaction: createNotificationTransaction,
createMail: createMail
};

0 comments on commit 1c93984

Please sign in to comment.