Exemplo n.º 1
0
        private (Person, Worker) GetUpdateAllocationRequirements(AllocationSet allocation,
                                                                 UpdateAllocationRequest request)
        {
            if (allocation.CaseStatus?.ToUpper() == "CLOSED")
            {
                throw new UpdateAllocationException("Allocation already closed");
            }

            var worker = _workerGateway.GetWorkerByWorkerId(allocation.WorkerId ?? 0);

            if (worker == null)
            {
                throw new UpdateAllocationException("Worker not found");
            }

            var person = _databaseContext.Persons.FirstOrDefault(x => x.Id == allocation.PersonId);

            if (person == null)
            {
                throw new UpdateAllocationException("Person not found");
            }

            var createdBy =
                _databaseContext.Workers.FirstOrDefault(x => x.Email.ToUpper().Equals(request.CreatedBy.ToUpper()));

            if (createdBy == null)
            {
                throw new UpdateAllocationException("CreatedBy not found");
            }

            return(person, createdBy);
        }
Exemplo n.º 2
0
        public void UpdateAllocationCallsDatabaseGatewayWithParameters()
        {
            UpdateAllocationRequest request = new UpdateAllocationRequest()
            {
                Id = 1
            };

            _allocationsUseCase.ExecuteUpdate(request);

            _mockDatabaseGateway.Verify(x => x.UpdateAllocation(It.Is <UpdateAllocationRequest>(x => x == request)), Times.Once);
        }
Exemplo n.º 3
0
        public void UpdateAllocationCallsDatabaseGateway()
        {
            UpdateAllocationRequest request = new UpdateAllocationRequest()
            {
                Id = 1, DeallocationReason = "reason"
            };

            _allocationsUseCase.ExecuteUpdate(request);

            _mockDatabaseGateway.Verify(x => x.UpdateAllocation(request));
        }
Exemplo n.º 4
0
        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 IActionResult UpdateAllocation([FromBody] UpdateAllocationRequest request)
        {
            var validator         = new UpdateAllocationRequestValidator();
            var validationResults = validator.Validate(request);

            if (!validationResults.IsValid)
            {
                return(BadRequest(validationResults.ToString()));
            }

            try
            {
                return(Ok(_allocationUseCase.ExecuteUpdate(request)));
            }
            catch (EntityUpdateException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (UpdateAllocationException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
 public UpdateAllocationResponse ExecuteUpdate(UpdateAllocationRequest request)
 {
     return(_databaseGateway.UpdateAllocation(request));
 }
Exemplo n.º 7
0
        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);
        }