From 506ac01cda98d0339c0d6b59b8ea3cbec33cda3e Mon Sep 17 00:00:00 2001 From: Mazyar Date: Wed, 1 Jul 2026 19:53:35 +0330 Subject: [PATCH] Update company form validation to make fields optional and enhance service logic for registration number and tax ID checks - Modified CompanyForm fields (RegistrationNumber, TaxID, Industry) to be optional, allowing for more flexible company creation. - Updated the Create method in company service to check for existing companies by registration number and tax ID only if provided, improving error handling and user experience during company creation. This update enhances the company creation process by allowing optional fields while ensuring that existing company checks are performed only when necessary. --- internal/company/form.go | 6 +++--- internal/company/service.go | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/internal/company/form.go b/internal/company/form.go index 41d730a..579e8f0 100644 --- a/internal/company/form.go +++ b/internal/company/form.go @@ -7,9 +7,9 @@ type ( CompanyForm struct { Name string `json:"name" valid:"required,length(2|200)" example:"Opplens"` Type string `json:"type" valid:"required,in(private|public|government|ngo|startup)" example:"private"` - RegistrationNumber string `json:"registration_number" valid:"required,length(5|50)" example:"1234567890"` - TaxID string `json:"tax_id" valid:"required,length(5|50)" example:"1234567890"` - Industry string `json:"industry" valid:"required,length(2|100)" example:"Technology"` + RegistrationNumber string `json:"registration_number" valid:"optional,length(5|50)" example:"1234567890"` + TaxID string `json:"tax_id" valid:"optional,length(5|50)" example:"1234567890"` + Industry string `json:"industry" valid:"optional,length(2|100)" example:"Technology"` Description *string `json:"description,omitempty" valid:"optional,length(10|1000)" example:"Opplens is a technology company"` Website *string `json:"website,omitempty" valid:"optional,url" example:"https://opplens.com"` diff --git a/internal/company/service.go b/internal/company/service.go index db99f42..6ed8685 100644 --- a/internal/company/service.go +++ b/internal/company/service.go @@ -103,16 +103,18 @@ func (s *companyService) Create(ctx context.Context, form *CompanyForm) (*Compan return nil, errors.New("company with this name already exists") } - // Check if registration number already exists - existingCompany, _ = s.repository.GetByRegistrationNumber(ctx, form.RegistrationNumber) - if existingCompany != nil { - return nil, errors.New("company with this registration number already exists") + if form.RegistrationNumber != "" { + existingCompany, _ = s.repository.GetByRegistrationNumber(ctx, form.RegistrationNumber) + if existingCompany != nil { + return nil, errors.New("company with this registration number already exists") + } } - // Check if tax ID already exists - existingCompany, _ = s.repository.GetByTaxID(ctx, form.TaxID) - if existingCompany != nil { - return nil, errors.New("company with this tax ID already exists") + if form.TaxID != "" { + existingCompany, _ = s.repository.GetByTaxID(ctx, form.TaxID) + if existingCompany != nil { + return nil, errors.New("company with this tax ID already exists") + } } // Create company