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

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/companieshouse/go-sdk-manager/manager"
     8  	"github.com/companieshouse/insolvency-api/models"
     9  	"github.com/companieshouse/insolvency-api/transformers"
    10  )
    11  
    12  // CheckTransactionID will check with the transaction api that the provided transaction id exists
    13  func CheckTransactionID(transactionID string, req *http.Request) (error, int) {
    14  
    15  	// Create SDK session
    16  	api, err := manager.GetSDK(req)
    17  
    18  	if err != nil {
    19  		return fmt.Errorf("error creating SDK to call transaction api: [%v]", err.Error()), http.StatusInternalServerError
    20  	}
    21  
    22  	// Call transaction api to retrieve details of the transaction
    23  	transactionProfile, err := api.Transaction.Get(transactionID).Do()
    24  
    25  	if err != nil {
    26  		// If 404 then return the transaction not found
    27  		if transactionProfile.HTTPStatusCode == http.StatusNotFound {
    28  			return fmt.Errorf("transaction not found"), http.StatusNotFound
    29  		}
    30  		// Else return that there has been an error contacting the transaction api
    31  		return fmt.Errorf("error communicating with the transaction api"), transactionProfile.HTTPStatusCode
    32  	}
    33  
    34  	return nil, transactionProfile.HTTPStatusCode
    35  }
    36  
    37  // PatchTransactionWithInsolvencyResource will patch the provided transaction with the created insolvency resource
    38  func PatchTransactionWithInsolvencyResource(transactionID string, insolvencyResource *models.InsolvencyResourceDao, req *http.Request) (error, int) {
    39  
    40  	// Create Private SDK session
    41  	api, err := manager.GetPrivateSDK(req)
    42  
    43  	if err != nil {
    44  		return fmt.Errorf("error creating SDK to call transaction api: [%v]", err.Error()), http.StatusInternalServerError
    45  	}
    46  
    47  	// Patch transaction api with insolvency resource
    48  	transactionProfile, err := api.Transaction.Patch(transactionID, transformers.InsolvencyResourceDaoToTransactionResource(insolvencyResource)).Do()
    49  
    50  	if err != nil {
    51  		// If 404 then return the transaction not found
    52  		if transactionProfile.HTTPStatusCode == http.StatusNotFound {
    53  			return fmt.Errorf("transaction not found"), http.StatusNotFound
    54  		}
    55  		// Else return that there has been an error contacting the transaction api
    56  		return fmt.Errorf("error communication with the transaction api"), transactionProfile.HTTPStatusCode
    57  	}
    58  
    59  	return nil, transactionProfile.HTTPStatusCode
    60  }
    61  
    62  // CheckIfTransactionClosed checks against the transaction api if the transaction is closed or not
    63  func CheckIfTransactionClosed(transactionID string, req *http.Request) (bool, error, int) {
    64  
    65  	// Create SDK session
    66  	api, err := manager.GetSDK(req)
    67  
    68  	if err != nil {
    69  		return false, fmt.Errorf("error creating SDK to call transaction api: [%v]", err.Error()), http.StatusInternalServerError
    70  	}
    71  
    72  	// Call transaction api to retrieve details of the transaction
    73  	transactionProfile, err := api.Transaction.Get(transactionID).Do()
    74  
    75  	if err != nil {
    76  		// If 404 then return the transaction not found
    77  		if transactionProfile.HTTPStatusCode == http.StatusNotFound {
    78  			return false, fmt.Errorf("transaction not found"), http.StatusNotFound
    79  		}
    80  		// Else return that there has been an error contacting the transaction api
    81  		return false, fmt.Errorf("error getting transaction from transaction api: [%v]", err.Error()), transactionProfile.HTTPStatusCode
    82  	}
    83  
    84  	if transactionProfile.Status == "closed" {
    85  		return true, nil, http.StatusForbidden
    86  	}
    87  
    88  	return false, nil, http.StatusOK
    89  }