Enhance Notification and User Services with Improved Error Handling and Data Consistency

- Updated GetByID methods in user and customer services to return specific "not found" errors for better clarity in error responses.
- Modified NotificationResponse structure to include a new Recipient field, allowing for more flexible recipient handling.
- Changed CreatedAt and UpdatedAt fields in NotificationResponse to use Unix timestamps (int64) for consistency across the application.
- Improved notification retrieval logic to handle potential nil values for user and customer lookups, ensuring robust data handling.
This commit is contained in:
n.nakhostin
2025-09-27 11:40:17 +03:30
parent 7110e55cf5
commit ac7eb0385d
4 changed files with 57 additions and 13 deletions
+44 -10
View File
@@ -192,17 +192,31 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF
notificationsResponse := make([]*NotificationResponse, 0)
for _, notification := range notifications.Data {
createdAt, err := time.Parse(time.RFC3339, notification.CreatedAt)
if err != nil {
return nil, err
createdAt, _ := time.Parse(time.RFC3339, notification.CreatedAt)
createdAtUnix := createdAt.Unix()
updatedAt, _ := time.Parse(time.RFC3339, notification.UpdatedAt)
updatedAtUnix := updatedAt.Unix()
if createdAtUnix < 0 {
createdAtUnix = 0
}
updatedAt, err := time.Parse(time.RFC3339, notification.UpdatedAt)
if err != nil {
return nil, err
if updatedAtUnix < 0 {
updatedAtUnix = 0
}
usr, _ := s.userService.GetUserByID(ctx, notification.UserID)
cust, _ := s.customerService.GetByID(ctx, notification.UserID)
var recipient any
if usr != nil {
recipient = usr
} else if cust != nil {
recipient = cust
}
notificationsResponse = append(notificationsResponse, &NotificationResponse{
ID: notification.ID,
UserID: notification.UserID,
Recipient: recipient,
Title: notification.Title,
Message: notification.Message,
Link: &notification.Link,
@@ -218,8 +232,8 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF
Seen: notification.Seen,
SeenAt: notification.SeenAt,
ScheduledAt: notification.ScheduledAt,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
CreatedAt: createdAtUnix,
UpdatedAt: updatedAtUnix,
})
}
@@ -260,9 +274,29 @@ func (s *notificationService) GetNotification(ctx context.Context, notificationI
imagePtr = &notification.Data.Image
}
usr, _ := s.userService.GetUserByID(ctx, notification.Data.UserID)
cust, _ := s.customerService.GetByID(ctx, notification.Data.UserID)
var recipient any
if usr != nil {
recipient = usr
} else if cust != nil {
recipient = cust
}
createdAt := notification.Data.CreatedAt.Unix()
updatedAt := notification.Data.UpdatedAt.Unix()
if createdAt < 0 {
createdAt = 0
}
if updatedAt < 0 {
updatedAt = 0
}
detailedResponse := &NotificationResponse{
ID: notification.Data.ID,
UserID: notification.Data.UserID,
Recipient: recipient,
Title: notification.Data.Title,
Message: notification.Data.Message,
Link: linkPtr,
@@ -270,8 +304,8 @@ func (s *notificationService) GetNotification(ctx context.Context, notificationI
Type: notification.Data.Type,
Priority: notification.Data.Priority,
EventType: notification.Data.EventType,
CreatedAt: notification.Data.CreatedAt,
UpdatedAt: notification.Data.UpdatedAt,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
Methods: notification.Data.Methods,
Metadata: notification.Data.Metadata,
Status: notification.Data.Status,