def begin_service(self, csr, snowplow_event): active_period = self.get_active_period() if active_period.ps.ps_name in ["Being Served"]: raise TypeError( "You cannot begin serving a citizen that is already being served" ) active_period.time_end = datetime.utcnow() # db.session.add(active_period) period_state_being_served = PeriodState.get_state_by_name( "Being Served") new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_being_served.ps_id, time_start=datetime.utcnow()) self.periods.append(new_period) # Calculate number of active periods, for Snowplow call. period_count = len(self.periods) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event, period_count=period_count, current_sr_number=self.sr_number)
def post(self, id): csr = CSR.find_by_username(g.jwt_oidc_token_info['username']) service_request = ServiceReq.query.filter_by(sr_id=id) \ .join(ServiceReq.citizen, aliased=True) \ .filter_by(office_id=csr.office_id).first_or_404() active_service_state = SRState.get_state_by_name("Active") complete_service_state = SRState.get_state_by_name("Complete") # Find the currently active service_request and close it current_sr_number = 0 for req in service_request.citizen.service_reqs: if req.sr_state_id == active_service_state.sr_state_id: req.sr_state_id = complete_service_state.sr_state_id req.finish_service(csr, clear_comments=False) current_sr_number = req.sr_number db.session.add(req) # Then set the requested service to active service_request.sr_state_id = active_service_state.sr_state_id period_state_being_served = PeriodState.get_state_by_name( "Being Served") new_period = Period(sr_id=service_request.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_being_served.ps_id, time_start=datetime.utcnow()) db.session.add(new_period) db.session.add(service_request) db.session.commit() # To make service active, stop current service, restart previous service. SnowPlow.snowplow_event(service_request.citizen.citizen_id, csr, "stopservice", current_sr_number=current_sr_number) SnowPlow.snowplow_event(service_request.citizen.citizen_id, csr, "restartservice", current_sr_number=service_request.sr_number) citizen_result = self.citizen_schema.dump(service_request.citizen) socketio.emit('update_active_citizen', citizen_result, room=csr.office_id) result = self.service_request_schema.dump(service_request) return { 'service_request': result, 'errors': self.service_request_schema.validate(service_request) }, 200
def add_to_queue(self, csr, snowplow_event): active_period = self.get_active_period() active_period.time_end = datetime.now() #db.session.add(active_period) period_state_waiting = PeriodState.get_state_by_name("Waiting") new_period = Period( sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_waiting.ps_id, time_start=datetime.now() ) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event, current_sr_number=self.sr_number)
def place_on_hold(self, csr): active_period = self.get_active_period() active_period.time_end = datetime.now() # db.session.add(active_period) period_state_on_hold = PeriodState.get_state_by_name("On hold") new_period = Period( sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_on_hold.ps_id, time_start=datetime.now() ) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, "hold", current_sr_number = self.sr_number)
def invite(self, csr, invite_type, sr_count=1): active_period = self.get_active_period() if active_period.ps.ps_name in ["Invited", "Being Served", "On hold"]: raise TypeError( "You cannot invite a citizen that has already been invited") # If a generic invite type, event is either invitecitizen or returninvite. if invite_type == "generic": # If only one SR, one period, an invitecitizen call, from First Time in Line state. if sr_count == 1 and len(self.periods) == 2: snowplow_event = "invitecitizen" # Otherwise from the Back in Line state. else: snowplow_event = "returninvite" # A specific invite type. Event is invitefromlist, returnfromlist or invitefromhold else: # If only one SR, one period, an invitefromlist call, from First Time in Line state. if sr_count == 1 and len(self.periods) == 2: snowplow_event = "invitefromlist" # Either from back in line or hold state. else: if active_period.ps.ps_name == "Waiting": snowplow_event = "returnfromlist" else: snowplow_event = "invitefromhold" active_period.time_end = datetime.utcnow() # db.session.add(active_period) period_state_invite = PeriodState.get_state_by_name("Invited") new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_invite.ps_id, time_start=datetime.utcnow()) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event, current_sr_number=self.sr_number)
def post(self): json_data = request.get_json() if not json_data: return {"message": "No input data received for creating service request"}, 400 csr = CSR.find_by_username(g.oidc_token_info['username']) try: service_request = self.service_request_schema.load(json_data['service_request']).data except ValidationError as err: return {"message": err.messages}, 422 except KeyError as err: print (err) return {"message": str(err)} active_sr_state = SRState.get_state_by_name("Active") complete_sr_state = SRState.get_state_by_name("Complete") citizen_state = CitizenState.query.filter_by(cs_state_name="Active").first() citizen = Citizen.query.get(service_request.citizen_id) service = Service.query.get(service_request.service_id) if citizen is None: return {"message": "No matching citizen found for citizen_id"}, 400 if service is None: return {"message": "No matching service found for service_id"}, 400 # Find the currently active service_request and close it (if it exists) current_sr_number = 0 for req in citizen.service_reqs: if req.sr_state_id == active_sr_state.sr_state_id: req.sr_state_id = complete_sr_state.sr_state_id req.finish_service(csr, clear_comments=False) current_sr_number = req.sr_number db.session.add(req) service_request.sr_state_id = active_sr_state.sr_state_id # Only add ticket creation period and ticket number if it's their first service_request if len(citizen.service_reqs) == 0: period_state_ticket_creation = PeriodState.get_state_by_name("Ticket Creation") ticket_create_period = Period( csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_ticket_creation.ps_id, time_start=citizen.get_service_start_time(), time_end=datetime.now() ) service_request.periods.append(ticket_create_period) # Move start_time back 6 hours to account for DST and UTC offsets # It's only important that the number carries over _around_ midnight offset_start_time = citizen.start_time - timedelta(hours=6) service_count = ServiceReq.query \ .join(ServiceReq.citizen, aliased=True) \ .filter(Citizen.start_time >= offset_start_time.strftime("%Y-%m-%d")) \ .filter_by(office_id=csr.office_id) \ .join(ServiceReq.service, aliased=True) \ .filter_by(prefix=service.prefix) \ .count() citizen.ticket_number = service.prefix + str(service_count) else: period_state_being_served = PeriodState.get_state_by_name("Being Served") ticket_create_period = Period( csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_being_served.ps_id, time_start=datetime.now() ) service_request.periods.append(ticket_create_period) citizen.cs_id = citizen_state.cs_id # If first service, just choose it. If additional service, more work needed. if len(citizen.service_reqs) == 0: snowplow_event = "chooseservice" else: snowplow_event = "additionalservice" service_request.sr_number = len(citizen.service_reqs) + 1 db.session.add(service_request) db.session.add(citizen) db.session.commit() # If first service, just need a choose service call. if snowplow_event == "chooseservice": SnowPlow.choose_service(service_request, csr, "chooseservice") # If not first service, need stop service, choose service, and additional service calls. else: SnowPlow.snowplow_event(citizen.citizen_id, csr, "stopservice", current_sr_number=current_sr_number) SnowPlow.choose_service(service_request, csr, "chooseservice") SnowPlow.snowplow_event(citizen.citizen_id, csr, "additionalservice", current_sr_number=service_request.sr_number) citizen_result = self.citizen_schema.dump(citizen) socketio.emit('update_active_citizen', citizen_result.data, room=csr.office_id) result = self.service_request_schema.dump(service_request) return {'service_request': result.data, 'errors': result.errors}, 201
def remove_from_queue(self): service_req_ids = [int(x.period_id) for x in self.periods] Period.delete_periods(service_req_ids)
def post(self): try: json_data = request.get_json() except Exception as error: return {"message": str(error)}, 401 csr = CSR.find_by_username(g.jwt_oidc_token_info['username']) service_request, message, code = get_service_request( self, json_data, csr) if (service_request is None): return {"message": message}, code active_sr_state = SRState.get_state_by_name("Active") complete_sr_state = SRState.get_state_by_name("Complete") citizen = None try: citizen = Citizen.query \ .options(joinedload(Citizen.service_reqs).joinedload(ServiceReq.periods).options(raiseload(Period.sr),joinedload(Period.csr).raiseload('*')),joinedload(Citizen.office),raiseload(Citizen.user)) \ .filter_by(citizen_id=service_request.citizen_id) citizen = citizen.first() except: print("==> An exception getting citizen info") print(" --> CSR: " + csr.username) print(" --> json_data: " + json.dumps(json_data['service_request'])) if citizen is None: return {"message": "No matching citizen found for citizen_id"}, 400 service, message, code = get_service(service_request, json_data, csr) if (service is None): return {"message": message}, code # Find the currently active service_request and close it (if it exists) current_sr_number = 0 for req in citizen.service_reqs: if req.sr_state_id == active_sr_state.sr_state_id: req.sr_state_id = complete_sr_state.sr_state_id req.finish_service(csr, clear_comments=False) current_sr_number = req.sr_number db.session.add(req) service_request.sr_state_id = active_sr_state.sr_state_id # Only add ticket creation period and ticket number if it's their first service_request if len(citizen.service_reqs) == 0: period_state_ticket_creation = PeriodState.get_state_by_name( "Ticket Creation") ticket_create_period = Period( csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_ticket_creation.ps_id, time_start=citizen.get_service_start_time(), time_end=datetime.now()) service_request.periods.append(ticket_create_period) # Move start_time back 11 hours to account for DST and UTC offsets # tickets up till 10pm will not affect ticket numbering on next day offset_start_time = citizen.start_time - timedelta(hours=11) service_count = ServiceReq.query \ .join(ServiceReq.citizen, aliased=True) \ .filter(Citizen.start_time >= offset_start_time.strftime("%Y-%m-%d")) \ .filter_by(office_id=csr.office_id) \ .join(ServiceReq.service, aliased=True) \ .filter_by(prefix=service.prefix) \ .count() citizen.ticket_number = service.prefix + str(service_count) else: period_state_being_served = PeriodState.get_state_by_name( "Being Served") ticket_create_period = Period( csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_being_served.ps_id, time_start=datetime.now()) service_request.periods.append(ticket_create_period) citizen.cs_id = active_id # If first service, just choose it. If additional service, more work needed. if len(citizen.service_reqs) == 0: snowplow_event = "chooseservice" else: snowplow_event = "additionalservice" service_request.sr_number = len(citizen.service_reqs) + 1 db.session.add(service_request) db.session.add(citizen) db.session.commit() # If first service, just need a choose service call. if snowplow_event == "chooseservice": SnowPlow.choose_service(service_request, csr, "chooseservice") # If not first service, need stop service, choose service, and additional service calls. else: SnowPlow.snowplow_event(citizen.citizen_id, csr, "stopservice", current_sr_number=current_sr_number) SnowPlow.choose_service(service_request, csr, "chooseservice") SnowPlow.snowplow_event( citizen.citizen_id, csr, "additionalservice", current_sr_number=service_request.sr_number) citizen_result = self.citizen_schema.dump(citizen) socketio.emit('update_active_citizen', citizen_result, room=csr.office.office_name) result = self.service_request_schema.dump(service_request) return { 'service_request': result, 'errors': self.service_request_schema.validate(service_request) }, 201