Refactor customer domain to use string IDs and integrate MongoDB ORM
- Updated Customer entity to embed MongoDB model and replace uuid.UUID with string for ID fields. - Modified repository methods to accept string IDs instead of uuid.UUID, enhancing compatibility with MongoDB. - Refactored service and handler methods to align with the new ID type, ensuring consistent usage across customer management functionality. - Improved response handling in CustomerResponse to directly use string IDs. - Streamlined index creation in the customer repository using the MongoDB ORM for better maintainability. - Added new forms for customer suspension and updated validation rules. - Enhanced Swagger documentation for customer-related endpoints to reflect changes in ID handling and request structures.
This commit is contained in:
+80
-67
@@ -1,7 +1,7 @@
|
||||
package customer
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"tm/pkg/mongo"
|
||||
)
|
||||
|
||||
// CustomerStatus represents customer account status
|
||||
@@ -25,77 +25,105 @@ const (
|
||||
|
||||
// Customer represents a customer in the tender management system
|
||||
type Customer struct {
|
||||
ID uuid.UUID `bson:"_id"`
|
||||
Type CustomerType `bson:"type"`
|
||||
Status CustomerStatus `bson:"status"`
|
||||
CompanyID *uuid.UUID `bson:"company_id,omitempty"`
|
||||
mongo.Model
|
||||
Type CustomerType `bson:"type" json:"type"`
|
||||
Status CustomerStatus `bson:"status" json:"status"`
|
||||
CompanyID *string `bson:"company_id,omitempty" json:"company_id,omitempty"`
|
||||
|
||||
// Individual customer fields
|
||||
FirstName *string `bson:"first_name,omitempty"`
|
||||
LastName *string `bson:"last_name,omitempty"`
|
||||
FullName *string `bson:"full_name,omitempty"`
|
||||
Username string `bson:"username"` // Username for authentication
|
||||
Email string `bson:"email"`
|
||||
Password string `bson:"password"` // Hashed password for authentication
|
||||
Phone *string `bson:"phone,omitempty"`
|
||||
Mobile *string `bson:"mobile,omitempty"`
|
||||
FirstName *string `bson:"first_name,omitempty" json:"first_name,omitempty"`
|
||||
LastName *string `bson:"last_name,omitempty" json:"last_name,omitempty"`
|
||||
FullName *string `bson:"full_name,omitempty" json:"full_name,omitempty"`
|
||||
Username string `bson:"username" json:"username"` // Username for authentication
|
||||
Email string `bson:"email" json:"email"`
|
||||
Password string `bson:"password" json:"-"` // Hashed password for authentication
|
||||
Phone *string `bson:"phone,omitempty" json:"phone,omitempty"`
|
||||
Mobile *string `bson:"mobile,omitempty" json:"mobile,omitempty"`
|
||||
|
||||
// Company customer fields
|
||||
CompanyName *string `bson:"company_name,omitempty"`
|
||||
RegistrationNumber *string `bson:"registration_number,omitempty"`
|
||||
TaxID *string `bson:"tax_id,omitempty"`
|
||||
Industry *string `bson:"industry,omitempty"`
|
||||
CompanyName *string `bson:"company_name,omitempty" json:"company_name,omitempty"`
|
||||
RegistrationNumber *string `bson:"registration_number,omitempty" json:"registration_number,omitempty"`
|
||||
TaxID *string `bson:"tax_id,omitempty" json:"tax_id,omitempty"`
|
||||
Industry *string `bson:"industry,omitempty" json:"industry,omitempty"`
|
||||
|
||||
// Address information
|
||||
Address *Address `bson:"address,omitempty"`
|
||||
Address *Address `bson:"address,omitempty" json:"address,omitempty"`
|
||||
|
||||
// Business information
|
||||
BusinessType *string `bson:"business_type,omitempty"`
|
||||
EmployeeCount *int `bson:"employee_count,omitempty"`
|
||||
AnnualRevenue *float64 `bson:"annual_revenue,omitempty"`
|
||||
FoundedYear *int `bson:"founded_year,omitempty"`
|
||||
BusinessType *string `bson:"business_type,omitempty" json:"business_type,omitempty"`
|
||||
EmployeeCount *int `bson:"employee_count,omitempty" json:"employee_count,omitempty"`
|
||||
AnnualRevenue *float64 `bson:"annual_revenue,omitempty" json:"annual_revenue,omitempty"`
|
||||
FoundedYear *int `bson:"founded_year,omitempty" json:"founded_year,omitempty"`
|
||||
|
||||
// Contact person (for company customers)
|
||||
ContactPerson *ContactPerson `bson:"contact_person,omitempty"`
|
||||
ContactPerson *ContactPerson `bson:"contact_person,omitempty" json:"contact_person,omitempty"`
|
||||
|
||||
// Verification and compliance
|
||||
IsVerified bool `bson:"is_verified"`
|
||||
IsCompliant bool `bson:"is_compliant"`
|
||||
ComplianceNotes *string `bson:"compliance_notes,omitempty"`
|
||||
IsVerified bool `bson:"is_verified" json:"is_verified"`
|
||||
IsCompliant bool `bson:"is_compliant" json:"is_compliant"`
|
||||
ComplianceNotes *string `bson:"compliance_notes,omitempty" json:"compliance_notes,omitempty"`
|
||||
|
||||
// Preferences and settings
|
||||
Language string `bson:"language"` // Default: "en"
|
||||
Currency string `bson:"currency"` // Default: "USD"
|
||||
Timezone string `bson:"timezone"` // Default: "UTC"
|
||||
Language string `bson:"language" json:"language"` // Default: "en"
|
||||
Currency string `bson:"currency" json:"currency"` // Default: "USD"
|
||||
Timezone string `bson:"timezone" json:"timezone"` // Default: "UTC"
|
||||
|
||||
// Audit fields
|
||||
CreatedAt int64 `bson:"created_at"` // Unix timestamp
|
||||
UpdatedAt int64 `bson:"updated_at"` // Unix timestamp
|
||||
CreatedBy *uuid.UUID `bson:"created_by,omitempty"`
|
||||
UpdatedBy *uuid.UUID `bson:"updated_by,omitempty"`
|
||||
CreatedBy *string `bson:"created_by,omitempty" json:"created_by,omitempty"`
|
||||
UpdatedBy *string `bson:"updated_by,omitempty" json:"updated_by,omitempty"`
|
||||
}
|
||||
|
||||
// SetID sets the customer ID (implements IDSetter interface)
|
||||
func (c *Customer) SetID(id string) {
|
||||
c.ID = id
|
||||
}
|
||||
|
||||
// GetID returns the customer ID (implements IDGetter interface)
|
||||
func (c *Customer) GetID() string {
|
||||
return c.ID
|
||||
}
|
||||
|
||||
// SetCreatedAt sets the created timestamp (implements Timestampable interface)
|
||||
func (c *Customer) SetCreatedAt(timestamp int64) {
|
||||
c.CreatedAt = timestamp
|
||||
}
|
||||
|
||||
// SetUpdatedAt sets the updated timestamp (implements Timestampable interface)
|
||||
func (c *Customer) SetUpdatedAt(timestamp int64) {
|
||||
c.UpdatedAt = timestamp
|
||||
}
|
||||
|
||||
// GetCreatedAt returns the created timestamp (implements Timestampable interface)
|
||||
func (c *Customer) GetCreatedAt() int64 {
|
||||
return c.CreatedAt
|
||||
}
|
||||
|
||||
// GetUpdatedAt returns the updated timestamp (implements Timestampable interface)
|
||||
func (c *Customer) GetUpdatedAt() int64 {
|
||||
return c.UpdatedAt
|
||||
}
|
||||
|
||||
// Address represents a customer's address
|
||||
type Address struct {
|
||||
Street string `bson:"street"`
|
||||
City string `bson:"city"`
|
||||
State string `bson:"state"`
|
||||
PostalCode string `bson:"postal_code"`
|
||||
Country string `bson:"country"`
|
||||
AddressType string `bson:"address_type"` // billing, shipping, etc.
|
||||
IsDefault bool `bson:"is_default"`
|
||||
Street string `bson:"street" json:"street"`
|
||||
City string `bson:"city" json:"city"`
|
||||
State string `bson:"state" json:"state"`
|
||||
PostalCode string `bson:"postal_code" json:"postal_code"`
|
||||
Country string `bson:"country" json:"country"`
|
||||
AddressType string `bson:"address_type" json:"address_type"` // billing, shipping, etc.
|
||||
IsDefault bool `bson:"is_default" json:"is_default"`
|
||||
}
|
||||
|
||||
// ContactPerson represents a contact person for company customers
|
||||
type ContactPerson struct {
|
||||
FirstName string `bson:"first_name"`
|
||||
LastName string `bson:"last_name"`
|
||||
FullName string `bson:"full_name"`
|
||||
Position string `bson:"position"`
|
||||
Email string `bson:"email"`
|
||||
Phone string `bson:"phone"`
|
||||
Mobile *string `bson:"mobile,omitempty"`
|
||||
IsPrimary bool `bson:"is_primary"`
|
||||
FirstName string `bson:"first_name" json:"first_name"`
|
||||
LastName string `bson:"last_name" json:"last_name"`
|
||||
FullName string `bson:"full_name" json:"full_name"`
|
||||
Position string `bson:"position" json:"position"`
|
||||
Email string `bson:"email" json:"email"`
|
||||
Phone string `bson:"phone" json:"phone"`
|
||||
Mobile *string `bson:"mobile,omitempty" json:"mobile,omitempty"`
|
||||
IsPrimary bool `bson:"is_primary" json:"is_primary"`
|
||||
}
|
||||
|
||||
// CustomerResponse represents the customer data sent in API responses
|
||||
@@ -135,26 +163,11 @@ type CustomerResponse struct {
|
||||
|
||||
// ToResponse converts Customer to CustomerResponse
|
||||
func (c *Customer) ToResponse() *CustomerResponse {
|
||||
companyID := ""
|
||||
if c.CompanyID != nil {
|
||||
companyID = c.CompanyID.String()
|
||||
}
|
||||
|
||||
createdBy := ""
|
||||
if c.CreatedBy != nil {
|
||||
createdBy = c.CreatedBy.String()
|
||||
}
|
||||
|
||||
updatedBy := ""
|
||||
if c.UpdatedBy != nil {
|
||||
updatedBy = c.UpdatedBy.String()
|
||||
}
|
||||
|
||||
return &CustomerResponse{
|
||||
ID: c.ID.String(),
|
||||
ID: c.ID,
|
||||
Type: string(c.Type),
|
||||
Status: string(c.Status),
|
||||
CompanyID: &companyID,
|
||||
CompanyID: c.CompanyID,
|
||||
FirstName: c.FirstName,
|
||||
LastName: c.LastName,
|
||||
FullName: c.FullName,
|
||||
@@ -180,7 +193,7 @@ func (c *Customer) ToResponse() *CustomerResponse {
|
||||
Timezone: c.Timezone,
|
||||
CreatedAt: c.CreatedAt,
|
||||
UpdatedAt: c.UpdatedAt,
|
||||
CreatedBy: &createdBy,
|
||||
UpdatedBy: &updatedBy,
|
||||
CreatedBy: c.CreatedBy,
|
||||
UpdatedBy: c.UpdatedBy,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user