Enhance tender and company context handling with new features and error management
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Introduced `PickAssignedCompanyID` function to determine the appropriate company ID for company-scoped operations, improving company assignment logic. - Added `ResolveMongoID` method in the tender service to map MongoDB IDs and AI procedure references to canonical tender IDs, enhancing ID resolution. - Updated `ToggleTenderApproval` to handle company ID validation and improve error handling, ensuring robust processing of tender approvals. - Enhanced logging for error scenarios in the tender approval service, providing better insights into failures during operations. - Refactored the `GetByID` and `GetByTenderAndCompany` methods in the tender approval repository to utilize custom error types for improved clarity. This update significantly improves the handling of company and tender operations, enhancing error management and overall service reliability.
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var errCompanyNotAssigned = errors.New("company is not assigned to customer")
|
||||
// ErrCompanyNotAssigned is returned when the requested company is not assigned to the customer.
|
||||
var ErrCompanyNotAssigned = errors.New("company is not assigned to customer")
|
||||
|
||||
var errCompanyNotAssigned = ErrCompanyNotAssigned
|
||||
|
||||
func normalizeAssignedCompanyIDs(assigned []string) []string {
|
||||
normalized := make([]string, 0, len(assigned))
|
||||
@@ -56,6 +59,27 @@ func pickActiveCompanyID(assigned []string, tokenCompanyID, requestedCompanyID s
|
||||
return assigned[0], nil
|
||||
}
|
||||
|
||||
// PickAssignedCompanyID chooses the company for a company-scoped write (e.g. tender approval).
|
||||
// When requestedCompanyID is set it must be in assigned; otherwise activeCompanyID is used.
|
||||
func PickAssignedCompanyID(activeCompanyID string, assigned []string, requestedCompanyID string) (string, error) {
|
||||
activeCompanyID = strings.TrimSpace(activeCompanyID)
|
||||
requestedCompanyID = strings.TrimSpace(requestedCompanyID)
|
||||
|
||||
if requestedCompanyID != "" {
|
||||
for _, id := range normalizeAssignedCompanyIDs(assigned) {
|
||||
if id == requestedCompanyID {
|
||||
return requestedCompanyID, nil
|
||||
}
|
||||
}
|
||||
return "", ErrCompanyNotAssigned
|
||||
}
|
||||
|
||||
if activeCompanyID == "" {
|
||||
return "", errors.New("company ID is required")
|
||||
}
|
||||
return activeCompanyID, nil
|
||||
}
|
||||
|
||||
// ResolveCompanyContext resolves the active company and assigned companies for an authenticated customer.
|
||||
func (s *customerService) ResolveCompanyContext(ctx context.Context, customerID, tokenCompanyID, requestedCompanyID string) (string, []string, error) {
|
||||
customer, err := s.repository.GetByID(ctx, customerID)
|
||||
|
||||
Reference in New Issue
Block a user