public async Task <IActionResult> PostStopService([FromBody] MoveOutStopServiceRequest stopServiceRequest) { IActionResult result; _logger.LogInformation($"PostStopService: {JsonConvert.SerializeObject(stopServiceRequest, Formatting.Indented)}"); try { var response = await _moveOutLogic.StopService(stopServiceRequest); if (response == null) { result = NotFound(); } else { result = Ok(response); _logger.LogInformation($"PostStopService: {JsonConvert.SerializeObject(response, Formatting.Indented)}"); } } catch (Exception e) { _logger.LogError(e, $"Error in PostStopService: {JsonConvert.SerializeObject(stopServiceRequest, Formatting.Indented)}"); result = e.ToActionResult(); } return(result); }
public void PostStopService_Valid_Request_Returns_Valid_Response() { // Arrange var logicResonse = new MoveOutStopServiceResponse { WarmHomeFund = 123.45M, FinalBillDate = DateTimeOffset.Now, FinalBillDueDate = DateTimeOffset.Now.AddDays(15), Status = new Dictionary <long, string> { { 12345678901, "Some status message 1" }, { 10987654321, "Some status message 2" } } }; MoveOutLogicMock.Setup(lm => lm.StopService(It.IsAny <MoveOutStopServiceRequest>())).Returns(Task.FromResult(logicResonse)); var controller = GetController(); var request = new MoveOutStopServiceRequest { ContractAccountId = 12345678901, MoveOutDate = DateTimeOffset.Now, InstallationIds = new List <long> { 12345678901, 10987654321 } }; // Act var response = controller.PostStopService(request); // Assert response.Result.ShouldBeOfType <OkObjectResult>(); var result = ((OkObjectResult)response.Result).Value as MoveOutStopServiceResponse; result.ShouldNotBeNull(); result.Status.Count.ShouldBe(2); }
public void PostStopService_InValid_Request_Returns_NotFound_Response() { // Arrange MoveOutStopServiceResponse logicResonse = null; MoveOutLogicMock.Setup(lm => lm.StopService(It.IsAny <MoveOutStopServiceRequest>())).Returns(Task.FromResult(logicResonse)); var controller = GetController(); var request = new MoveOutStopServiceRequest { ContractAccountId = 12345678901, MoveOutDate = DateTimeOffset.Now, InstallationIds = new List <long> { 12345678901, 10987654321 } }; // Act var response = controller.PostStopService(request); // Assert response.Result.ShouldBeOfType <NotFoundResult>(); }
/// <inheritdoc /> public async Task <StopServiceResponse> StopService(MoveOutStopServiceRequest stopServiceRequest) { const int BillDueDays = 15; var response = new StopServiceResponse(); response.Status = new Dictionary <long, string>(); // ContractItem and installation have a 1 to 1 relationship. // We need to convert installation Ids from the caller to contractItem Ids. if (stopServiceRequest.InstallationIds != null && stopServiceRequest.InstallationIds.Count > 0) { // Get all ContractItems for the contract account id var contractItems = (await _accountApi.GetContractItems(stopServiceRequest.ContractAccountId)).ContractItems; if (contractItems.Count > 0) { // Success is used to avoid calling Warm Home Fund endpoint if not needed bool success = false; // Check each installation passed in the request foreach (long installationId in stopServiceRequest.InstallationIds) { var installation = await _deviceApi.GetInstallationDetail(installationId); if (installation?.Data?.Installation != null) { // Get the contract info for that installlation var contractItem = contractItems.Find(ci => ci.PointOfDeliveryGuid.ToLower() == installation.Data.Installation.InstallationGuid.ToLower()); // If they match on installation Guid attempt stop service if (contractItem != null) { var mcfResponse = _mcfClient.StopService(contractItem.ContractId, contractItem.PremiseId, stopServiceRequest.MoveOutDate); if (mcfResponse.Error == null) { response.Status.Add(installationId, "Stop service request succeeded."); success = true; } else { // Stop service mcf request failed response.Status.Add(installationId, $"{mcfResponse.Error.Code}: {mcfResponse.Error.Message.Value}."); } } else { // Contract match by installationGuid failed response.Status.Add(installationId, $"InstallationId not found for this contractAccountId."); } } else { // InstallationId lookup failed response.Status.Add(installationId, "InstallationId not found."); } } // Add warm home fund amount & dates from request data if any succeeded if (success) { response.FinalBillDate = stopServiceRequest.MoveOutDate; response.FinalBillDueDate = stopServiceRequest.MoveOutDate.AddDays(BillDueDays); // Get warmHomeFundAmount from account service var contractDetails = await _accountApi.GetContractAccountDetails(stopServiceRequest.ContractAccountId); if (contractDetails != null) { response.WarmHomeFund = contractDetails.WarmHomeFundAmount; } else { // Since actual Stop Service calls have succeeded, if this fails don't throw error, just log. // An alternative would be to go back and cancel each stop however Cancel function is out of scope for this project per Lucas. _logger.LogError($"Error in StopService. Unable to get WarmHomeFund value from account service for contractAccountId: {stopServiceRequest.ContractAccountId}."); } } } else { // ContractItems lookup for contractAccountId failed - 404? response = null; } } else { // Null or empty installationId list - 404? response = null; } return(response); }