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) {
|
func (s *customerService) GetByID(ctx context.Context, id string) (*CustomerResponse, error) {
|
||||||
customer, err := s.repository.GetByID(ctx, id)
|
customer, err := s.repository.GetByID(ctx, id)
|
||||||
if err != nil {
|
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{}{
|
s.logger.Error("Failed to get customer by ID", map[string]interface{}{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
"customer_id": id,
|
"customer_id": id,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package notification
|
package notification
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
"tm/pkg/response"
|
"tm/pkg/response"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,6 +37,7 @@ type NotificationListResponse struct {
|
|||||||
type NotificationResponse struct {
|
type NotificationResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
UserID string `json:"user_id"`
|
UserID string `json:"user_id"`
|
||||||
|
Recipient any `json:"recipient"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Link *string `json:"link"`
|
Link *string `json:"link"`
|
||||||
@@ -49,8 +49,8 @@ type NotificationResponse struct {
|
|||||||
Methods map[string]string `json:"methods"`
|
Methods map[string]string `json:"methods"`
|
||||||
Metadata map[string]any `json:"metadata"`
|
Metadata map[string]any `json:"metadata"`
|
||||||
ScheduleAt int64 `json:"schedule_at"`
|
ScheduleAt int64 `json:"schedule_at"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt int64 `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt int64 `json:"updated_at"`
|
||||||
Seen bool `json:"seen"`
|
Seen bool `json:"seen"`
|
||||||
SeenAt int64 `json:"seen_at"`
|
SeenAt int64 `json:"seen_at"`
|
||||||
IsScheduled bool `json:"is_scheduled"`
|
IsScheduled bool `json:"is_scheduled"`
|
||||||
|
|||||||
@@ -192,17 +192,31 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF
|
|||||||
|
|
||||||
notificationsResponse := make([]*NotificationResponse, 0)
|
notificationsResponse := make([]*NotificationResponse, 0)
|
||||||
for _, notification := range notifications.Data {
|
for _, notification := range notifications.Data {
|
||||||
createdAt, err := time.Parse(time.RFC3339, notification.CreatedAt)
|
createdAt, _ := time.Parse(time.RFC3339, notification.CreatedAt)
|
||||||
if err != nil {
|
createdAtUnix := createdAt.Unix()
|
||||||
return nil, err
|
updatedAt, _ := time.Parse(time.RFC3339, notification.UpdatedAt)
|
||||||
|
updatedAtUnix := updatedAt.Unix()
|
||||||
|
if createdAtUnix < 0 {
|
||||||
|
createdAtUnix = 0
|
||||||
}
|
}
|
||||||
updatedAt, err := time.Parse(time.RFC3339, notification.UpdatedAt)
|
if updatedAtUnix < 0 {
|
||||||
if err != nil {
|
updatedAtUnix = 0
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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{
|
notificationsResponse = append(notificationsResponse, &NotificationResponse{
|
||||||
ID: notification.ID,
|
ID: notification.ID,
|
||||||
UserID: notification.UserID,
|
UserID: notification.UserID,
|
||||||
|
Recipient: recipient,
|
||||||
Title: notification.Title,
|
Title: notification.Title,
|
||||||
Message: notification.Message,
|
Message: notification.Message,
|
||||||
Link: ¬ification.Link,
|
Link: ¬ification.Link,
|
||||||
@@ -218,8 +232,8 @@ func (s *notificationService) GetNotifications(ctx context.Context, req *SearchF
|
|||||||
Seen: notification.Seen,
|
Seen: notification.Seen,
|
||||||
SeenAt: notification.SeenAt,
|
SeenAt: notification.SeenAt,
|
||||||
ScheduledAt: notification.ScheduledAt,
|
ScheduledAt: notification.ScheduledAt,
|
||||||
CreatedAt: createdAt,
|
CreatedAt: createdAtUnix,
|
||||||
UpdatedAt: updatedAt,
|
UpdatedAt: updatedAtUnix,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,9 +274,29 @@ func (s *notificationService) GetNotification(ctx context.Context, notificationI
|
|||||||
imagePtr = ¬ification.Data.Image
|
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{
|
detailedResponse := &NotificationResponse{
|
||||||
ID: notification.Data.ID,
|
ID: notification.Data.ID,
|
||||||
UserID: notification.Data.UserID,
|
UserID: notification.Data.UserID,
|
||||||
|
Recipient: recipient,
|
||||||
Title: notification.Data.Title,
|
Title: notification.Data.Title,
|
||||||
Message: notification.Data.Message,
|
Message: notification.Data.Message,
|
||||||
Link: linkPtr,
|
Link: linkPtr,
|
||||||
@@ -270,8 +304,8 @@ func (s *notificationService) GetNotification(ctx context.Context, notificationI
|
|||||||
Type: notification.Data.Type,
|
Type: notification.Data.Type,
|
||||||
Priority: notification.Data.Priority,
|
Priority: notification.Data.Priority,
|
||||||
EventType: notification.Data.EventType,
|
EventType: notification.Data.EventType,
|
||||||
CreatedAt: notification.Data.CreatedAt,
|
CreatedAt: createdAt,
|
||||||
UpdatedAt: notification.Data.UpdatedAt,
|
UpdatedAt: updatedAt,
|
||||||
Methods: notification.Data.Methods,
|
Methods: notification.Data.Methods,
|
||||||
Metadata: notification.Data.Metadata,
|
Metadata: notification.Data.Metadata,
|
||||||
Status: notification.Data.Status,
|
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) {
|
func (s *userService) GetUserByID(ctx context.Context, id string) (*UserResponse, error) {
|
||||||
user, err := s.repository.GetByID(ctx, id)
|
user, err := s.repository.GetByID(ctx, id)
|
||||||
if err != nil {
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user