@@ -40,12 +40,29 @@ public async Task<Notification> SendNotificationAsync(
4040 string type ,
4141 string category ,
4242 Guid ? relatedEntityId = null ,
43- string ? relatedEntityType = null )
43+ string ? relatedEntityType = null ,
44+ Guid ? organizationId = null ,
45+ string ? senderUserId = null )
4446 {
45- var organizationId = await _userContext . GetActiveOrganizationIdAsync ( ) ;
47+ organizationId ?? = await _userContext . GetActiveOrganizationIdAsync ( ) ;
4648
4749 // Get user preferences
48- var preferences = await GetNotificationPreferencesAsync ( recipientUserId ) ;
50+ var preferences = await GetNotificationPreferencesAsync ( recipientUserId , organizationId ) ;
51+
52+ // Resolve the sender: explicit caller-provided ID (e.g. SystemUser for background jobs),
53+ // otherwise require an authenticated user.
54+ string resolvedSenderUserId ;
55+ if ( ! string . IsNullOrEmpty ( senderUserId ) )
56+ {
57+ resolvedSenderUserId = senderUserId ;
58+ }
59+ else
60+ {
61+ var authenticatedUserId = await _userContext . GetUserIdAsync ( ) ;
62+ if ( string . IsNullOrEmpty ( authenticatedUserId ) )
63+ throw new UnauthorizedAccessException ( "User is not authenticated." ) ;
64+ resolvedSenderUserId = authenticatedUserId ;
65+ }
4966
5067 var notification = new Notification
5168 {
@@ -62,7 +79,8 @@ public async Task<Notification> SendNotificationAsync(
6279 IsRead = false ,
6380 SendInApp = preferences . EnableInAppNotifications ,
6481 SendEmail = preferences . EnableEmailNotifications && ShouldSendEmail ( category , preferences ) ,
65- SendSMS = preferences . EnableSMSNotifications && ShouldSendSMS ( category , preferences )
82+ SendSMS = preferences . EnableSMSNotifications && ShouldSendSMS ( category , preferences ) ,
83+ CreatedBy = resolvedSenderUserId
6684 } ;
6785
6886 // Save in-app notification
@@ -107,6 +125,7 @@ await _smsService.SendSMSAsync(
107125 }
108126 }
109127
128+ notification . LastModifiedBy = resolvedSenderUserId ;
110129 await UpdateAsync ( notification ) ;
111130
112131 // Broadcast new notification via SignalR
@@ -141,7 +160,9 @@ public async Task<Notification> NotifyAllUsersAsync(
141160 type ,
142161 category ,
143162 relatedEntityId ,
144- relatedEntityType ) ;
163+ relatedEntityType ,
164+ organizationId ,
165+ senderUserId : ApplicationConstants . SystemUser . Id ) ;
145166 }
146167
147168 return lastNotification ! ;
@@ -281,9 +302,9 @@ public async Task<NotificationPreferences> UpdateUserPreferencesAsync(Notificati
281302 /// <summary>
282303 /// Get or create notification preferences for user
283304 /// </summary>
284- private async Task < NotificationPreferences > GetNotificationPreferencesAsync ( string userId )
305+ private async Task < NotificationPreferences > GetNotificationPreferencesAsync ( string userId , Guid ? organizationId = null )
285306 {
286- var organizationId = await _userContext . GetActiveOrganizationIdAsync ( ) ;
307+ organizationId ?? = await _userContext . GetActiveOrganizationIdAsync ( ) ;
287308
288309 var preferences = await _context . NotificationPreferences
289310 . FirstOrDefaultAsync ( p => p . OrganizationId == organizationId
0 commit comments