Add Assign Companies Feature for Customers

- Introduced a new endpoint to assign companies to a customer, enhancing the customer management capabilities.
- Implemented the AssignCompanies handler in the customer service layer, including validation and structured responses.
- Updated the routes to include the new PATCH /admin/v1/customers/{id}/companies endpoint, improving administrative functionalities.
- Enhanced API documentation with Swagger comments for the new endpoint, ensuring clarity for API consumers.
This commit is contained in:
n.nakhostin
2025-09-20 10:35:28 +03:30
parent 6dcee0bc4b
commit c6801ef2f3
3 changed files with 36 additions and 2 deletions
+1
View File
@@ -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
+31
View File
@@ -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 *
// **************************************************
+4 -2
View File
@@ -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,
})