diff --git a/internal/customer/service.go b/internal/customer/service.go index a0f6146..78b53d9 100644 --- a/internal/customer/service.go +++ b/internal/customer/service.go @@ -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, diff --git a/internal/notification/form.go b/internal/notification/form.go index b565db0..61bb067 100644 --- a/internal/notification/form.go +++ b/internal/notification/form.go @@ -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"` diff --git a/internal/notification/service.go b/internal/notification/service.go index 2f42504..9cad95f 100644 --- a/internal/notification/service.go +++ b/internal/notification/service.go @@ -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, diff --git a/internal/user/service.go b/internal/user/service.go index a3a3952..d1b6a82 100644 --- a/internal/user/service.go +++ b/internal/user/service.go @@ -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 }