Files
tm_back/internal/customer/form.go
T

223 lines
8.5 KiB
Go

package customer
import "tm/pkg/response"
// DeviceTokenForm represents the form for adding a device token to a customer
type DeviceTokenForm struct {
DeviceToken string `json:"device_token" valid:"required" example:"device_token"`
}
// 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"`
Companies []string `json:"companies,omitempty" valid:"optional"`
Username string `json:"username" valid:"required,username,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"`
Companies []string `json:"companies,omitempty" valid:"optional"`
Username string `json:"username" valid:"required,username,length(3|30)" example:"user"`
Email string `json:"email" valid:"required,email" example:"user@opplens.com"`
Phone *string `json:"phone,omitempty" valid:"optional,length(10|20)" example:"+1234567890"`
Keywords []string `json:"keywords,omitempty" valid:"optional"`
}
// 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"`
}
// AdminResetPasswordResponse is returned when an admin resets a customer's password.
type AdminResetPasswordResponse struct {
Password string `json:"password"`
}
// 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"`
UpdatedAt int64 `json:"updated_at"`
CreatedAt int64 `json:"created_at"`
LastLoginAt *int64 `json:"last_login_at"`
Companies []*CompanySummary `json:"companies"`
Keywords []string `json:"keywords"`
KeywordsStatus string `json:"keywords_status"`
DeviceToken []string `json:"-"`
}
// ToResponse converts Customer to CustomerResponse
func (c *Customer) ToResponse(companies []*CompanySummary) *CustomerResponse {
keywords := c.Keywords
if keywords == nil {
keywords = []string{}
}
keywordsStatus := string(c.KeywordsStatus)
if keywordsStatus == "" {
keywordsStatus = string(KeywordsStatusReady)
}
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,
UpdatedAt: c.UpdatedAt,
CreatedAt: c.CreatedAt,
LastLoginAt: c.LastLoginAt,
Companies: companies,
Keywords: keywords,
KeywordsStatus: keywordsStatus,
}
}
type CustomerNotificationResponse struct {
ID string `json:"id"`
FullName *string `json:"full_name"`
Username string `json:"username"`
Email string `json:"email"`
Type string `json:"type"`
Role string `json:"role"`
Phone *string `json:"phone"`
DeviceToken []string `json:"device_token"`
}
func (c *Customer) ToNotificationResponse() *CustomerNotificationResponse {
return &CustomerNotificationResponse{
ID: c.ID.Hex(),
FullName: c.FullName,
Username: c.Username,
Email: c.Email,
Type: string(c.Type),
Role: string(c.Role),
Phone: c.Phone,
DeviceToken: c.DeviceToken,
}
}
// 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"`
DeviceToken string `json:"device_token" valid:"optional" example:"device_token"`
}
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,username,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 {
Companies []string `json:"companies" valid:"required"`
}
type RemoveCompaniesForm struct {
Companies []string `json:"companies" 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"`
}
// UpdateProfileKeywordsForm represents the form for updating profile keywords
type UpdateProfileKeywordsForm struct {
Keywords []string `json:"keywords" valid:"required"`
}
// 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"`
}