Files
eamco_authorize/app/models.py
Edwin Eames 97261f6c51 Refactor payment service, fix DB session, and consolidate endpoints
- Fix critical NameError in database.py by restoring Session factory
- Refactor payment_service.py and crud.py to use shared constants.py and utils.py
- Deduplicate state mapping and input sanitization logic
- Move transaction amount calculation logic from CRUD to Router layer
- Enforce type safety in schemas using IntEnum for TransactionType/Status
- Move capture endpoint from transaction.py to payment.py (now /payments/capture)
- Update create_customer_profile signature for clarity
2026-02-01 12:31:42 -05:00

125 lines
4.8 KiB
Python

## File: app/models.py
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, Numeric
from .database import Base
import datetime
class Customer(Base):
__tablename__ = "customer_customer"
id = Column(Integer, primary_key=True, index=True)
# --- ADD THIS COLUMN ---
# This stores the master profile ID from Authorize.Net's CIM.
auth_net_profile_id = Column(String(100))
# --- YOUR EXISTING COLUMNS ---
account_number = Column(String(25))
customer_last_name = Column(String(250))
customer_first_name = Column(String(250))
customer_town = Column(String(140))
customer_state = Column(Integer)
customer_zip = Column(String(25))
customer_first_call = Column(DateTime)
customer_email = Column(String(500))
customer_automatic = Column(Integer)
customer_phone_number = Column(String(25))
customer_home_type = Column(Integer)
customer_apt = Column(String(140))
customer_address = Column(String(1000))
company_id = Column(Integer)
customer_latitude = Column(String(250))
customer_longitude = Column(String(250))
correct_address = Column(Boolean)
# --- ADD THIS ENTIRE NEW MODEL ---
class Card(Base):
__tablename__ = "card_card"
id = Column(Integer, primary_key=True, index=True)
date_added = Column(DateTime, default=datetime.datetime.utcnow)
user_id = Column(Integer, nullable=False)
# This stores the payment profile ID for this specific card from Authorize.Net's CIM.
auth_net_payment_profile_id = Column(String, nullable=True)
# Columns to store non-sensitive card info for display purposes
card_number = Column(String(50), nullable=True)
last_four_digits = Column(String(4), nullable=False)
name_on_card = Column(String(500), nullable=True)
expiration_month = Column(String(20), nullable=False)
expiration_year = Column(String(20), nullable=False)
type_of_card = Column(String(50), nullable=True)
security_number = Column(String(10), nullable=True)
accepted_or_declined = Column(Integer, nullable=True)
main_card = Column(Boolean, nullable=True)
zip_code = Column(String(20), nullable=True)
class Transaction(Base):
__tablename__ = "transactions"
id = Column(Integer, primary_key=True, index=True)
# Recommended change: Use Numeric for precision
preauthorize_amount = Column(Numeric(10, 2), nullable=True)
charge_amount = Column(Numeric(10, 2), nullable=True)
customer_id = Column(Integer)
transaction_type = Column(Integer)# 0 = charge, 1 = auth, 2 = capture
status = Column(Integer)
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 removed (duplicate)
payment_type = Column(Integer, nullable=True)
payment_card_id = Column(Integer, nullable=True)
payment_status = Column(Integer, nullable=True)