-
Notifications
You must be signed in to change notification settings - Fork 6
Switch from compiling Notification Types to Event Types #125
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
Draft
eric-pSAP
wants to merge
5
commits into
main
Choose a base branch
from
eventTypes
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d107c9
Initial commit
eric-pSAP 074a7e7
Merge branch 'main' into eventTypes
eric-pSAP 8bc21ce
Basic i18n support
eric-pSAP 076a625
Merge branch 'eventTypes' of https://github.com/cap-js/notifications …
eric-pSAP d5ad1b6
# handling
eric-pSAP 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 hidden or 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 hidden or 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,29 @@ | ||
| const cds = require('@sap/cds') | ||
|
|
||
| const { copy, exists, path } = cds.utils | ||
| const { path } = cds.utils | ||
|
|
||
| module.exports = class NotificationsBuildPlugin extends cds.build.Plugin { | ||
| static taskDefaults = { src: cds.env.folders.srv } | ||
|
|
||
| static hasTask() { | ||
| const notificationTypesFile = cds.env.requires?.notifications?.types; | ||
| return notificationTypesFile === undefined ? false : exists(notificationTypesFile); | ||
| return !!cds.env.requires?.notifications | ||
|
eric-pSAP marked this conversation as resolved.
|
||
| } | ||
|
|
||
| async build() { | ||
| if (exists(cds.env.requires.notifications?.types)) { | ||
| const fileName = path.basename(cds.env.requires.notifications.types); | ||
| await copy(cds.env.requires.notifications.types).to(path.join(this.task.dest, fileName)); | ||
| const model = await this.model() | ||
| if (!model) return | ||
|
|
||
| const { notificationTypesFromModel } = require('./compile') | ||
| const { readFile } = require('./utils') | ||
|
|
||
| const typesPath = cds.env.requires.notifications?.types | ||
| const types = [ | ||
| ...notificationTypesFromModel(model), | ||
| ...(typesPath ? readFile(typesPath) : []) | ||
| ] | ||
|
|
||
| if (types.length) { | ||
| await this.write(JSON.stringify(types, null, 2)).to(path.join(this.task.dest, 'notification-types.json')) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,61 @@ | ||
| const cds = require('@sap/cds') | ||
|
|
||
| function resolveEnum(val) { | ||
| if (val && typeof val === 'object') { | ||
| if ('=' in val) return val['='] | ||
| if ('#' in val) return val['#'] | ||
| } | ||
| return val | ||
| } | ||
|
|
||
| function notificationTypesFromModel(model) { | ||
| if (!model) return [] | ||
| const types = [] | ||
|
|
||
| for (const def of Object.values(cds.reflect(model).definitions)) { | ||
| if (def.kind !== 'event') continue | ||
| if (!Object.keys(def).some(k => k === '@notification' || k.startsWith('@notification.'))) continue | ||
|
|
||
| const tmpl = { Language: 'en', TemplateLanguage: 'mustache' } | ||
| if (def['@description']) tmpl.Description = resolveI18n(def['@description']) | ||
| if (def['@notification.template.title']) tmpl.TemplateSensitive = resolveI18n(def['@notification.template.title']) | ||
| if (def['@notification.template.publicTitle']) tmpl.TemplatePublic = resolveI18n(def['@notification.template.publicTitle']) | ||
| if (def['@notification.template.subtitle']) tmpl.Subtitle = resolveI18n(def['@notification.template.subtitle']) | ||
| if (def['@notification.template.groupedTitle']) tmpl.TemplateGrouped = resolveI18n(def['@notification.template.groupedTitle']) | ||
| if (def['@notification.template.email.subject']) tmpl.EmailSubject = resolveI18n(def['@notification.template.email.subject']) | ||
| if (def['@notification.template.email.html']) tmpl.EmailHtml = resolveI18n(def['@notification.template.email.html']) | ||
|
|
||
| const type = { | ||
| NotificationTypeKey: def.name.split('.').pop(), | ||
| NotificationTypeVersion: '1', | ||
| Templates: [tmpl], | ||
| } | ||
|
|
||
| if (def['@Common.SemanticObject']) type.NavigationTargetObject = def['@Common.SemanticObject'] | ||
| if (def['@Common.SemanticObjectAction']) type.NavigationTargetAction = def['@Common.SemanticObjectAction'] | ||
|
|
||
| if (def['@notification.deliveryChannels']?.length) { | ||
| type.DeliveryChannels = def['@notification.deliveryChannels'].map(ch => { | ||
| if (!ch.channel) return null | ||
| const entry = { Type: resolveEnum(ch.channel).toUpperCase() } | ||
|
eric-pSAP marked this conversation as resolved.
|
||
| if (ch.enabled !== undefined) entry.Enabled = ch.enabled | ||
| if (ch.defaultPreference !== undefined) entry.DefaultPreference = ch.defaultPreference | ||
| if (ch.editablePreference !== undefined) entry.EditablePreference = ch.editablePreference | ||
| return entry | ||
| }).filter(Boolean) | ||
| } | ||
|
|
||
| types.push(type) | ||
| } | ||
|
|
||
| return types | ||
| } | ||
|
|
||
| function resolveI18n(value) { | ||
| if (typeof value !== 'string') return value | ||
| const match = value.match(/^\{i18n>([^}]+)\}$/) | ||
| if (!match) return value | ||
| return cds.i18n?.labels?.at(match[1], 'en') ?? value | ||
| } | ||
|
|
||
| module.exports = { notificationTypesFromModel } | ||
This file contains hidden or 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,5 @@ | ||
| BOOK_ORDERED_DESCRIPTION=Book Ordered | ||
| BOOK_ORDERED_TITLE=Book Ordered | ||
| BOOK_ORDERED_PUBLIC_TITLE=Book Ordered | ||
| BOOK_ORDERED_SUBTITLE={{buyer}} ordered {{title}} | ||
| BOOK_ORDERED_GROUPED_TITLE=Bookshop Updates |
This file contains hidden or 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 hidden or 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,14 @@ | ||
| @description: '{i18n>BOOK_ORDERED_DESCRIPTION}' | ||
| @notification: { | ||
| template: { | ||
| title : '{i18n>BOOK_ORDERED_TITLE}', | ||
| publicTitle : '{i18n>BOOK_ORDERED_PUBLIC_TITLE}', | ||
| subtitle : '{i18n>BOOK_ORDERED_SUBTITLE}', | ||
| groupedTitle : '{i18n>BOOK_ORDERED_GROUPED_TITLE}', | ||
| } | ||
| } | ||
| event BookOrdered { | ||
| title : String; | ||
| buyer : String; | ||
| recipients: array of String; | ||
| } |
This file contains hidden or 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,62 +1,73 @@ | ||
| const cds = require("@sap/cds"); | ||
| const { join } = cds.utils.path; | ||
| const { messages } = require("../../lib/utils"); | ||
| const cds = require("@sap/cds") | ||
| const { join } = cds.utils.path | ||
| const { messages } = require("../../lib/utils") | ||
|
|
||
| cds.test(join(__dirname, "../bookshop")); | ||
| cds.test(join(__dirname, "../bookshop")) | ||
|
|
||
| describe("Notifications Integration", () => { | ||
| let log = cds.test.log() | ||
| let alert; | ||
| let alert | ||
|
|
||
| beforeAll(async () => { | ||
| alert = await cds.connect.to("notifications"); | ||
| }); | ||
| alert = await cds.connect.to("notifications") | ||
| }) | ||
|
|
||
| test("Notifications service resolves to console implementation in development", async () => { | ||
| expect(alert.constructor.name).toBe("NotifyToConsole"); | ||
| }); | ||
| expect(alert.constructor.name).toBe("NotifyToConsole") | ||
| }) | ||
|
|
||
| test("Notification types are loaded into cds.notifications on startup", () => { | ||
| expect(cds.notifications?.local?.types).toBeDefined(); | ||
| expect(cds.notifications.local.types).toHaveProperty("bookshop/BookOrdered"); | ||
| }); | ||
| expect(cds.notifications?.local?.types).toBeDefined() | ||
| expect(cds.notifications.local.types).toHaveProperty("bookshop/BookOrdered") | ||
| }) | ||
|
|
||
| test("Sending a notification with unknown type key gives a warning", async () => { | ||
| await alert.notify("UnknownType", { | ||
| recipients: ["reader@bookshop.com"], | ||
| data: { title: "test" } | ||
| }); | ||
| }) | ||
|
|
||
| expect(log.output).toContain("UnknownType is not in the notification types file"); | ||
| }); | ||
| expect(log.output).toContain("UnknownType is not in the notification types file") | ||
| }) | ||
|
|
||
| test("Sending a default notification logs to console", async () => { | ||
| await alert.notify({ | ||
| recipients: ["reader@bookshop.com"], | ||
| title: "New book arrived", | ||
| description: "A new book has been added to the catalogue" | ||
| }); | ||
| }) | ||
|
|
||
| expect(log.output).toContain("Notification:"); | ||
| expect(log.output).toContain("NotificationTypeKey: 'Default'"); | ||
| expect(log.output).toContain("RecipientId: 'reader@bookshop.com'"); | ||
| expect(log.output).toContain("Value: 'New book arrived'"); | ||
| }); | ||
| expect(log.output).toContain("Notification:") | ||
| expect(log.output).toContain("NotificationTypeKey: 'Default'") | ||
| expect(log.output).toContain("RecipientId: 'reader@bookshop.com'") | ||
| expect(log.output).toContain("Value: 'New book arrived'") | ||
| }) | ||
|
|
||
| test("Sending a notification with no arguments warns and does nothing", async () => { | ||
| await alert.notify(); | ||
| await alert.notify() | ||
|
|
||
| expect(log.output).toContain(messages.NO_OBJECT_FOR_NOTIFY); | ||
| expect(log.output).not.toContain("Notification:"); | ||
| }); | ||
| expect(log.output).toContain(messages.NO_OBJECT_FOR_NOTIFY) | ||
| expect(log.output).not.toContain("Notification:") | ||
| }) | ||
|
|
||
| test("Custom typed notification uses prefixed type key from types file", async () => { | ||
| await alert.notify("BookOrdered", { | ||
| recipients: ["reader@bookshop.com"], | ||
| data: { title: "Moby Dick", buyer: "reader@bookshop.com" } | ||
| }); | ||
| }) | ||
|
|
||
| expect(log.output).toContain("bookshop/BookOrdered"); | ||
| expect(log.output).not.toContain("is not in the notification types file"); | ||
| }); | ||
| }); | ||
| expect(log.output).toContain("bookshop/BookOrdered") | ||
| expect(log.output).not.toContain("is not in the notification types file") | ||
| }) | ||
|
|
||
| test("Notification types from CDS and JSON are merged", () => { | ||
| expect(cds.notifications.local.types).toHaveProperty("bookshop/BookOrdered") | ||
| expect(cds.notifications.local.types).toHaveProperty("bookshop/BookReturned") | ||
| }) | ||
|
|
||
| test("Notification type templates have resolved i18n values", () => { | ||
| const type = cds.notifications.local.types["bookshop/BookOrdered"]["1"] | ||
| expect(type.Templates[0].TemplateSensitive).toBe("Book Ordered") | ||
| expect(type.Templates[0].Subtitle).toBe("{{buyer}} ordered {{title}}") | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.