Add Role Assignment Feature for Customers
- Introduced a new role assignment feature for customers, allowing roles to be assigned as either 'admin' or 'analyst'. - Updated the Customer entity to include a Role field, enhancing the data model to support role management. - Created new request and response forms for role assignment, ensuring proper validation and structured responses. - Implemented the AssignRole method in the customer service layer, handling role assignment logic and error management. - Added a new AssignRole handler to process role assignment requests via the API, complete with Swagger documentation for clarity. - Updated routes to include the new role assignment endpoint, improving the administrative capabilities of the system. - Enhanced logging for role assignment operations to ensure traceability and debugging ease.
This commit is contained in:
@@ -228,6 +228,43 @@ func (h *Handler) UpdateStatus(c echo.Context) error {
|
||||
}, "Customer status updated successfully")
|
||||
}
|
||||
|
||||
// AssignRole assigns a role to a customer (Web Panel)
|
||||
// @Summary Assign role to customer
|
||||
// @Description Assign a role (admin or analyst) to a customer
|
||||
// @Tags Admin-Customers
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Customer ID"
|
||||
// @Param role body AssignRoleForm true "Role assignment information"
|
||||
// @Success 200 {object} response.APIResponse{data=AssignRoleResponse} "Role assigned successfully"
|
||||
// @Failure 400 {object} response.APIResponse "Bad request - Invalid input data"
|
||||
// @Failure 404 {object} response.APIResponse "Not found - Customer not found"
|
||||
// @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}/role [patch]
|
||||
func (h *Handler) AssignRole(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
|
||||
form, err := response.Parse[AssignRoleForm](c)
|
||||
if err != nil {
|
||||
return response.ValidationError(c, "Invalid request data", err.Error())
|
||||
}
|
||||
|
||||
result, err := h.service.AssignRole(c.Request().Context(), id, form)
|
||||
if err != nil {
|
||||
if err.Error() == "customer not found" {
|
||||
return response.NotFound(c, "Customer not found")
|
||||
}
|
||||
if err.Error() == "invalid role. Must be 'admin' or 'analyst'" {
|
||||
return response.BadRequest(c, err.Error(), "")
|
||||
}
|
||||
return response.InternalServerError(c, "Failed to assign role to customer")
|
||||
}
|
||||
|
||||
return response.Success(c, result, "Role assigned successfully")
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// * PROFILE HANDLERS *
|
||||
// **************************************************
|
||||
|
||||
Reference in New Issue
Block a user