예제 #1
0
async def get_contact_by_tags(request):
    verify_access(request)
    current_tenant = get_current_tenant(request)
    if current_tenant is None or 'error_code' in current_tenant:
        return json({
            'error_code': 'TENANT_UNKNOWN',
            'error_message': 'Thông tin request không xác định'
        }, status=523)

    tenant_id = current_tenant.get('id')
    body_data = request.json
    if body_data is None or isinstance(body_data, list) == False or len(body_data) == 0:
        return json({
            'error_code': 'BODY_DATA_ERROR',
            'error_message': 'Dữ liệu không đúng định dạng'
        }, status=520)

    tags_ids = [_.get('id') for _ in body_data]

    contact_tags_details = db.session.query(ContactTagsDetails.contact_id,\
        func.count(ContactTagsDetails.id)).filter(and_(ContactTagsDetails.tenant_id == tenant_id,\
                                                       ContactTagsDetails.contact_tags_id.in_(tags_ids))).group_by(ContactTagsDetails.contact_id).all()

    results = []
    if contact_tags_details is not None and isinstance(contact_tags_details, list):
        for tag_detail in contact_tags_details:
            if tag_detail[1] == len(tags_ids):
                results.append(str(tag_detail[0]))

    return json(results)
예제 #2
0
async def update_properties(request):
    verify_access(request)

    try:
        data = request.json
        if data is None or 'id' not in data or data['id'] is None:
            return json(
                {
                    "error_code": ERROR_CODE['DATA_FORMAT'],
                    "error_message": ERROR_MSG['DATA_FORMAT']
                },
                status=STATUS_CODE['ERROR'])

        contact = User.query.get(data['id'])
        if contact is not None:
            for name, value in data.items():
                if name != 'id':
                    setattr(contact, name, value)

            db.session.add(contact)
            db.session.commit()

            return json({"message": "success"})
    except:
        return json(
            {
                "error_code": ERROR_CODE['EXCEPTION'],
                "error_message": ERROR_MSG['EXCEPTION']
            },
            status=STATUS_CODE['ERROR'])
예제 #3
0
async def get_all_workstation(request):
    verify_access(request)

    workstations = Workstation.query.filter().all()

    results = []
    for ws in workstations:
        results.append(to_dict(ws))

    return json(results)
예제 #4
0
async def get_list_item(request):
    verify_access(request)
    tenant_id = request.headers.get('tenant_id')

    categories = db.session.query(ItemCategory).filter(and_(ItemCategory.tenant_id == tenant_id,\
                                                            ItemCategory.status == 'active',\
                                                            ItemCategory.deleted == False,\
                                                            ItemCategory.is_show == True)).all()
    result_categories = []
    result_items = MOCK_ITEMS
    for category in categories:
        category_dict = to_dict(category)
        for key in exclude_attrs:
            if key in category_dict:
                del category_dict[key]

        category_dict['items'] = MOCK_ITEMS

        items_of_category = db.session.query(Item).filter(and_(Item.id == ItemCategoryRelation.item_id,\
                                                               ItemCategory.id == ItemCategoryRelation.category_id,\
                                                               ItemCategory.id == category.id,\
                                                               Item.tenant_id == tenant_id,\
                                                               Item.deleted == False,\
                                                               Item.active == True)).all()
        for item in items_of_category:
            item_dict = {
                'id': str(item.id),
                'item_no': item.item_no,
                'item_name': item.item_name,
                'type': item.item_type,
                'thumbnail': item.thumbnail,
                'items': item.images,
                'brief_desc': item.brief_desc,
                'description': item.description,
                'price_list': {},
                'is_trending': item.is_trending,
                'is_product': True,
                'is_service': False,
                'is_combo': False,
                'combo_items': [],
                'toppings': []
            }
            category_dict['items'].append(item_dict)

        result_categories.append(category_dict)

    return json({
        'time': now_timestamp(),
        'categories': result_categories,
        'items': result_items
    })
예제 #5
0
async def get_workstation_info(request):
    verify_access(request)

    exid = None
    try:
        exid = request.args.get("exid")
    except:
        pass

    if exid is not None:
        workstation = Workstation.query.filter(
            Workstation.workstation_exid == exid).first()
        return json(to_dict(workstation))

    return json(None)
예제 #6
0
async def set_current_tenant(request):
    verify_access(request)

    body_data = request.json

    tenant_id = body_data.get('tenant_id', None)
    if tenant_id is None:
        return json({
            'error_code': 'DATA_ERROR',
            'error_message': 'Dữ liệu không hợp lệ'
        }, status=520)

    request['session']['current_tenant_id'] = tenant_id

    return json({}, status=200)
예제 #7
0
async def update_properties(request):
    verify_access(request)
    if request.method == "OPTIONS":
        return json(None)
    try:
        data = request.json
        if data is None:
            return json({"error_code": "", "error_message": ""}, status=520)

        contact_dict = dynamic_save_contact(data)

        return json(contact_dict)

    except:
        return json({
            "error_code": ERROR_CODE['EXCEPTION'],
            "error_message": ERROR_MSG['EXCEPTION']
        }, status=STATUS_CODE['ERROR'])
예제 #8
0
async def change_password(request):
    verify_access(request)

    current_tenant = get_current_tenant(request)
    if current_tenant is None or 'error_code' in current_tenant:
        return json(
            {
                'error_code': 'TENANT_UNKNOW',
                'error_message': 'Request Unknown'
            },
            status=523)

    current_user = auth.current_user(request)
    if current_user is None:
        auth.logout_user(request)
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Phiên làm việc hết hạn, đăng nhập lại.'
            },
            status=523)

    current_tenant_id = current_tenant.get('id')
    current_user_id = current_user['uid']
    if current_user_id is None:
        auth.logout_user(request)
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Phiên làm việc hết hạn, đăng nhập lại.'
            },
            status=523)

    user_info = db.session.query(User).filter(and_(User.tenant_id == current_tenant_id,\
                                                   User.id == current_user_id)).first()
    if user_info is None:
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Tài khoản không tồn tại.'
            },
            status=520)

    body_data = request.json
    current_password = body_data.get('current_password', None)
    new_password = body_data.get('new_password', None)

    # CHECK CURRENT PASSWORD CORRECT OR NOT
    if auth.verify_password(current_password, user_info.password,
                            user_info.salt) == False:
        return json(
            {
                'error_code': 'E523',
                'error_message': 'Tên tài khoản hoặc mật khẩu không đúng'
            },
            status=523)

    user_info.password = auth.encrypt_password(new_password, user_info['salt'])
    user_info.updated_at = now_timestamp()
    db.session.commit()
    return json({'code': 'S200', 'message': 'Thành công'}, status=200)