public void CancelAppointment([FromBody] CancelAppointmentDto cancelAppointmentDto) { using (var dbContext = new ApplicationDbContext()) { var userId = GetUserId(); var appointment = dbContext.Clinic_Appointments.FirstOrDefault(a => a.Id == cancelAppointmentDto.Id); if (appointment == null) { throw new BadRequestException(ExceptionMessages.BadRequest); } var patient = dbContext.Clinic_Patients.FirstOrDefault(p => p.Id == appointment.PatientId); if (patient == null || patient.Client.UserId != userId) { throw new BadRequestException(ExceptionMessages.BadRequest); } var maxCancelDateTime = appointment.DateTime.AddHours(-24); if (DateTime.Now >= maxCancelDateTime) { throw new BadRequestException(ExceptionMessages.AppointmentCantBeCanceled); } appointment.State = AppointmentStateEnum.Cancelled; dbContext.SaveChanges(); } }
public void CancelAppointmentByClinic([FromBody] CancelAppointmentDto cancelAppointmentDto) { var emailMessage = new EmailMessage(); using (var dbContext = new ApplicationDbContext()) { var userId = GetUserId(); var appointment = dbContext.Clinic_Appointments.FirstOrDefault(a => a.Id == cancelAppointmentDto.Id && a.UserId == userId); if (appointment == null) { throw new BadRequestException(ExceptionMessages.BadRequest); } var clinic = dbContext.Clinics.FirstOrDefault(c => c.UserId == userId); if (clinic == null) { throw new BadRequestException(ExceptionMessages.BadRequest); } var maxCancelDateTime = appointment.DateTime.AddHours(-24); if (DateTime.Now >= maxCancelDateTime) { throw new BadRequestException(ExceptionMessages.AppointmentCantBeCanceled); } emailMessage = new EmailMessage { Subject = $"{clinic.Name} - Cancelacion de turno", To = new List <string> { appointment.Patient.Client.User.Email }, Message = cancelAppointmentDto.Comment }; appointment.State = AppointmentStateEnum.Cancelled; dbContext.SaveChanges(); } EmailSender.Send(emailMessage); }