"""Logic for Track Spatial.""" from backend.constants import error from backend.models import track_additional_isrc from backend.models import track_spatial from backend.models.track_persister import TrackPersister from backend.utils import api as api_utils from backend.utils import logic as logic_util @logic_util.verify_product_ownership def get_spatial_isrc_map_by_product_id(product_id, account_type, account_id): """Get a mapping of track_id to spatial ISRC for all tracks in a product. Args: product_id (int): product primary key Returns: Response: A Response obj with {track_id: isrc} dict """ return TrackPersister.get_spatial_isrc_map_by_product_id(product_id) @logic_util.verify_product_ownership def get_track_spatial(tuid, account_type, account_id): """Get track spatial data. Args: tuid (int): track primary key Returns: Response: A Response obj with track spatial JSON data """ return track_spatial.get_track_spatial(tuid) @logic_util.verify_product_ownership def create_track_spatial(tuid, data, account_type, account_id): """Create a new track spatial. Args: tuid (int): track primary key data (dict): track spatial data Returns: Response: A Response obj with track spatial JSON data, or 409 if a spatial record already exists for this track. """ data['track_id'] = tuid if 'isrc' not in data or not data['isrc'].strip(): # Reuse the track's permanent atmos ISRC if it already has one, so a # re-add doesn't claim (and waste) a fresh ISRC. existing_response = track_additional_isrc.get_atmos_isrc(tuid) if not existing_response: return existing_response data['isrc'] = existing_response.message if not data.get('isrc'): isrc_response = TrackPersister.claim_new_isrc() if not isrc_response: return isrc_response data['isrc'] = isrc_response.message['isrc'] return track_spatial.create_track_spatial(data) @logic_util.verify_product_ownership def update_track_spatial(tuid, data, account_type, account_id): """Upsert a track spatial. Inserts when no row exists for the track, updates otherwise. When isrc is not supplied, the track's existing atmos ISRC is reused, else a new one is claimed (matching create_track_spatial). Args: tuid (int): track primary key data (dict): track spatial data Returns: Response: A Response obj with track spatial JSON data """ data['track_id'] = tuid if 'isrc' not in data or not data['isrc'].strip(): # Reuse the track's permanent atmos ISRC if it already has one, so a # re-add doesn't claim (and waste) a fresh ISRC. existing_response = track_additional_isrc.get_atmos_isrc(tuid) if not existing_response: return existing_response data['isrc'] = existing_response.message if not data.get('isrc'): isrc_response = TrackPersister.claim_new_isrc() if not isrc_response: return isrc_response data['isrc'] = isrc_response.message['isrc'] return track_spatial.update_track_spatial(data) @logic_util.verify_product_ownership def delete_track_spatial(tuid, account_type, account_id): """Create a new track spatial. Args: tuid (int): track primary key Returns: Response: A Response obj with track spatial JSON data """ track_response = TrackPersister.get_by_tuid(tuid) if not track_response: return api_utils.create_validation_error_response( error.ERROR_MESSAGE_TRACK_NOT_FOUND) return track_spatial.delete_track_spatial(tuid)