def put(self, customer_id): """ marks email address as primary :param customer_id: customer to be updated :return: customer object """ if request.json.get("email") is None: return jsonify({"error": EMAIL_IS_REQUIRED_FIELD}), 403 customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 new_primary_email = customer.make_email_primary( new_primay_email=request.json.get("email")) if new_primary_email is None: response = { "result": "not found", "customer": customer_obj(customer) } return jsonify(response), 404 response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 200
def post(self, customer_id): """ creates a new email query_parameters: is_primary :param customer_id: customer to be updated :return: customer object """ if request.json.get("email") is None: return jsonify({"error": EMAIL_IS_REQUIRED_FIELD}), 403 customer = Customer.get_customer(customer_id=customer_id, request=request) is_primary = False if request.args.get("is_primary") is not None: if request.args.get("is_primary").lower() == "true": is_primary = True if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 try: customer.add_email(new_email=request.json.get("email"), is_primary=is_primary) except DuplicateDataError: response = { "result": "already exists", "customer": customer_obj(customer) } return jsonify(response), 303 response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 201
def delete(self, customer_id): """ deletes an email query_parameters: is_primary :param customer_id: customer to be updated :return: customer object """ if request.json.get("email") is None: return jsonify({"error": EMAIL_IS_REQUIRED_FIELD}), 403 customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 deleted_email = customer.delete_email( email_to_delete=request.json.get("email")) if deleted_email is None: response = { "result": "not found", "customer": customer_obj(customer) } return jsonify(response), 404 response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 204
def gift_card_obj(gift_card): return { "gift_card_id": gift_card.gift_card_id, "original_balance_in_cents": gift_card.original_balance_in_cents, "current_balance_in_cents": gift_card.current_balance_in_cents, "created_at": gift_card.created_at, "updated_at": gift_card.updated_at, "gifter_customer": customer_obj(gift_card.gifter_customer), "recipient_customer": customer_obj(gift_card.recipient_customer), "links": { "gifter_customer": "/customer/" + gift_card.gifter_customer.customer_id, "recipient_customer": "/customer/" + gift_card.recipient_customer.customer_id, "self": "/giftcard/" + gift_card.gift_card_id } }
def credit_obj(credit): return { "credit_id": credit.credit_id, "customer": customer_obj(credit.customer), "original_balance_in_cents": credit.original_balance_in_cents, "current_balance_in_cents": credit.current_balance_in_cents, "created_at": credit.created_at, "updated_at": credit.updated_at, "voided_at": credit.voided_at, "links": { "customer": "/customer/" + credit.customer.customer_id } }
def get(self, customer_id): """ returns extended customer object :return: customer snapshot object """ customer = Customer.get_customer(customer_id=customer_id, request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 obj = customer_obj(customer=customer) cart = Cart.objects.filter(customer_id=customer.customer_id, closed_at=None).first() if cart: obj['cart'] = cart_obj(cart) invoices = Invoice.objects.filter( customer=customer).order_by("-created_at").paginate(page=1, per_page=10) if invoices: obj["last_10_invoices"] = invoice_objs(invoices) orders = Order.get_orders(request=request, customer_id=customer_id).order_by("-created_at")\ .paginate(page=1, per_page=10) if orders: obj["last_10_orders"] = order_objs(orders) gift_cards = GiftCard.objects.filter(recipient_customer=customer, current_balance_in_cents__gt=0)\ .order_by("-created_at").paginate(page=1, per_page=10) if gift_cards: obj["active_giftcards"] = gift_card_objs(gift_cards) credits = Credit.objects.filter(customer=customer, current_balance_in_cents__gt=0) \ .order_by("-created_at").paginate(page=1, per_page=10) if credits: obj["active_credits"] = credit_objs(credits) products = Invoice.get_top_N_products(customer=customer, num_items=10, request=request) if products: obj["top_10_ordered_products"] = products response = {"result": "ok", "customer": obj} return jsonify(response), 200
def invoice_obj(invoice): invoice_line_items = InvoiceLineItem.objects.filter(invoice=invoice).all() links = { "self": "/invoice/" + invoice.invoice_id, "collected": "/invoice/" + invoice.invoice_id + "/collected", "failed": "/invoice/" + invoice.invoice_id + "/failed" } refund = Refund.objects.filter(invoice=invoice) if refund: links[ "close_refund"] = "/invoice/" + invoice.invoice_id + "/refund/close" else: links["refund"] = "/invoice/" + invoice.invoice_id + "/refund/" return { "invoice_id": invoice.invoice_id, "customer": customer_obj(invoice.customer), "cart": cart_obj(invoice.cart), "state": invoice.state, "gift_card_used_amount_in_cents": invoice.gift_card_used_amount_in_cents, "credit_used_amount_in_cents": invoice.credit_used_amount_in_cents, "total_amount_in_cents": invoice.get_total_amount(), "tax_amount_in_cents": invoice.get_tax_amount(), "subtotal_amount_in_cents": invoice.get_subtotal_amount(), "created_at": invoice.created_at, "closed_at": invoice.closed_at, "invoice_line_items": invoice_line_item_objs(invoice_line_items=invoice_line_items), "links": links }
def put(self): """ logs customer out by providing email :return: customer """ if request.json.get("email") is None: return jsonify({"error": MISSING_CRENDENTIALS}), 403 customer = Customer.get_customer_by_email( email=request.json.get("email"), request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 customer.logout() response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 200
def put(self): """ logs customer in by providing email and password :return: customer object """ if request.json.get("password") is None or request.json.get( "email") is None: return jsonify({"error": MISSING_CRENDENTIALS}), 403 customer = Customer.get_customer_by_email( email=request.json.get("email"), request=request) if customer is None: return jsonify({"error": CUSTOMER_NOT_FOUND}), 404 if customer.check_password(request.json.get("password")): customer.login() response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 200 return jsonify({"error": INCORRECT_PASSWORD}), 403
def get(self, customer_id=None): """ Gets a customer by providing customer_id Endpoint: /customer/209485626598208918917359936593901836672 Example Response: { "customer": { "addresses": [ { "address_id": "160725188092123457335884996198595450510", "city": "townsville", "created_at": "Sun, 13 Jan 2019 19:48:27 GMT", "deleted_at": null, "is_primary": true, "state": "CA", "street": "1236 Main Street", "updated_at": "Sun, 13 Jan 2019 19:48:27 GMT", "zip": "1234" } ], "created_at": "Sun, 13 Jan 2019 19:48:27 GMT", "currency": "USD", "customer_id": "209485626598208918917359936593901836672", "deleted_at": null, "email": "*****@*****.**", "first_name": "John", "last_name": "Smith4", "last_order_date": null, "links": [ { "href": "/customer/209485626598208918917359936593901836672", "rel": "self" } ], "total_spent": "0.00", "updated_at": "Sun, 13 Jan 2019 19:48:27 GMT" }, "result": "ok" } """ store = Store.objects.filter(app_id=request.headers.get('APP-ID'), deleted_at=None).first() if customer_id: customer = Customer.objects.filter(customer_id=customer_id, deleted_at=None, store_id=store).first() if customer: response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 200 else: return jsonify({}), 404 else: customers = Customer.objects.filter(store_id=store, deleted_at=None) return paginated_results(objects=customers, collection_name='customer', request=request, per_page=self.PER_PAGE, serialization_func=customer_objs), 200
def put(self, customer_id): """ update one or more customer fields Endpoint: /customer/209485626598208918917359936593901836672 Example Post Body: { "currency": "GBR" } Example Response: { "customer": { "addresses": [ { "address_id": "160725188092123457335884996198595450510", "city": "townsville", "created_at": "Sun, 13 Jan 2019 19:48:27 GMT", "deleted_at": null, "is_primary": true, "state": "CA", "street": "1236 Main Street", "updated_at": "Sun, 13 Jan 2019 19:48:27 GMT", "zip": "1234" } ], "created_at": "Sun, 13 Jan 2019 19:48:27 GMT", "currency": "GBR", "customer_id": "209485626598208918917359936593901836672", "deleted_at": null, "email": "*****@*****.**", "first_name": "John", "last_name": "Smith4", "last_order_date": null, "links": [ { "href": "/customer/209485626598208918917359936593901836672", "rel": "self" } ], "total_spent": "0.00", "updated_at": "Sun, 13 Jan 2019 19:48:27 GMT" }, "result": "ok" } """ store = Store.objects.filter(app_id=request.headers.get('APP-ID'), deleted_at=None).first() customer = Customer.objects.filter(customer_id=customer_id, deleted_at=None, store_id=store).first() if not customer: return jsonify({}), 404 customer_json = request.json items_updated = 0 if customer_json.get("currency"): customer.currency = customer_json.get("currency") items_updated += 1 if customer_json.get("email"): customer.add_email(customer_json.get("email"), is_primary=True) items_updated += 1 if customer_json.get("first_name"): customer.first_name = customer_json.get("first_name") items_updated += 1 if customer_json.get("last_name"): customer.last_name = customer_json.get("last_name") items_updated += 1 if items_updated == 0: return jsonify({"error": "No fields supplied for update"}), 400 else: customer.updated_at = datetime.now() customer.save() response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 201
def post(self): """ Creates a new customer Endpoint: /customer/ Example Post Body: { "currency": "USD", "email": "*****@*****.**", "first_name": "John", "last_name": "Smith4", "addresses": [ { "street": "1236 Main Street", "city": "townsville", "zip": "1234", "state": "CA", "country": "USA", "is_primary": "true" }, { "street": "1215 Main Street", "city": "townsville", "zip": "500", "state": "CA", "country": "USA" }, { "street": "1216 Main Street", "city": "townsville", "zip": "500", "state": "CA", "country": "USA" } ] } { "customer": { "addresses": [ { "address_id": "224473682041851492125327501325163956867", "city": "townsville", "created_at": "Thu, 10 Jan 2019 03:56:26 GMT", "deleted_at": null, "is_primary": false, "state": "CA", "street": "1215 Main Street", "updated_at": "Thu, 10 Jan 2019 03:56:26 GMT", "zip": "500" }, { "address_id": "245608141370371202915656949519861248348", "city": "townsville", "created_at": "Thu, 10 Jan 2019 03:56:26 GMT", "deleted_at": null, "is_primary": true, "state": "CA", "street": "1236 Main Street", "updated_at": "Thu, 10 Jan 2019 03:56:26 GMT", "zip": "1234" }, { "address_id": "274242069278329272621665758252140893540", "city": "townsville", "created_at": "Thu, 10 Jan 2019 03:56:26 GMT", "deleted_at": null, "is_primary": false, "state": "CA", "street": "1216 Main Street", "updated_at": "Thu, 10 Jan 2019 03:56:26 GMT", "zip": "500" } ], "created_at": "Thu, 10 Jan 2019 03:56:26 GMT", "currency": "USD", "customer_id": "204987158183621343381078484949153439747", "deleted_at": null, "email": "*****@*****.**", "first_name": "John", "last_name": "Smith4", "last_order_date": null, "links": [ { "href": "/customer/204987158183621343381078484949153439747", "rel": "self" } ], "total_spent": 0, "updated_at": "Thu, 10 Jan 2019 03:56:26 GMT" }, "result": "ok" } """ customer_json = request.json error = best_match(Draft4Validator(schema).iter_errors(customer_json)) if error: return jsonify({"error": error.message}), 400 store = Store.objects.filter(app_id=request.headers.get('APP-ID'), deleted_at=None).first() existing_customer = Customer.objects.filter( email=customer_json.get("email"), store_id=store).first() if existing_customer: return jsonify({"error": "CUSTOMER_ALREADY_EXISTS"}), 400 count_is_primary = 0 if customer_json.get("addresses"): for address in customer_json.get("addresses"): if address.get("is_primary"): if address.get("is_primary") == "true": count_is_primary += 1 if count_is_primary > 1: return jsonify({ "error": "MULTIPLE_PRIMARY_ADDRESSES_SUPPLIED" }), 400 customer = Customer(currency=customer_json.get("currency"), email=customer_json.get("email"), first_name=customer_json.get("first_name"), last_name=customer_json.get("last_name"), customer_id=str(uuid.uuid4().int), store_id=store).save() customer.set_password(customer_json.get("password")) if customer_json.get("addresses"): addresses = [] for address in customer_json.get("addresses"): is_primary = False if address.get("is_primary"): is_primary = True if address.get( "is_primary").lower() == "true" else False address = Address(street=address.get("street"), city=address.get("city"), zip=address.get("zip"), state=address.get("state"), country=address.get("country"), is_primary=is_primary, customer_id=customer, address_id=str(uuid.uuid4().int)) addresses.append(address) for add in addresses: Address.objects.insert(add) response = {"result": "ok", "customer": customer_obj(customer)} return jsonify(response), 201