Example #1
0
        public void InsertPatient_MissingCondition()
        {
            var patient = new Patient
            {
                Name = "Joakim"
            };

            _context.Add(patient);
            Assert.Throws <DbUpdateException>(() => _context.SaveChanges());
        }
        private void ImportRooms(JsonDataFileImporterReader importer)
        {
            var rooms = importer.Rooms(_filePath);

            foreach (var room in rooms)
            {
                var r = new TreatmentRoom
                {
                    Name = room.Name,
                    TreatmentMachineId = room.TreatmentMachine
                };
                _context.Add(r);
            }
            _context.SaveChanges();
        }
Example #3
0
            public async Task <Result> Handle(Command message)
            {
                PatientDomain patient;

                try
                {
                    patient = AutoMapper.Mapper.Map <PatientDomain>(message);
                }
                catch (Enumeration.InvalidValueException ive)
                {
                    return(Result.InvalidCondition(nameof(Command.Condition), ive.Value, ive.Values));
                }
                catch
                {
                    return(Result.Exception());
                }

                try
                {
                    _context.Add(patient);
                    await _context.SaveChangesAsync();

                    // This should be put on MQ for safe processing
                    await _mediator.Publish(new Consultation.Schedule.PatientRegistered(patient.Id));

                    return(new Result(patient.Id));
                }
                catch
                {
                    return(Result.Exception());
                }
            }
Example #4
0
        public void InsertConsultation()
        {
            var patient      = _context.Set <Patient>().First();
            var consultation = new Consultation
            {
                ConsultationDate = DateTime.UtcNow.Date,
                TreatmentRoomId  = "One",
                DoctorId         = 1,
                Patient          = patient
            };

            _context.Add(consultation);
            var affected = _context.SaveChanges();

            Assert.Equal(1, affected);
        }
Example #5
0
            public async Task Handle(PatientRegistered notification)
            {
                var patient = await _context.Set <Data.Domain.Patient>()
                              .Include(p => p.Condition)
                              .FirstAsync(p => p.Id == notification.Id);

                // Find those doctors that has
                // the correct roles to treat the patient
                var doctorResources = new List <Data.Domain.Doctor>();
                var doctors         = await _context.Set <Data.Domain.Doctor>()
                                      .Include(d => d.Roles)
                                      .ThenInclude(r => r.Role)
                                      .ToListAsync();

                foreach (var doctor in doctors)
                {
                    if (doctor.CanTreat(patient))
                    {
                        doctorResources.Add(doctor);
                    }
                }
                // The doctor resources that can treat the patient's condition are known
                var treatmentRooms = await _context.Set <Data.Domain.TreatmentRoom>()
                                     .Include(r => r.TreatmentMachine)
                                     .ThenInclude(m => m.Capability)
                                     .ToListAsync();

                var treatmentRoomsResources = new Dictionary <string, Data.Domain.TreatmentRoom>();

                foreach (var room in treatmentRooms)
                {
                    foreach (var doctor in doctorResources)
                    {
                        if (doctor.CanBook(patient, room) && !treatmentRoomsResources.ContainsKey(room.Name))
                        {
                            treatmentRoomsResources.Add(room.Name, room);
                        }
                    }
                }
                // So we have doctor resources and treatment room resources that conforms
                // to the rules for treating the patient.
                // Check time and resource constraint
                // for doctor and rooms to create consultation record as soon as possible
                // but no earlier than the patient was registered
                var startDate           = patient.Registered;
                var consultationCreated = false;

                do
                {
                    startDate = startDate.AddDays(1);
                    foreach (var doctor in doctorResources)
                    {
                        // Check if doctor is already booked for this day then we can skip check
                        var doctorBusy = await _context.Set <Data.Domain.Consultation>()
                                         .AnyAsync(c => c.DoctorId == doctor.Id && c.ConsultationDate == startDate);

                        if (doctorBusy)
                        {
                            continue;
                        }
                        foreach (var room in treatmentRoomsResources.Values)
                        {
                            var roomBusy = await _context.Set <Data.Domain.Consultation>()
                                           .AnyAsync(c => c.TreatmentRoomId == room.TreatmentMachineId &&
                                                     c.ConsultationDate == startDate);

                            if (roomBusy)
                            {
                                continue;
                            }
                            var consultation = new Data.Domain.Consultation
                            {
                                Patient          = patient,
                                Doctor           = doctor,
                                TreatmentRoom    = room,
                                ConsultationDate = startDate
                            };
                            try
                            {
                                _context.Add(consultation);
                                await _context.SaveChangesAsync();

                                consultationCreated = true;
                                break;
                            }
                            catch (DbUpdateException)
                            {
                                // Consultation could not be created due to
                                // unique constraint violation. Either
                                // 1. DoctorId and ConsultationDate
                                // 2. TreatmentRoomId and ConsultationDate
                                // 3. PatientId and ConsultationDate
                                _context.Entry(consultation).State = EntityState.Detached;
                            }
                            catch (Exception)
                            {
                                // Consultation could not be created
                                _context.Entry(consultation).State = EntityState.Detached;
                            }
                        }
                        if (consultationCreated)
                        {
                            break;
                        }
                    }
                } while (!consultationCreated);
            }