def call_customer(id): line_up = LineUp.query.get(id) if not line_up: return (jsonify({'message': 'Record not found'}), 404) if line_up.first_call_at is None: line_up.first_call_at = datetime.now() line_up.status = 1 # first_call else: if line_up.second_call_at is None: line_up.second_call_at = datetime.now() line_up.status = 2 # second call else: line_up.cancelled_at = datetime.now() line_up.status = 4 # cancelled db.session.add(line_up) db.session.commit() return (jsonify({'message': 'Atendimento Cancelado! Cliente não atendeu a segunda chamada!'}), 400) db.session.add(line_up) db.session.commit() response = flask.make_response(jsonify({ 'data': { 'id': line_up.id, 'customer_id': line_up.customer_id, 'waiting_line_id': line_up.waiting_line_id, 'joined_at': format_datetime(line_up.joined_at), 'first_call_at': format_datetime(line_up.first_call_at), 'second_call_at': format_datetime(line_up.second_call_at), 'completed_at': format_datetime(line_up.completed_at), 'cancelled_at': format_datetime(line_up.cancelled_at), 'status': line_up.status}}), 200) response.headers["Content-Type"] = "application/json" return response
def get_auth_token(data): company = Company.query.get(g.user.company_id) if data['email'] != company.admin_email: return (jsonify({'message': 'Not Authorized'})), 401 token = g.user.generate_auth_token(current_app.config['TOKEN_TTL']) response = flask.make_response( jsonify({ 'data': { 'id': g.user.id, 'username': g.user.username, 'company_id': g.user.company_id, 'email': g.user.email, 'phone_region': g.user.phone_region, 'phone_number': g.user.phone_number, 'status': g.user.status, 'role_id': g.user.role_id, 'created_at': format_datetime(g.user.created_at), 'updated_at': format_datetime(g.user.updated_at) } }), 200) response.headers["Content-Type"] = "application/json" setCookie(response, token) return response
def save(data): name = data['name'] waiting_line = WaitingLine(name=name, company_id=data['company_id'], status=1, # ATIVO is_priority=data['is_priority'], created_at=datetime.now(), updated_at=datetime.now()) if waiting_line.query.filter(and_(WaitingLine.company_id==data['company_id'],WaitingLine.name==name)).first() is not None: return (jsonify({'message': 'Company already exists'}), 400) db.session.add(waiting_line) db.session.commit() response = flask.make_response(jsonify({ 'data': { 'id': waiting_line.id, 'company_id': waiting_line.company_id, 'name': waiting_line.name, 'is_priority': waiting_line.is_priority, 'status': waiting_line.status, 'created_at': format_datetime(waiting_line.created_at), 'updated_at': format_datetime(waiting_line.updated_at)}}), 201) response.headers["Content-Type"] = "application/json" return response
def save(data): username = data['username'] email = data['email'] if 'phone_region' not in data: phone_region = '+55' else: phone_region = data['phone_region'] if User.query.filter(or_(User.email == email, User.username == username)).first() is not None: return (jsonify({'message': 'User already exists'}), 400) user = User( username=username, email=email, company_id=data['company_id'], phone_number=data['phone_number'], phone_region=phone_region, role_id=data['role_id'], status=1, # ATIVO created_at=datetime.now(), updated_at=datetime.now()) user.hash_password(data['password']) if data['role_id'] == 1: # ADMIN company = Company.query.get(data['company_id']) company.admin_email = data['email'] db.session.add(company) db.session.add(user) db.session.commit() verify_password(user.username, data['password']) token = g.user.generate_auth_token(current_app.config['TOKEN_TTL']) response = flask.make_response( jsonify({ 'data': { 'id': user.id, 'username': user.username, 'company_id': user.company_id, 'email': user.email, 'phone_region': user.phone_region, 'phone_number': user.phone_number, 'status': user.status, 'role_id': user.role_id, 'created_at': format_datetime(user.created_at), 'updated_at': format_datetime(user.updated_at) } }), 201) response.headers["Content-Type"] = "application/json" response = setCookie(response, token) return response
def get_current_user(): return flask.make_response( { 'data': { 'id': g.user.id, 'username': g.user.username, 'company_id': g.user.company_id, 'email': g.user.email, 'phone_region': g.user.phone_region, 'phone_number': g.user.phone_number, 'status': g.user.status, 'role_id': g.user.role_id, 'created_at': format_datetime(g.user.created_at), 'updated_at': format_datetime(g.user.updated_at) } }, 200)
def get(id): customer = Customer.query.get(id) if not customer: return (jsonify({'message': 'Customer not found'}), 404) response = flask.make_response(jsonify({ 'data': { 'id': customer.id, 'name': customer.name, 'phone_region': customer.phone_region, 'phone_number': customer.phone_number, 'description': customer.description, 'email': customer.email, 'status': customer.status, 'created_at': format_datetime(customer.created_at), 'updated_at': format_datetime(customer.updated_at)}}), 200) response.headers["Content-Type"] = "application/json" return response
def getAll(): waiting_lines = WaitingLine.query.filter_by(company_id=g.user.company_id).all() if not waiting_lines: return (jsonify({'message': 'No Waiting Lines found'}), 404) resp = {'data': []} for waiting_line in waiting_lines: resp['data'].append( { 'id': waiting_line.id, 'company_id': waiting_line.company_id, 'name': waiting_line.name, 'is_priority': waiting_line.is_priority, 'status': waiting_line.status, 'created_at': format_datetime(waiting_line.created_at), 'updated_at': format_datetime(waiting_line.updated_at)}) response = flask.make_response(jsonify(resp), 200) response.headers["Content-Type"] = "application/json" return response
def get(id): company = Company.query.get(id) if not company: return (jsonify({'message': 'Company not found'}), 404) response = flask.make_response( jsonify({ 'data': { 'id': company.id, 'name': company.name, 'phone_region': company.phone_region, 'phone_number': company.phone_number, 'admin_email': company.admin_email, 'status': company.status, 'created_at': format_datetime(company.created_at), 'updated_at': format_datetime(company.updated_at) } }), 200) response.headers["Content-Type"] = "application/json" return response
def save(data): name = data['name'] if 'phone_region' not in data: phone_region = '+55' else: phone_region = data['phone_region'] phone_number=data['phone_number'] description = None if 'description' in data: description = data['description'] email = None if 'email' in data: email = data['email'] customer = Customer(name=name, phone_number=phone_number, phone_region=phone_region, description=description, email=email, status=1, # ATIVO created_at=datetime.now(), updated_at=datetime.now()) if customer.query.filter(and_(Customer.phone_region==phone_region,Customer.phone_number==phone_number)).first() is not None: return (jsonify({'message': 'Customer already exists'}), 400) # user.hash_password(data['password']) db.session.add(customer) db.session.commit() response = flask.make_response(jsonify({ 'data': { 'id': customer.id, 'name': customer.name, 'phone_region': customer.phone_region, 'phone_number': customer.phone_number, 'description': description, 'email': email, 'status': customer.status, 'created_at': format_datetime(customer.created_at), 'updated_at': format_datetime(customer.updated_at)}}), 201) response.headers["Content-Type"] = "application/json" return response
def get(id): user = User.query.filter( and_(User.id == id, User.company_id == g.user.company_id)).first() if not user: return (jsonify({'message': 'User not found'}), 404) response = flask.make_response( jsonify({ 'data': { 'id': user.id, 'username': user.username, 'company_id': user.company_id, 'email': user.email, 'phone_region': user.phone_region, 'phone_number': user.phone_number, 'status': user.status, 'role_id': user.role_id, 'created_at': format_datetime(user.created_at), 'updated_at': format_datetime(user.updated_at) } }), 200) response.headers["Content-Type"] = "application/json" return response
def save(data): name = data['name'] if 'phone_region' not in data: phone_region = '+55' else: phone_region = data['phone_region'] phone_number = data['phone_number'] company = Company( name=name, phone_number=phone_number, phone_region=phone_region, status=1, # ATIVO created_at=datetime.now(), updated_at=datetime.now()) if company.query.filter( and_(Company.phone_region == phone_region, Company.phone_number == phone_number)).first() is not None: return (jsonify({'message': 'Company already exists'}), 400) db.session.add(company) db.session.commit() response = flask.make_response( jsonify({ 'data': { 'id': company.id, 'name': company.name, 'phone_region': company.phone_region, 'phone_number': company.phone_number, 'status': company.status, 'created_at': format_datetime(company.created_at), 'updated_at': format_datetime(company.updated_at) } }), 201) response.headers["Content-Type"] = "application/json" return response
def get_next_customer(waiting_line_id): line_up = LineUp() next_customer = line_up.query.filter(and_(LineUp.waiting_line_id==waiting_line_id,LineUp.status == 0)).order_by(db.asc('joined_at')).first() if not next_customer: return (jsonify({'message': 'Fila de espera vazia'}), 404) response = flask.make_response(jsonify({ 'data': { 'id': next_customer.id, 'customer_id': next_customer.customer_id, 'waiting_line_id': next_customer.waiting_line_id, 'status': next_customer.status, 'joined_at': format_datetime(next_customer.joined_at) }}), 200) response.headers["Content-Type"] = "application/json" return response
def save(data): line_up = LineUp(waiting_line_id= data['waiting_line_id'], customer_id= data['customer_id'], status=0, # ENTROU NA FILA joined_at=datetime.now()) if not is_logged(): # TODO: VALIDAR SE O USUÁRIO PERTENCE A EMPRESA return (jsonify({'message': 'Not Authorized' })), 401 if line_up.query.filter(and_(LineUp.customer_id==data['customer_id'],LineUp.status < 3)).first() is not None: return (jsonify({'message': 'Customer already in an active Waiting Line'}), 400) db.session.add(line_up) db.session.commit() response = flask.make_response(jsonify({ 'data': { 'id': line_up.id, 'customer_id': line_up.customer_id, 'waiting_line_id': line_up.waiting_line_id, 'status': line_up.status, 'joined_at': format_datetime(line_up.joined_at)}}), 201) response.headers["Content-Type"] = "application/json" return response