Files
tm_back/internal/customer/form.go
T
n.nakhostin b429bcd24f 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.
2025-09-17 10:15:14 +03:30

172 lines
6.8 KiB
Go

package customer
import "tm/pkg/response"
// CreateCustomerForm represents the form for creating a new customer
type CreateCustomerForm struct {
FullName *string `json:"full_name,omitempty" valid:"optional,length(2|100)" example:"User"`
Type string `json:"type" valid:"required,in(individual|company|government)" example:"individual"`
Role string `json:"role" valid:"required,in(admin|analyst)" example:"analyst"`
CompanyIDs []string `json:"company_ids,omitempty" valid:"optional"`
Username string `json:"username" valid:"required,alphanum,length(3|30)" example:"user"`
Email string `json:"email" valid:"required,email" example:"user@opplens.com"`
Password string `json:"password" valid:"required,length(8|128)" example:"User!1234"`
Phone *string `json:"phone,omitempty" valid:"optional,length(10|20)" example:"+1234567890"`
}
// UpdateCustomerForm represents the form for updating a customer
type UpdateCustomerForm struct {
FullName *string `json:"full_name,omitempty" valid:"optional,length(2|100)" example:"User"`
Type string `json:"type" valid:"required,in(individual|company|government)" example:"individual"`
Role string `json:"role" valid:"required,in(admin|analyst)" example:"analyst"`
CompanyIDs []string `json:"company_ids,omitempty" valid:"optional"`
Username string `json:"username" valid:"required,alphanum,length(3|30)" example:"user"`
Email string `json:"email" valid:"required,email" example:"user@opplens.com"`
Password string `json:"password" valid:"required,length(8|128)" example:"User!1234"`
Phone *string `json:"phone,omitempty" valid:"optional,length(10|20)" example:"+1234567890"`
}
// CustomerStatusForm represents the form for suspending a customer
type UpdateStatusForm struct {
Status string `json:"status" valid:"required,in(active|inactive|suspended)" example:"active"`
Reason string `json:"reason" valid:"required,length(10|500)" example:"Customer is active"`
}
// ListCustomersForm represents the form for listing customers with filters
type SearchCustomersForm struct {
Search *string `query:"q" valid:"optional"`
Type *string `query:"type" valid:"optional,in(individual|company|government)"`
Status *string `query:"status" valid:"optional,in(active|inactive|suspended)"`
CompanyID *string `query:"company_id" valid:"optional"`
}
// CompanySummary represents company summary data for customer responses
type CompanySummary struct {
ID string `json:"id"`
Name string `json:"name"`
}
// CustomerResponse represents the customer data sent in API responses
type CustomerResponse struct {
ID string `json:"id"`
FullName *string `json:"full_name"`
Username string `json:"username"`
Email string `json:"email"`
Status string `json:"status"`
Type string `json:"type"`
Role string `json:"role"`
Phone *string `json:"phone"`
CompanyIDs []string `json:"company_ids"`
UpdatedAt int64 `json:"updated_at"`
CreatedAt int64 `json:"created_at"`
LastLoginAt *int64 `json:"last_login_at"`
Companies []*CompanySummary `json:"companies"`
}
// ToResponse converts Customer to CustomerResponse
func (c *Customer) ToResponse(companies []*CompanySummary) *CustomerResponse {
return &CustomerResponse{
ID: c.ID.Hex(),
FullName: c.FullName,
Username: c.Username,
Email: c.Email,
Type: string(c.Type),
Status: string(c.Status),
Role: string(c.Role),
Phone: c.Phone,
CompanyIDs: c.CompanyIDs,
UpdatedAt: c.UpdatedAt,
CreatedAt: c.CreatedAt,
LastLoginAt: c.LastLoginAt,
Companies: companies,
}
}
// CustomerListResponse represents the response for listing customers
type CustomerListResponse struct {
Customers []*CustomerResponse `json:"customers"`
Meta *response.Meta `json:"-"`
}
type LoginForm struct {
Username string `json:"username" valid:"required" example:"app"`
Password string `json:"password" valid:"required" example:"App!1234"`
}
type RefreshTokenForm struct {
RefreshToken string `json:"refresh_token" valid:"required"`
}
type ChangePasswordForm struct {
OldPassword string `json:"old_password" valid:"required"`
NewPassword string `json:"new_password" valid:"required,length(8|128)"`
}
type UpdateProfileForm struct {
FullName *string `json:"full_name,omitempty" valid:"optional,length(2|100)"`
Username *string `json:"username,omitempty" valid:"optional,alphanum,length(3|30)"`
Phone *string `json:"phone,omitempty" valid:"optional,length(10|20)"`
}
type AuthResponse struct {
Customer *CustomerResponse `json:"customer"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresAt int64 `json:"expires_at"`
}
type AssignCompaniesForm struct {
CompanyIDs []string `json:"company_ids" valid:"required"`
}
type RemoveCompaniesForm struct {
CompanyIDs []string `json:"company_ids" valid:"required"`
}
// RequestResetPasswordForm represents the form for requesting password reset
type RequestResetPasswordForm struct {
Email string `json:"email" valid:"required,email" example:"app@opplens.com"`
}
// VerifyOTPForm represents the form for verifying OTP code
type VerifyOTPForm struct {
Email string `json:"email" valid:"required,email" example:"app@opplens.com"`
Code string `json:"code" valid:"required,length(6|6)" example:"123456"`
}
// ResetPasswordForm represents the form for resetting password
type ResetPasswordForm struct {
Token string `json:"token" valid:"required" example:"reset_token_here"`
NewPassword string `json:"new_password" valid:"required,length(5|128)" example:"NewPass!123"`
}
// RequestResetPasswordResponse represents the response for password reset request
type RequestResetPasswordResponse struct {
Message string `json:"message" example:"Password reset code sent to your email"`
Success bool `json:"success" example:"true"`
}
// VerifyOTPResponse represents the response for OTP verification
type VerifyOTPResponse struct {
Message string `json:"message" example:"OTP verified successfully"`
Token string `json:"token" example:"reset_token_here"`
Success bool `json:"success" example:"true"`
}
// ResetPasswordResponse represents the response for password reset
type ResetPasswordResponse struct {
Message string `json:"message" example:"Password reset successfully"`
Success bool `json:"success" example:"true"`
}
// AssignRoleForm represents the form for assigning a role to a customer
type AssignRoleForm struct {
Role string `json:"role" valid:"required,in(admin|analyst)" example:"analyst"`
}
// AssignRoleResponse represents the response for role assignment
type AssignRoleResponse struct {
Message string `json:"message" example:"Role assigned successfully"`
Success bool `json:"success" example:"true"`
}