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:
@@ -42,6 +42,9 @@ type Service interface {
|
||||
RequestResetPassword(ctx context.Context, form *RequestResetPasswordForm) (*RequestResetPasswordResponse, error)
|
||||
VerifyOTP(ctx context.Context, form *VerifyOTPForm) (*VerifyOTPResponse, error)
|
||||
ResetPassword(ctx context.Context, form *ResetPasswordForm) (*ResetPasswordResponse, error)
|
||||
|
||||
// Role assignment operations
|
||||
AssignRole(ctx context.Context, customerID string, form *AssignRoleForm) (*AssignRoleResponse, error)
|
||||
}
|
||||
|
||||
// customerService implements the Service interface
|
||||
@@ -102,6 +105,7 @@ func (s *customerService) Register(ctx context.Context, form *CreateCustomerForm
|
||||
// Create customer
|
||||
customer := &Customer{
|
||||
Type: CustomerType(form.Type),
|
||||
Role: CustomerRole(form.Role),
|
||||
Status: CustomerStatusActive,
|
||||
CompanyIDs: companyIDs,
|
||||
FullName: form.FullName,
|
||||
@@ -249,6 +253,11 @@ func (s *customerService) Update(ctx context.Context, id string, form *UpdateCus
|
||||
customer.Phone = form.Phone
|
||||
}
|
||||
|
||||
// Update role if provided
|
||||
if form.Role != "" {
|
||||
customer.Role = CustomerRole(form.Role)
|
||||
}
|
||||
|
||||
// Update in database
|
||||
err = s.repository.Update(ctx, customer)
|
||||
if err != nil {
|
||||
@@ -509,7 +518,7 @@ func (s *customerService) Login(ctx context.Context, form *LoginForm) (*AuthResp
|
||||
tokenPair, err := s.authService.GenerateTokenPair(
|
||||
customer.ID.Hex(),
|
||||
customer.Email,
|
||||
"customer", // Role for customers
|
||||
string(customer.Role), // Use customer's actual role
|
||||
companyID,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -1055,3 +1064,55 @@ func (s *customerService) loadCompaniesForCustomer(ctx context.Context, customer
|
||||
|
||||
return companySummaries, nil
|
||||
}
|
||||
|
||||
// AssignRole assigns a role to a customer
|
||||
func (s *customerService) AssignRole(ctx context.Context, customerID string, form *AssignRoleForm) (*AssignRoleResponse, error) {
|
||||
s.logger.Info("Assigning role to customer", map[string]interface{}{
|
||||
"customer_id": customerID,
|
||||
"role": form.Role,
|
||||
})
|
||||
|
||||
// Get customer to check if exists
|
||||
customer, err := s.repository.GetByID(ctx, customerID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get customer for role assignment", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"customer_id": customerID,
|
||||
})
|
||||
return nil, errors.New("customer not found")
|
||||
}
|
||||
|
||||
// Validate role
|
||||
role := CustomerRole(form.Role)
|
||||
if role != CustomerRoleAdmin && role != CustomerRoleAnalyst {
|
||||
s.logger.Error("Invalid role provided", map[string]interface{}{
|
||||
"customer_id": customerID,
|
||||
"role": form.Role,
|
||||
})
|
||||
return nil, errors.New("invalid role. Must be 'admin' or 'analyst'")
|
||||
}
|
||||
|
||||
// Update customer role
|
||||
customer.Role = role
|
||||
customer.UpdatedAt = time.Now().Unix()
|
||||
|
||||
err = s.repository.Update(ctx, customer)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to update customer role", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"customer_id": customerID,
|
||||
"role": form.Role,
|
||||
})
|
||||
return nil, errors.New("failed to assign role to customer")
|
||||
}
|
||||
|
||||
s.logger.Info("Role assigned to customer successfully", map[string]interface{}{
|
||||
"customer_id": customerID,
|
||||
"role": form.Role,
|
||||
})
|
||||
|
||||
return &AssignRoleResponse{
|
||||
Message: "Role assigned successfully",
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user