예제 #1
0
    def post(self):
        my_print("==> In AppointmentDraftPost, POST /appointments/draft")
        json_data = request.get_json()
        
        office_id = json_data.get('office_id')
        service_id = json_data.get('service_id')
        start_time = parse(json_data.get('start_time'))
        end_time = parse(json_data.get('end_time'))
        office = Office.find_by_id(office_id)
        service = Service.query.get(int(service_id)) if service_id else None

        # end_time can be null for CSRs when they click; whereas citizens know end-time.
        if not end_time:
            end_time = add_delta_to_time(start_time, minutes=office.appointment_duration, timezone=office.timezone.timezone_name)

        # Unauthenticated requests from citizens won't have name, so we set a fallback
        if (hasattr(g, 'oidc_token_info') and hasattr(g.oidc_token_info, 'username')):
            user = PublicUser.find_by_username(g.oidc_token_info['username'])
            citizen_name = user.display_name
        else:
            citizen_name = 'Draft'

        # Delete all expired drafts before checking availability
        Appointment.delete_expired_drafts()



        csr = None
        if (hasattr(g, 'oidc_token_info')):
            csr = CSR.find_by_username(g.oidc_token_info['username'])

        # CSRs are not limited by drafts,  can always see other CSRs drafts
        # This mitigates two CSRs in office creating at same time for same meeting
        # Ensure there's no race condition when submitting a draft
        if not csr and not AvailabilityService.has_available_slots(
                                    office=office, 
                                    start_time=start_time, 
                                    end_time=end_time, 
                                    service=service):
                    return {"code": "CONFLICT_APPOINTMENT",
                            "message": "Cannot create appointment due to scheduling conflict.  Please pick another time."}, 400
        
        # Set draft specific data
        json_data['is_draft'] = True
        json_data['citizen_name'] = citizen_name

        appointment, warning = self.appointment_schema.load(json_data)
        
        if warning:
            logging.warning("WARNING: %s", warning)
            return {"message": warning}, 422

        db.session.add(appointment)
        db.session.commit()
        
        result = self.appointment_schema.dump(appointment)

        socketio.emit('appointment_create', result.data)

        return {"appointment": result.data, "warning" : warning}, 201
 def post(self):
     my_print(
         "==> In AppointmentDraftFlush, POST /appointments/draft/flush")
     draft_ids = Appointment.delete_expired_drafts()
     return {"deleted_draft_ids": draft_ids}, 200