"""update-dim-tables Lambda handler.""" from typing import Any from src import config from src.client import get_transfer_job, get_transfer_job_products from src.dim_release_history import update_dim_release_history def _job_id_from_event(event: dict[str, Any]) -> int: raw_job_id = event["job_id"] if isinstance(raw_job_id, bool): raise ValueError("event['job_id'] must be an integer.") try: return int(raw_job_id) except (TypeError, ValueError) as exc: raise ValueError("event['job_id'] must be an integer.") from exc def handler(event: dict[str, Any], context: object) -> dict[str, Any]: del context job_id = _job_id_from_event(event) config.logger.info(f"Fetching details for transfer job {job_id}") job = get_transfer_job(job_id) products = get_transfer_job_products(job_id, job["destination_vendor_id"]) release_ids = [p["release_id"] for p in products] config.logger.info(f"Transfer job {job_id} has {len(release_ids)} products: {release_ids}") update_dim_release_history( release_ids=release_ids, destination_vendor_id=job["destination_vendor_id"], destination_subaccount_id=job["destination_subaccount_id"], revenue_cutoff_date=job["revenue_cutoff_date"], ) return {**event, "update_dim_tables_result": {"job_id": job_id, "release_ids": release_ids}}