|
| 1 | +const cloudSDK = require("@sap-cloud-sdk/core"); |
| 2 | +const { getDestination, executeHttpRequest, buildCsrfHeaders } = cloudSDK; |
| 3 | +const { messages } = require("./utils"); |
| 4 | + |
| 5 | +const NOTIFICATIONS_DESTINATION_NAME = "SAP_Notifications"; |
| 6 | +const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc"; |
| 7 | +const NOTIFICATION_TYPES_API_ENDPOINT = "v2/NotificationType.svc"; |
| 8 | + |
| 9 | +async function _getDestination(destinationName) { |
| 10 | + const notifServiceDest = await getDestination(destinationName); |
| 11 | + if (!notifServiceDest) { |
| 12 | + throw new Error(messages.DESTINATION_NOT_FOUND + destinationName); |
| 13 | + } |
| 14 | + return notifServiceDest; |
| 15 | +} |
| 16 | + |
| 17 | +async function getNotificationTypes( |
| 18 | + destinationName = NOTIFICATIONS_DESTINATION_NAME |
| 19 | +) { |
| 20 | + const notifServiceDest = await _getDestination(destinationName); |
| 21 | + const response = await executeHttpRequest(notifServiceDest, { |
| 22 | + url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes`, |
| 23 | + method: "get", |
| 24 | + }); |
| 25 | + return response.data.d.results; |
| 26 | +} |
| 27 | + |
| 28 | +async function postNotificationType( |
| 29 | + notificationType, |
| 30 | + destinationName = NOTIFICATIONS_DESTINATION_NAME |
| 31 | +) { |
| 32 | + const notifServiceDest = await _getDestination(destinationName); |
| 33 | + const csrfHeaders = await buildCsrfHeaders(notifServiceDest, { |
| 34 | + url: NOTIFICATION_TYPES_API_ENDPOINT, |
| 35 | + }); |
| 36 | + const aExistingNotificationTypes = await this.getNotificationTypes(); |
| 37 | + const bIsDuplicateNotificationType = aExistingNotificationTypes.find( |
| 38 | + (nType) => |
| 39 | + nType.NotificationTypeKey === notificationType.NotificationTypeKey && |
| 40 | + nType.NotificationTypeVersion === notificationType.NotificationTypeVersion |
| 41 | + ); |
| 42 | + |
| 43 | + if (!bIsDuplicateNotificationType) { |
| 44 | + console.log( |
| 45 | + `Notification Type of key ${notificationType.NotificationTypeKey} and version ${notificationType.NotificationTypeVersion} was not found. Creating it...` |
| 46 | + ); |
| 47 | + const response = await executeHttpRequest(notifServiceDest, { |
| 48 | + url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes`, |
| 49 | + method: "post", |
| 50 | + data: notificationType, |
| 51 | + headers: csrfHeaders, |
| 52 | + }); |
| 53 | + return response.data.d; |
| 54 | + } else { |
| 55 | + return true; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +async function createNotificationObject( |
| 60 | + recipients, |
| 61 | + notificationTypeKey, |
| 62 | + notificationTypeVersion, |
| 63 | + notificationData, |
| 64 | + language = "en" |
| 65 | +) { |
| 66 | + let aProperties = []; |
| 67 | + if (typeof notificationData === "object") { |
| 68 | + for (sProperty of Object.keys(notificationData)) { |
| 69 | + //TODO: recheck if can be sent from application. Check for localization |
| 70 | + aProperties.push({ |
| 71 | + Key: sProperty, |
| 72 | + Language: language, |
| 73 | + Value: notificationData[sProperty], |
| 74 | + Type: "String", |
| 75 | + IsSensitive: false, |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + return { |
| 80 | + NotificationTypeKey: notificationTypeKey, |
| 81 | + NotificationTypeVersion: notificationTypeVersion, |
| 82 | + Priority: "High", |
| 83 | + Properties: aProperties, |
| 84 | + Recipients: recipients.map((recipient) => ({ RecipientId: recipient })), |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +async function postNotification( |
| 89 | + recipients, |
| 90 | + notificationTypeKey, |
| 91 | + notificationTypeVersion, |
| 92 | + notificationData, |
| 93 | + language = "en", |
| 94 | + destinationName = NOTIFICATIONS_DESTINATION_NAME |
| 95 | +) { |
| 96 | + const notification = await createNotificationObject( |
| 97 | + recipients, |
| 98 | + notificationTypeKey, |
| 99 | + notificationTypeVersion, |
| 100 | + notificationData, |
| 101 | + language |
| 102 | + ); |
| 103 | + const notifServiceDest = await _getDestination(destinationName); |
| 104 | + const csrfHeaders = await buildCsrfHeaders(notifServiceDest, { |
| 105 | + url: NOTIFICATIONS_API_ENDPOINT, |
| 106 | + }); |
| 107 | + |
| 108 | + let response = {}; |
| 109 | + try { |
| 110 | + response = await executeHttpRequest(notifServiceDest, { |
| 111 | + url: `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, |
| 112 | + method: "post", |
| 113 | + data: notification, |
| 114 | + headers: csrfHeaders, |
| 115 | + }); |
| 116 | + } catch (e) { |
| 117 | + console.log(e); |
| 118 | + } |
| 119 | + return response.data.d; |
| 120 | +} |
| 121 | + |
| 122 | +module.exports = { |
| 123 | + getNotificationTypes, |
| 124 | + postNotificationType, |
| 125 | + postNotification, |
| 126 | +}; |
0 commit comments