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

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/companieshouse/chs.go/log"
     9  	"github.com/companieshouse/insolvency-api/dao"
    10  	"github.com/companieshouse/insolvency-api/models"
    11  	"github.com/companieshouse/insolvency-api/utils"
    12  )
    13  
    14  // ValidateResolutionRequest checks that the incoming resolution request is valid
    15  func ValidateResolutionRequest(resolution models.Resolution) string {
    16  	var errs []string
    17  
    18  	// Check that the attachment has been submitted correctly
    19  	if len(resolution.Attachments) == 0 || len(resolution.Attachments) > 1 {
    20  		errs = append(errs, "please supply only one attachment")
    21  	}
    22  	return strings.Join(errs, ", ")
    23  }
    24  
    25  // ValidateResolutionDate checks that the incoming resolution date is valid
    26  func ValidateResolutionDate(svc dao.Service, resolution *models.ResolutionResourceDao, transactionID string, req *http.Request) (string, error) {
    27  	var errs []string
    28  
    29  	// Check if resolution date supplied is in the future or before company was incorporated
    30  	insolvencyResource, err := svc.GetInsolvencyResource(transactionID)
    31  	if err != nil {
    32  		err = fmt.Errorf("error getting insolvency resource from DB: [%s]", err)
    33  		log.ErrorR(req, err)
    34  		return "", err
    35  	}
    36  	// Retrieve company incorporation date
    37  	incorporatedOn, err := GetCompanyIncorporatedOn(insolvencyResource.Data.CompanyNumber, req)
    38  	if err != nil {
    39  		err = fmt.Errorf("error getting company details from DB: [%s]", err)
    40  		log.ErrorR(req, err)
    41  		return "", err
    42  	}
    43  
    44  	ok, err := utils.IsDateBetweenIncorporationAndNow(resolution.DateOfResolution, incorporatedOn)
    45  	if err != nil {
    46  		err = fmt.Errorf("error parsing date: [%s]", err)
    47  		log.ErrorR(req, err)
    48  		return "", err
    49  	}
    50  	if !ok {
    51  		errs = append(errs, fmt.Sprintf("date_of_resolution [%s] should not be in the future or before the company was incorporated", resolution.DateOfResolution))
    52  	}
    53  
    54  	return strings.Join(errs, ", "), nil
    55  }