Add company selection handling in customer forms with unit tests
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Introduced new functionality in `form_companies.go` to handle company selection from various input formats, including legacy `company_ids` and company summary objects. - Implemented `UnmarshalJSON` methods for `CreateCustomerForm`, `UpdateCustomerForm`, and `AssignCompaniesForm` to support flexible company data parsing. - Added unit tests in `form_companies_test.go` to validate the correct unmarshalling of company IDs and summaries, ensuring robust handling of different input scenarios. - Enhanced the `GetByCompanies` method in the customer repository to directly use string company IDs, improving data retrieval efficiency. This update significantly improves the handling of company data in customer forms, ensuring accurate processing and validation of company selections, while expanding test coverage for these functionalities.
This commit is contained in:
@@ -249,16 +249,11 @@ func (r *customerRepository) GetByRole(ctx context.Context, role CustomerRole) (
|
||||
// GetByCompanies retrieves customers by companies
|
||||
|
||||
func (r *customerRepository) GetByCompanies(ctx context.Context, companies []string) ([]Customer, error) {
|
||||
companyIDs := make([]bson.ObjectID, 0)
|
||||
for _, company := range companies {
|
||||
id, err := bson.ObjectIDFromHex(company)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
companyIDs = append(companyIDs, id)
|
||||
if len(companies) == 0 {
|
||||
return []Customer{}, nil
|
||||
}
|
||||
|
||||
customers, err := r.ormRepo.FindAll(ctx, bson.M{"companies": bson.M{"$in": companyIDs}}, orm.NewPaginationBuilder().Build())
|
||||
customers, err := r.ormRepo.FindAll(ctx, bson.M{"companies": bson.M{"$in": companies}}, orm.NewPaginationBuilder().Build())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -274,10 +269,10 @@ func (r *customerRepository) GetByCompanyID(ctx context.Context, companyID strin
|
||||
SortBy(pagination.SortBy, pagination.SortOrder).
|
||||
Build()
|
||||
|
||||
// Filter by company ID and active status
|
||||
// Filter by assigned company ID and active status
|
||||
filter := bson.M{
|
||||
"company_id": companyID,
|
||||
"status": bson.M{"$ne": CustomerStatusInactive},
|
||||
"companies": companyID,
|
||||
"status": bson.M{"$ne": CustomerStatusInactive},
|
||||
}
|
||||
|
||||
// Use ORM to find customers
|
||||
|
||||
Reference in New Issue
Block a user