Implement Company Search and Update API Documentation
- Introduced a new search functionality for companies, allowing advanced filtering capabilities including tags, business criteria, and location. - Updated the API documentation to reflect changes in the search endpoint, enhancing clarity for API consumers. - Refactored existing company-related API endpoints for consistency, including renaming and restructuring routes. - Enhanced response structures to return company entities directly, simplifying the response handling in API endpoints. - Removed unused query parameters and handlers, streamlining the company management functionality.
This commit is contained in:
+56
-453
@@ -3,46 +3,29 @@ package company
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"tm/pkg/logger"
|
||||
"tm/pkg/response"
|
||||
)
|
||||
|
||||
// Service defines business logic for company operations
|
||||
type Service interface {
|
||||
// Core company operations
|
||||
CreateCompany(ctx context.Context, form *CreateCompanyForm, createdBy *string) (*Company, error)
|
||||
GetCompanyByID(ctx context.Context, id string) (*Company, error)
|
||||
GetCompaniesByIDs(ctx context.Context, ids []string) ([]*Company, error)
|
||||
UpdateCompany(ctx context.Context, id string, form *UpdateCompanyForm, updatedBy *string) (*Company, error)
|
||||
DeleteCompany(ctx context.Context, id string, deletedBy *string) error
|
||||
Create(ctx context.Context, form *CompanyForm) (*CompanyResponse, error)
|
||||
Update(ctx context.Context, id string, form *CompanyForm) (*CompanyResponse, error)
|
||||
GetByID(ctx context.Context, id string) (*CompanyResponse, error)
|
||||
GetByIDs(ctx context.Context, ids []string) ([]*CompanyResponse, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
|
||||
// Company listing and search
|
||||
ListCompanies(ctx context.Context, form *ListCompaniesForm) (*CompanyListResponse, error)
|
||||
SearchCompanies(ctx context.Context, form *CompanySearchForm) (*CompanyListResponse, error)
|
||||
SearchCompaniesByTags(ctx context.Context, cpvCodes []string, categories []string, keywords []string, specializations []string, limit, offset int) ([]*Company, error)
|
||||
SearchCompanies(ctx context.Context, form *SearchForm, pagination *response.Pagination) (*CompanyListResponse, error)
|
||||
|
||||
// Company management
|
||||
UpdateCompanyStatus(ctx context.Context, id string, form *UpdateCompanyStatusForm, updatedBy *string) error
|
||||
UpdateCompanyVerification(ctx context.Context, id string, form *UpdateCompanyVerificationForm, updatedBy *string) error
|
||||
UpdateStatus(ctx context.Context, id string, form *StatusForm) error
|
||||
UpdateVerification(ctx context.Context, id string, form *VerificationForm) error
|
||||
|
||||
// Tag management
|
||||
UpdateCompanyTags(ctx context.Context, id string, form *UpdateCompanyTagsForm, updatedBy *string) error
|
||||
AddTags(ctx context.Context, id string, form *AddTagsForm, updatedBy *string) error
|
||||
RemoveTags(ctx context.Context, id string, form *RemoveTagsForm, updatedBy *string) error
|
||||
|
||||
// Business operations
|
||||
VerifyCompany(ctx context.Context, id string, verifiedBy *string) error
|
||||
SuspendCompany(ctx context.Context, id string, reason string, suspendedBy *string) error
|
||||
ActivateCompany(ctx context.Context, id string, activatedBy *string) error
|
||||
|
||||
// Statistics and analytics
|
||||
GetCompanyStats(ctx context.Context) (*CompanyStatsResponse, error)
|
||||
|
||||
// Search and filtering helpers
|
||||
GetCompaniesByType(ctx context.Context, companyType CompanyType, limit, offset int) ([]*Company, int64, error)
|
||||
GetCompaniesByStatus(ctx context.Context, status CompanyStatus, limit, offset int) ([]*Company, int64, error)
|
||||
GetCompaniesByIndustry(ctx context.Context, industry string, limit, offset int) ([]*Company, int64, error)
|
||||
// Get my company
|
||||
GetMyCompany(ctx context.Context, id string) (*CompanyProfileResponse, error)
|
||||
}
|
||||
|
||||
// companyService implements the Service interface
|
||||
@@ -59,8 +42,8 @@ func NewCompanyService(repository Repository, logger logger.Logger) Service {
|
||||
}
|
||||
}
|
||||
|
||||
// CreateCompany creates a new company
|
||||
func (s *companyService) CreateCompany(ctx context.Context, form *CreateCompanyForm, createdBy *string) (*Company, error) {
|
||||
// Create creates a new company
|
||||
func (s *companyService) Create(ctx context.Context, form *CompanyForm) (*CompanyResponse, error) {
|
||||
// Check if company name already exists
|
||||
existingCompany, _ := s.repository.GetByName(ctx, form.Name)
|
||||
if existingCompany != nil {
|
||||
@@ -83,7 +66,7 @@ func (s *companyService) CreateCompany(ctx context.Context, form *CreateCompanyF
|
||||
company := &Company{
|
||||
Name: form.Name,
|
||||
Type: CompanyType(form.Type),
|
||||
Status: CompanyStatusPending,
|
||||
Status: CompanyStatusActive,
|
||||
RegistrationNumber: form.RegistrationNumber,
|
||||
TaxID: form.TaxID,
|
||||
Industry: form.Industry,
|
||||
@@ -93,7 +76,6 @@ func (s *companyService) CreateCompany(ctx context.Context, form *CreateCompanyF
|
||||
AnnualRevenue: form.AnnualRevenue,
|
||||
FoundedYear: form.FoundedYear,
|
||||
Address: s.convertAddressForm(form.Address),
|
||||
ContactPerson: s.convertContactPersonForm(form.ContactPerson),
|
||||
Phone: form.Phone,
|
||||
Email: form.Email,
|
||||
Tags: s.convertCompanyTagsForm(form.Tags),
|
||||
@@ -102,7 +84,6 @@ func (s *companyService) CreateCompany(ctx context.Context, form *CreateCompanyF
|
||||
Language: s.getDefaultLanguage(form.Language),
|
||||
Currency: s.getDefaultCurrency(form.Currency),
|
||||
Timezone: s.getDefaultTimezone(form.Timezone),
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
|
||||
// Save to database
|
||||
@@ -120,14 +101,13 @@ func (s *companyService) CreateCompany(ctx context.Context, form *CreateCompanyF
|
||||
"company_id": company.ID,
|
||||
"name": company.Name,
|
||||
"type": company.Type,
|
||||
"created_by": createdBy,
|
||||
})
|
||||
|
||||
return company, nil
|
||||
return company.ToResponse(), nil
|
||||
}
|
||||
|
||||
// GetCompanyByID retrieves a company by ID
|
||||
func (s *companyService) GetCompanyByID(ctx context.Context, id string) (*Company, error) {
|
||||
func (s *companyService) GetByID(ctx context.Context, id string) (*CompanyResponse, error) {
|
||||
company, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get company by ID", map[string]interface{}{
|
||||
@@ -142,34 +122,11 @@ func (s *companyService) GetCompanyByID(ctx context.Context, id string) (*Compan
|
||||
"name": company.Name,
|
||||
})
|
||||
|
||||
return company, nil
|
||||
}
|
||||
|
||||
// GetCompaniesByIDs retrieves multiple companies by their IDs
|
||||
func (s *companyService) GetCompaniesByIDs(ctx context.Context, ids []string) ([]*Company, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*Company{}, nil
|
||||
}
|
||||
|
||||
companies, err := s.repository.GetByIDs(ctx, ids)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get companies by IDs", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"ids": ids,
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("Companies retrieved successfully", map[string]interface{}{
|
||||
"requested_count": len(ids),
|
||||
"found_count": len(companies),
|
||||
})
|
||||
|
||||
return companies, nil
|
||||
return company.ToResponse(), nil
|
||||
}
|
||||
|
||||
// UpdateCompany updates a company
|
||||
func (s *companyService) UpdateCompany(ctx context.Context, id string, form *UpdateCompanyForm, updatedBy *string) (*Company, error) {
|
||||
func (s *companyService) Update(ctx context.Context, id string, form *CompanyForm) (*CompanyResponse, error) {
|
||||
// Get existing company
|
||||
company, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -181,39 +138,39 @@ func (s *companyService) UpdateCompany(ctx context.Context, id string, form *Upd
|
||||
}
|
||||
|
||||
// Check if name already exists (if changing name)
|
||||
if form.Name != nil && *form.Name != company.Name {
|
||||
existingCompany, _ := s.repository.GetByName(ctx, *form.Name)
|
||||
if form.Name != "" && form.Name != company.Name {
|
||||
existingCompany, _ := s.repository.GetByName(ctx, form.Name)
|
||||
if existingCompany != nil {
|
||||
return nil, errors.New("company with this name already exists")
|
||||
}
|
||||
company.Name = *form.Name
|
||||
company.Name = form.Name
|
||||
}
|
||||
|
||||
// Check if registration number already exists (if changing)
|
||||
if form.RegistrationNumber != nil && *form.RegistrationNumber != company.RegistrationNumber {
|
||||
existingCompany, _ := s.repository.GetByRegistrationNumber(ctx, *form.RegistrationNumber)
|
||||
if form.RegistrationNumber != "" && form.RegistrationNumber != company.RegistrationNumber {
|
||||
existingCompany, _ := s.repository.GetByRegistrationNumber(ctx, form.RegistrationNumber)
|
||||
if existingCompany != nil {
|
||||
return nil, errors.New("company with this registration number already exists")
|
||||
}
|
||||
company.RegistrationNumber = *form.RegistrationNumber
|
||||
company.RegistrationNumber = form.RegistrationNumber
|
||||
}
|
||||
|
||||
// Check if tax ID already exists (if changing)
|
||||
if form.TaxID != nil && *form.TaxID != company.TaxID {
|
||||
existingCompany, _ := s.repository.GetByTaxID(ctx, *form.TaxID)
|
||||
if form.TaxID != "" && form.TaxID != company.TaxID {
|
||||
existingCompany, _ := s.repository.GetByTaxID(ctx, form.TaxID)
|
||||
if existingCompany != nil {
|
||||
return nil, errors.New("company with this tax ID already exists")
|
||||
}
|
||||
company.TaxID = *form.TaxID
|
||||
company.TaxID = form.TaxID
|
||||
}
|
||||
|
||||
// Update fields if provided
|
||||
if form.Type != nil {
|
||||
company.Type = CompanyType(*form.Type)
|
||||
if form.Type != "" {
|
||||
company.Type = CompanyType(form.Type)
|
||||
}
|
||||
|
||||
if form.Industry != nil {
|
||||
company.Industry = *form.Industry
|
||||
if form.Industry != "" {
|
||||
company.Industry = form.Industry
|
||||
}
|
||||
|
||||
if form.Description != nil {
|
||||
@@ -240,10 +197,6 @@ func (s *companyService) UpdateCompany(ctx context.Context, id string, form *Upd
|
||||
company.Address = s.convertAddressForm(form.Address)
|
||||
}
|
||||
|
||||
if form.ContactPerson != nil {
|
||||
company.ContactPerson = s.convertContactPersonForm(form.ContactPerson)
|
||||
}
|
||||
|
||||
if form.Phone != nil {
|
||||
company.Phone = form.Phone
|
||||
}
|
||||
@@ -268,10 +221,6 @@ func (s *companyService) UpdateCompany(ctx context.Context, id string, form *Upd
|
||||
company.Timezone = *form.Timezone
|
||||
}
|
||||
|
||||
// Set updated by
|
||||
company.UpdatedBy = updatedBy
|
||||
company.UpdatedAt = time.Now().Unix()
|
||||
|
||||
// Update in database
|
||||
err = s.repository.Update(ctx, company)
|
||||
if err != nil {
|
||||
@@ -285,25 +234,15 @@ func (s *companyService) UpdateCompany(ctx context.Context, id string, form *Upd
|
||||
s.logger.Info("Company updated successfully", map[string]interface{}{
|
||||
"company_id": company.ID,
|
||||
"name": company.Name,
|
||||
"updated_by": updatedBy,
|
||||
})
|
||||
|
||||
return company, nil
|
||||
return company.ToResponse(), nil
|
||||
}
|
||||
|
||||
// DeleteCompany deletes a company (soft delete)
|
||||
func (s *companyService) DeleteCompany(ctx context.Context, id string, deletedBy *string) error {
|
||||
// Get company to check if exists
|
||||
company, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
}
|
||||
|
||||
// Set updated by before deletion
|
||||
company.UpdatedBy = deletedBy
|
||||
|
||||
func (s *companyService) Delete(ctx context.Context, id string) error {
|
||||
// Delete company (soft delete)
|
||||
err = s.repository.Delete(ctx, id)
|
||||
err := s.repository.Delete(ctx, id)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to delete company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
@@ -314,92 +253,19 @@ func (s *companyService) DeleteCompany(ctx context.Context, id string, deletedBy
|
||||
|
||||
s.logger.Info("Company deleted successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"deleted_by": deletedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListCompanies lists companies with search and filters
|
||||
func (s *companyService) ListCompanies(ctx context.Context, form *ListCompaniesForm) (*CompanyListResponse, error) {
|
||||
// Set defaults
|
||||
limit := 20
|
||||
if form.Limit != nil {
|
||||
limit = *form.Limit
|
||||
}
|
||||
|
||||
offset := 0
|
||||
if form.Offset != nil {
|
||||
offset = *form.Offset
|
||||
}
|
||||
|
||||
search := ""
|
||||
if form.Search != nil {
|
||||
search = *form.Search
|
||||
}
|
||||
|
||||
sortBy := "created_at"
|
||||
if form.SortBy != nil {
|
||||
sortBy = *form.SortBy
|
||||
}
|
||||
|
||||
sortOrder := "desc"
|
||||
if form.SortOrder != nil {
|
||||
sortOrder = *form.SortOrder
|
||||
}
|
||||
|
||||
// Get companies
|
||||
companies, err := s.repository.Search(ctx, search, form.Type, form.Status, form.Industry, form.IsVerified, form.IsCompliant, form.Language, form.Currency, form.CPVCodes, form.Categories, form.Keywords, form.Specializations, form.EmployeeCountMin, form.EmployeeCountMax, form.AnnualRevenueMin, form.AnnualRevenueMax, form.FoundedYearMin, form.FoundedYearMax, limit, offset, sortBy, sortOrder)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to list companies", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, errors.New("failed to list companies")
|
||||
}
|
||||
|
||||
// Convert to responses
|
||||
var companyResponses []*CompanyResponse
|
||||
for _, company := range companies {
|
||||
companyResponses = append(companyResponses, company.ToResponse())
|
||||
}
|
||||
|
||||
// TODO: Implement proper count for pagination metadata
|
||||
total := int64(len(companies)) // This should be a separate count query
|
||||
totalPages := (total + int64(limit) - 1) / int64(limit)
|
||||
|
||||
return &CompanyListResponse{
|
||||
Companies: companyResponses,
|
||||
Total: total,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
TotalPages: int(totalPages),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SearchCompanies searches companies with advanced filters
|
||||
func (s *companyService) SearchCompanies(ctx context.Context, form *CompanySearchForm) (*CompanyListResponse, error) {
|
||||
// Set defaults
|
||||
limit := 20
|
||||
if form.Limit != nil {
|
||||
limit = *form.Limit
|
||||
}
|
||||
|
||||
offset := 0
|
||||
if form.Offset != nil {
|
||||
offset = *form.Offset
|
||||
}
|
||||
|
||||
query := ""
|
||||
if form.Query != nil {
|
||||
query = *form.Query
|
||||
}
|
||||
func (s *companyService) SearchCompanies(ctx context.Context, form *SearchForm, pagination *response.Pagination) (*CompanyListResponse, error) {
|
||||
|
||||
// Get companies using search
|
||||
companies, err := s.repository.Search(ctx, query, nil, nil, form.Industry, form.IsVerified, form.IsCompliant, nil, nil, form.CPVCodes, form.Categories, form.Keywords, form.Specializations, form.MinEmployees, form.MaxEmployees, form.MinRevenue, form.MaxRevenue, nil, nil, limit, offset, "created_at", "desc")
|
||||
companies, total, err := s.repository.Search(ctx, form, pagination)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to search companies", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"query": query,
|
||||
})
|
||||
return nil, errors.New("failed to search companies")
|
||||
}
|
||||
@@ -410,34 +276,29 @@ func (s *companyService) SearchCompanies(ctx context.Context, form *CompanySearc
|
||||
companyResponses = append(companyResponses, company.ToResponse())
|
||||
}
|
||||
|
||||
// TODO: Implement proper count for pagination metadata
|
||||
total := int64(len(companies)) // This should be a separate count query
|
||||
totalPages := (total + int64(limit) - 1) / int64(limit)
|
||||
|
||||
return &CompanyListResponse{
|
||||
Companies: companyResponses,
|
||||
Total: total,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
TotalPages: int(totalPages),
|
||||
Companies: companyResponses,
|
||||
Meta: pagination.Response(total),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SearchCompaniesByTags searches companies by tags
|
||||
func (s *companyService) SearchCompaniesByTags(ctx context.Context, cpvCodes []string, categories []string, keywords []string, specializations []string, limit, offset int) ([]*Company, error) {
|
||||
companies, err := s.repository.SearchByTags(ctx, cpvCodes, categories, keywords, specializations, limit, offset)
|
||||
// GetByIDs retrieves companies by IDs
|
||||
func (s *companyService) GetByIDs(ctx context.Context, ids []string) ([]*CompanyResponse, error) {
|
||||
companies, err := s.repository.GetByIDs(ctx, ids)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to search companies by tags", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, errors.New("failed to search companies by tags")
|
||||
return nil, errors.New("failed to get companies by IDs")
|
||||
}
|
||||
|
||||
return companies, nil
|
||||
var companyResponses []*CompanyResponse
|
||||
for _, company := range companies {
|
||||
companyResponses = append(companyResponses, company.ToResponse())
|
||||
}
|
||||
|
||||
return companyResponses, nil
|
||||
}
|
||||
|
||||
// UpdateCompanyStatus updates company status
|
||||
func (s *companyService) UpdateCompanyStatus(ctx context.Context, id string, form *UpdateCompanyStatusForm, updatedBy *string) error {
|
||||
func (s *companyService) UpdateStatus(ctx context.Context, id string, form *StatusForm) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -445,13 +306,12 @@ func (s *companyService) UpdateCompanyStatus(ctx context.Context, id string, for
|
||||
}
|
||||
|
||||
// Update status
|
||||
status := CompanyStatus(form.Status)
|
||||
err = s.repository.UpdateStatus(ctx, id, status)
|
||||
err = s.repository.UpdateStatus(ctx, id, CompanyStatus(form.Status))
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to update company status", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
"status": form.Status,
|
||||
"status": CompanyStatus(form.Status),
|
||||
})
|
||||
return errors.New("failed to update company status")
|
||||
}
|
||||
@@ -459,14 +319,13 @@ func (s *companyService) UpdateCompanyStatus(ctx context.Context, id string, for
|
||||
s.logger.Info("Company status updated successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"status": form.Status,
|
||||
"updated_by": updatedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateCompanyVerification updates company verification status
|
||||
func (s *companyService) UpdateCompanyVerification(ctx context.Context, id string, form *UpdateCompanyVerificationForm, updatedBy *string) error {
|
||||
func (s *companyService) UpdateVerification(ctx context.Context, id string, form *VerificationForm) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -487,258 +346,19 @@ func (s *companyService) UpdateCompanyVerification(ctx context.Context, id strin
|
||||
"company_id": id,
|
||||
"is_verified": form.IsVerified,
|
||||
"is_compliant": form.IsCompliant,
|
||||
"updated_by": updatedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateCompanyTags updates company tags
|
||||
func (s *companyService) UpdateCompanyTags(ctx context.Context, id string, form *UpdateCompanyTagsForm, updatedBy *string) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
}
|
||||
|
||||
// Update tags
|
||||
tags := s.convertCompanyTagsForm(form.Tags)
|
||||
err = s.repository.UpdateTags(ctx, id, tags)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to update company tags", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
})
|
||||
return errors.New("failed to update company tags")
|
||||
}
|
||||
|
||||
s.logger.Info("Company tags updated successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"updated_by": updatedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddTags adds tags to a company
|
||||
func (s *companyService) AddTags(ctx context.Context, id string, form *AddTagsForm, updatedBy *string) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
}
|
||||
|
||||
// Add tags
|
||||
err = s.repository.AddTags(ctx, id, form.CPVCodes, form.Categories, form.Keywords, form.Specializations, form.Certifications)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to add tags to company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
})
|
||||
return errors.New("failed to add tags to company")
|
||||
}
|
||||
|
||||
s.logger.Info("Tags added to company successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"updated_by": updatedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveTags removes tags from a company
|
||||
func (s *companyService) RemoveTags(ctx context.Context, id string, form *RemoveTagsForm, updatedBy *string) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
}
|
||||
|
||||
// Remove tags
|
||||
err = s.repository.RemoveTags(ctx, id, form.CPVCodes, form.Categories, form.Keywords, form.Specializations, form.Certifications)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to remove tags from company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
})
|
||||
return errors.New("failed to remove tags from company")
|
||||
}
|
||||
|
||||
s.logger.Info("Tags removed from company successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"updated_by": updatedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// VerifyCompany verifies a company
|
||||
func (s *companyService) VerifyCompany(ctx context.Context, id string, verifiedBy *string) error {
|
||||
// Get company to check if exists
|
||||
// GetMyCompany retrieves the company for the authenticated user
|
||||
func (s *companyService) GetMyCompany(ctx context.Context, id string) (*CompanyProfileResponse, error) {
|
||||
company, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
return nil, errors.New("company not found")
|
||||
}
|
||||
|
||||
// Update verification
|
||||
err = s.repository.UpdateVerification(ctx, id, true, company.IsCompliant, company.ComplianceNotes)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to verify company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
})
|
||||
return errors.New("failed to verify company")
|
||||
}
|
||||
|
||||
s.logger.Info("Company verified successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"verified_by": verifiedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SuspendCompany suspends a company
|
||||
func (s *companyService) SuspendCompany(ctx context.Context, id string, reason string, suspendedBy *string) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
}
|
||||
|
||||
// Update status to suspended
|
||||
err = s.repository.UpdateStatus(ctx, id, CompanyStatusSuspended)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to suspend company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
})
|
||||
return errors.New("failed to suspend company")
|
||||
}
|
||||
|
||||
s.logger.Info("Company suspended successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"reason": reason,
|
||||
"suspended_by": suspendedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ActivateCompany activates a company
|
||||
func (s *companyService) ActivateCompany(ctx context.Context, id string, activatedBy *string) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
}
|
||||
|
||||
// Update status to active
|
||||
err = s.repository.UpdateStatus(ctx, id, CompanyStatusActive)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to activate company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
})
|
||||
return errors.New("failed to activate company")
|
||||
}
|
||||
|
||||
s.logger.Info("Company activated successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"activated_by": activatedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCompanyStats returns company statistics
|
||||
func (s *companyService) GetCompanyStats(ctx context.Context) (*CompanyStatsResponse, error) {
|
||||
stats, err := s.repository.GetCompanyStats(ctx)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get company stats", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, errors.New("failed to get company statistics")
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// GetCompaniesByType retrieves companies by type with pagination
|
||||
func (s *companyService) GetCompaniesByType(ctx context.Context, companyType CompanyType, limit, offset int) ([]*Company, int64, error) {
|
||||
// Use search method with type filter
|
||||
typeStr := string(companyType)
|
||||
companies, err := s.repository.Search(ctx, "", &typeStr, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, limit, offset, "created_at", "desc")
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get companies by type", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_type": companyType,
|
||||
})
|
||||
return nil, 0, errors.New("failed to get companies by type")
|
||||
}
|
||||
|
||||
// Get total count
|
||||
total, err := s.repository.CountByType(ctx, companyType)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to count companies by type", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_type": companyType,
|
||||
})
|
||||
return companies, 0, nil // Return companies even if count fails
|
||||
}
|
||||
|
||||
return companies, total, nil
|
||||
}
|
||||
|
||||
// GetCompaniesByStatus retrieves companies by status with pagination
|
||||
func (s *companyService) GetCompaniesByStatus(ctx context.Context, status CompanyStatus, limit, offset int) ([]*Company, int64, error) {
|
||||
// Use search method with status filter
|
||||
statusStr := string(status)
|
||||
companies, err := s.repository.Search(ctx, "", nil, &statusStr, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, limit, offset, "created_at", "desc")
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get companies by status", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"status": status,
|
||||
})
|
||||
return nil, 0, errors.New("failed to get companies by status")
|
||||
}
|
||||
|
||||
// Get total count
|
||||
total, err := s.repository.CountByStatus(ctx, status)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to count companies by status", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"status": status,
|
||||
})
|
||||
return companies, 0, nil
|
||||
}
|
||||
|
||||
return companies, total, nil
|
||||
}
|
||||
|
||||
// GetCompaniesByIndustry retrieves companies by industry with pagination
|
||||
func (s *companyService) GetCompaniesByIndustry(ctx context.Context, industry string, limit, offset int) ([]*Company, int64, error) {
|
||||
// Use search method with industry filter
|
||||
companies, err := s.repository.Search(ctx, "", nil, nil, &industry, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, limit, offset, "created_at", "desc")
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get companies by industry", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"industry": industry,
|
||||
})
|
||||
return nil, 0, errors.New("failed to get companies by industry")
|
||||
}
|
||||
|
||||
// Get total count
|
||||
total, err := s.repository.CountByIndustry(ctx, industry)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to count companies by industry", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"industry": industry,
|
||||
})
|
||||
return companies, 0, nil
|
||||
}
|
||||
|
||||
return companies, total, nil
|
||||
return company.ToProfileResponse(), nil
|
||||
}
|
||||
|
||||
// Helper methods for converting forms to domain objects
|
||||
@@ -756,23 +376,6 @@ func (s *companyService) convertAddressForm(form *AddressForm) *Address {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *companyService) convertContactPersonForm(form *ContactPersonForm) *ContactPerson {
|
||||
if form == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ContactPerson{
|
||||
FirstName: form.FirstName,
|
||||
LastName: form.LastName,
|
||||
FullName: form.FullName,
|
||||
Position: form.Position,
|
||||
Email: form.Email,
|
||||
Phone: form.Phone,
|
||||
Mobile: form.Mobile,
|
||||
IsPrimary: form.IsPrimary,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *companyService) convertCompanyTagsForm(form *CompanyTagsForm) *CompanyTags {
|
||||
if form == nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user