public async Task <Unit> Handle(UpdateAppointmentCommand request, CancellationToken cancellationToken)
        {
            var appointment = await this.context.Appointments
                              .SingleOrDefaultAsync(c => c.Id == request.Id && c.IsDeleted != true, cancellationToken);

            if (appointment == null)
            {
                throw new NotFoundException(GConst.Appointment, request.Id);
            }

            var service = await this.context.Services.FindAsync(request.ServiceId);

            if (service == null || service.IsDeleted == true)
            {
                throw new UpdateFailureException(GConst.Appointment, request.Id, string.Format(GConst.RefereceException, GConst.ServiceLower, request.ServiceId));
            }

            var employee = await this.context.Employees.FindAsync(request.EmployeeId);

            if (employee == null || employee.IsDeleted == true)
            {
                throw new UpdateFailureException(GConst.Appointment, request.Id, string.Format(GConst.RefereceException, GConst.EmployeeLower, request.EmployeeId));
            }

            // Set Time
            request.ReservationTime = DateTime.Parse(request.TimeBlockHelper);

            // CheckWorkingHours
            DateTime start = request.ReservationDate.Add(request.ReservationTime.Value.TimeOfDay);
            DateTime end   = request.ReservationDate.Add(request.ReservationTime.Value.TimeOfDay).AddMinutes(double.Parse(this.context.EmployeeServices.Find(employee.Id, service.Id).DurationInMinutes));

            if (!AppointmentHelper.IsInWorkingHours(this.context, employee, start, end))
            {
                throw new UpdateFailureException(GConst.Appointment, request.TimeBlockHelper, string.Format(GConst.InvalidAppointmentHourException, employee.Location.StartHour, employee.Location.EndHour));
            }
            ;

            appointment.ReservationTime = request.ReservationTime.Value;
            appointment.ReservationDate = request.ReservationDate;
            appointment.TimeBlockHelper = request.TimeBlockHelper;
            appointment.Comment         = request.Comment;
            appointment.ServiceId       = request.ServiceId;
            appointment.EmployeeId      = request.EmployeeId;
            appointment.ModifiedOn      = DateTime.UtcNow;

            this.context.Appointments.Update(appointment);
            await this.context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #2
0
        public async Task <AvailableAppointmentsViewModel> Handle(GetAvailableAppointmentsQuery request, CancellationToken cancellationToken)
        {
            var command = request.Command;
            var availableAppointments = new AvailableAppointmentsViewModel();

            var employeeService = await this.context.EmployeeServices.FindAsync(command.EmployeeId, command.ServiceId);

            var employee = await this.context.Employees.Include(emp => emp.Location).SingleOrDefaultAsync(emp => emp.Id == command.EmployeeId);

            var startHour = employee.Location.StartHour;
            var endHour   = employee.Location.EndHour;
            var duration  = employeeService.DurationInMinutes;

            int time, start, end;

            time  = int.Parse(duration);
            start = int.Parse(startHour);
            end   = int.Parse(endHour);

            TimeBlock timeBlock = new TimeBlockExtension(new DateTime(command.ReservationDate.Year, command.ReservationDate.Month, command.ReservationDate.Day, start, 0, 0), new TimeSpan(0, time, 0));

            List <SelectListItem> itemsList = new List <SelectListItem>();

            // No Appointments for past!!
            while (timeBlock.Start.CompareTo(DateTime.Now) <= 0)
            {
                timeBlock.Move(new TimeSpan(0, time, 0));
                if (!AppointmentHelper.IsInWorkingHours(startHour, endHour, timeBlock, employee))
                {
                    break;
                }
            }

            var appointments = this.context.Appointments.Where(x => x.EmployeeId == command.EmployeeId && x.IsDeleted != true);

            bool overlaps = false;

            while (AppointmentHelper.IsInWorkingHours(startHour, endHour, timeBlock, employee))
            {
                foreach (var appointment in appointments)
                {
                    var       serviceDuration = int.Parse(this.context.EmployeeServices.Find(appointment.EmployeeId, appointment.ServiceId).DurationInMinutes);
                    TimeBlock BookedTimeBlock = new TimeBlockExtension(appointment.ReservationDate.Date.Add(appointment.ReservationTime.TimeOfDay), new TimeSpan(0, serviceDuration, 0));
                    if (BookedTimeBlock.OverlapsWith(timeBlock))
                    {
                        overlaps = true;
                    }
                }

                if (!overlaps)
                {
                    itemsList.Add(new SelectListItem()
                    {
                        Text = timeBlock.ToString(), Value = timeBlock.Start.ToString("HH:mm")
                    });
                }

                overlaps = false;
                timeBlock.Move(new TimeSpan(0, time, 0));
            }

            if (itemsList.Count != 0)
            {
                availableAppointments.AvailableAppointments = itemsList;
                return(availableAppointments);
            }

            itemsList.Add(new SelectListItem()
            {
                Text = GConst.NotAvalableAppointments, Value = GConst.AllHoursBusy
            });

            availableAppointments.AvailableAppointments = itemsList;
            return(availableAppointments);
        }
        public async Task <Unit> Handle(CreateAppointmentCommand request, CancellationToken cancellationToken)
        {
            var user = await this.context.StudioUsers.FindAsync(request.UserId);

            if (user == null || user.IsDeleted == true)
            {
                throw new CreateFailureException(GConst.Appointment, request.UserId, string.Format(GConst.RefereceException, GConst.UserLower, request.UserId));
            }

            var service = await this.context.Services.FindAsync(request.ServiceId);

            if (service == null || service.IsDeleted == true)
            {
                throw new CreateFailureException(GConst.Appointment, request.ServiceId, string.Format(GConst.RefereceException, GConst.ServiceLower, request.ServiceId));
            }

            var employee = await this.context.Employees.Include(e => e.Location).SingleOrDefaultAsync(e => e.Id == request.EmployeeId);

            if (employee == null || employee.IsDeleted == true)
            {
                throw new CreateFailureException(GConst.Appointment, request.EmployeeId, string.Format(GConst.RefereceException, GConst.EmployeeLower, request.EmployeeId));
            }

            if (request.TimeBlockHelper == GConst.AllHoursBusy)
            {
                throw new CreateFailureException(GConst.Appointment, request.UserId, string.Format(GConst.NotAvalableHours, request.ReservationDate.ToShortDateString()));
            }

            // Set Time
            request.ReservationTime = DateTime.Parse(request.TimeBlockHelper);

            // CheckWorkingHours
            DateTime start = request.ReservationDate.Add(request.ReservationTime.Value.TimeOfDay);
            DateTime end   = request.ReservationDate.Add(request.ReservationTime.Value.TimeOfDay).AddMinutes(double.Parse(this.context.EmployeeServices.Find(employee.Id, service.Id).DurationInMinutes));

            if (!AppointmentHelper.IsInWorkingHours(this.context, employee, start, end))
            {
                throw new CreateFailureException(GConst.Appointment, request.UserId, string.Format(GConst.InvalidAppointmentHourException, employee.Location.StartHour, employee.Location.EndHour));
            }

            // Check Appointment Clash
            string check = AppointmentHelper.ValidateNoAppoinmentClash(this.context, request);

            if (check != string.Empty)
            {
                throw new CreateFailureException(GConst.Appointment, request.UserId, check);
            }

            var appointment = new Appointment
            {
                ReservationTime = request.ReservationTime.Value,
                ReservationDate = request.ReservationDate,
                TimeBlockHelper = request.TimeBlockHelper,
                Comment         = request.Comment,
                ServiceId       = request.ServiceId,
                EmployeeId      = request.EmployeeId,
                UserId          = user.Id,
                CreatedOn       = DateTime.UtcNow,
                IsDeleted       = false
            };

            this.context.Appointments.Add(appointment);

            await this.context.SaveChangesAsync(cancellationToken);

            this.emailSender.ConfigureSendGridEmailSender(this.loggerFactory, GConst.ApiKey, GConst.SenderEmail, GConst.SenderName);
            await this.emailSender.SendEmailAsync(user.Email, GConst.AppointmentSubject, string.Format(GConst.AppointmentMessage, service.Name, employee.Location.Name, request.TimeBlockHelper));

            return(Unit.Value);
        }