Enhance Customer Service with Notification Response Structures
- Introduced CustomerNotificationResponse struct to provide a detailed response format for customer notifications, improving the clarity of notification data. - Updated the Customer service interface to include methods for searching and retrieving customers with notification-specific responses, enhancing the flexibility of customer data retrieval. - Modified the Login method to accept a device token, allowing for better tracking of customer devices during login. - Refactored existing methods to utilize the new notification response structure, ensuring consistency across customer-related API responses. - Enhanced error handling and logging in the service layer for improved traceability during customer searches and logins.
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -29,9 +28,10 @@ type Service interface {
|
||||
Register(ctx context.Context, form *CreateCustomerForm) (*CustomerResponse, error)
|
||||
GetByID(ctx context.Context, id string) (*CustomerResponse, error)
|
||||
Search(ctx context.Context, form *SearchCustomersForm, pagination *response.Pagination) (*CustomerListResponse, error)
|
||||
GetCustomersByIDs(ctx context.Context, ids []string) (*CustomerListResponse, error)
|
||||
GetCustomersByRole(ctx context.Context, role string) (*CustomerListResponse, error)
|
||||
GetCustomersByCompanies(ctx context.Context, companies []string) (*CustomerListResponse, error)
|
||||
SearchNotification(ctx context.Context, form *SearchCustomersForm, pagination *response.Pagination) ([]*CustomerNotificationResponse, error)
|
||||
GetCustomersByIDs(ctx context.Context, ids []string) ([]*CustomerNotificationResponse, error)
|
||||
GetCustomersByRole(ctx context.Context, role string) ([]*CustomerNotificationResponse, error)
|
||||
GetCustomersByCompanies(ctx context.Context, companies []string) ([]*CustomerNotificationResponse, error)
|
||||
Update(ctx context.Context, id string, form *UpdateCustomerForm) (*CustomerResponse, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
UpdateStatus(ctx context.Context, id string, form *UpdateStatusForm) error
|
||||
@@ -216,25 +216,40 @@ func (s *customerService) Search(ctx context.Context, form *SearchCustomersForm,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *customerService) SearchNotification(ctx context.Context, form *SearchCustomersForm, pagination *response.Pagination) ([]*CustomerNotificationResponse, error) {
|
||||
customers, _, err := s.repository.Search(ctx, form, pagination)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to search customers", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, errors.New("failed to search customers")
|
||||
}
|
||||
|
||||
var customerResponses []*CustomerNotificationResponse
|
||||
for _, customer := range customers {
|
||||
customerResponses = append(customerResponses, customer.ToNotificationResponse())
|
||||
}
|
||||
|
||||
return customerResponses, nil
|
||||
}
|
||||
|
||||
// GetCustomersByIDs retrieves customers by IDs
|
||||
func (s *customerService) GetCustomersByIDs(ctx context.Context, ids []string) (*CustomerListResponse, error) {
|
||||
func (s *customerService) GetCustomersByIDs(ctx context.Context, ids []string) ([]*CustomerNotificationResponse, error) {
|
||||
customers, err := s.repository.GetByIDs(ctx, ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var customerResponses []*CustomerResponse
|
||||
var customerResponses []*CustomerNotificationResponse
|
||||
for _, customer := range customers {
|
||||
customerResponses = append(customerResponses, customer.ToResponse(nil))
|
||||
customerResponses = append(customerResponses, customer.ToNotificationResponse())
|
||||
}
|
||||
|
||||
return &CustomerListResponse{
|
||||
Customers: customerResponses,
|
||||
}, nil
|
||||
return customerResponses, nil
|
||||
}
|
||||
|
||||
// GetCustomersByRole retrieves customers by role
|
||||
func (s *customerService) GetCustomersByRole(ctx context.Context, role string) (*CustomerListResponse, error) {
|
||||
func (s *customerService) GetCustomersByRole(ctx context.Context, role string) ([]*CustomerNotificationResponse, error) {
|
||||
customers, err := s.repository.GetByRole(ctx, CustomerRole(role))
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get customers by role", map[string]interface{}{
|
||||
@@ -244,18 +259,16 @@ func (s *customerService) GetCustomersByRole(ctx context.Context, role string) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var customerResponses []*CustomerResponse
|
||||
var customerResponses []*CustomerNotificationResponse
|
||||
for _, customer := range customers {
|
||||
customerResponses = append(customerResponses, customer.ToResponse(nil))
|
||||
customerResponses = append(customerResponses, customer.ToNotificationResponse())
|
||||
}
|
||||
|
||||
return &CustomerListResponse{
|
||||
Customers: customerResponses,
|
||||
}, nil
|
||||
return customerResponses, nil
|
||||
}
|
||||
|
||||
// GetCustomersByCompanies retrieves customers by companies
|
||||
func (s *customerService) GetCustomersByCompanies(ctx context.Context, companies []string) (*CustomerListResponse, error) {
|
||||
func (s *customerService) GetCustomersByCompanies(ctx context.Context, companies []string) ([]*CustomerNotificationResponse, error) {
|
||||
customers, err := s.repository.GetByCompanies(ctx, companies)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get customers by companies", map[string]interface{}{
|
||||
@@ -265,24 +278,12 @@ func (s *customerService) GetCustomersByCompanies(ctx context.Context, companies
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var customerResponses []*CustomerResponse
|
||||
var customerResponses []*CustomerNotificationResponse
|
||||
for _, customer := range customers {
|
||||
companies := make([]*CompanySummary, 0)
|
||||
if len(customer.Companies) > 0 {
|
||||
companies, err = s.loadCompaniesForCustomer(ctx, &customer)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to load companies for customer", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"customer_id": customer.ID,
|
||||
})
|
||||
}
|
||||
}
|
||||
customerResponses = append(customerResponses, customer.ToResponse(companies))
|
||||
customerResponses = append(customerResponses, customer.ToNotificationResponse())
|
||||
}
|
||||
|
||||
return &CustomerListResponse{
|
||||
Customers: customerResponses,
|
||||
}, nil
|
||||
return customerResponses, nil
|
||||
}
|
||||
|
||||
// Update updates a customer
|
||||
@@ -619,12 +620,7 @@ func (s *customerService) Login(ctx context.Context, form *LoginForm) (*AuthResp
|
||||
return nil, errors.New("failed to generate authentication tokens")
|
||||
}
|
||||
|
||||
// append device token to customer if not exists
|
||||
if !slices.Contains(customer.DeviceToken, form.DeviceToken) {
|
||||
customer.DeviceToken = append(customer.DeviceToken, form.DeviceToken)
|
||||
}
|
||||
|
||||
err = s.repository.Login(ctx, customer.ID.Hex())
|
||||
err = s.repository.Login(ctx, customer.ID.Hex(), form.DeviceToken)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to update customer last login", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
|
||||
Reference in New Issue
Block a user