-
Notifications
You must be signed in to change notification settings - Fork 4
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
Features for 1.0.0 of package #60
Open
schiwekM
wants to merge
7
commits into
main
Choose a base branch
from
feat-1.0.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d2be26b
Use cds.context.locale instead of hardcoded en
schiwekM 67d59b6
Merge branch 'main' into feat-use-context-language
schiwekM a8d68ab
Fixed issue with languages
schiwekM 7b59084
Features for 1.0.0
schiwekM 2b6be1b
Naming changes + fixes for global user id
schiwekM 91f62b8
Also store properties
schiwekM e5a6d79
Fix modelling
schiwekM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
using from './lib/notifications'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,47 @@ | ||
const cds = require('@sap/cds') | ||
const cds = require('@sap/cds'); | ||
const { readFile, getPrefix } = require('./utils'); | ||
const { preprocessTypes } = require('./buildNotificationTypes'); | ||
const { readFileSync } = require('fs'); | ||
const { BuildPlugin } = cds.build | ||
|
||
const { copy, exists, path } = cds.utils | ||
const { copy, exists, path, write } = cds.utils | ||
|
||
module.exports = class NotificationsBuildPlugin extends BuildPlugin { | ||
|
||
static taskDefaults = { src: cds.env.folders.srv } | ||
static hasTask() { | ||
const notificationTypesFile = cds.env.requires?.notifications?.types; | ||
return notificationTypesFile === undefined ? false : exists(notificationTypesFile); | ||
} | ||
|
||
async build() { | ||
if (exists(cds.env.requires.notifications?.types)) { | ||
const notificationTypes = require(path.join(cds.root, cds.env.requires.notifications.types)); | ||
|
||
if (exists(cds.env.requires.notifications?.build?.before)) { | ||
const handler = require(cds.env.requires.notifications?.build?.before); | ||
await handler(notificationTypes); | ||
} | ||
|
||
preprocessTypes(notificationTypes); | ||
|
||
if (exists(cds.env.requires.notifications?.build?.after)) { | ||
const handler = require(path.join(cds.root, cds.env.requires.notifications?.build?.after)); | ||
await handler(notificationTypes); | ||
} | ||
|
||
const fileName = path.basename(cds.env.requires.notifications.types); | ||
await copy(cds.env.requires.notifications.types).to(path.join(this.task.dest, fileName)); | ||
await this.write(JSON.stringify(notificationTypes)).to(path.join(this.task.dest, fileName)); | ||
|
||
await this.write(JSON.stringify(notificationTypes)).to(path.join(this.task.dest, '../notifications/notification-types.json')); | ||
|
||
if (exists(path.join(this.task.src, '../node_modules/@cap-js/notifications/lib/deployer'))) { | ||
await this.copy(path.join(this.task.src, '../node_modules/@cap-js/notifications/lib/deployer')).to(path.join(this.task.dest, '../notifications')) | ||
} | ||
const config = { | ||
prefix: getPrefix(), | ||
destination: cds.env.requires.notifications?.destination ?? "SAP_Notifications" | ||
} | ||
await this.write(JSON.stringify(config)).to(path.join(this.task.dest, '../notifications/config.json')) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const { supportedANSLanguages } = require("./utils"); | ||
const cds = require("@sap/cds"); | ||
const path = require("path"); | ||
const {TextBundle} = require('@sap/textbundle'); | ||
|
||
function buildNotificationType(_) { | ||
const notificationType = { | ||
NotificationTypeKey: _.NotificationTypeKey, | ||
NotificationTypeVersion: _.NotificationTypeVersion, | ||
Templates: _.Templates?.map(t => ({ | ||
Language: t.Language, | ||
TemplateSensitive: t.TemplateSensitive, | ||
TemplatePublic: t.TemplatePublic, | ||
TemplateGrouped: t.TemplateGrouped, | ||
Subtitle: t.Subtitle, | ||
Description: t.Description, | ||
TemplateLanguage: t.TemplateLanguage, | ||
EmailSubject: t.EmailSubject, | ||
EmailHtml: t.EmailHtml, | ||
EmailText: t.EmailText | ||
})), | ||
Actions: _.Actions?.map(a => ({ | ||
ActionId: a.ActionId, | ||
ActionText: a.ActionText, | ||
GroupActionText: a.GroupActionText, | ||
})), | ||
DeliveryChannels: _.DeliveryChannels?.map(d => ({ | ||
Type: d.Type, | ||
Enabled: d.Enabled, | ||
DefaultPreference: d.DefaultPreference, | ||
EditablePreference: d.EditablePreference | ||
})) | ||
} | ||
return JSON.parse(JSON.stringify(notificationType)); | ||
} | ||
|
||
const preprocessTypes = function (types) { | ||
if (!cds.env.requires.notifications) cds.env.requires.notifications = {} | ||
if (!cds.env.requires.notifications.defaults) cds.env.requires.notifications.defaults = {} | ||
|
||
for (let i = 0; i < types.length; i++) { | ||
const notificationType = types[i] | ||
if (notificationType.ID) { | ||
notificationType.NotificationTypeKey = notificationType.ID; | ||
} | ||
if (!notificationType.NotificationTypeVersion) notificationType.NotificationTypeVersion = "1" | ||
if (!notificationType.Templates) { //-> Languages not manually specified | ||
notificationType.Templates = []; | ||
for (const language in supportedANSLanguages) { | ||
const textBundle = new TextBundle(path.join(cds.root, (cds.env.i18n?.folders[0] ?? '_i18n') + '/notifications' ), language); | ||
const newTemplate = { | ||
Language: language, | ||
TemplateLanguage: 'Mustache' | ||
} | ||
const getVal = (property) => textBundle.getText(notificationType[property]?.code ?? notificationType[property], (notificationType[property]?.args ?? []).map(a => `{{${a}}}`)) | ||
if (notificationType.TemplateSensitive) newTemplate.TemplateSensitive = getVal('TemplateSensitive'); | ||
if (notificationType.TemplatePublic) newTemplate.TemplatePublic = getVal('TemplatePublic'); | ||
if (notificationType.TemplateGrouped) newTemplate.TemplateGrouped = getVal('TemplateGrouped'); | ||
if (notificationType.Subtitle) newTemplate.Subtitle = getVal('Subtitle'); | ||
if (notificationType.Description) newTemplate.Description = getVal('Description'); | ||
if (notificationType.EmailSubject) newTemplate.EmailSubject = getVal('EmailSubject'); | ||
if (notificationType.EmailHtml) newTemplate.EmailHtml = getVal('EmailHtml'); | ||
if (notificationType.EmailText) newTemplate.EmailText = getVal('EmailText'); | ||
notificationType.Templates.push(newTemplate) | ||
} | ||
} | ||
|
||
if (notificationType.DeliveryChannels && !Array.isArray(notificationType.DeliveryChannels)) { | ||
const deliveryChannels = [] | ||
for (const option in notificationType.DeliveryChannels) { | ||
deliveryChannels.push({ | ||
Type: option.toUpperCase(), | ||
Enabled: !!notificationType.DeliveryChannels[option], | ||
DefaultPreference: !!notificationType.DeliveryChannels[option], | ||
EditablePreference: !!notificationType.DeliveryChannels[option] | ||
}) | ||
} | ||
notificationType.DeliveryChannels = deliveryChannels; | ||
} | ||
|
||
// | ||
if (!cds.env.requires.notifications.defaults[notificationType.NotificationTypeKey]) | ||
cds.env.requires.notifications.defaults[notificationType.NotificationTypeKey] = {} | ||
if (!cds.env.requires.notifications.defaults[notificationType.NotificationTypeKey][notificationType.NotificationTypeVersion]) { | ||
cds.env.requires.notifications.defaults[notificationType.NotificationTypeKey][notificationType.NotificationTypeVersion] = { | ||
priority: notificationType.Priority ?? 'Neutral', | ||
navigation: notificationType.Navigation ? { | ||
semanticObject : notificationType.SemanticObject ?? notificationType.semanticObject, | ||
semanticAction : notificationType.SemanticAction ?? notificationType.semanticAction, | ||
} : null, | ||
minTimeBetweenNotifications: notificationType.MinTimeBetweenNotifications ?? 0, | ||
properties : notificationType.Properties ?? {}, | ||
targetParameters : notificationType.TargetParameters ? new Set(...notificationType.TargetParameters) : new Set() | ||
} | ||
} | ||
|
||
types[i] = buildNotificationType(notificationType) | ||
} | ||
} | ||
|
||
module.exports = { | ||
preprocessTypes | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const { readFileSync } = require('fs'); | ||
const { processNotificationTypes } = require("./notificationTypes"); | ||
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util"); | ||
|
||
async function deployNotificationTypes() { | ||
setGlobalLogLevel("error"); | ||
|
||
// read notification types | ||
const notificationTypes = JSON.parse(readFileSync('./notification-types.json')); | ||
await processNotificationTypes(notificationTypes); | ||
} | ||
|
||
deployNotificationTypes(); | ||
|
||
module.exports = { | ||
deployNotificationTypes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@cap-js/notifications-deployer", | ||
"dependencies": { | ||
"@sap-cloud-sdk/connectivity": "^3.13.0", | ||
"@sap-cloud-sdk/http-client": "^3.13.0", | ||
"@sap-cloud-sdk/util": "^3.13.0" | ||
}, | ||
"scripts": { | ||
"start": "node ./content-deployment.js" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const { getDestination } = require("@sap-cloud-sdk/connectivity"); | ||
const {existsSync} = require('fs') | ||
const messages = { | ||
DESTINATION_NOT_FOUND: "Failed to get destination: ", | ||
}; | ||
|
||
let prefix // be filled in below... | ||
function getPrefixCdsEnv() { | ||
if (!prefix) { | ||
prefix = cds.env.requires.notifications?.prefix | ||
if (prefix === "$app-name") try { | ||
prefix = require(cds.root + '/package.json').name | ||
} catch { prefix = null } | ||
if (!prefix) prefix = basename(cds.root) | ||
} | ||
return prefix | ||
} | ||
|
||
function getConfig() { | ||
if (existsSync('./config.json')) { | ||
return JSON.parse(require('./config.json')); | ||
} else { | ||
return { | ||
destination: cds.env.requires.notifications?.destination ?? "SAP_Notifications", | ||
prefix: getPrefixCdsEnv() | ||
} | ||
} | ||
} | ||
|
||
async function getNotificationDestination() { | ||
const config = getConfig(); | ||
const destinationName = config.destination ?? "SAP_Notifications"; | ||
const notificationDestination = await getDestination({ destinationName, useCache: true }); | ||
if (!notificationDestination) { | ||
// TODO: What to do if destination isn't found?? | ||
throw new Error(messages.DESTINATION_NOT_FOUND + destinationName); | ||
} | ||
return notificationDestination; | ||
} | ||
|
||
function getPrefix() { | ||
const config = getConfig(); | ||
return config.prefix; | ||
} | ||
|
||
function getNotificationTypesKeyWithPrefix(notificationTypeKey) { | ||
const prefix = getPrefix(); | ||
return `${prefix}/${notificationTypeKey}`; | ||
} | ||
|
||
module.exports = { | ||
getNotificationDestination, | ||
getPrefix, | ||
getNotificationTypesKeyWithPrefix, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the advantage of using
@sap-cloud-sdk/util
overcds.log
. IMOcds.log
should cover all the required features and if not those should be requestedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention was that the deployer module runs with the bare minimum required dependencies and utils is anyways needed for executeHttpRequest thus @sao/cds as a dependency for the deployer can be avoided.