Added account_ids to the config
This commit is contained in:
@@ -38,7 +38,7 @@ from .alpinebits_server import (
|
||||
)
|
||||
from .auth import generate_unique_id, validate_api_key
|
||||
from .config_loader import load_config
|
||||
from .const import HttpStatusCode
|
||||
from .const import CONF_GOOGLE_ACCOUNT, CONF_HOTEL_ID, CONF_META_ACCOUNT, HttpStatusCode
|
||||
from .conversion_service import ConversionService
|
||||
from .customer_service import CustomerService
|
||||
from .db import Base, get_database_url
|
||||
@@ -71,85 +71,47 @@ security_bearer = HTTPBearer()
|
||||
# Constants for token sanitization
|
||||
TOKEN_LOG_LENGTH = 10
|
||||
|
||||
# Country name to ISO 3166-1 alpha-2 code mapping
|
||||
COUNTRY_NAME_TO_CODE = {
|
||||
# English names
|
||||
"germany": "DE",
|
||||
"italy": "IT",
|
||||
"austria": "AT",
|
||||
"switzerland": "CH",
|
||||
"france": "FR",
|
||||
"netherlands": "NL",
|
||||
"belgium": "BE",
|
||||
"spain": "ES",
|
||||
"portugal": "PT",
|
||||
"united kingdom": "GB",
|
||||
"uk": "GB",
|
||||
"czech republic": "CZ",
|
||||
"poland": "PL",
|
||||
"hungary": "HU",
|
||||
"croatia": "HR",
|
||||
"slovenia": "SI",
|
||||
# German names
|
||||
"deutschland": "DE",
|
||||
"italien": "IT",
|
||||
"österreich": "AT",
|
||||
"schweiz": "CH",
|
||||
"frankreich": "FR",
|
||||
"niederlande": "NL",
|
||||
"belgien": "BE",
|
||||
"spanien": "ES",
|
||||
"vereinigtes königreich": "GB",
|
||||
"tschechien": "CZ",
|
||||
"polen": "PL",
|
||||
"ungarn": "HU",
|
||||
"kroatien": "HR",
|
||||
"slowenien": "SI",
|
||||
# Italian names
|
||||
"germania": "DE",
|
||||
"italia": "IT",
|
||||
"svizzera": "CH",
|
||||
"francia": "FR",
|
||||
"paesi bassi": "NL",
|
||||
"belgio": "BE",
|
||||
"spagna": "ES",
|
||||
"portogallo": "PT",
|
||||
"regno unito": "GB",
|
||||
"repubblica ceca": "CZ",
|
||||
"polonia": "PL",
|
||||
"ungheria": "HU",
|
||||
"croazia": "HR",
|
||||
}
|
||||
|
||||
|
||||
def normalize_country_input(country_input: str | None) -> str | None:
|
||||
"""Normalize country input to ISO 3166-1 alpha-2 code.
|
||||
|
||||
Handles:
|
||||
- Country names in English, German, and Italian
|
||||
- Already valid 2-letter codes (case-insensitive)
|
||||
- None/empty values
|
||||
def get_advertising_account_ids(
|
||||
config: dict[str, Any],
|
||||
hotel_code: str,
|
||||
fbclid: str | None,
|
||||
gclid: str | None
|
||||
) -> tuple[str | None, str | None]:
|
||||
"""Get advertising account IDs based on hotel config and click IDs.
|
||||
|
||||
Args:
|
||||
country_input: Country name or code (case-insensitive)
|
||||
config: Application configuration dict
|
||||
hotel_code: Hotel identifier to look up in config
|
||||
fbclid: Facebook click ID (if present, meta_account_id will be returned)
|
||||
gclid: Google click ID (if present, google_account_id will be returned)
|
||||
|
||||
Returns:
|
||||
2-letter ISO country code (uppercase) or None if input is None/empty
|
||||
Tuple of (meta_account_id, google_account_id) based on conditional logic:
|
||||
- meta_account_id is set only if fbclid is present AND hotel has
|
||||
meta_account configured
|
||||
- google_account_id is set only if gclid is present AND hotel has
|
||||
google_account configured
|
||||
|
||||
"""
|
||||
if not country_input:
|
||||
return None
|
||||
meta_account_id = None
|
||||
google_account_id = None
|
||||
|
||||
country_input = country_input.strip()
|
||||
# Look up hotel in config
|
||||
alpine_bits_auth = config.get("alpine_bits_auth", [])
|
||||
for hotel in alpine_bits_auth:
|
||||
if hotel.get(CONF_HOTEL_ID) == hotel_code:
|
||||
# Conditionally set meta_account_id if fbclid is present
|
||||
if fbclid:
|
||||
meta_account_id = hotel.get(CONF_META_ACCOUNT)
|
||||
|
||||
# If already 2 letters, assume it's a country code (ISO 3166-1 alpha-2)
|
||||
iso_country_code_length = 2
|
||||
if len(country_input) == iso_country_code_length and country_input.isalpha():
|
||||
return country_input.upper()
|
||||
# Conditionally set google_account_id if gclid is present
|
||||
if gclid:
|
||||
google_account_id = hotel.get(CONF_GOOGLE_ACCOUNT)
|
||||
|
||||
# Try to match as country name (case-insensitive)
|
||||
country_lower = country_input.lower()
|
||||
return COUNTRY_NAME_TO_CODE.get(country_lower, country_input)
|
||||
break
|
||||
|
||||
return meta_account_id, google_account_id
|
||||
|
||||
|
||||
# Pydantic models for language detection
|
||||
@@ -369,7 +331,7 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
# Run migrations after tables exist (only primary worker for race conditions)
|
||||
if is_primary:
|
||||
await run_all_migrations(engine)
|
||||
await run_all_migrations(engine, config)
|
||||
else:
|
||||
_LOGGER.info("Skipping migrations (non-primary worker)")
|
||||
|
||||
@@ -690,6 +652,15 @@ async def process_wix_form_submission(request: Request, data: dict[str, Any], db
|
||||
_LOGGER.exception("Error parsing submissionTime: %s", e)
|
||||
submissionTime = None
|
||||
|
||||
# Extract fbclid and gclid for conditional account ID lookup
|
||||
fbclid = data.get("field:fbclid")
|
||||
gclid = data.get("field:gclid")
|
||||
|
||||
# Get advertising account IDs conditionally based on fbclid/gclid presence
|
||||
meta_account_id, google_account_id = get_advertising_account_ids(
|
||||
request.app.state.config, hotel_code, fbclid, gclid
|
||||
)
|
||||
|
||||
reservation = ReservationData(
|
||||
unique_id=unique_id,
|
||||
start_date=date.fromisoformat(start_date),
|
||||
@@ -707,8 +678,10 @@ async def process_wix_form_submission(request: Request, data: dict[str, Any], db
|
||||
utm_term=data.get("field:utm_term"),
|
||||
utm_content=data.get("field:utm_content"),
|
||||
user_comment=data.get("field:long_answer_3524", ""),
|
||||
fbclid=data.get("field:fbclid"),
|
||||
gclid=data.get("field:gclid"),
|
||||
fbclid=fbclid,
|
||||
gclid=gclid,
|
||||
meta_account_id=meta_account_id,
|
||||
google_account_id=google_account_id,
|
||||
)
|
||||
|
||||
if reservation.md5_unique_id is None:
|
||||
@@ -818,9 +791,6 @@ async def process_generic_webhook_submission(
|
||||
city = form_data.get("stadt", "")
|
||||
country = form_data.get("land", "")
|
||||
|
||||
# Normalize country input (convert names to codes, handle case)
|
||||
country = normalize_country_input(country)
|
||||
|
||||
# Parse dates - handle DD.MM.YYYY format
|
||||
start_date_str = form_data.get("anreise")
|
||||
end_date_str = form_data.get("abreise")
|
||||
@@ -912,6 +882,11 @@ async def process_generic_webhook_submission(
|
||||
# Create/update customer
|
||||
db_customer = await customer_service.get_or_create_customer(customer_data)
|
||||
|
||||
# Get advertising account IDs conditionally based on fbclid/gclid presence
|
||||
meta_account_id, google_account_id = get_advertising_account_ids(
|
||||
request.app.state.config, hotel_code, fbclid, gclid
|
||||
)
|
||||
|
||||
# Create reservation
|
||||
reservation_kwargs = {
|
||||
"unique_id": unique_id,
|
||||
@@ -931,6 +906,8 @@ async def process_generic_webhook_submission(
|
||||
"user_comment": user_comment,
|
||||
"fbclid": fbclid,
|
||||
"gclid": gclid,
|
||||
"meta_account_id": meta_account_id,
|
||||
"google_account_id": google_account_id,
|
||||
}
|
||||
|
||||
# Only include created_at if we have a valid submission_time
|
||||
|
||||
Reference in New Issue
Block a user