Refactor Customer Entity and Response Structures

- Moved CompanySummary and CustomerResponse structures from entity.go to form.go, improving organization and clarity of customer-related data representations.
- Updated the ToResponse method to maintain functionality while aligning with the new structure, ensuring consistent API responses for customer data.
- This refactor enhances the maintainability of the codebase by adhering to clean architecture principles and improving the separation of concerns.
This commit is contained in:
n.nakhostin
2025-09-08 13:08:29 +03:30
parent 8670b3272d
commit 3b265e567d
2 changed files with 42 additions and 42 deletions
-42
View File
@@ -70,45 +70,3 @@ func (c *Customer) GetCreatedAt() int64 {
func (c *Customer) GetUpdatedAt() int64 {
return c.UpdatedAt
}
// 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 `bson:"id"`
FullName *string `bson:"full_name"`
Username string `bson:"username"`
Email string `bson:"email"`
Password string `bson:"password"`
Status string `bson:"status"`
Type string `bson:"type"`
Phone *string `bson:"phone"`
CompanyIDs []string `bson:"company_ids"`
UpdatedAt int64 `bson:"updated_at"`
CreatedAt int64 `bson:"created_at"`
LastLoginAt *int64 `bson:"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,
Password: c.Password,
Type: string(c.Type),
Status: string(c.Status),
Phone: c.Phone,
CompanyIDs: c.CompanyIDs,
UpdatedAt: c.UpdatedAt,
CreatedAt: c.CreatedAt,
LastLoginAt: c.LastLoginAt,
Companies: companies,
}
}
+42
View File
@@ -38,6 +38,48 @@ type SearchCustomersForm struct {
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 `bson:"id"`
FullName *string `bson:"full_name"`
Username string `bson:"username"`
Email string `bson:"email"`
Password string `bson:"password"`
Status string `bson:"status"`
Type string `bson:"type"`
Phone *string `bson:"phone"`
CompanyIDs []string `bson:"company_ids"`
UpdatedAt int64 `bson:"updated_at"`
CreatedAt int64 `bson:"created_at"`
LastLoginAt *int64 `bson:"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,
Password: c.Password,
Type: string(c.Type),
Status: string(c.Status),
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"`