public void CanMapCreateAllocationRequestDomainObjectToDatabaseEntity()
        {
            var personId   = _faker.Random.Long();
            var createdBy  = _faker.Internet.Email();
            var workerId   = _faker.Random.Number();
            var dt         = DateTime.Now;
            var caseStatus = "Open";

            var allocationRequest = new CreateAllocationRequest()
            {
                MosaicId          = personId,
                CreatedBy         = createdBy,
                AllocatedWorkerId = workerId
            };

            var expectedResponse = new AllocationSet()
            {
                PersonId            = personId,
                WorkerId            = workerId,
                AllocationStartDate = dt,
                CaseStatus          = caseStatus,
                CreatedBy           = createdBy
            };

            allocationRequest.ToEntity(workerId, dt, caseStatus).Should().BeEquivalentTo(expectedResponse);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public CreateAllocationResponse CreateAllocation(CreateAllocationRequest request)
        {
            var(worker, team, person, allocatedBy) = GetCreateAllocationRequirements(request);

            var allocation = new AllocationSet
            {
                PersonId            = person.Id,
                WorkerId            = worker.Id,
                TeamId              = team.Id,
                AllocationStartDate = request.AllocationStartDate,
                CaseStatus          = "Open",
                CreatedBy           = allocatedBy.Email
            };

            _databaseContext.Allocations.Add(allocation);
            _databaseContext.SaveChanges();


            var response = new CreateAllocationResponse();

            //Add note
            try
            {
                var dt = DateTime.Now;

                var note = new AllocationCaseNote
                {
                    FirstName   = person.FirstName,
                    LastName    = person.LastName,
                    MosaicId    = person.Id.ToString(),
                    Timestamp   = dt.ToString("dd/MM/yyyy H:mm:ss"), //in line with imported form data
                    WorkerEmail = allocatedBy.Email,
                    Note        =
                        $"{dt.ToShortDateString()} | Allocation | {worker.FirstName} {worker.LastName} in {team.Name} was allocated to this person (by {allocatedBy.FirstName} {allocatedBy.LastName})",
                    FormNameOverall = "API_Allocation",
                    FormName        = "Worker allocated",
                    AllocationId    = allocation.Id.ToString(),
                    CreatedBy       = request.CreatedBy
                };

                var caseNotesDocument = new CaseNotesDocument()
                {
                    CaseFormData = JsonConvert.SerializeObject(note)
                };

                response.CaseNoteId   = _processDataGateway.InsertCaseNoteDocument(caseNotesDocument).Result;
                response.AllocationId = allocation.Id;
            }
            catch (Exception ex)
            {
                //roll back allocation record
                _databaseContext.Allocations.Remove(allocation);
                _databaseContext.SaveChanges();

                throw new UpdateAllocationException(
                          $"Unable to create a case note. Allocation not created: {ex.Message}");
            }

            return(response);
        }
Beispiel #4
0
 private static void RestoreAllocationValues(AllocationSet tmpAllocation, AllocationSet allocationToRestore)
 {
     allocationToRestore.AllocationEndDate = tmpAllocation.AllocationEndDate;
     allocationToRestore.CaseStatus        = tmpAllocation.CaseStatus;
     allocationToRestore.WorkerId          = tmpAllocation.WorkerId;
     allocationToRestore.TeamId            = tmpAllocation.TeamId;
     allocationToRestore.CaseClosureDate   = tmpAllocation.CaseClosureDate;
 }
Beispiel #5
0
 private static AllocationSet SetDeallocationValues(AllocationSet allocation, DateTime dt, string modifiedBy)
 {
     //keep workerId and TeamId in the record so they can be easily exposed to front end
     allocation.AllocationEndDate = dt;
     allocation.CaseStatus        = "Closed";
     allocation.CaseClosureDate   = dt;
     allocation.LastModifiedBy    = modifiedBy;
     return(allocation);
 }