From 1eaef0804646419a190e7b29926a50694c256c9b Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Mon, 16 Nov 2020 08:15:30 -0300 Subject: [PATCH 01/12] add collumns table order --- app/constants.py | 9 ++++++ app/domains/domain_order.py | 12 +++++++- ...28974_add_order_status_ans_last_updated.py | 30 +++++++++++++++++++ app/models/order.py | 2 ++ app/schemas/order_schema.py | 2 ++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/migrations/versions/fbb5bca28974_add_order_status_ans_last_updated.py diff --git a/app/constants.py b/app/constants.py index c5493c26..7b2f55fe 100644 --- a/app/constants.py +++ b/app/constants.py @@ -19,3 +19,12 @@ class StepsOrder(enum.Enum): GENERATE_INVOICE = 'GENERATE_INVOICE' IN_TRANSIT = 'IN_TRANSIT' DELIVERED_ORDER = 'DELIVERED_ORDER' + +class OrderSatus(enum.Enum): + PAYMENT_PENDING = 'PAYMENT_PENDING' + PAYMENT_PAID = 'PAYMENT_PAID' + PAYMENT_CANCELLED = 'PAYMENT_CANCELLED' + PREPARING_ORDER = 'PREPARING_ORDER' + SHIPPING_ORDER = 'SHIPPING_ORDER' + GENERATE_INVOICE = 'GENERATE_INVOICE' + SHIPPING_COMPLETE = 'SHIPPING_COMPLETE' \ No newline at end of file diff --git a/app/domains/domain_order.py b/app/domains/domain_order.py index 6c88be39..d39b6943 100644 --- a/app/domains/domain_order.py +++ b/app/domains/domain_order.py @@ -4,6 +4,7 @@ from schemas.order_schema import ProductSchema, OrderSchema, OrderFullResponse from models.order import Product, Order, OrderItems +from models.transaction import Payment from models.users import User def get_product(db : Session, uri): @@ -64,4 +65,13 @@ def create_order(db: Session, order_data: OrderSchema): db.add(db_order) db.commit() db.refresh(db_order) - return db_order \ No newline at end of file + return db_order + + +def mydecoretor(func): + def order_status(*args, **kwargs, db: Session): + orders = db.query(Order) + orderState = [{ + "Order_id": order.id, + "Payment_id":order.payment_id} for order in orders] + return order_status \ No newline at end of file diff --git a/app/migrations/versions/fbb5bca28974_add_order_status_ans_last_updated.py b/app/migrations/versions/fbb5bca28974_add_order_status_ans_last_updated.py new file mode 100644 index 00000000..a732182c --- /dev/null +++ b/app/migrations/versions/fbb5bca28974_add_order_status_ans_last_updated.py @@ -0,0 +1,30 @@ +"""add order_status ans last_updated + +Revision ID: fbb5bca28974 +Revises: dad47228ad42 +Create Date: 2020-11-11 14:45:01.511562 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'fbb5bca28974' +down_revision = 'dad47228ad42' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('order', sa.Column('last_updated', sa.DateTime(), nullable=True)) + op.add_column('order', sa.Column('order_status', sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('order', 'order_status') + op.drop_column('order', 'last_updated') + # ### end Alembic commands ### diff --git a/app/models/order.py b/app/models/order.py index 53d236bb..9ca0e6fe 100644 --- a/app/models/order.py +++ b/app/models/order.py @@ -35,6 +35,8 @@ class Order(Base): order_date = Column(DateTime) tracking_number = Column(Integer, nullable=True) payment_id = Column(Integer, nullable=True) + order_status = Column(String) + last_updated = Column(DateTime) class OrderItems(Base): diff --git a/app/schemas/order_schema.py b/app/schemas/order_schema.py index 83d1bf03..61788ec4 100644 --- a/app/schemas/order_schema.py +++ b/app/schemas/order_schema.py @@ -28,6 +28,8 @@ class OrderSchema(BaseModel): # order_items_id: int tracking_number: int payment_id: int + order_status: str + last_updated: datetime class OrderFullResponse(BaseModel): From dec1220a756ec4c888fc18f514ba351160cda547 Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Mon, 23 Nov 2020 16:22:25 -0300 Subject: [PATCH 02/12] resolvend conflict two --- app/domains/domain_order.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/domains/domain_order.py b/app/domains/domain_order.py index a736f3ca..1f2821be 100644 --- a/app/domains/domain_order.py +++ b/app/domains/domain_order.py @@ -79,7 +79,6 @@ def create_order(db: Session, order_data: OrderSchema): db.commit() db.refresh(db_order) return db_order -<<<<<<< HEAD def mydecoretor(func): @@ -89,5 +88,3 @@ def order_status(*args, **kwargs, db: Session): "Order_id": order.id, "Payment_id":order.payment_id} for order in orders] return order_status -======= ->>>>>>> upstream/staging From 0f73147f2097d351b6d8440cc5a6d01aeb50554d Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Mon, 23 Nov 2020 16:36:34 -0300 Subject: [PATCH 03/12] add enum OrderStatus --- app/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/constants.py b/app/constants.py index 7b2f55fe..29557b4a 100644 --- a/app/constants.py +++ b/app/constants.py @@ -20,7 +20,7 @@ class StepsOrder(enum.Enum): IN_TRANSIT = 'IN_TRANSIT' DELIVERED_ORDER = 'DELIVERED_ORDER' -class OrderSatus(enum.Enum): +class OrderStatus(enum.Enum): PAYMENT_PENDING = 'PAYMENT_PENDING' PAYMENT_PAID = 'PAYMENT_PAID' PAYMENT_CANCELLED = 'PAYMENT_CANCELLED' From c78b39f73b4c848b0ac529ea9c8cb28dfea7efbe Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Mon, 23 Nov 2020 16:37:44 -0300 Subject: [PATCH 04/12] add collum gateway_id --- .../versions/558effa87e2e_add_gateway_id.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/migrations/versions/558effa87e2e_add_gateway_id.py diff --git a/app/migrations/versions/558effa87e2e_add_gateway_id.py b/app/migrations/versions/558effa87e2e_add_gateway_id.py new file mode 100644 index 00000000..62b2a463 --- /dev/null +++ b/app/migrations/versions/558effa87e2e_add_gateway_id.py @@ -0,0 +1,28 @@ +"""add gateway_id + +Revision ID: 558effa87e2e +Revises: fbb5bca28974 +Create Date: 2020-11-20 17:48:58.190668 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '558effa87e2e' +down_revision = 'fbb5bca28974' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('payment', sa.Column('gateway_id', sa.Integer(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('payment', 'gateway_id') + # ### end Alembic commands ### From e4f25892bc78ef3502f3cc5525341ec1c60ad45c Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Mon, 23 Nov 2020 16:38:26 -0300 Subject: [PATCH 05/12] add test --- app/gateway/tests/test_gateway.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 app/gateway/tests/test_gateway.py diff --git a/app/gateway/tests/test_gateway.py b/app/gateway/tests/test_gateway.py new file mode 100644 index 00000000..6671c8bf --- /dev/null +++ b/app/gateway/tests/test_gateway.py @@ -0,0 +1,5 @@ +from gateway.payment_gateway import return_transaction + +def test_return_transaction(): + transaction = return_transaction(10460784) + assert transaction.get("gateway_id") == 10460784 From 319de97194a6a9466ecb6401cc2556f8fe10aed9 Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Mon, 23 Nov 2020 16:40:21 -0300 Subject: [PATCH 06/12] add collum gateway_id --- app/models/transaction.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/transaction.py b/app/models/transaction.py index 01f1dbe1..9e4daab9 100644 --- a/app/models/transaction.py +++ b/app/models/transaction.py @@ -30,6 +30,7 @@ class Payment(Base): user = relationship("User", foreign_keys=[user_id], backref="payment", cascade="all,delete", uselist=False) amount = Column(Integer) token = Column(String(25), nullable=True) + gateway_id = Column(Integer) status = Column(String) authorization = Column(String, nullable=True) payment_method = Column(String) @@ -38,6 +39,7 @@ class Payment(Base): processed = Column(Boolean, default=False, server_default='0', nullable=False) processed_at = Column(DateTime, nullable=True) + class CreditCardFeeConfig(Base): id = Column(Integer, nullable=False, primary_key=True) From f4d4bdf6ae0e6ebd3b243870780397c20d25ba7f Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Wed, 25 Nov 2020 21:45:07 -0300 Subject: [PATCH 07/12] add tests --- app/endpoints/v1/tests/test_order.py | 45 ++++++++++++++++++++++++---- app/gateway/tests/test_gateway.py | 4 +-- app/models/tests/test_user.py | 2 +- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/endpoints/v1/tests/test_order.py b/app/endpoints/v1/tests/test_order.py index 1282e270..18871eb8 100644 --- a/app/endpoints/v1/tests/test_order.py +++ b/app/endpoints/v1/tests/test_order.py @@ -7,22 +7,27 @@ from app.endpoints.deps import get_db -from app.schemas.order_schema import ProductSchema +from app.schemas.order_schema import ProductSchema, OrderStatusSchema from domains.domain_order import create_order +from endpoints.v1.order import status_pending, status_paid, order_status, check_status_pedding + + def test_create_order(t_client): order = { - "id": 100, + "id": 3, "customer_id": 1, "order_date": "2020-11-11 17:01:01", "tracking_number": 2341231, - "payment_id":1 + "payment_id":1, + "last_updated":"2020-11-11 17:01:01", + "order_status":"pending" } r = t_client.post("/order/create_order", json=order) response = r.json() assert r.status_code == 200 - assert response.get("id") == 100 + assert response.get("id") == 3 def test_put_order(t_client): @@ -30,22 +35,50 @@ def test_put_order(t_client): "id": 2, "customer_id": 1, "order_date": "2020-11-11 17:30:01", + "payment_id":1, "tracking_number": 3334131, - "payment_id":1 } r = t_client.put("/order/2", json=order) response = r.json() assert r.status_code == 200 assert response.get("tracking_number") == 3334131 + def test_get_order_id(t_client): r = t_client.get("/order/1") response = r.json() assert r.status_code == 200 assert response.get("name") == "Jonatas L Oliveira" + def test_get_order_user_id(t_client): r = t_client.get("/order/user/1") response = r.json() assert r.status_code == 200 - assert response.get("name") == "Jonatas L Oliveira" + assert response.get("name") == "User Test" + + +def test_order_status(t_client): + orderState = { + "order_id": 1, + "payment_id":1, + "order_status": "paid" + } + r = t_client.post('/update-payment-and-order-status', json=orderState) + response = r.json() + assert r.status_code == 200 + + +def test_check_status(): + response = check_status_pedding() + assert response == 'pending' + + +def test_status_pending(): + response = status_pending() + assert response.get('status') == 'paid' + + +def test_status_paid(): + data = status_paid() + assert data.get('order_status') == 'PAYMENT_PAID' \ No newline at end of file diff --git a/app/gateway/tests/test_gateway.py b/app/gateway/tests/test_gateway.py index 6671c8bf..594585ec 100644 --- a/app/gateway/tests/test_gateway.py +++ b/app/gateway/tests/test_gateway.py @@ -1,5 +1,5 @@ from gateway.payment_gateway import return_transaction def test_return_transaction(): - transaction = return_transaction(10460784) - assert transaction.get("gateway_id") == 10460784 + transaction = return_transaction(10517987) + assert transaction.get("gateway_id") == 10517987 \ No newline at end of file diff --git a/app/models/tests/test_user.py b/app/models/tests/test_user.py index 2cb5472f..bded8c17 100644 --- a/app/models/tests/test_user.py +++ b/app/models/tests/test_user.py @@ -30,5 +30,5 @@ def test_add_user(db_models): logger.info(db_user) db_models.commit() - assert db_user.id == 3 + assert db_user.id == 1 assert db_user.role == 2 From 19de6517f8b2d1068b4109f24e87e64beaa6fe6b Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Wed, 25 Nov 2020 21:46:28 -0300 Subject: [PATCH 08/12] add gateway_id and update status --- app/domains/domain_order.py | 22 +++++++++----- app/domains/domain_payment.py | 27 ++++++++++++++--- app/endpoints/v1/order.py | 55 +++++++++++++++++++++++++++++++++- app/endpoints/v1/payment.py | 2 ++ app/gateway/payment_gateway.py | 18 +++++++++++ app/schemas/order_schema.py | 6 ++++ 6 files changed, 118 insertions(+), 12 deletions(-) diff --git a/app/domains/domain_order.py b/app/domains/domain_order.py index 1f2821be..03a8970e 100644 --- a/app/domains/domain_order.py +++ b/app/domains/domain_order.py @@ -6,6 +6,9 @@ from models.order import Product, Order, OrderItems from models.transaction import Payment from models.users import User +from loguru import logger +import requests +import json def get_product(db : Session, uri): return db.query(Product).filter(Product.uri == uri).first() @@ -81,10 +84,15 @@ def create_order(db: Session, order_data: OrderSchema): return db_order -def mydecoretor(func): - def order_status(*args, **kwargs, db: Session): - orders = db.query(Order) - orderState = [{ - "Order_id": order.id, - "Payment_id":order.payment_id} for order in orders] - return order_status +# def mydecoretor(func): +# def order_status(*args, **kwargs): +# orders = Session.query(Order).filter(Order.id == id) +# for order in orders: +# orderState ={ +# "order_id": order.id, +# "payment_id": order.payment_id, +# "order_status": order.order_status} +# return requests.post('/update-payment-and-order-status', data=order_states) + + + diff --git a/app/domains/domain_payment.py b/app/domains/domain_payment.py index c1847bb5..49b5d719 100644 --- a/app/domains/domain_payment.py +++ b/app/domains/domain_payment.py @@ -1,3 +1,4 @@ + import requests import json @@ -48,7 +49,8 @@ def credit_card_payment(db: Session, payment: CreditCardPayment): "user": "usuario", "token": r.get("acquirer_id"), "status": r.get('status'), - "authorization_code": r.get("authorization_code"), + "authorization_code": r.get('authorization_code'), + "gateway_id": r.get("id"), "payment_method": "credit-card", "errors": r.get("errors")} except Exception as e: @@ -90,6 +92,7 @@ def process_checkout(db: Session, checkout_data: CheckoutSchema, affiliate=None, db=db, shopping_cart=checkout_data.get('shopping_cart')[0].get("itens"), user=_user) + logger.info(_order) _payment = process_payment( db=db, checkout_data=checkout_data, @@ -99,6 +102,8 @@ def process_checkout(db: Session, checkout_data: CheckoutSchema, affiliate=None, payment_address=_payment_address, affiliate=affiliate, cupom=cupom) + if "pending" or "paid" in _payment.values(): + update_gateway_id(db=db, payment_data=_payment, order=_order) if "paid" in _payment.values(): update_payment_status(db=db, payment_data=_payment, order=_order) if "credit-card" in _payment.values(): @@ -136,6 +141,7 @@ def check_user(db: Session, checkout_data: CheckoutSchema): _document = checkout_data.get('document') _phone = checkout_data.get('phone') logger.info(f"DOCUMENT -----------------{_document}") + _user = check_existent_user(db=db, email=_user_email, document=_document, password=_password) logger.info("----------------USER----------------") @@ -183,8 +189,9 @@ def process_order(db: Session, shopping_cart, user): try: db_order = Order( customer_id=user.id, - order_date=datetime.now() - ) + order_date=datetime.now(), + order_status = 'pending', + payment_id = 1) db.add(db_order) for cart in shopping_cart: db_item = OrderItems( @@ -355,6 +362,17 @@ def postback_payment(db: Session, payment_data, order): except Exception as e: raise e +def update_gateway_id(db:Session, payment_data, order): + try: + db_transaction = db.query(Transaction).filter_by(order_id=order.id).first() + db_payment = db.query(Payment).filter_by(id=db_transaction.payment_id).first() + + db_payment.gateway_id=payment_data.get("gateway_id") + db.add(db_payment) + db.commit() + + except Exception as e: + raise e def update_payment_status(db:Session, payment_data, order): try: @@ -366,6 +384,7 @@ def update_payment_status(db:Session, payment_data, order): db_payment.processed_at=datetime.now() db_payment.token=payment_data.get("token") db_payment.authorization=payment_data.get("authorization_code") + db_payment.gateway_id=payment_data.get("gateway_id") db_payment.status='paid' db.add(db_transaction) db.add(db_payment) @@ -374,4 +393,4 @@ def update_payment_status(db:Session, payment_data, order): # send_payment_mail(db=db, user=_user, payment=_payment) except Exception as e: - raise e + raise e \ No newline at end of file diff --git a/app/endpoints/v1/order.py b/app/endpoints/v1/order.py index c60d493b..c0d28812 100644 --- a/app/endpoints/v1/order.py +++ b/app/endpoints/v1/order.py @@ -1,8 +1,17 @@ from sqlalchemy.orm import Session from fastapi import Header, APIRouter, Depends from domains import domain_order -from schemas.order_schema import OrderSchema, OrderFullResponse +from schemas.order_schema import OrderSchema, OrderFullResponse, OrderStatusSchema +from models.order import Order +from models.transaction import Payment from endpoints.deps import get_db +from constants import OrderStatus +from gateway.payment_gateway import return_transaction +from loguru import logger +from job_service.service import get_session + +import requests +import json order = APIRouter() @@ -45,3 +54,47 @@ async def create_order( ): return domain_order.create_order(db=db, order_data=order_data) + +@order.post('/update-payment-and-order-status', status_code=200) +def order_status(): + db = get_session() + orders = db.query(Order).filter(Order.id.isnot(None)) + for order in orders: + return { + "order_id": order.id, + "payment_id": order.payment_id, + "order_status": order.order_status} + + +def check_status_pedding(): + data = order_status() + logger.debug(data) + if data.get('order_status') == 'pending': + return 'pending' + + +def status_pending(): + data = order_status() + logger.debug(data) + db = get_session() + payment = db.query(Payment).filter_by(id= data.get('payment_id')).first() + return return_transaction(payment.gateway_id) + + +def status_paid(): + gateway = status_pending() + data = order_status() + logger.debug(gateway.get('status')) + if gateway.get('status') == 'paid' and data.get('order_status') == 'pending': + logger.debug(data) + data['order_status'] = OrderStatus.PAYMENT_PAID.value + logger.debug(data) + return data + + +def alternate_status(status): + order_status = { + 'pending' : status_pending, + 'paid': status_paid + } + return order_status[status] \ No newline at end of file diff --git a/app/endpoints/v1/payment.py b/app/endpoints/v1/payment.py index 54998203..a0823df6 100644 --- a/app/endpoints/v1/payment.py +++ b/app/endpoints/v1/payment.py @@ -7,6 +7,8 @@ from domains import domain_payment from endpoints import deps +from loguru import logger + payment = APIRouter() diff --git a/app/gateway/payment_gateway.py b/app/gateway/payment_gateway.py index e69de29b..d92ddde1 100644 --- a/app/gateway/payment_gateway.py +++ b/app/gateway/payment_gateway.py @@ -0,0 +1,18 @@ +import requests +import json + +from dynaconf import settings + +def return_transaction(gateway_id): + try: + headers = {'Content-Type': 'application/json'} + data = { + "api_key": settings.GATEWAY_API + } + r = requests.get(f'https://api.pagar.me/1/transactions/{gateway_id}', headers=headers, json=data ) + r = r.json() + return { + "gateway_id": r.get("tid"), + "status": r.get("status") } + except Exception as e: + raise e \ No newline at end of file diff --git a/app/schemas/order_schema.py b/app/schemas/order_schema.py index ff422fc3..fe31bdfe 100644 --- a/app/schemas/order_schema.py +++ b/app/schemas/order_schema.py @@ -121,3 +121,9 @@ class CheckoutResponseSchema(BaseModel): order_id: int name: str slip_payment: Optional[str] + + +class OrderStatusSchema(BaseModel): + order_id: int + payment_id: int + order_status: str \ No newline at end of file From f4ec74341746a75de587cd6e6a590c35919a1568 Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Wed, 25 Nov 2020 21:51:04 -0300 Subject: [PATCH 09/12] delete decorator --- app/domains/domain_order.py | 14 -------------- app/endpoints/v1/order.py | 2 +- app/schemas/order_schema.py | 7 +------ 3 files changed, 2 insertions(+), 21 deletions(-) diff --git a/app/domains/domain_order.py b/app/domains/domain_order.py index 03a8970e..a316875c 100644 --- a/app/domains/domain_order.py +++ b/app/domains/domain_order.py @@ -82,17 +82,3 @@ def create_order(db: Session, order_data: OrderSchema): db.commit() db.refresh(db_order) return db_order - - -# def mydecoretor(func): -# def order_status(*args, **kwargs): -# orders = Session.query(Order).filter(Order.id == id) -# for order in orders: -# orderState ={ -# "order_id": order.id, -# "payment_id": order.payment_id, -# "order_status": order.order_status} -# return requests.post('/update-payment-and-order-status', data=order_states) - - - diff --git a/app/endpoints/v1/order.py b/app/endpoints/v1/order.py index c0d28812..3b33489b 100644 --- a/app/endpoints/v1/order.py +++ b/app/endpoints/v1/order.py @@ -1,7 +1,7 @@ from sqlalchemy.orm import Session from fastapi import Header, APIRouter, Depends from domains import domain_order -from schemas.order_schema import OrderSchema, OrderFullResponse, OrderStatusSchema +from schemas.order_schema import OrderSchema, OrderFullResponse from models.order import Order from models.transaction import Payment from endpoints.deps import get_db diff --git a/app/schemas/order_schema.py b/app/schemas/order_schema.py index fe31bdfe..acc3e3af 100644 --- a/app/schemas/order_schema.py +++ b/app/schemas/order_schema.py @@ -121,9 +121,4 @@ class CheckoutResponseSchema(BaseModel): order_id: int name: str slip_payment: Optional[str] - - -class OrderStatusSchema(BaseModel): - order_id: int - payment_id: int - order_status: str \ No newline at end of file + \ No newline at end of file From bc6326a1f58cf8a868852e1a2290616808ccc8e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B4natas=20Oliveira?= Date: Thu, 26 Nov 2020 00:43:11 -0300 Subject: [PATCH 10/12] add shopping cart --- app/domains/domain_order.py | 29 ++++++++++++++++++++++++++++ app/endpoints/v1/product.py | 30 +++++++++++++++++++++++++++-- app/endpoints/v1/tests/test_mail.py | 2 +- app/schemas/order_schema.py | 6 ++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/app/domains/domain_order.py b/app/domains/domain_order.py index 2e88a048..b3ef96c5 100644 --- a/app/domains/domain_order.py +++ b/app/domains/domain_order.py @@ -29,6 +29,35 @@ def get_showcase(db:Session): return { "products": []} +def get_installments(db: Session, cart): + _cart = cart.dict() + _product_id = _cart['cart'][0]['product_id'] + _product_config = db.query(Product).filter_by(id=int(_product_id)).first() + _total_amount = 0 + _installments = [] + + for item in _cart['cart']: + _total_amount += (item['amount'] * item['qty']) + + for n in range(1,13): + _installment = (_total_amount/n)/100 + _installments.append({"name": f"{n} x R${_installment}", "value": f"{n}"}) + + + return _installments + # _config_installments = db.query(CreditCardFeeConfig)\ + # .filter_by(id=_product_config.installments_config)\ + # .first() + # if _installments > 12: + # raise Exception("O número máximo de parcelas é 12") + # elif _installments >= _config_installments.min_installment_with_fee: + # _total_amount = _total_amount * ((1+_config_installments.fee) ** _installments) + + +def get_product_by_id(db: Session, id): + product = db.query(Product).filter_by(id=id).first() + return ProductInDB.from_orm(product) + def get_order(db: Session, id): users = db.query(User).join(Order, Order.customer_id == User.id).filter(Order.id == id) diff --git a/app/endpoints/v1/product.py b/app/endpoints/v1/product.py index e6e69a8d..6e01ad16 100644 --- a/app/endpoints/v1/product.py +++ b/app/endpoints/v1/product.py @@ -1,14 +1,15 @@ from sqlalchemy.orm import Session from fastapi import Header, APIRouter, Depends from domains import domain_order -from schemas.order_schema import OrderSchema, OrderFullResponse, ProductSchema +from schemas.order_schema import OrderSchema, OrderFullResponse,\ + ProductSchema, InstallmentSchema from endpoints.deps import get_db from loguru import logger product = APIRouter() -@product.get('/product/showcase/all', status_code=200) +@product.get('/showcase/all', status_code=200) async def get_showcase( *, db: Session = Depends(get_db) @@ -18,3 +19,28 @@ async def get_showcase( except Exception as e: logger.error(f"Erro em obter os produtos - { e }") raise e + + +@product.get('/{id}', status_code=200) +async def get_product_id( + *, + db: Session = Depends(get_db), + id + ): + try: + return domain_order.get_product_by_id(db, id) + except Exception as e: + logger.error(f"Erro em obter os produto - { e }") + raise e + +@product.post('/cart/installments', status_code=200) +async def get_installments( + *, + db: Session = Depends(get_db), + cart: InstallmentSchema + ): + try: + return domain_order.get_installments(db, cart=cart) + except Exception as e: + logger.error(f"Erro ao coletar o parcelamento - {e}") + raise e diff --git a/app/endpoints/v1/tests/test_mail.py b/app/endpoints/v1/tests/test_mail.py index 483e3f0b..35a17071 100644 --- a/app/endpoints/v1/tests/test_mail.py +++ b/app/endpoints/v1/tests/test_mail.py @@ -23,6 +23,6 @@ def test_send_mail(t_client): data=mail_data.json() ) - assert resp.status_code == 201 + assert resp.status_code == 200 logger.debug(f"JSON --- {resp.json()}") assert resp.json().get("message") == "Mail Sended" diff --git a/app/schemas/order_schema.py b/app/schemas/order_schema.py index cc2d9246..587d24d7 100644 --- a/app/schemas/order_schema.py +++ b/app/schemas/order_schema.py @@ -24,6 +24,7 @@ class ProductResponseSchema(ProductSchema): class ProductInDB(BaseModel): + id: int name: str uri: str price: int @@ -49,6 +50,11 @@ class ListProducts(BaseModel): class Config: orm_mode = True + +class InstallmentSchema(BaseModel): + cart: list + + class OrderSchema(BaseModel): id: int customer_id: int From a580b6f02e89bdd1c10d9faeade799f5c70180b6 Mon Sep 17 00:00:00 2001 From: Thais Martins Date: Thu, 26 Nov 2020 11:16:36 -0300 Subject: [PATCH 11/12] add collumns in product --- app/models/order.py | 4 ++++ app/schemas/order_schema.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/app/models/order.py b/app/models/order.py index a79c818e..993fbd5f 100644 --- a/app/models/order.py +++ b/app/models/order.py @@ -25,6 +25,10 @@ class Product(Base): quantity = Column(Integer, server_default="9999") showcase = Column(Boolean, default=False, server_default='0') show_discount = Column(Boolean, default=False, server_default='0') + heigth= Column(Integer, nullable=True) + width= Column(Integer, nullable=True) + weigth= Column(Integer, nullable=True) + depthe= Column(Integer, nullable=True) class Cupons(Base): diff --git a/app/schemas/order_schema.py b/app/schemas/order_schema.py index acc3e3af..e2ed41f0 100644 --- a/app/schemas/order_schema.py +++ b/app/schemas/order_schema.py @@ -38,6 +38,10 @@ class ProductInDB(BaseModel): quantity: Optional[int] showcase: bool show_discount: bool + heigth: int + width: int + weigth: int + depthe: int class Config: orm_mode = True From fb439ab8fa695f85b740cd53e71d80c9711fa947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B4natas=20Oliveira?= Date: Thu, 26 Nov 2020 11:59:35 -0300 Subject: [PATCH 12/12] api calc shipping --- app/domains/domain_shipping.py | 31 +++++++++-------- app/endpoints/v1/shipping.py | 10 ++++-- .../versions/e4dd5fb9b8ac_add_new_columns.py | 34 +++++++++++++++++++ ...839ce991ab7_merge_migrations_to_product.py | 24 +++++++++++++ app/schemas/order_schema.py | 10 +++--- app/schemas/shipping_schema.py | 7 +++- 6 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 app/migrations/versions/e4dd5fb9b8ac_add_new_columns.py create mode 100644 app/migrations/versions/f839ce991ab7_merge_migrations_to_product.py diff --git a/app/domains/domain_shipping.py b/app/domains/domain_shipping.py index ba889c42..60d4c075 100644 --- a/app/domains/domain_shipping.py +++ b/app/domains/domain_shipping.py @@ -3,20 +3,21 @@ from dynaconf import settings def shipping_zip_code(shipping: Shipping): - try: - shipping = CalculateShipping( - zip_code_source=settings.ZIP_CODE_SOURCE, - zip_code_target=shipping.zip_code_target, - weigth=shipping.weigth, - length=shipping.length, - heigth=shipping.heigth, - width=shipping.width, - diameter=shipping.diameter - ) - shipping = shipping.calculate_shipping() - return shipping - except Exception as e: - raise e + return { "shipping": 1000} + # try: + # shipping = CalculateShipping( + # zip_code_source=settings.ZIP_CODE_SOURCE, + # zip_code_target=shipping.zip_code_target, + # weigth=shipping.weigth, + # length=shipping.length, + # heigth=shipping.heigth, + # width=shipping.width, + # diameter=shipping.diameter + # ) + # shipping = shipping.calculate_shipping() + # return shipping + # except Exception as e: + # raise e def adress_zip_code(shipping: Shipping, status_code=200): try: @@ -27,4 +28,4 @@ def adress_zip_code(shipping: Shipping, status_code=200): raise e - \ No newline at end of file + diff --git a/app/endpoints/v1/shipping.py b/app/endpoints/v1/shipping.py index 75feaf9b..1ab4d275 100644 --- a/app/endpoints/v1/shipping.py +++ b/app/endpoints/v1/shipping.py @@ -1,6 +1,6 @@ from fastapi import Header, APIRouter, Depends from sqlalchemy.orm import Session -from schemas.shipping_schema import Shipping +from schemas.shipping_schema import Shipping, ShippingCalc from domains import domain_shipping from endpoints.deps import get_db @@ -14,4 +14,10 @@ def zip_code_shipping(shipping_data: Shipping): @shipping.post('/zip_code/adress', status_code=200) def zip_code_adress(shipping_data: Shipping): adress = domain_shipping.adress_zip_code(shipping=shipping_data) - return adress \ No newline at end of file + return adress + +@shipping.post('/zip_code/shipping/calc', status_code=200) +def zip_code_shipping(shipping_data: ShippingCalc): + from loguru import logger + logger.debug(shipping_data) + return { "shipping": 1000} diff --git a/app/migrations/versions/e4dd5fb9b8ac_add_new_columns.py b/app/migrations/versions/e4dd5fb9b8ac_add_new_columns.py new file mode 100644 index 00000000..da870648 --- /dev/null +++ b/app/migrations/versions/e4dd5fb9b8ac_add_new_columns.py @@ -0,0 +1,34 @@ +"""add new columns + +Revision ID: e4dd5fb9b8ac +Revises: f839ce991ab7 +Create Date: 2020-11-26 11:45:18.583785 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'e4dd5fb9b8ac' +down_revision = 'f839ce991ab7' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('product', sa.Column('depthe', sa.Integer(), nullable=True)) + op.add_column('product', sa.Column('heigth', sa.Integer(), nullable=True)) + op.add_column('product', sa.Column('weigth', sa.Integer(), nullable=True)) + op.add_column('product', sa.Column('width', sa.Integer(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('product', 'width') + op.drop_column('product', 'weigth') + op.drop_column('product', 'heigth') + op.drop_column('product', 'depthe') + # ### end Alembic commands ### diff --git a/app/migrations/versions/f839ce991ab7_merge_migrations_to_product.py b/app/migrations/versions/f839ce991ab7_merge_migrations_to_product.py new file mode 100644 index 00000000..54d7b3b6 --- /dev/null +++ b/app/migrations/versions/f839ce991ab7_merge_migrations_to_product.py @@ -0,0 +1,24 @@ +"""merge migrations to product + +Revision ID: f839ce991ab7 +Revises: 558effa87e2e, 6f91dc2c2189 +Create Date: 2020-11-26 11:44:12.928088 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f839ce991ab7' +down_revision = ('558effa87e2e', '6f91dc2c2189') +branch_labels = None +depends_on = None + + +def upgrade(): + pass + + +def downgrade(): + pass diff --git a/app/schemas/order_schema.py b/app/schemas/order_schema.py index 5017ea7b..a2143131 100644 --- a/app/schemas/order_schema.py +++ b/app/schemas/order_schema.py @@ -39,10 +39,10 @@ class ProductInDB(BaseModel): quantity: Optional[int] showcase: bool show_discount: bool - heigth: int - width: int - weigth: int - depthe: int + heigth: Optional[int] + width: Optional[int] + weigth: Optional[int] + depthe: Optional[int] class Config: orm_mode = True @@ -131,4 +131,4 @@ class CheckoutResponseSchema(BaseModel): order_id: int name: str slip_payment: Optional[str] - \ No newline at end of file + diff --git a/app/schemas/shipping_schema.py b/app/schemas/shipping_schema.py index 508836af..70a21cbc 100644 --- a/app/schemas/shipping_schema.py +++ b/app/schemas/shipping_schema.py @@ -6,4 +6,9 @@ class Shipping(BaseModel): length: str heigth: str width: str - diameter: str \ No newline at end of file + diameter: str + + +class ShippingCalc(BaseModel): + shipping: str + cart: list