diff --git a/cmd/web/router/routes.go b/cmd/web/router/routes.go index a274432..4ed6820 100644 --- a/cmd/web/router/routes.go +++ b/cmd/web/router/routes.go @@ -82,6 +82,7 @@ func RegisterAdminRoutes(e *echo.Echo, userHandler *user.Handler, companyHandler customersGP.DELETE("/:id", customerHandler.DeleteCustomer) customersGP.PUT("/:id/status", customerHandler.UpdateStatus) customersGP.PATCH("/:id/role", customerHandler.AssignRole) + customersGP.PATCH("/:id/companies", customerHandler.AssignCompanies) } // Admin Tenders Routes diff --git a/internal/customer/handler.go b/internal/customer/handler.go index 9d3c8d2..da26268 100644 --- a/internal/customer/handler.go +++ b/internal/customer/handler.go @@ -265,6 +265,37 @@ func (h *Handler) AssignRole(c echo.Context) error { return response.Success(c, result, "Role assigned successfully") } +// AssignCompanies assigns companies to a customer +// @Summary Assign companies to a customer +// @Description Assign companies to a customer +// @Tags Admin-Customers +// @Accept json +// @Produce json +// @Param id path string true "Customer ID" +// @Param companies body AssignCompaniesForm true "Companies to assign" +// @Success 200 {object} response.APIResponse "Companies assigned successfully" +// @Failure 400 {object} response.APIResponse "Bad request - Invalid input data" +// @Failure 422 {object} response.APIResponse "Validation error - Invalid request data" +// @Failure 500 {object} response.APIResponse "Internal server error" +// @Security BearerAuth +// @Router /admin/v1/customers/{id}/companies [patch] +func (h *Handler) AssignCompanies(c echo.Context) error { + id := c.Param("id") + form, err := response.Parse[AssignCompaniesForm](c) + if err != nil { + return response.ValidationError(c, "Invalid request data", err.Error()) + } + + err = h.service.AssignCompanies(c.Request().Context(), id, form.Companies) + if err != nil { + return response.InternalServerError(c, "Failed to assign companies to customer") + } + + return response.Success(c, map[string]interface{}{ + "message": "Companies assigned successfully", + }, "Companies assigned successfully") +} + // ************************************************** // * PROFILE HANDLERS * // ************************************************** diff --git a/internal/customer/service.go b/internal/customer/service.go index 0109ab1..ca3af1a 100644 --- a/internal/customer/service.go +++ b/internal/customer/service.go @@ -46,6 +46,7 @@ type Service interface { // Role assignment operations AssignRole(ctx context.Context, customerID string, form *AssignRoleForm) (*AssignRoleResponse, error) + AssignCompanies(ctx context.Context, customerID string, Companies []string) error } // customerService implements the Service interface @@ -360,6 +361,7 @@ func (s *customerService) AssignCompanies(ctx context.Context, customerID string } // Update customer's company IDs + customer.Companies = make([]string, 0) customer.Companies = append(customer.Companies, validCompanies...) customer.UpdatedAt = time.Now().Unix() @@ -380,7 +382,7 @@ func (s *customerService) AssignCompanies(ctx context.Context, customerID string return nil } -// RemoveCompaniesFromCustomer removes companies from a customer +// RemoveCompanies removes companies from a customer func (s *customerService) RemoveCompanies(ctx context.Context, customerID string, Companies []string) error { // Get customer to check if exists customer, err := s.repository.GetByID(ctx, customerID) @@ -409,7 +411,7 @@ func (s *customerService) RemoveCompanies(ctx context.Context, customerID string err = s.repository.Update(ctx, customer) if err != nil { - s.logger.Error("Failed to update customer after removing company assignments", map[string]interface{}{ + s.logger.Error("Failed to update customer after removing companies", map[string]interface{}{ "error": err.Error(), "customer_id": customerID, })