Created hashed customers. migrated to service instead of using db logic directly
This commit is contained in:
@@ -15,7 +15,6 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import HTMLResponse, Response
|
||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||
from slowapi.errors import RateLimitExceeded
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from alpine_bits_python.schemas import ReservationData
|
||||
@@ -28,6 +27,7 @@ from .alpinebits_server import (
|
||||
)
|
||||
from .auth import generate_unique_id, validate_api_key
|
||||
from .config_loader import load_config
|
||||
from .customer_service import CustomerService
|
||||
from .db import Base, get_database_url
|
||||
from .db import Customer as DBCustomer
|
||||
from .db import Reservation as DBReservation
|
||||
@@ -222,6 +222,17 @@ async def lifespan(app: FastAPI):
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
_LOGGER.info("Database tables checked/created at startup.")
|
||||
|
||||
# Hash any existing customers that don't have hashed versions yet
|
||||
async with AsyncSessionLocal() as session:
|
||||
customer_service = CustomerService(session)
|
||||
hashed_count = await customer_service.hash_existing_customers()
|
||||
if hashed_count > 0:
|
||||
_LOGGER.info(
|
||||
"Backfilled hashed data for %d existing customers", hashed_count
|
||||
)
|
||||
else:
|
||||
_LOGGER.info("All existing customers already have hashed data")
|
||||
|
||||
yield
|
||||
|
||||
# Optional: Dispose engine on shutdown
|
||||
@@ -372,59 +383,30 @@ async def process_wix_form_submission(request: Request, data: dict[str, Any], db
|
||||
|
||||
unique_id = data.get("submissionId", generate_unique_id())
|
||||
|
||||
# use database session
|
||||
# Use CustomerService to handle customer creation/update with hashing
|
||||
customer_service = CustomerService(db)
|
||||
|
||||
# Check if customer with this contact_id already exists
|
||||
existing_customer = None
|
||||
if contact_id:
|
||||
result = await db.execute(
|
||||
select(DBCustomer).where(DBCustomer.contact_id == contact_id)
|
||||
)
|
||||
existing_customer = result.scalar_one_or_none()
|
||||
customer_data = {
|
||||
"given_name": first_name,
|
||||
"surname": last_name,
|
||||
"contact_id": contact_id,
|
||||
"name_prefix": name_prefix,
|
||||
"email_address": email,
|
||||
"phone": phone_number,
|
||||
"email_newsletter": email_newsletter,
|
||||
"address_line": address_line,
|
||||
"city_name": city_name,
|
||||
"postal_code": postal_code,
|
||||
"country_code": country_code,
|
||||
"gender": gender,
|
||||
"birth_date": birth_date,
|
||||
"language": language,
|
||||
"address_catalog": False,
|
||||
"name_title": None,
|
||||
}
|
||||
|
||||
if existing_customer:
|
||||
# Update existing customer with new information
|
||||
_LOGGER.info("Updating existing customer with contact_id: %s", contact_id)
|
||||
existing_customer.given_name = first_name
|
||||
existing_customer.surname = last_name
|
||||
existing_customer.name_prefix = name_prefix
|
||||
existing_customer.email_address = email
|
||||
existing_customer.phone = phone_number
|
||||
existing_customer.email_newsletter = email_newsletter
|
||||
existing_customer.address_line = address_line
|
||||
existing_customer.city_name = city_name
|
||||
existing_customer.postal_code = postal_code
|
||||
existing_customer.country_code = country_code
|
||||
existing_customer.gender = gender
|
||||
existing_customer.birth_date = birth_date
|
||||
existing_customer.language = language
|
||||
existing_customer.address_catalog = False
|
||||
existing_customer.name_title = None
|
||||
db_customer = existing_customer
|
||||
await db.flush()
|
||||
else:
|
||||
# Create new customer
|
||||
_LOGGER.info("Creating new customer with contact_id: %s", contact_id)
|
||||
db_customer = DBCustomer(
|
||||
given_name=first_name,
|
||||
surname=last_name,
|
||||
contact_id=contact_id,
|
||||
name_prefix=name_prefix,
|
||||
email_address=email,
|
||||
phone=phone_number,
|
||||
email_newsletter=email_newsletter,
|
||||
address_line=address_line,
|
||||
city_name=city_name,
|
||||
postal_code=postal_code,
|
||||
country_code=country_code,
|
||||
gender=gender,
|
||||
birth_date=birth_date,
|
||||
language=language,
|
||||
address_catalog=False,
|
||||
name_title=None,
|
||||
)
|
||||
db.add(db_customer)
|
||||
await db.flush() # This assigns db_customer.id without committing
|
||||
# This automatically creates/updates both Customer and HashedCustomer
|
||||
db_customer = await customer_service.get_or_create_customer(customer_data)
|
||||
|
||||
# Determine hotel_code and hotel_name
|
||||
# Priority: 1) Form field, 2) Configuration default, 3) Hardcoded fallback
|
||||
|
||||
Reference in New Issue
Block a user