diff --git a/packages/be/modules/notification/logic/push-laudspeaker-notification.js b/packages/be/modules/notification/logic/push-laudspeaker-notification.js new file mode 100644 index 00000000..bacbe1f5 --- /dev/null +++ b/packages/be/modules/notification/logic/push-laudspeaker-notification.js @@ -0,0 +1,30 @@ +// ///////////////////////////////////////////////////////////////////// Imports +// ----------------------------------------------------------------------------- +const Axios = require('axios') +const Path = require('path') + +require('dotenv').config({ path: Path.resolve(__dirname, '../.env') }) + +// ////////////////////////////////////////////////////////////////////// Export +// ----------------------------------------------------------------------------- +module.exports = async (eventData, upsertData = false) => { + try { + const options = { + headers: { + 'Content-Type': 'application/json', + Authorization: `Api-Key ${process.env.LAUDSPEAKER_API_TOKEN}` + } + } + if (upsertData) { + await Axios.post('https://api.laudspeaker.com/customers/upsert', upsertData, options) + .then(async () => { + await Axios.post('https://api.laudspeaker.com/events', eventData, options) + }) + } else { + await Axios.post('https://api.laudspeaker.com/events', eventData, options) + } + } catch (e) { + console.log('=========================[Logic: PushLaudspeakerNotification]') + console.log(e) + } +} diff --git a/packages/be/modules/user/rest/post-kyc-result.js b/packages/be/modules/user/rest/post-kyc-result.js index 1b1a7f23..9963b071 100644 --- a/packages/be/modules/user/rest/post-kyc-result.js +++ b/packages/be/modules/user/rest/post-kyc-result.js @@ -6,6 +6,7 @@ const { SendData } = require('@Module_Utilities') const FindUser = require('@Module_User/logic/find-user') const UpdateUser = require('@Module_User/logic/update-user') const PushNotification = require('@Module_Notification/logic/push-notification') +const PushLaudspeakerNotification = require('@Module_Notification/logic/push-laudspeaker-notification') const Moment = require('moment-timezone') const MC = require('@Root/config') @@ -50,7 +51,7 @@ MC.app.post('/post-kyc-result', async (req, res) => { kycHistory.push(kyc) const saved = await UpdateUser({ _id: user._id, kyc, kycHistory }) MC.socket.io.to(`${saved._id}`).emit('module|kyc-updated|payload', saved) - // ------------------------------------------------------- push notification + // ---------------------------- push notification + laudspeaker notification const notification = { ownerId: user._id, bucket: 'kyc', @@ -61,17 +62,47 @@ MC.app.post('/post-kyc-result', async (req, res) => { failure: null } } + let upsertData + let eventData if (kyc.event === 'success' || kyc.event === 'approve') { notification.custom.success = { createdAt: kyc.data.custom.createdAt } + // data that needs to be added to Laudspeaker user for email template + upsertData = { + correlationKey: 'email', + correlationValue: kyc.data.custom.email, + githubUsername: kyc.data.custom.identifier + } + // data needed for event trigger in Journey + eventData = { + correlationKey: 'email', + correlationValue: kyc.data.custom.email, + source: 'custom', + event: `kyc-${kyc.event}` + } } else if (kyc.event === 'failure') { notification.custom.failure = { createdAt: kyc.error.custom.createdAt, name: kyc.error.name, message: kyc.error.message } + // data that needs to be added to Laudspeaker user for email template + upsertData = { + correlationKey: 'email', + correlationValue: kyc.error.custom.email, + githubUsername: kyc.error.custom.identifier, + failureMessage: kyc.error.message + } + // data needed for event trigger in Journey + eventData = { + correlationKey: 'email', + correlationValue: kyc.error.custom.email, + source: 'custom', + event: `kyc-${kyc.event}` + } } + await PushLaudspeakerNotification(eventData, upsertData) await PushNotification(notification) // -------------------------------------------------------------------- send SendData(res, 200, 'KYC result recorded successfully', saved)