Enhance Customer Service with Company Loading Logic
- Added functionality to load associated companies for customers during registration and updates, improving the completeness of customer responses. - Implemented error logging for failures in loading companies, ensuring better traceability of issues. - Updated the response structure to include company information in customer responses, enhancing the API's usability and data richness. - Removed the unused GetProfile method to streamline the service layer and improve code maintainability.
This commit is contained in:
@@ -130,6 +130,13 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
|||||||
})
|
})
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
s.logger.Info("Customer created successfully", map[string]interface{}{
|
s.logger.Info("Customer created successfully", map[string]interface{}{
|
||||||
"customer_id": customer.ID,
|
"customer_id": customer.ID,
|
||||||
@@ -147,7 +154,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
|||||||
UserID: customer.ID.Hex(),
|
UserID: customer.ID.Hex(),
|
||||||
})
|
})
|
||||||
|
|
||||||
return customer.ToResponse(nil), nil
|
return customer.ToResponse(companies), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByID retrieves a customer by ID
|
// GetByID retrieves a customer by ID
|
||||||
@@ -260,7 +267,17 @@ func (s *customerService) GetCustomersByCompanies(ctx context.Context, companies
|
|||||||
|
|
||||||
var customerResponses []*CustomerResponse
|
var customerResponses []*CustomerResponse
|
||||||
for _, customer := range customers {
|
for _, customer := range customers {
|
||||||
customerResponses = append(customerResponses, customer.ToResponse(nil))
|
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &CustomerListResponse{
|
return &CustomerListResponse{
|
||||||
@@ -299,21 +316,15 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle company assignments
|
// Handle company assignments
|
||||||
|
companies := make([]*CompanySummary, 0)
|
||||||
if len(form.Companies) > 0 {
|
if len(form.Companies) > 0 {
|
||||||
// Verify that all company IDs exist
|
companies, err = s.loadCompaniesForCustomer(ctx, customer)
|
||||||
var validCompanies []string
|
if err != nil {
|
||||||
for _, companyID := range form.Companies {
|
s.logger.Error("Failed to load companies for customer", map[string]interface{}{
|
||||||
_, err := s.companyService.GetByID(ctx, companyID)
|
"error": err.Error(),
|
||||||
if err != nil {
|
"customer_id": customer.ID,
|
||||||
s.logger.Error("Invalid company ID provided in update", map[string]interface{}{
|
})
|
||||||
"error": err.Error(),
|
|
||||||
"company_id": companyID,
|
|
||||||
})
|
|
||||||
return nil, errors.New("invalid company ID: " + companyID)
|
|
||||||
}
|
|
||||||
validCompanies = append(validCompanies, companyID)
|
|
||||||
}
|
}
|
||||||
customer.Companies = validCompanies
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if form.FullName != nil {
|
if form.FullName != nil {
|
||||||
@@ -344,7 +355,7 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus
|
|||||||
"email": customer.Email,
|
"email": customer.Email,
|
||||||
})
|
})
|
||||||
|
|
||||||
return customer.ToResponse(nil), nil
|
return customer.ToResponse(companies), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes a customer (soft delete)
|
// Delete deletes a customer (soft delete)
|
||||||
@@ -718,29 +729,6 @@ func (s *customerService) RefreshToken(ctx context.Context, refreshToken string)
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetProfile retrieves customer profile information
|
|
||||||
func (s *customerService) GetProfile(ctx context.Context, customerID string) (*Customer, error) {
|
|
||||||
s.logger.Info("Getting customer profile", map[string]interface{}{
|
|
||||||
"customer_id": customerID,
|
|
||||||
})
|
|
||||||
|
|
||||||
customer, err := s.repository.GetByID(ctx, customerID)
|
|
||||||
if err != nil {
|
|
||||||
s.logger.Error("Failed to get customer profile", map[string]interface{}{
|
|
||||||
"error": err.Error(),
|
|
||||||
"customer_id": customerID,
|
|
||||||
})
|
|
||||||
return nil, errors.New("customer not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
s.logger.Info("Customer profile retrieved successfully", map[string]interface{}{
|
|
||||||
"customer_id": customerID,
|
|
||||||
"customer_type": string(customer.Type),
|
|
||||||
})
|
|
||||||
|
|
||||||
return customer, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetProfileWithCompanies retrieves customer profile information including companies
|
// GetProfileWithCompanies retrieves customer profile information including companies
|
||||||
func (s *customerService) GetProfileWithCompanies(ctx context.Context, customerID string) (*CustomerResponse, error) {
|
func (s *customerService) GetProfileWithCompanies(ctx context.Context, customerID string) (*CustomerResponse, error) {
|
||||||
s.logger.Info("Getting customer profile with companies", map[string]interface{}{
|
s.logger.Info("Getting customer profile with companies", map[string]interface{}{
|
||||||
|
|||||||
Reference in New Issue
Block a user