From 4f24570d3cbc7e46eea9f8b2e7324f6a61ad7dfb Mon Sep 17 00:00:00 2001 From: Edwin Eames Date: Fri, 26 Sep 2025 20:30:42 -0400 Subject: [PATCH] working auto --- app/crud.py | 7 +++++ app/main.py | 2 ++ app/models.py | 53 +++++++++++++++++++++++++++++++++++++- app/routers/auto.py | 45 ++++++++++++++++++++++++++++++++ app/routers/payment.py | 7 +++-- app/routers/transaction.py | 33 +++++++++++++++++++++++- app/schemas.py | 4 ++- 7 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 app/routers/auto.py diff --git a/app/crud.py b/app/crud.py index aeaac48..c6b7865 100644 --- a/app/crud.py +++ b/app/crud.py @@ -90,6 +90,13 @@ def get_transaction_by_auth_id(db: Session, auth_net_transaction_id: str): models.Transaction.auth_net_transaction_id == auth_net_transaction_id ).first() +def get_transaction_by_auto_id(db: Session, auto_id: int): + return db.query(models.Transaction).filter( + models.Transaction.auto_id == auto_id, + models.Transaction.transaction_type == 1, + models.Transaction.status == 0 + ).first() + def update_transaction_for_capture(db: Session, auth_net_transaction_id: str, charge_amount: Decimal, status: int, rejection_reason: str = None): transaction = db.query(models.Transaction).filter(models.Transaction.auth_net_transaction_id == auth_net_transaction_id).first() if not transaction: diff --git a/app/main.py b/app/main.py index 3b87616..931ae29 100644 --- a/app/main.py +++ b/app/main.py @@ -3,6 +3,7 @@ from .database import engine from . import models from .routers import payment from .routers.transaction import transaction_router +from .routers.auto import auto_router from .routers.user_check import user_check_router from fastapi.middleware.cors import CORSMiddleware from config import load_config @@ -29,6 +30,7 @@ app.add_middleware( app.include_router(payment.router, prefix="/api", tags=["payment"]) app.include_router(transaction_router, prefix="/api", tags=["transactions"]) +app.include_router(auto_router, prefix="/api", tags=["auto"]) app.include_router(user_check_router, prefix="/user", tags=["usercheck"]) diff --git a/app/models.py b/app/models.py index 63c1632..e635e7a 100644 --- a/app/models.py +++ b/app/models.py @@ -1,6 +1,6 @@ ## File: your_app/models.py -from sqlalchemy import Column, Integer, String, Float, DateTime, Boolean, ForeignKey, Numeric +from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, Numeric from .database import Base import datetime @@ -68,7 +68,58 @@ class Transaction(Base): auth_net_transaction_id = Column(String, unique=True, index=True, nullable=True) service_id = Column(Integer, nullable=True) delivery_id = Column(Integer, nullable=True) + auto_id = Column(Integer, nullable=True) card_id = Column(Integer, nullable=True) payment_gateway = Column(Integer, default=1) rejection_reason = Column(String, nullable=True) created_at = Column(DateTime, default=datetime.datetime.utcnow) + + + + +class Auto_Delivery(Base): + __tablename__ = 'auto_delivery' + + id = Column(Integer, primary_key=True, index=True) + customer_id = Column(Integer, nullable=False) + account_number = Column(String(25)) + customer_town = Column(String(140)) + customer_state = Column(Integer) + customer_address = Column(String(1000)) + customer_zip = Column(String(25)) + customer_full_name = Column(String(250)) + last_fill = Column(DateTime, nullable=True) + days_since_last_fill = Column(Integer) + last_updated = Column(DateTime, nullable=True) + estimated_gallons_left = Column(Numeric(10, 2), nullable=True) + estimated_gallons_left_prev_day = Column(Numeric(10, 2), nullable=True) + tank_height = Column(String(25)) + tank_size = Column(String(25)) + house_factor = Column(Numeric(5, 2), nullable=True) + auto_status = Column(Integer, nullable=True) + open_ticket_id = Column(Integer, nullable=True) + +class Tickets_Auto_Delivery(Base): + __tablename__ = 'auto_tickets' + + id = Column(Integer, primary_key=True, index=True) + customer_id = Column(Integer, nullable=False) + account_number = Column(String(25)) + + customer_town = Column(String(140)) + customer_state = Column(Integer) + customer_address = Column(String(1000)) + customer_zip = Column(String(25)) + customer_full_name = Column(String(250)) + + oil_prices_id = Column(Integer, nullable=True) + fill_date = Column(DateTime, nullable=True) + gallons_delivered = Column(Numeric(10, 2), nullable=True) + price_per_gallon = Column(Numeric(10, 2), nullable=True) + + total_amount_customer = Column(Numeric(10, 2), nullable=True) + customer_town = Column(String(140)) + + payment_type = Column(Integer, nullable=True) + payment_card_id = Column(Integer, nullable=True) + payment_status = Column(Integer, nullable=True) \ No newline at end of file diff --git a/app/routers/auto.py b/app/routers/auto.py new file mode 100644 index 0000000..0b2ffa4 --- /dev/null +++ b/app/routers/auto.py @@ -0,0 +1,45 @@ +from fastapi import APIRouter, Depends, HTTPException +from sqlalchemy.orm import Session + +from app import crud, database + +# Create a router for auto-specific transaction endpoints +auto_router = APIRouter( + prefix="/auto/transaction", + tags=["auto_transaction"], + responses={404: {"description": "Not found"}}, +) + +@auto_router.get("/delivery/{auto_id}", summary="Get pre-authorization transaction for auto delivery") +def get_auto_delivery_transaction(auto_id: int, db: Session = Depends(database.get_db)): + """ + Get the pre-authorization transaction for an auto ticket delivery. + Used by the capture page for auto deliveries. + """ + transaction = crud.get_transaction_by_auto_id(db, auto_id=auto_id) + if not transaction: + raise HTTPException(status_code=404, detail="No pre-authorization transaction found for this auto delivery") + + return { + "id": transaction.id, + "transaction_type": transaction.transaction_type, + "status": transaction.status, + "auth_net_transaction_id": transaction.auth_net_transaction_id, + "preauthorize_amount": transaction.preauthorize_amount + } + + +@auto_router.put("/delivery/{current_auto_id}/update/{new_auto_id}", summary="Update auto_id for transaction") +def update_transaction_auto_id(current_auto_id: int, new_auto_id: int, db: Session = Depends(database.get_db)): + """ + Update the auto_id of a transaction based on the current auto_id. + Used to change transaction from auto_delivery.id to auto_ticket.id + """ + transaction = crud.get_transaction_by_auto_id(db, auto_id=current_auto_id) + if not transaction: + raise HTTPException(status_code=404, detail="No transaction found for this auto delivery") + + transaction.delivery_id = new_auto_id + transaction.auto_id = new_auto_id + db.commit() + return {"message": "Transaction auto_id updated"} diff --git a/app/routers/payment.py b/app/routers/payment.py index 884e125..57c1899 100644 --- a/app/routers/payment.py +++ b/app/routers/payment.py @@ -238,18 +238,19 @@ def authorize_saved_card(customer_id: int, transaction_req: schemas.TransactionA ) status, auth_net_transaction_id, rejection_reason = _parse_authnet_response(auth_net_response) - + print(transaction_req) # Create the transaction record in your database with the correct type transaction_data = schemas.TransactionBase( preauthorize_amount=transaction_req.preauthorize_amount, transaction_type=TransactionType.AUTHORIZE, # This is key service_id=transaction_req.service_id, delivery_id=transaction_req.delivery_id, + auto_id=transaction_req.auto_id, card_id=transaction_req.card_id, rejection_reason=rejection_reason ) - return crud.create_transaction( + db_transaction = crud.create_transaction( db=db, transaction=transaction_data, customer_id=customer_id, @@ -257,4 +258,6 @@ def authorize_saved_card(customer_id: int, transaction_req: schemas.TransactionA auth_net_transaction_id=auth_net_transaction_id ) + return db_transaction + # --- CAPTURE ENDPOINT MOVED TO TRANSACTION ROUTER --- diff --git a/app/routers/transaction.py b/app/routers/transaction.py index 0d4683b..05f6620 100644 --- a/app/routers/transaction.py +++ b/app/routers/transaction.py @@ -8,7 +8,7 @@ import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) -from app import crud, database, schemas +from app import crud, database, schemas, models from app.services import payment_service # Create a router for transaction endpoints @@ -42,6 +42,37 @@ def get_delivery_transaction(delivery_id: int, db: Session = Depends(database.ge "preauthorize_amount": transaction.preauthorize_amount } + +@transaction_router.put("/transaction/delivery/{delivery_id}/update/{new_delivery_id}", summary="Update delivery_id for transaction") +def update_transaction_delivery(delivery_id: int, new_delivery_id: int, db: Session = Depends(database.get_db)): + """ + Update the delivery_id of a transaction based on the current delivery_id. + Used to change transaction from auto_delivery.id to auto_ticket.id + """ + transaction = crud.get_transaction_by_delivery_id(db, delivery_id=delivery_id) + if not transaction: + raise HTTPException(status_code=404, detail="No transaction found for this delivery") + + transaction.delivery_id = new_delivery_id + db.commit() + return {"message": "Transaction delivery_id updated"} + + +@transaction_router.put("/transaction/{transaction_id}/update_auto_id/{new_auto_id}", summary="Update auto_id for transaction") +def update_transaction_auto_id(transaction_id: int, new_auto_id: int, db: Session = Depends(database.get_db)): + """ + Update the auto_id of a transaction by its ID. + Used to set auto_id to auto_ticket.id after ticket creation. + """ + transaction = db.query(models.Transaction).filter(models.Transaction.id == transaction_id).first() + if not transaction: + raise HTTPException(status_code=404, detail="Transaction not found") + + transaction.auto_id = new_auto_id + db.commit() + return {"message": "Transaction auto_id updated"} + + @transaction_router.post("/capture/", response_model=schemas.Transaction, summary="Capture a previously authorized amount") def capture_authorized_amount(transaction: schemas.TransactionCapture, db: Session = Depends(database.get_db)): # This endpoint captures a previously authorized transaction diff --git a/app/schemas.py b/app/schemas.py index c7f1c45..9144fcf 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -38,6 +38,7 @@ class TransactionAuthorizeByCardID(BaseModel): tax_amount: Optional[Decimal] = Decimal("0.0") service_id: Optional[int] = None delivery_id: Optional[int] = None + auto_id: Optional[int] = None # --- YOUR EXISTING SCHEMAS (UPDATED for Pydantic V2) --- @@ -47,6 +48,7 @@ class TransactionBase(BaseModel): transaction_type: int service_id: Optional[int] = None delivery_id: Optional[int] = None + auto_id: Optional[int] = None card_id: Optional[int] = None payment_gateway: int = 1 rejection_reason: Optional[str] = None @@ -111,4 +113,4 @@ class CustomerCardResponse(Card): # Now, add the extra customer fields the frontend needs. # We define it as a string because we will be returning the # two-letter abbreviation, not the database ID. - customer_state: str \ No newline at end of file + customer_state: str