def get_available_slots(office: Office, days: [datetime], format_time: bool = True, service: Service = None): """Return the available slots for the office""" try: available_slots_per_day = {} if office.appointments_enabled_ind == 0: return available_slots_per_day # find appointment duration per office and fetch timeslot master data appointment_duration = office.appointment_duration # If user has passed in service and it has duration, use that instead if (service and service.timeslot_duration): appointment_duration = service.timeslot_duration service_is_dltk = service and service.is_dlkt == YesNo.YES # Dictionary to store the available slots per day tz = pytz.timezone(office.timezone.timezone_name) # today's date and time today = datetime.datetime.now().astimezone(tz) # soonest a citizen can book an appointment soonest_appointment_date = today + datetime.timedelta(minutes = office.soonest_appointment or 0) # Find all appointments between the dates appointments = Appointment.find_appointment_availability(office_id=office.office_id, first_date=today, last_date=days[-1], timezone=office.timezone.timezone_name) grouped_appointments = AvailabilityService.group_appointments(appointments, office.timezone.timezone_name) # For each of the day calculate the slots based on time slots for day_in_month in days: formatted_date = day_in_month.strftime('%m/%d/%Y') available_slots_per_day[formatted_date] = [] for timeslot in office.timeslots: # Calculate the slots per day timeslot_end_time = timeslot.end_time.replace(tzinfo=tz) timeslot_start_time = timeslot.start_time.replace(tzinfo=tz) if day_in_month.isoweekday() in day_indexes(timeslot.day_of_week): start_time = timeslot_start_time end_time = add_delta_to_time(timeslot_start_time, minutes=appointment_duration, timezone=office.timezone.timezone_name) # Cannot exceed office timeslot slots. dlkt_slots = office.number_of_dlkt or 0 if ( dlkt_slots > timeslot.no_of_slots): dlkt_slots = timeslot.no_of_slots # Limit DLKT slots only for DLKT services. no_of_slots = timeslot.no_of_slots while end_time <= timeslot_end_time: slot = { 'start_time': start_time, 'end_time': end_time, 'no_of_slots': no_of_slots, 'no_of_dlkt_slots': dlkt_slots } # Check if today's time is past appointment slot # Arc - also check if in office.soonest_appointment if ((day_in_month.date() == soonest_appointment_date.date() and start_time >= soonest_appointment_date.time()) or day_in_month.date() > soonest_appointment_date.date()): if slot not in available_slots_per_day[formatted_date]: available_slots_per_day[formatted_date].append(slot) start_time = end_time.replace(tzinfo=tz) end_time = add_delta_to_time(end_time, minutes=appointment_duration, timezone=office.timezone.timezone_name) # Sort the slot by time for the day available_slots_per_day[formatted_date].sort(key=lambda x: x['start_time']) # Check if the slots are already booked for actual_slot in available_slots_per_day[formatted_date]: booked_slots = 0 booked_dlkt_slots = 0 for booked_slot in grouped_appointments.get(formatted_date, []): if booked_slot.get('start_time') \ <= actual_slot.get('start_time') \ < booked_slot.get('end_time') \ or \ actual_slot.get('end_time') \ > booked_slot.get('start_time') \ >= actual_slot.get('start_time'): if booked_slot.get('blackout_flag', False): # If it's blackout override the no of slots actual_slot['no_of_slots'] = 0 else: if booked_slot['is_dlkt']: booked_dlkt_slots += 1 else: booked_slots += 1 if service_is_dltk: dlkt_nos = actual_slot['no_of_dlkt_slots'] - booked_dlkt_slots if actual_slot['no_of_slots'] <= (booked_slots + booked_dlkt_slots): actual_slot['no_of_slots'] = 0 elif actual_slot['no_of_slots'] - booked_slots >= dlkt_nos: actual_slot['no_of_slots'] = dlkt_nos else: actual_slot['no_of_slots'] = dlkt_nos - (actual_slot['no_of_slots'] - booked_slots) else: actual_slot['no_of_slots'] = actual_slot['no_of_slots'] - (booked_slots + booked_dlkt_slots) del actual_slot['no_of_dlkt_slots'] # no need to expose if format_time: # If true send formatted time actual_slot['start_time'] = actual_slot['start_time'].strftime('%H:%M') actual_slot['end_time'] = actual_slot['end_time'].strftime('%H:%M') return AvailabilityService.prune_appointments(available_slots_per_day) except exc.SQLAlchemyError as e: print(e) return {'message': 'API is down'}, 500
def get_available_slots(office: Office, days: [datetime], format_time: bool = True): """Return the available slots for the office""" try: available_slots_per_day = {} if office.appointments_enabled_ind == 0: return available_slots_per_day # find appointment duration per office and fetch timeslot master data appointment_duration = office.appointment_duration # Dictionary to store the available slots per day tz = pytz.timezone(office.timezone.timezone_name) # today's date and time today = datetime.datetime.now().astimezone(tz) # Find all appointments between the dates appointments = Appointment.find_appointment_availability( office_id=office.office_id, first_date=today, last_date=days[-1], timezone=office.timezone.timezone_name) grouped_appointments = AvailabilityService.group_appointments( appointments, office.timezone.timezone_name) # For each of the day calculate the slots based on time slots for day_in_month in days: formatted_date = day_in_month.strftime('%m/%d/%Y') available_slots_per_day[formatted_date] = [] for timeslot in office.timeslots: # Calculate the slots per day timeslot_end_time = timeslot.end_time.replace(tzinfo=tz) timeslot_start_time = timeslot.start_time.replace( tzinfo=tz) if day_in_month.isoweekday() in day_indexes( timeslot.day_of_week): start_time = timeslot_start_time end_time = add_delta_to_time( timeslot_start_time, minutes=appointment_duration, timezone=office.timezone.timezone_name) # print(start_time, end_time) while end_time <= timeslot_end_time: slot = { 'start_time': start_time, 'end_time': end_time, 'no_of_slots': timeslot.no_of_slots } # Check if today's time is past appointment slot if not (today.date() == day_in_month.date() and today.time() > start_time): available_slots_per_day[formatted_date].append( slot) start_time = end_time.replace(tzinfo=tz) end_time = add_delta_to_time( end_time, minutes=appointment_duration, timezone=office.timezone.timezone_name) # Check if the slots are already booked for actual_slot in available_slots_per_day[formatted_date]: for booked_slot in grouped_appointments.get( formatted_date, []): # print('>>>>>>', booked_slot.get('start_time'), actual_slot.get('start_time'), booked_slot.get('end_time')) # print('<<<<<<', booked_slot.get('end_time'), actual_slot.get('end_time'), # booked_slot.get('start_time')) if booked_slot.get('start_time') \ <= actual_slot.get('start_time') \ < booked_slot.get('end_time') \ or \ booked_slot.get('end_time') \ < actual_slot.get('end_time') \ <= booked_slot.get('start_time'): if booked_slot.get( 'blackout_flag', False ): # If it's blackout override the no of slots actual_slot['no_of_slots'] = 0 else: actual_slot['no_of_slots'] -= 1 if format_time: # If true send formatted time actual_slot['start_time'] = actual_slot[ 'start_time'].strftime('%H:%M') actual_slot['end_time'] = actual_slot[ 'end_time'].strftime('%H:%M') return AvailabilityService.prune_appointments( available_slots_per_day) except exc.SQLAlchemyError as e: print(e) return {'message': 'API is down'}, 500