github.com/companieshouse/insolvency-api@v0.0.0-20231024103413-440c973d9e9b/service/company_service.go (about) 1 package service 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/companieshouse/api-sdk-go/companieshouseapi" 8 "github.com/companieshouse/go-sdk-manager/manager" 9 "github.com/companieshouse/insolvency-api/constants" 10 "github.com/companieshouse/insolvency-api/models" 11 ) 12 13 // CheckCompanyExists will check that the company exists against the company profile api to make a valid insolvency 14 func CheckCompanyExists(insolvencyRequest *models.InsolvencyRequest, req *http.Request) (error, int, *companieshouseapi.CompanyProfile) { 15 16 // Create SDK session 17 api, err := manager.GetSDK(req) 18 if err != nil { 19 return fmt.Errorf("error creating SDK to call company profile: [%v]", err.Error()), http.StatusInternalServerError, nil 20 } 21 22 // Call company profile api to retrieve company details 23 companyProfile, err := api.Profile.Get(insolvencyRequest.CompanyNumber).Do() 24 if err != nil { 25 // If 404 then return that company not found 26 if companyProfile.HTTPStatusCode == http.StatusNotFound { 27 return fmt.Errorf("company not found"), http.StatusNotFound, nil 28 } 29 // Else there has been an error contacting the company profile api 30 return fmt.Errorf("error communicating with the company profile api"), companyProfile.HTTPStatusCode, nil 31 } 32 33 // If no errors then the company exists 34 return nil, companyProfile.HTTPStatusCode, companyProfile 35 36 } 37 38 // GetCompanyIncorporatedOn retrieves the date that the company was created 39 func GetCompanyIncorporatedOn(companyNumber string, req *http.Request) (string, error) { 40 // Create SDK session 41 api, err := manager.GetSDK(req) 42 if err != nil { 43 return "", fmt.Errorf("error creating SDK to call company profile: [%v]", err.Error()) 44 } 45 46 // Call company profile api to retrieve company details 47 companyProfile, err := api.Profile.Get(companyNumber).Do() 48 if err != nil { 49 // If 404 then return that company not found 50 if companyProfile.HTTPStatusCode == http.StatusNotFound { 51 return "", fmt.Errorf("company not found") 52 } 53 // Else there has been an error contacting the company profile api 54 return "", fmt.Errorf("error communicating with the company profile api") 55 } 56 57 return companyProfile.DateOfCreation, nil 58 } 59 60 // CheckCompanyDetailsAreValid checks the incoming company profile to see if it's valid for insolvency 61 func CheckCompanyDetailsAreValid(companyProfile *companieshouseapi.CompanyProfile) error { 62 63 // Check if company jurisdiction is allowed 64 if !checkJurisdictionIsAllowed(companyProfile.Jurisdiction) { 65 return fmt.Errorf("jurisdiction [%s] not permitted", companyProfile.Jurisdiction) 66 } 67 68 // Check if company status is allowed 69 if !checkCompanyStatusIsAllowed(companyProfile.CompanyStatus) { 70 return fmt.Errorf("company status [%s] not permitted", companyProfile.CompanyStatus) 71 } 72 73 // Check is company type is allowed 74 if !checkCompanyTypeIsAllowed(companyProfile.Type) { 75 return fmt.Errorf("company type [%s] not permitted", companyProfile.Type) 76 } 77 78 return nil 79 } 80 81 // checkJurisdictionIsAllowed checks if the provided jurisdiction of the company is allowed 82 func checkJurisdictionIsAllowed(providedJurisdiction string) bool { 83 for _, allowedJurisdiction := range constants.AllowedJurisdictions { 84 if providedJurisdiction == allowedJurisdiction { 85 return true 86 } 87 } 88 return false 89 } 90 91 // checkCompanyStatusIsAllowed checks if the provided company status is allowed 92 func checkCompanyStatusIsAllowed(providedStatus string) bool { 93 for _, forbiddenStatus := range constants.ForbiddenCompanyStatus { 94 if providedStatus == forbiddenStatus { 95 return false 96 } 97 } 98 return true 99 } 100 101 // checkCompanyTypeIsAllowed checks if the provided company type is allowed 102 func checkCompanyTypeIsAllowed(providedType string) bool { 103 for _, forbiddenType := range constants.ForbiddenCompanyTypes { 104 if providedType == forbiddenType { 105 return false 106 } 107 } 108 return true 109 }