github.com/companieshouse/insolvency-api@v0.0.0-20231024103413-440c973d9e9b/service/alpha_key_service.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/companieshouse/chs.go/log"
     8  	"github.com/companieshouse/go-sdk-manager/manager"
     9  	"github.com/companieshouse/insolvency-api/models"
    10  )
    11  
    12  func CheckCompanyNameAlphaKey(companyProfileCompanyName string, insolvencyRequest *models.InsolvencyRequest, req *http.Request) (error, int) {
    13  
    14  	api, err := manager.GetPrivateSDK(req)
    15  	if err != nil {
    16  		return fmt.Errorf("error creating private SDK to call alphakeyservice: [%v]", err.Error()), http.StatusInternalServerError
    17  	}
    18  
    19  	requestAlphaKeyResponse, err := api.AlphaKey.Get(insolvencyRequest.CompanyName).Do()
    20  	if err != nil {
    21  		log.ErrorR(req, fmt.Errorf("error communicating with alphakey service [%v]", err))
    22  		return fmt.Errorf("error communicating with alphakey service"), http.StatusInternalServerError
    23  	}
    24  
    25  	insolvencyRequestAlphaKey := requestAlphaKeyResponse.SameAsAlphaKey
    26  
    27  	profileAlphaKeyResponse, err := api.AlphaKey.Get(companyProfileCompanyName).Do()
    28  	if err != nil {
    29  		log.ErrorR(req, fmt.Errorf("error communicating with alphakey service [%v]", err))
    30  		return fmt.Errorf("error communicating with alphakey service"), http.StatusInternalServerError
    31  	}
    32  
    33  	profileAlphaKey := profileAlphaKeyResponse.SameAsAlphaKey
    34  
    35  	if insolvencyRequestAlphaKey != profileAlphaKey {
    36  		return fmt.Errorf("company names do not match - provided: [%s], expected: [%s]", insolvencyRequest.CompanyName, companyProfileCompanyName), http.StatusBadRequest
    37  	}
    38  
    39  	return nil, requestAlphaKeyResponse.HTTPStatusCode
    40  }