def create( cls, created_by, dept_code, details, appointment_type, student_sid, advisor_attrs=None, topics=(), scheduled_time=None, student_contact_info=None, student_contact_type=None, ): # If this appointment comes in already assigned to the intake desk, we treat it as resolved. if advisor_attrs and advisor_attrs['role'] == 'Intake Desk': status = 'checked_in' elif advisor_attrs: status = 'reserved' else: status = 'waiting' appointment = cls( advisor_uid=advisor_attrs and advisor_attrs['uid'], advisor_name=advisor_attrs and advisor_attrs['name'], advisor_role=advisor_attrs and advisor_attrs['role'], advisor_dept_codes=advisor_attrs and advisor_attrs['deptCodes'], appointment_type=appointment_type, created_by=created_by, dept_code=dept_code, details=details, scheduled_time=scheduled_time, status=status, student_contact_info=student_contact_info, student_contact_type=student_contact_type, student_sid=student_sid, updated_by=created_by, ) for topic in topics: appointment.topics.append( AppointmentTopic.create(appointment, topic), ) db.session.add(appointment) std_commit() AppointmentEvent.create( appointment_id=appointment.id, advisor_id=advisor_attrs and advisor_attrs['id'], user_id=created_by, event_type=status, ) cls.refresh_search_index() return appointment
def set_to_waiting(self, updated_by): event_type = 'waiting' self.status = event_type self.updated_by = updated_by self.advisor_uid = None self.advisor_name = None self.advisor_role = None self.advisor_dept_codes = None AppointmentEvent.create( appointment_id=self.id, user_id=updated_by, event_type=event_type, ) std_commit() db.session.refresh(self)
def unreserve_all_for_advisor(cls, advisor_uid, updated_by): appointments = cls.query.filter(and_(cls.status == 'reserved', cls.advisor_uid == advisor_uid, cls.deleted_at == None)).all() # noqa: E711 event_type = 'waiting' for appointment in appointments: appointment.status = event_type appointment.advisor_uid = None appointment.advisor_name = None appointment.advisor_role = None appointment.advisor_dept_codes = None appointment.updated_by = updated_by AppointmentEvent.create( appointment_id=appointment.id, user_id=updated_by, event_type=event_type, ) std_commit()
def unreserve(cls, appointment_id, unreserved_by): appointment = cls.find_by_id(appointment_id=appointment_id) if appointment: event_type = 'waiting' appointment.status = event_type appointment.updated_by = unreserved_by AppointmentEvent.create( appointment_id=appointment.id, user_id=unreserved_by, event_type=event_type, ) std_commit() db.session.refresh(appointment) return appointment else: return None
def check_in(cls, appointment_id, checked_in_by, advisor_attrs): appointment = cls.find_by_id(appointment_id=appointment_id) if appointment: appointment.status = 'checked_in' appointment.advisor_uid = advisor_attrs['uid'] appointment.advisor_name = advisor_attrs['name'] appointment.advisor_role = advisor_attrs['role'] appointment.advisor_dept_codes = advisor_attrs['deptCodes'] appointment.updated_by = checked_in_by std_commit() db.session.refresh(appointment) AppointmentEvent.create( appointment_id=appointment.id, user_id=checked_in_by, advisor_id=advisor_attrs['id'], event_type='checked_in', ) return appointment else: return None
def cancel(cls, appointment_id, canceled_by, cancel_reason, cancel_reason_explained): appointment = cls.find_by_id(appointment_id=appointment_id) if appointment: event_type = 'canceled' appointment.status = event_type appointment.updated_by = canceled_by AppointmentEvent.create( appointment_id=appointment.id, user_id=canceled_by, event_type=event_type, cancel_reason=cancel_reason, cancel_reason_explained=cancel_reason_explained, ) std_commit() db.session.refresh(appointment) cls.refresh_search_index() return appointment else: return None
def reserve(cls, appointment_id, reserved_by, advisor_attrs): appointment = cls.find_by_id(appointment_id=appointment_id) if appointment: event_type = 'reserved' appointment.status = event_type appointment.updated_by = reserved_by appointment.advisor_uid = advisor_attrs['uid'] appointment.advisor_name = advisor_attrs['name'] appointment.advisor_role = advisor_attrs['role'] appointment.advisor_dept_codes = advisor_attrs['deptCodes'] AppointmentEvent.create( appointment_id=appointment.id, user_id=reserved_by, advisor_id=advisor_attrs['id'], event_type=event_type, ) std_commit() db.session.refresh(appointment) return appointment else: return None
def create( cls, created_by, dept_code, details, appointment_type, student_sid, advisor_uid=None, topics=(), ): if advisor_uid: status = 'reserved' status_by = AuthorizedUser.get_id_per_uid(advisor_uid) else: status = 'waiting' status_by = created_by appointment = cls( advisor_uid=advisor_uid, appointment_type=appointment_type, created_by=created_by, dept_code=dept_code, details=details, status=status, student_sid=student_sid, updated_by=created_by, ) for topic in topics: appointment.topics.append( AppointmentTopic.create(appointment, topic), ) db.session.add(appointment) std_commit() AppointmentEvent.create( appointment_id=appointment.id, user_id=status_by, event_type=status, ) cls.refresh_search_index() return appointment
def appointment_event_to_json(appointment_id, event_type): event = AppointmentEvent.get_most_recent_per_type( appointment_id=appointment_id, event_type=event_type, ) if event_type else None def _status_by_user(): uid = AuthorizedUser.get_uid_per_id(event.user_id) return { 'id': event.user_id, **calnet.get_calnet_user_for_uid(app, uid), } return { 'cancelReason': event and event.cancel_reason, 'cancelReasonExplained': event and event.cancel_reason_explained, 'status': event_type, 'statusBy': event and _status_by_user(), 'statusDate': event and _isoformat(event.created_at), }
def unreserve_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') has_privilege = current_user.is_admin or appointment.dept_code in _dept_codes_with_scheduler_privilege( ) if has_privilege and appointment.status == 'reserved': event = AppointmentEvent.get_most_recent_per_type( appointment.id, 'reserved') if event.user_id == current_user.get_id(): appointment = Appointment.unreserve( appointment_id=appointment_id, unreserved_by=current_user.get_id(), ) api_json = appointment.to_api_json(current_user.get_id()) _put_student_profile_per_appointment([api_json]) return tolerant_jsonify(api_json) else: raise ForbiddenRequestError( f'You did not reserve appointment {appointment_id}.') else: raise ForbiddenRequestError( f'You are unauthorized to manage appointment {appointment_id}.')