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

     1  package handlers
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     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/service"
    12  	"github.com/companieshouse/insolvency-api/transformers"
    13  	"github.com/companieshouse/insolvency-api/utils"
    14  	"github.com/gorilla/mux"
    15  )
    16  
    17  // HandleCreateProgressReport receives a progress report to be stored against the Insolvency case
    18  func HandleCreateProgressReport(svc dao.Service, helperService utils.HelperService) http.Handler {
    19  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    20  
    21  		// Check transaction is valid
    22  		transactionID, isValidTransaction := utils.ValidateTransaction(helperService, req, w, "progress report", service.CheckIfTransactionClosed)
    23  		if !isValidTransaction {
    24  			return
    25  		}
    26  
    27  		// Decode Request body
    28  		var request models.ProgressReport
    29  		err := json.NewDecoder(req.Body).Decode(&request)
    30  		isValidDecoded := helperService.HandleBodyDecodedValidation(w, req, transactionID, err)
    31  		if !isValidDecoded {
    32  			return
    33  		}
    34  
    35  		progressReportDao := transformers.ProgressReportResourceRequestToDB(&request, transactionID, helperService)
    36  
    37  		// Validate all mandatory fields
    38  		errs := utils.Validate(request)
    39  		isValidMarshallToDB := helperService.HandleMandatoryFieldValidation(w, req, errs)
    40  		if !isValidMarshallToDB {
    41  			return
    42  		}
    43  
    44  		// Validate the provided statement details are in the correct format
    45  		validationErrs, err := service.ValidateProgressReportDetails(svc, progressReportDao, transactionID, req)
    46  		if err != nil {
    47  			log.ErrorR(req, fmt.Errorf("failed to validate progress report: [%s]", err))
    48  			m := models.NewMessageResponse(fmt.Sprintf("there was a problem handling your request for transaction ID [%s]", transactionID))
    49  			utils.WriteJSONWithStatus(w, req, m, http.StatusInternalServerError)
    50  			return
    51  		}
    52  		if validationErrs != "" {
    53  			log.ErrorR(req, fmt.Errorf("invalid request - failed validation on the following: %s", validationErrs))
    54  			m := models.NewMessageResponse("invalid request body: " + validationErrs)
    55  			utils.WriteJSONWithStatus(w, req, m, http.StatusBadRequest)
    56  			return
    57  		}
    58  
    59  		// Validate if supplied attachment matches attachments associated with supplied transactionID in mongo db
    60  		attachment, err := svc.GetAttachmentFromInsolvencyResource(transactionID, progressReportDao.Attachments[0])
    61  		isValidAttachment := helperService.HandleAttachmentValidation(w, req, transactionID, attachment, err)
    62  		if !isValidAttachment {
    63  			return
    64  		}
    65  
    66  		// Validate the supplied attachment is a valid type
    67  		if attachment.Type != "progress-report" {
    68  			err := fmt.Errorf("attachment id [%s] is an invalid type for this request: %v", progressReportDao.Attachments[0], attachment.Type)
    69  			responseMessage := "attachment is not a progress-report"
    70  
    71  			helperService.HandleAttachmentTypeValidation(w, req, responseMessage, err)
    72  			return
    73  		}
    74  
    75  		// Creates the progress report resource in mongo if all previous checks pass
    76  		statusCode, err := svc.CreateProgressReportResource(progressReportDao, transactionID)
    77  		isValidCreateResource := helperService.HandleCreateResourceValidation(w, req, statusCode, err)
    78  
    79  		if !isValidCreateResource {
    80  			return
    81  		}
    82  
    83  		daoResponse := transformers.ProgressReportDaoToResponse(progressReportDao)
    84  
    85  		log.InfoR(req, fmt.Sprintf("successfully added statement of progress report with transaction ID: %s, to mongo", transactionID))
    86  
    87  		utils.WriteJSONWithStatus(w, req, daoResponse, http.StatusCreated)
    88  	})
    89  }
    90  
    91  // HandleGetProgressReport retrieves a progress report stored against the Insolvency Case
    92  func HandleGetProgressReport(svc dao.Service) http.Handler {
    93  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    94  		vars := mux.Vars(req)
    95  		transactionID := utils.GetTransactionIDFromVars(vars)
    96  		if transactionID == "" {
    97  			log.ErrorR(req, fmt.Errorf("there is no transaction ID in the URL path"))
    98  			m := models.NewMessageResponse("transaction ID is not in the URL path")
    99  			utils.WriteJSONWithStatus(w, req, m, http.StatusBadRequest)
   100  			return
   101  		}
   102  
   103  		log.InfoR(req, fmt.Sprintf("start GET request for get progress report with transaction id: %s", transactionID))
   104  
   105  		progressReport, err := svc.GetProgressReportResource(transactionID)
   106  		if err != nil {
   107  			log.ErrorR(req, fmt.Errorf("failed to get progress report from insolvency resource in db for transaction [%s]: %v", transactionID, err))
   108  			m := models.NewMessageResponse("there was a problem handling your request")
   109  			utils.WriteJSONWithStatus(w, req, m, http.StatusInternalServerError)
   110  			return
   111  		}
   112  		if progressReport.FromDate == "" || progressReport.ToDate == "" {
   113  			m := models.NewMessageResponse(fmt.Sprintf("progress report not found on transaction with ID: [%s]", transactionID))
   114  			utils.WriteJSONWithStatus(w, req, m, http.StatusNotFound)
   115  			return
   116  		}
   117  
   118  		log.InfoR(req, fmt.Sprintf("successfully retrieved progress report resource with transaction ID: %s, from mongo", transactionID))
   119  
   120  		utils.WriteJSONWithStatus(w, req, transformers.ProgressReportDaoToResponse(progressReport), http.StatusOK)
   121  	})
   122  }
   123  
   124  // HandleDeleteProgressReport deletes a progress report resource from an insolvency case
   125  func HandleDeleteProgressReport(svc dao.Service, helperService utils.HelperService) http.Handler {
   126  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
   127  
   128  		transactionID, isValidTransaction := utils.ValidateTransaction(helperService, req, w, "progress report", service.CheckIfTransactionClosed)
   129  		if !isValidTransaction {
   130  			return
   131  		}
   132  
   133  		// Delete progress report from DB
   134  		statusCode, err := svc.DeleteProgressReportResource(transactionID)
   135  		if err != nil {
   136  			log.ErrorR(req, err)
   137  			m := models.NewMessageResponse(err.Error())
   138  			utils.WriteJSONWithStatus(w, req, m, statusCode)
   139  			return
   140  		}
   141  
   142  		log.InfoR(req, fmt.Sprintf("successfully deleted progress report from insolvency case with transaction ID: %s", transactionID))
   143  
   144  		w.Header().Set("Content-Type", "application/json")
   145  		w.WriteHeader(statusCode)
   146  	})
   147  }
   148