Refactor company domain by removing customer-related fields and methods

- Removed OwnerCustomerID from Company entity and CompanyResponse to streamline ownership representation.
- Eliminated customer assignment fields from CreateCompanyForm and ListCompaniesForm for clarity.
- Updated repository and service methods to remove GetByCustomerID and CountWithCustomer, simplifying the codebase.
- Adjusted Search method parameters to exclude hasCustomer filter, enhancing search functionality.
- Improved API documentation by removing references to customer assignment in handler comments.
This commit is contained in:
n.nakhostin
2025-08-11 18:31:15 +03:30
parent 3e4831c2e7
commit f5407abbf0
5 changed files with 23 additions and 107 deletions
+10 -59
View File
@@ -17,17 +17,15 @@ type Repository interface {
GetByName(ctx context.Context, name string) (*Company, error)
GetByRegistrationNumber(ctx context.Context, registrationNumber string) (*Company, error)
GetByTaxID(ctx context.Context, taxID string) (*Company, error)
GetByCustomerID(ctx context.Context, customerID string) (*Company, error)
Update(ctx context.Context, company *Company) error
Delete(ctx context.Context, id string) error
List(ctx context.Context, limit, offset int) ([]*Company, error)
Search(ctx context.Context, search string, companyType *string, status *string, industry *string, isVerified *bool, isCompliant *bool, language *string, currency *string, hasCustomer *bool, cpvCodes []string, categories []string, keywords []string, specializations []string, employeeCountMin *int, employeeCountMax *int, annualRevenueMin *float64, annualRevenueMax *float64, foundedYearMin *int, foundedYearMax *int, limit, offset int, sortBy, sortOrder string) ([]*Company, error)
Search(ctx context.Context, search string, companyType *string, status *string, industry *string, isVerified *bool, isCompliant *bool, language *string, currency *string, cpvCodes []string, categories []string, keywords []string, specializations []string, employeeCountMin *int, employeeCountMax *int, annualRevenueMin *float64, annualRevenueMax *float64, foundedYearMin *int, foundedYearMax *int, limit, offset int, sortBy, sortOrder string) ([]*Company, error)
Count(ctx context.Context) (int64, error)
CountByType(ctx context.Context, companyType CompanyType) (int64, error)
CountByStatus(ctx context.Context, status CompanyStatus) (int64, error)
CountByIndustry(ctx context.Context, industry string) (int64, error)
CountVerified(ctx context.Context) (int64, error)
CountWithCustomer(ctx context.Context) (int64, error)
UpdateStatus(ctx context.Context, id string, status CompanyStatus) error
UpdateVerification(ctx context.Context, id string, isVerified, isCompliant bool, complianceNotes *string) error
UpdateTags(ctx context.Context, id string, tags *CompanyTags) error
@@ -50,7 +48,6 @@ func NewCompanyRepository(mongoManager *mongopkg.ConnectionManager, logger logge
*mongopkg.CreateUniqueIndex("name_idx", bson.D{{Key: "name", Value: 1}}),
*mongopkg.CreateUniqueIndex("registration_number_idx", bson.D{{Key: "registration_number", Value: 1}}),
*mongopkg.CreateUniqueIndex("tax_id_idx", bson.D{{Key: "tax_id", Value: 1}}),
*mongopkg.NewIndex("customer_id_idx", bson.D{{Key: "customer_id", Value: 1}}),
*mongopkg.NewIndex("type_idx", bson.D{{Key: "type", Value: 1}}),
*mongopkg.NewIndex("status_idx", bson.D{{Key: "status", Value: 1}}),
*mongopkg.NewIndex("industry_idx", bson.D{{Key: "industry", Value: 1}}),
@@ -187,24 +184,6 @@ func (r *companyRepository) GetByTaxID(ctx context.Context, taxID string) (*Comp
return company, nil
}
// GetByCustomerID retrieves a company by customer ID
func (r *companyRepository) GetByCustomerID(ctx context.Context, customerID string) (*Company, error) {
filter := bson.M{"customer_id": customerID}
company, err := r.ormRepo.FindOne(ctx, filter)
if err != nil {
if errors.Is(err, mongopkg.ErrDocumentNotFound) {
return nil, errors.New("company not found")
}
r.logger.Error("Failed to get company by customer ID", map[string]interface{}{
"error": err.Error(),
"customer_id": customerID,
})
return nil, err
}
return company, nil
}
// Update updates a company
func (r *companyRepository) Update(ctx context.Context, company *Company) error {
// Set updated timestamp using Unix timestamp
@@ -290,7 +269,7 @@ func (r *companyRepository) List(ctx context.Context, limit, offset int) ([]*Com
}
// Search retrieves companies with search and filters
func (r *companyRepository) Search(ctx context.Context, search string, companyType *string, status *string, industry *string, isVerified *bool, isCompliant *bool, language *string, currency *string, hasCustomer *bool, cpvCodes []string, categories []string, keywords []string, specializations []string, employeeCountMin *int, employeeCountMax *int, annualRevenueMin *float64, annualRevenueMax *float64, foundedYearMin *int, foundedYearMax *int, limit, offset int, sortBy, sortOrder string) ([]*Company, error) {
func (r *companyRepository) Search(ctx context.Context, search string, companyType *string, status *string, industry *string, isVerified *bool, isCompliant *bool, language *string, currency *string, cpvCodes []string, categories []string, keywords []string, specializations []string, employeeCountMin *int, employeeCountMax *int, annualRevenueMin *float64, annualRevenueMax *float64, foundedYearMin *int, foundedYearMax *int, limit, offset int, sortBy, sortOrder string) ([]*Company, error) {
// Build filter
filter := bson.M{}
@@ -326,14 +305,6 @@ func (r *companyRepository) Search(ctx context.Context, search string, companyTy
filter["currency"] = *currency
}
if hasCustomer != nil {
if *hasCustomer {
filter["customer_id"] = bson.M{"$ne": nil}
} else {
filter["customer_id"] = nil
}
}
// Tag filters
if len(cpvCodes) > 0 {
filter["tags.cpv_codes"] = bson.M{"$in": cpvCodes}
@@ -507,24 +478,6 @@ func (r *companyRepository) CountVerified(ctx context.Context) (int64, error) {
return count, nil
}
// CountWithCustomer counts companies with assigned customers
func (r *companyRepository) CountWithCustomer(ctx context.Context) (int64, error) {
filter := bson.M{
"customer_id": bson.M{"$ne": nil},
"status": bson.M{"$ne": CompanyStatusInactive},
}
count, err := r.ormRepo.Count(ctx, filter)
if err != nil {
r.logger.Error("Failed to count companies with customer", map[string]interface{}{
"error": err.Error(),
})
return 0, err
}
return count, nil
}
// UpdateStatus updates company status
func (r *companyRepository) UpdateStatus(ctx context.Context, id string, status CompanyStatus) error {
// Get company first
@@ -704,20 +657,18 @@ func (r *companyRepository) GetCompanyStats(ctx context.Context) (*CompanyStatsR
totalCompanies, _ := r.Count(ctx)
activeCompanies, _ := r.CountByStatus(ctx, CompanyStatusActive)
verifiedCompanies, _ := r.CountVerified(ctx)
companiesWithCustomer, _ := r.CountWithCustomer(ctx)
// For this implementation, we'll return basic stats
// In a real implementation, you'd use MongoDB aggregation pipelines
stats := &CompanyStatsResponse{
TotalCompanies: totalCompanies,
ActiveCompanies: activeCompanies,
VerifiedCompanies: verifiedCompanies,
CompaniesWithCustomer: companiesWithCustomer,
CompaniesByType: make(map[string]int64),
CompaniesByIndustry: make(map[string]int64),
CompaniesByStatus: make(map[string]int64),
TopCategories: []CategoryCount{},
TopCPVCodes: []CPVCodeCount{},
TotalCompanies: totalCompanies,
ActiveCompanies: activeCompanies,
VerifiedCompanies: verifiedCompanies,
CompaniesByType: make(map[string]int64),
CompaniesByIndustry: make(map[string]int64),
CompaniesByStatus: make(map[string]int64),
TopCategories: []CategoryCount{},
TopCPVCodes: []CPVCodeCount{},
}
return stats, nil