/// <summary> /// Creates and saves a new Appointment Session and Appointment Slots per the config specification /// </summary> /// <param name="config"></param> /// <returns></returns> public async Task <AppointmentBookResults> CreateNewAppointmentSessionAsync(AppointmentSessionConfiguration config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } // Generate the new session and slots per the config var newSession = GenerateNewAppointmentSession(config); //Attempt to save the new session to the db var result = await _apptSessionDal.AddAsync(newSession); if (result == null) { // Could not create the new session as another already exists in the same time block for the same Medical Practitioner return(new AppointmentBookResults() { ResultCode = ServiceResultStatusCode.AlreadyExists }); } // Convert the saved session to an AppointmentSessionDetails object for return var sessionDetails = new AppointmentSessionDetails(newSession); // Return results return(new AppointmentBookResults() { ResultCode = ServiceResultStatusCode.Success, AppointmentSessionDetails = new List <AppointmentSessionDetails>() { sessionDetails } }); }
/// <summary> /// Generates the Appointment Session and Appointment Slots per the config specification /// </summary> /// <param name="config"></param> /// <returns></returns> private AppointmentSession GenerateNewAppointmentSession(AppointmentSessionConfiguration config) { var slotDuration = config.DurationInMins / config.NumberOfAppointmentSlots; var slots = new List <AppointmentSlot>(); var appointmentDateTime = config.StartDateTime; for (var i = 0; i < config.NumberOfAppointmentSlots; i++) { slots.Add(new AppointmentSlot() { State = SlotState.Available, AppointmentDateTime = appointmentDateTime, AppointmentDurationInMins = slotDuration, }); //Advance the start time of the next appointment appointmentDateTime = appointmentDateTime.AddMinutes(slotDuration); } var session = new AppointmentSession() { DurationInMins = config.DurationInMins, StartDateTime = config.StartDateTime, MedicalPractitionerId = config.MedicalPractitionerId, AppointmentSlots = slots }; return(session); }
/// <summary> /// Creates and saves a new Appointment Session and Appointment Slots per the config specification /// </summary> /// <param name="config"></param> /// <returns></returns> public Task <AppointmentBookResults> CreateNewAppointmentSession(AppointmentSessionConfiguration config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } return(_sessionMgr.CreateNewAppointmentSessionAsync(config)); }
public async Task <IActionResult> CreateSession([FromForm] CreateAppointmentSessionViewModel createSessionDetails) { if (createSessionDetails == null) { _log.LogError($"Failed to create Appointment Session. No Create Session Details received."); Alert("Failed to create Appointment Session due to an internal error.", AlertType.danger); return(RedirectToAction(nameof(Index))); } // Round up the session duration to the next 5 mins var validSessionDuration = createSessionDetails.DurationInMins; if (createSessionDetails.DurationInMins < 5) { validSessionDuration = 5; } if (validSessionDuration % 5 != 0) { var remainder = validSessionDuration % 5; validSessionDuration = remainder >= 3 ? validSessionDuration + (5 - remainder) : validSessionDuration - remainder; } var validStartDateTime = new DateTime(createSessionDetails.Date.Year, createSessionDetails.Date.Month, createSessionDetails.Date.Day, createSessionDetails.StartTime.Hour, createSessionDetails.StartTime.Minute, createSessionDetails.StartTime.Second); var newSessionConfig = new AppointmentSessionConfiguration() { DurationInMins = validSessionDuration, StartDateTime = validStartDateTime, NumberOfAppointmentSlots = createSessionDetails.NumberOfAppointmentSlots, MedicalPractitionerId = createSessionDetails.MedicalPractitionerId }; var result = await _apptSvc.CreateNewAppointmentSession(newSessionConfig); if (result.ResultCode != ServiceResultStatusCode.Success) { _log.LogError($"Failed to create Appointment Session."); Alert("Failed to create Appointment Session.", AlertType.danger); return(RedirectToAction(nameof(Index))); } Alert("Successfully created Appointment Session.", AlertType.success); return(RedirectToAction(nameof(Index))); }