예제 #1
0
def create_address(customer_id):
    data = request.get_json()
    required = ['contact', 'address_line1', 'address_line2', 'postcode', 'country']
    lacked = lack_keys(data, required)
    if lacked:
        raise LackRequiredData(lacked)

    new_address = Address(data)
    cs.create_customer_address(customer_id, new_address)
    return new_address.json(), 201
예제 #2
0
def test_create():
    global new_customer_id, new_address_id
    sr = SR()

    new_customer = C({
        'customer_code': 'Test1',
        'title': 'Miss',
        'organization_desc': 'holySAS',
        'first_name': 'b1LL',
        'last_name': 'ZX',
        'phone': '0123456789',
        'email': '*****@*****.**'
    })

    new_address = Addr({
        'contact': '9876541230',
        'address_line1': 'Unit 5',
        'address_line2': 'Ss Ave, Caulfield, VIC',
        'postcode': 'VIC0000',
        'region': 'Victoria',
        'country': 'Australia'
    })

    cust_result = sr.insert(new_customer)
    new_customer_id = new_customer.id
    new_address.customer_id = new_customer_id
    addr_result = sr.insert(new_address)
    new_address_id = new_address.id

    assert cust_result.id == new_customer_id
    assert cust_result.last_name == 'ZX'
    assert addr_result.id == new_address_id
    assert addr_result.customer_id == new_address.customer_id == new_customer_id
    assert addr_result.postcode == 'VIC0000'
    with pytest.raises(AlreadyExists):
        sr.insert(new_customer)

    # Violate referential integrity
    bad_address = Addr({
        'customer_id': 0,
        'contact': '9876541230',
        'address_line1': 'Unit 5',
        'address_line2': 'Ss Ave, Caulfield, VIC',
        'postcode': 'VIC0000',
        'region': 'Victoria',
        'country': 'Australia'
    })

    with pytest.raises(ViolateFKConstraint):
        sr.insert(bad_address)
예제 #3
0
def get_one_address(customer_id, address_id):
    addr = SR().find_one(
        Address({
            'id': address_id,
            'customer_id': customer_id
        }))
    return addr
예제 #4
0
def delete_address(customer_id, address_id):
    address = SR().find_one(
        Address({
            'id': address_id,
            'customer_id': customer_id
        }))
    SR().delete(address)
예제 #5
0
def save_order(session_key,
               customer_id,
               delivery_addr_id,
               billing_addr_id,
               lines,
               instructions=""):
    # Retrieve objs
    sess = SR().find_one(Session({'sessionKey': session_key}))
    org = SR().get_one_by_id(Organization(pk=sess.orgId))
    cust = SR().get_one_by_id(Customer(pk=customer_id))
    deli = SR().get_one_by_id(Address(pk=delivery_addr_id))
    bill = SR().get_one_by_id(Address(pk=billing_addr_id))
    # Lines Info
    details_list = []
    for line in lines:
        product = SR().get_one_by_id(Product(pk=line['product_id']))
        PR().assign_price_and_images_to_product([product])
        details = OrderDetail({
            'lineType': 'PRODUCT',
            'keyProductID': product.keyProductID,
            'productName': product.name,
            'quantity': line['quantity'],
            'unitPrice': product.price,
            'productCode': product.productCode,
            'productId': product.id
        })
        details_list.append(details)

    # Post Order to SQUIZZ to get tax info
    connection = authUtil.build_connection()
    result_code, tax_lines = connection.submit_order(org, cust, details_list)
    if result_code != 'SERVER_SUCCESS':
        raise SquizzException(
            f'Post order to SQUIZZ failed, code: {result_code}')
    # Assign tax info into order lines
    for idx, line in enumerate(details_list):
        tax_info = tax_lines[idx]
        line.priceTotalExTax = tax_info['priceTotalExTax']
        line.priceTotalIncTax = tax_info['priceTotalIncTax']
        line.totalPrice = line.priceTotalIncTax

    # Save order and order lines
    return OR().create_order(org, cust, deli, bill, details_list, result_code,
                             instructions)
예제 #6
0
def create_customer():
    data = request.get_json()
    cust_data = data.get('customer')
    addr_data = data.get('address')

    # Check customer necessary keys
    cust_required = ['customer_code', 'title', 'first_name', 'last_name']
    cust_lacked = lack_keys(cust_data, cust_required, prefix='customer')

    if cust_lacked:
        raise LackRequiredData(cust_lacked)
    # Check customer_code
    cust_code = cust_data['customer_code']
    if cust_code not in cs.customer_codes:
        return jsonify({'message': f'Provided customer code {cust_code} does not exist'}), 404

    if cust_code not in cs.list_unused_customer_codes():
        return jsonify({'message': f'Provided customer code {cust_code} is already used'}), 409

    new_customer = Customer(cust_data)

    # Create customer only
    if addr_data is None:
        cs.create_customer(new_customer)
        return jsonify({"customer": new_customer.__dict__}), 201

    # Create customer with address
    addr_data = data['address']
    addr_required = ['contact', 'address_line1', 'address_line2', 'postcode', 'country']
    addr_lacked = lack_keys(addr_data, addr_required, prefix='address')
    if addr_lacked:
        raise LackRequiredData(addr_lacked)

    new_address = Address(addr_data)
    cs.create_customer(new_customer, new_address)
    return jsonify({'customer': new_customer.__dict__, 'address': new_address.__dict__}), 201
예제 #7
0
def list_customer_addresses(customer_id):
    SR().get_one_by_id(Customer(pk=customer_id))
    cust_addr = Address()
    cust_addr.customer_id = customer_id
    return SR().find_all(cust_addr)