"""Create new contract terms for destination account in ows-royalties.""" from __future__ import annotations from typing import Any from src import config from src.ows_response import parse_json_response _PRODUCT = "product" _TRACK = "track" _SUPPORTED_TYPES = (_PRODUCT, _TRACK) def _build_conditions_payload(term: dict[str, Any]) -> list[dict[str, Any]]: payload = [] for cond in term.get("conditions") or []: payload.append( { "conditions": cond.get("conditions") or {}, "term_rate": str(cond.get("term_rate")), "priority": cond.get("priority"), } ) return payload def _post_transfer_create(destination_vendor_id: int, body: dict[str, Any]) -> dict[str, Any]: resp = config.ows_client.post( "ows-royalties", path=f"/account/{destination_vendor_id}/contract-terms/transfer-create", json=body, ) return parse_json_response("ows-royalties", resp) def add_to_destination_for_each_term( destination_vendor_id: int, staged_terms: list[dict[str, Any]], ) -> list[dict[str, Any]]: """For each product/track staged term create a new contract term. Each term is created with its OWN attachments (the UPCs/ISRCs configured on that staged term), not the job-wide attachment set. A product term carries UPCs, a track term carries ISRCs; both already live on the staged term's ``attachments`` field as written at staging time. """ results: list[dict[str, Any]] = [] for term in staged_terms: term_type = term.get("term_type") contract_id = term.get("contract_id") project_transfer_term_id = term.get("project_transfer_term_id") if term_type not in _SUPPORTED_TYPES: config.logger.info( "Skipping staged term %s (term_type=%s) — not attachment-based.", project_transfer_term_id, term_type, ) results.append( { "project_transfer_term_id": project_transfer_term_id, "contract_id": contract_id, "term_type": term_type, "skipped": "unsupported_term_type", } ) continue if term.get("destination_contract_term_id") is not None: config.logger.info( "Skipping staged term %s — destination_contract_term_id=%s already set (prior run).", project_transfer_term_id, term.get("destination_contract_term_id"), ) results.append( { "project_transfer_term_id": project_transfer_term_id, "contract_id": contract_id, "term_type": term_type, "skipped": "already_created", } ) continue attachments = term.get("attachments") or [] if not attachments: config.logger.info( "Skipping staged term %s (term_type=%s): term has no attachments.", project_transfer_term_id, term_type, ) results.append( { "project_transfer_term_id": project_transfer_term_id, "contract_id": contract_id, "term_type": term_type, "skipped": "no_attachments_to_add", } ) continue body = { "project_transfer_term_id": project_transfer_term_id, "contract_id": contract_id, "term_type": term_type, "attachments": attachments, "conditions": _build_conditions_payload(term), "attachment_relations": term.get("attachment_relations"), "name": term.get("name"), } config.logger.info( "Creating transfer term for staged term %s (term_type=%s) on contract %s for account %d.", project_transfer_term_id, term_type, contract_id, destination_vendor_id, ) response_body = _post_transfer_create(destination_vendor_id=destination_vendor_id, body=body) results.append( { "project_transfer_term_id": project_transfer_term_id, "contract_id": contract_id, "term_type": term_type, "destination_contract_term_id": response_body.get("contract_term_id"), "response": response_body, } ) return results