"""Product model.""" from sqlalchemy import text from sqlalchemy.exc import NoResultFound from vectororder.connectors.mysql import art_db_connector from vectororder.constants import sql from vectororder.exceptions import ProductNotFoundError, ProductNotInContent from vectororder.models.schemas import ( DownloadStreamRights, Product, Store, Track, TrackOfferType, ) def get_product(upc: int, store: Store) -> Product: """Get product by UPC. Args: upc (int) store (Store) Returns: Product """ with art_db_connector.db_session() as session: product_result = session.execute( text(sql.AR_GET_PRODUCT), { "upc": upc, }, ) try: product = product_result.mappings().one() except NoResultFound as e: raise ProductNotFoundError(f"Product not found for UPC {upc}") from e if product["release_status"] != "in_content": raise ProductNotInContent(f"Product {upc} not in content") tracks_result = session.execute( text(sql.AR_GET_TRACKS), { "release_id": product["release_id"], }, ) tracks = tracks_result.mappings().all() return Product( product_id=product["release_id"], distribution_format_id=product["distribution_format_id"], not_for_distribution=product["not_for_distribution"], context_type=product["context_type"], tracks=[ Track( track_id=track["id"], track_type=track["track_type"], offer_type=TrackOfferType(track["offer_type"]), distribution_rights=_get_track_distribution_rights( store, TrackOfferType(track["offer_type"]) ), ) for track in tracks ], ) def _get_track_distribution_rights( store: Store, track_offer_type: TrackOfferType, ) -> set[DownloadStreamRights]: """Get track distribution rights.""" if ( store.ddex_commercial_model_to_use_type_map and not store.is_download_supported and not store.is_streaming_supported ): return { DownloadStreamRights.DOWNLOAD, DownloadStreamRights.STREAM, } track_distribution_rights = set() match track_offer_type: case TrackOfferType.ALL | TrackOfferType.TRACK_DOWNLOAD_STREAM: track_distribution_rights = { DownloadStreamRights.DOWNLOAD, DownloadStreamRights.STREAM, } case TrackOfferType.TRACK_DOWNLOAD_ONLY | TrackOfferType.ALBUM_TRACK_DOWNLOAD: if store.is_download_supported: track_distribution_rights = {DownloadStreamRights.DOWNLOAD} case TrackOfferType.STREAM_ONLY: if store.is_streaming_supported: track_distribution_rights = {DownloadStreamRights.STREAM} case TrackOfferType.ALBUM_DOWNLOAD_STREAM: if store.is_streaming_supported: track_distribution_rights.add(DownloadStreamRights.STREAM) return track_distribution_rights def get_release_distribution_rights( tracks: list[Track], store: Store, ) -> set[DownloadStreamRights]: """Get product distribution rights.""" product_distribution_rights = set() if ( store.ddex_commercial_model_to_use_type_map and not store.is_download_supported and not store.is_streaming_supported ): return { DownloadStreamRights.DOWNLOAD, DownloadStreamRights.STREAM, } for track in tracks: if DownloadStreamRights.DOWNLOAD in track.distribution_rights: product_distribution_rights.add(DownloadStreamRights.DOWNLOAD) if DownloadStreamRights.STREAM in track.distribution_rights: product_distribution_rights.add(DownloadStreamRights.STREAM) return product_distribution_rights