public void UpdateAllocationReturns200WhenSuccessful() { var request = TestHelpers.CreateUpdateAllocationRequest().Item1; var result = new UpdateAllocationResponse() { CaseNoteId = _fixture.Create <string>() }; _mockAllocationsUseCase.Setup(x => x.ExecuteUpdate(It.IsAny <UpdateAllocationRequest>())).Returns(result); var response = _classUnderTest.UpdateAllocation(request) as OkObjectResult; response?.StatusCode.Should().Be(200); response?.Value.Should().BeEquivalentTo(result); }
public void UpdateAllocationReturnsCorrectCaseNoteId() { UpdateAllocationRequest request = new UpdateAllocationRequest() { Id = 1 }; UpdateAllocationResponse expectedResponse = new UpdateAllocationResponse() { CaseNoteId = _fixture.Create <string>() }; _mockDatabaseGateway.Setup(x => x.UpdateAllocation(It.Is <UpdateAllocationRequest>(x => x == request))).Returns(expectedResponse); var response = _allocationsUseCase.ExecuteUpdate(request); Assert.AreEqual(expectedResponse.CaseNoteId, response.CaseNoteId); }
public UpdateAllocationResponse UpdateAllocation(UpdateAllocationRequest request) { var response = new UpdateAllocationResponse(); try { var allocation = _databaseContext.Allocations.FirstOrDefault(x => x.Id == request.Id); if (allocation == null) { throw new EntityUpdateException($"Allocation {request.Id} not found"); } if (allocation.CaseStatus?.ToUpper() == "CLOSED") { throw new UpdateAllocationException("Allocation already closed"); } var(person, createdBy) = GetUpdateAllocationRequirements(allocation, request); //copy existing values in case adding note fails var tmpAllocation = (AllocationSet)allocation.Clone(); SetDeallocationValues(allocation, request.DeallocationDate, request.CreatedBy); _databaseContext.SaveChanges(); try { var note = new DeallocationCaseNote { FirstName = person.FirstName, LastName = person.LastName, MosaicId = person.Id.ToString(), Timestamp = DateTime.Now.ToString("dd/MM/yyyy H:mm:ss"), WorkerEmail = createdBy.Email, //required for my cases search DeallocationReason = request.DeallocationReason, FormNameOverall = "API_Deallocation", //prefix API notes so they are easy to identify FormName = "Worker deallocated", AllocationId = request.Id.ToString(), CreatedBy = request.CreatedBy }; var caseNotesDocument = new CaseNotesDocument() { CaseFormData = JsonConvert.SerializeObject(note) }; response.CaseNoteId = _processDataGateway.InsertCaseNoteDocument(caseNotesDocument).Result; } catch (Exception ex) { var allocationToRestore = _databaseContext.Allocations.FirstOrDefault(x => x.Id == request.Id); RestoreAllocationValues(tmpAllocation, allocationToRestore); _databaseContext.SaveChanges(); throw new UpdateAllocationException( $"Unable to create a case note. Allocation not updated: {ex.Message}"); } } catch (Exception ex) { throw new EntityUpdateException($"Unable to update allocation {request.Id}: {ex.Message}"); } return(response); }