diff --git a/internal/customer/service.go b/internal/customer/service.go index e2d0280..910aa97 100644 --- a/internal/customer/service.go +++ b/internal/customer/service.go @@ -130,6 +130,13 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm }) 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{}{ "customer_id": customer.ID, @@ -147,7 +154,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm UserID: customer.ID.Hex(), }) - return customer.ToResponse(nil), nil + return customer.ToResponse(companies), nil } // GetByID retrieves a customer by ID @@ -260,7 +267,17 @@ func (s *customerService) GetCustomersByCompanies(ctx context.Context, companies var customerResponses []*CustomerResponse 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{ @@ -299,21 +316,15 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus } // Handle company assignments + companies := make([]*CompanySummary, 0) if len(form.Companies) > 0 { - // Verify that all company IDs exist - var validCompanies []string - for _, companyID := range form.Companies { - _, err := s.companyService.GetByID(ctx, companyID) - if err != nil { - 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) + 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, + }) } - customer.Companies = validCompanies } if form.FullName != nil { @@ -344,7 +355,7 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus "email": customer.Email, }) - return customer.ToResponse(nil), nil + return customer.ToResponse(companies), nil } // Delete deletes a customer (soft delete) @@ -718,29 +729,6 @@ func (s *customerService) RefreshToken(ctx context.Context, refreshToken string) }, 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 func (s *customerService) GetProfileWithCompanies(ctx context.Context, customerID string) (*CustomerResponse, error) { s.logger.Info("Getting customer profile with companies", map[string]interface{}{