-
Notifications
You must be signed in to change notification settings - Fork 0
feat: store sent notifications to DB #52
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
Open
busehalis-sap
wants to merge
13
commits into
main
Choose a base branch
from
feat/store-notifications-db
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
13 commits
Select commit
Hold shift + click to select a range
e49220d
feat: store sent notifications to DB when storeNotifications is enabled
busehalis-sap 385cfcb
test: add integration tests for storing notifications to DB
busehalis-sap 3197265
chore: demonstrate storeNotifications and TargetParameters usage in s…
busehalis-sap 4e22234
docs: document Storing Notifications to DB feature in README and CHAN…
busehalis-sap 74e8aa7
test: await DB write instead of mock handler notification count in St…
busehalis-sap a406f99
fix: import Service class in StoreNotificationsHandler instead of usi…
busehalis-sap 24cef81
feat: support storing notifications to DB in local mode
busehalis-sap 2060d53
test: add integration test for storing notifications to DB in local mode
busehalis-sap a8eb6f6
refactor: simplify StoreNotificationsHandler using typed method signa…
busehalis-sap 3cb1745
refactor: use cds.notifications namespace for storeNotifications flag…
busehalis-sap dd4a586
refactor: rename NotificationStorageService to NotificationStorageHelper
busehalis-sap 30fc3da
docs: document stored fields for notifications and mention sentAt fie…
busehalis-sap ce9aedc
refactor: remove unnecessary loop in StoreNotificationsHandler as han…
busehalis-sap 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
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
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
36 changes: 36 additions & 0 deletions
36
...fications/src/main/java/com/sap/cds/notifications/handlers/StoreNotificationsHandler.java
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,36 @@ | ||
| /* | ||
| * © 2026 SAP SE or an SAP affiliate company and cds-feature-notifications contributors. | ||
| */ | ||
| package com.sap.cds.notifications.handlers; | ||
|
|
||
| import cds.gen.notificationproviderservice.NotificationProviderService_; | ||
| import cds.gen.notificationproviderservice.Notifications; | ||
| import com.sap.cds.notifications.helpers.NotificationStorageHelper; | ||
| import com.sap.cds.services.Service; | ||
| import com.sap.cds.services.cds.CdsCreateEventContext; | ||
| import com.sap.cds.services.handler.EventHandler; | ||
| import com.sap.cds.services.handler.annotations.After; | ||
| import com.sap.cds.services.handler.annotations.ServiceName; | ||
| import java.time.Instant; | ||
| import java.util.List; | ||
|
|
||
| @ServiceName(value = NotificationProviderService_.CDS_NAME, type = Service.class) | ||
| public class StoreNotificationsHandler implements EventHandler { | ||
|
|
||
| private final NotificationStorageHelper storageHelper; | ||
|
|
||
| public StoreNotificationsHandler(NotificationStorageHelper storageHelper) { | ||
| this.storageHelper = storageHelper; | ||
| } | ||
|
|
||
| @After | ||
| public void storeNotifications(CdsCreateEventContext context, List<Notifications> results) { | ||
| if (results == null || results.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| Notifications result = results.get(0); | ||
| Notifications requestEntry = Notifications.of(context.getCqn().entries().get(0)); | ||
| storageHelper.store(result.getId(), requestEntry, Instant.now()); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
...ions/src/main/java/com/sap/cds/notifications/handlers/StoreNotificationsLocalHandler.java
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,41 @@ | ||
| /* | ||
| * © 2026 SAP SE or an SAP affiliate company and cds-feature-notifications contributors. | ||
| */ | ||
| package com.sap.cds.notifications.handlers; | ||
|
|
||
| import cds.gen.notificationproviderservice.Notifications; | ||
| import com.sap.cds.notifications.helpers.NotificationStorageHelper; | ||
| import com.sap.cds.services.EventContext; | ||
| import com.sap.cds.services.cds.ApplicationService; | ||
| import com.sap.cds.services.handler.EventHandler; | ||
| import com.sap.cds.services.handler.annotations.After; | ||
| import com.sap.cds.services.handler.annotations.ServiceName; | ||
| import java.time.Instant; | ||
| import java.util.List; | ||
|
|
||
| @ServiceName(value = "*", type = ApplicationService.class) | ||
| public class StoreNotificationsLocalHandler implements EventHandler { | ||
|
|
||
| private final NotificationStorageHelper storageService; | ||
|
|
||
| public StoreNotificationsLocalHandler(NotificationStorageHelper storageService) { | ||
| this.storageService = storageService; | ||
| } | ||
|
|
||
| @After(event = "*") | ||
| public void storeNotifications(EventContext context) { | ||
| @SuppressWarnings("unchecked") | ||
| List<Notifications> sentNotifications = | ||
| (List<Notifications>) context.get(LocalHandler.SENT_NOTIFICATIONS_KEY); | ||
|
|
||
| if (sentNotifications == null || sentNotifications.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| Instant sentAt = Instant.now(); | ||
|
|
||
| for (Notifications notification : sentNotifications) { | ||
| storageService.store(notification.getId(), notification, sentAt); | ||
| } | ||
| } | ||
| } | ||
108 changes: 108 additions & 0 deletions
108
...ifications/src/main/java/com/sap/cds/notifications/helpers/NotificationStorageHelper.java
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,108 @@ | ||
| /* | ||
| * © 2026 SAP SE or an SAP affiliate company and cds-feature-notifications contributors. | ||
| */ | ||
| package com.sap.cds.notifications.helpers; | ||
|
|
||
| import cds.gen.notificationproviderservice.Notifications; | ||
| import cds.gen.notificationproviderservice.Recipients; | ||
| import cds.gen.sap.cds.notifications.NotificationProperties; | ||
| import cds.gen.sap.cds.notifications.NotificationTargetParameters; | ||
| import com.sap.cds.ql.Insert; | ||
| import com.sap.cds.services.persistence.PersistenceService; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** Shared service for persisting notifications to the database. */ | ||
| public class NotificationStorageHelper { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(NotificationStorageHelper.class); | ||
|
|
||
| private final PersistenceService persistenceService; | ||
|
|
||
| public NotificationStorageHelper(PersistenceService persistenceService) { | ||
| this.persistenceService = persistenceService; | ||
| } | ||
|
|
||
| public void store(String notificationId, Notifications request, Instant sentAt) { | ||
| if (notificationId == null) { | ||
| logger.warn("Skipping notification with null ID, cannot store to DB"); | ||
| return; | ||
| } | ||
|
|
||
| logger.info( | ||
| "Storing notification '{}' for {} recipient(s) to DB", | ||
| notificationId, | ||
| request.getRecipients().size()); | ||
|
|
||
| for (Recipients recipient : request.getRecipients()) { | ||
|
Schmarvinius marked this conversation as resolved.
|
||
| String recipientId = resolveRecipientId(recipient); | ||
| cds.gen.sap.cds.notifications.Notifications stored = | ||
| buildStoredNotification(notificationId, request, recipientId, sentAt); | ||
| persistenceService.run( | ||
| Insert.into(cds.gen.sap.cds.notifications.Notifications_.class).entry(stored)); | ||
| logger.debug("Stored notification '{}' for recipient '{}'", notificationId, recipientId); | ||
| } | ||
| } | ||
|
|
||
| private cds.gen.sap.cds.notifications.Notifications buildStoredNotification( | ||
| String notificationId, Notifications request, String recipientId, Instant sentAt) { | ||
| cds.gen.sap.cds.notifications.Notifications stored = | ||
| cds.gen.sap.cds.notifications.Notifications.create(); | ||
| stored.setId(notificationId); | ||
| stored.setRecipient(recipientId); | ||
| stored.setNotificationTypeKey(request.getNotificationTypeKey()); | ||
| stored.setNotificationTemplateKey(request.getNotificationTemplateKey()); | ||
| stored.setPriority(request.getPriority()); | ||
| stored.setNavigationTargetObject(request.getNavigationTargetObject()); | ||
| stored.setNavigationTargetAction(request.getNavigationTargetAction()); | ||
| stored.setSentAt(sentAt); | ||
|
|
||
| if (request.getProperties() != null) { | ||
| stored.setProperties(buildProperties(request)); | ||
| } | ||
|
|
||
| if (request.getTargetParameters() != null) { | ||
| stored.setTargetParameters(buildTargetParameters(request)); | ||
| } | ||
|
|
||
| return stored; | ||
| } | ||
|
|
||
| private List<NotificationProperties> buildProperties(Notifications notification) { | ||
| List<NotificationProperties> properties = new ArrayList<>(); | ||
| notification | ||
| .getProperties() | ||
| .forEach( | ||
| prop -> { | ||
| NotificationProperties p = NotificationProperties.create(); | ||
| p.setPropertyKey(prop.getKey()); | ||
| p.setPropertyValue(prop.getValue()); | ||
| properties.add(p); | ||
| }); | ||
| return properties; | ||
| } | ||
|
|
||
| private List<NotificationTargetParameters> buildTargetParameters(Notifications notification) { | ||
| List<NotificationTargetParameters> params = new ArrayList<>(); | ||
| notification | ||
| .getTargetParameters() | ||
| .forEach( | ||
| param -> { | ||
| NotificationTargetParameters p = NotificationTargetParameters.create(); | ||
| p.setParamKey(param.getKey()); | ||
| p.setParamValue(param.getValue()); | ||
| params.add(p); | ||
| }); | ||
| return params; | ||
| } | ||
|
|
||
| private String resolveRecipientId(Recipients recipient) { | ||
| if (recipient.getGlobalUserId() != null && !recipient.getGlobalUserId().isBlank()) { | ||
| return recipient.getGlobalUserId(); | ||
| } | ||
| return recipient.getRecipientId(); | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
...ions/src/main/resources/cds/com.sap.cds/cds-feature-notifications/NotificationStorage.cds
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,32 @@ | ||
| namespace sap.cds.notifications; | ||
|
|
||
| @PersonalData.EntitySemantics : 'Other' | ||
| entity Notifications { | ||
| key ID : UUID not null; | ||
| @PersonalData.FieldSemantics : 'DataSubjectID' | ||
| key recipient : String(254) not null; | ||
| notificationTypeKey : String(128); | ||
| notificationTemplateKey : String; | ||
| priority : String(20); | ||
| navigationTargetObject : String(500); | ||
| navigationTargetAction : String(500); | ||
| sentAt : Timestamp; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention in the readme that this is UTC time. |
||
| properties : Composition of many NotificationProperties | ||
| on properties.notification = $self; | ||
| targetParameters : Composition of many NotificationTargetParameters | ||
| on targetParameters.notification = $self; | ||
| } | ||
|
|
||
| entity NotificationProperties { | ||
| key notification : Association to Notifications not null; | ||
| key propertyKey : String(128) not null; | ||
| @PersonalData.FieldSemantics : 'IsPotentiallyPersonal' | ||
| propertyValue : String; | ||
| } | ||
|
|
||
| entity NotificationTargetParameters { | ||
| key notification : Association to Notifications not null; | ||
| key paramKey : String(250) not null; | ||
| @PersonalData.FieldSemantics : 'IsPotentiallyPersonal' | ||
| paramValue : String; | ||
| } | ||
3 changes: 2 additions & 1 deletion
3
...ture-notifications/src/main/resources/cds/com.sap.cds/cds-feature-notifications/index.cds
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,3 +1,4 @@ | ||
| using from './NotificationProviderService.cds'; | ||
| using from './NotificationTypeProviderService.cds'; | ||
| using from './NotificationTemplateProviderService.cds'; | ||
| using from './NotificationTemplateProviderService.cds'; | ||
| using from './NotificationStorage'; |
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
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.
This will be after every event, right? Is it possible to narrow that down?
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.
Narrowing it down would require more complex architectural changes. The filtering already happens at the top of the method with a simple map lookup, which is very cheap. EntityNotificationHandler uses the same pattern.
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.
Also, it affects local only therefore not that relevant anyway. Tested it out and it worked :)