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:
@@ -168,6 +168,9 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
||||
func (s *customerService) GetByID(ctx context.Context, id string) (*CustomerResponse, error) {
|
||||
customer, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
if err.Error() == "customer not found" {
|
||||
return nil, errors.New("customer not found")
|
||||
}
|
||||
s.logger.Error("Failed to get customer by ID", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"customer_id": id,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"time"
|
||||
"tm/pkg/response"
|
||||
)
|
||||
|
||||
@@ -38,6 +37,7 @@ type NotificationListResponse struct {
|
||||
type NotificationResponse struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
Recipient any `json:"recipient"`
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
Link *string `json:"link"`
|
||||
@@ -49,8 +49,8 @@ type NotificationResponse struct {
|
||||
Methods map[string]string `json:"methods"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
ScheduleAt int64 `json:"schedule_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
Seen bool `json:"seen"`
|
||||
SeenAt int64 `json:"seen_at"`
|
||||
IsScheduled bool `json:"is_scheduled"`
|
||||
|
||||
@@ -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: ¬ification.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 = ¬ification.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,
|
||||
|
||||
@@ -245,6 +245,13 @@ func (s *userService) UpdateStatus(ctx context.Context, id string, form *UpdateU
|
||||
func (s *userService) GetUserByID(ctx context.Context, id string) (*UserResponse, error) {
|
||||
user, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
if err.Error() == "user not found" {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
s.logger.Error("Failed to get user by ID", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"user_id": id,
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user