From 3b265e567d6bc2d5110d7d01620ce687ae91e0f6 Mon Sep 17 00:00:00 2001 From: "n.nakhostin" Date: Mon, 8 Sep 2025 13:08:29 +0330 Subject: [PATCH] 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. --- internal/customer/entity.go | 42 ------------------------------------- internal/customer/form.go | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/internal/customer/entity.go b/internal/customer/entity.go index 1b30cb8..3acf755 100644 --- a/internal/customer/entity.go +++ b/internal/customer/entity.go @@ -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, - } -} diff --git a/internal/customer/form.go b/internal/customer/form.go index 78b4a67..99053a2 100644 --- a/internal/customer/form.go +++ b/internal/customer/form.go @@ -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"`