from datetime import timedelta import pytest from fansifter_common.auth.account import Account from fansifter_common.utils import timezone from dmp.crm_campaigns.handlers import GetCrmCampaignsHandler, GetCrmCampaignsRequest from dmp.crm_campaigns.models import CrmCampaignDbt from dmp.rosters.models import ArtistRosterLocalRep, ArtistRosterMainRep from tests.unit.faker import FakerTyped from tests.unit.types import CreateReportingModel class TestGetCrmCampaignIdsHandler: @pytest.mark.db def test_get_crm_campaign_id_page_empty( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, ) -> None: vendor_id = fake.integer() subaccount_id = 0 create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id, subaccount_id=subaccount_id ) result = handler.handle( GetCrmCampaignsRequest( identity_id=fake.uuid4_string(), vendor_id=vendor_id, subaccount_id=subaccount_id, limit=50, search=None, ) ) assert result == [] @pytest.mark.db def test_get_crm_campaign_id_page( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, fake: FakerTyped, account: Account, ) -> None: vendor_id = fake.integer() subaccount_id = 0 crm_campaign_id = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id, subaccount_id=subaccount_id ) result = handler.handle( GetCrmCampaignsRequest( identity_id=fake.uuid4_string(), vendor_id=account.vendor_id, subaccount_id=subaccount_id, limit=50, search=None, ) ) assert len(result) == 1 crm_campaign_id_result = result[0] assert crm_campaign_id_result.id == crm_campaign_id.id assert crm_campaign_id_result.name == crm_campaign_id.name assert crm_campaign_id_result.url == crm_campaign_id.url assert crm_campaign_id_result.vendor_id == crm_campaign_id.vendor_id assert crm_campaign_id_result.subaccount_id == crm_campaign_id.subaccount_id assert ( crm_campaign_id_result.global_participant_id == crm_campaign_id.global_participant_id ) assert crm_campaign_id_result.custom_list_id == crm_campaign_id.custom_list_id assert crm_campaign_id_result.created_at == crm_campaign_id.created_at @pytest.mark.db def test_get_crm_campaign_id_ids_filter( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: crm_campaign_id_1 = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_id=None, ) crm_campaign_id_2 = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_id=None, created_at=crm_campaign_id_1.created_at + timedelta(seconds=1), ) create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_id=None, ) result = handler.handle( GetCrmCampaignsRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, campaign_ids=[ crm_campaign_id_1.id, crm_campaign_id_2.id, ], limit=50, search=None, ) ) assert len(result) == 2 assert result[0].id == crm_campaign_id_2.id assert result[1].id == crm_campaign_id_1.id @pytest.mark.db def test_get_crm_campaign_id_global_participant_ids_filter( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: crm_campaign_id = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_id=None, ) assert crm_campaign_id.global_participant_id result = handler.handle( GetCrmCampaignsRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_ids=[crm_campaign_id.global_participant_id], limit=50, search=None, ) ) assert len(result) == 1 crm_campaign_id_result = result[0] assert crm_campaign_id_result.id == crm_campaign_id.id assert crm_campaign_id_result.name == crm_campaign_id.name assert crm_campaign_id_result.url == crm_campaign_id.url assert crm_campaign_id_result.vendor_id == crm_campaign_id.vendor_id assert crm_campaign_id_result.subaccount_id == crm_campaign_id.subaccount_id assert ( crm_campaign_id_result.global_participant_id == crm_campaign_id.global_participant_id ) assert crm_campaign_id_result.custom_list_id == crm_campaign_id.custom_list_id assert crm_campaign_id_result.created_at == crm_campaign_id.created_at @pytest.mark.db def test_get_crm_campaign_id_custom_list_ids_filter( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: crm_campaign_id = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=None, ) assert crm_campaign_id.custom_list_id result = handler.handle( GetCrmCampaignsRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_ids=[crm_campaign_id.custom_list_id], limit=50, search=None, ) ) assert len(result) == 1 crm_campaign_id_result = result[0] assert crm_campaign_id_result.id == crm_campaign_id.id assert crm_campaign_id_result.name == crm_campaign_id.name assert crm_campaign_id_result.url == crm_campaign_id.url assert crm_campaign_id_result.vendor_id == crm_campaign_id.vendor_id assert crm_campaign_id_result.subaccount_id == crm_campaign_id.subaccount_id assert ( crm_campaign_id_result.global_participant_id == crm_campaign_id.global_participant_id ) assert crm_campaign_id_result.custom_list_id == crm_campaign_id.custom_list_id assert crm_campaign_id_result.created_at == crm_campaign_id.created_at @pytest.mark.db def test_get_crm_campaign_id_account_filter( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, ) -> None: crm_campaign_id = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, ) result = handler.handle( GetCrmCampaignsRequest( identity_id=identity_id, vendor_id=crm_campaign_id.vendor_id, subaccount_id=crm_campaign_id.subaccount_id, limit=50, search=None, ) ) assert len(result) == 1 crm_campaign_id_result = result[0] assert crm_campaign_id_result.id == crm_campaign_id.id @pytest.mark.db def test_get_crm_campaign_id_for_gp_from_non_sme_label( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, fake: FakerTyped, ) -> None: """ Verifies that the handler correctly returns single campaign only for vendor_id, global_participant_id specified in the request and ignores other global_participant_id related records. """ vendor_id_2 = fake.integer() vendor_id_3 = fake.integer() vendor_id_4 = fake.integer() global_participant_id = fake.uuid4_string() crm_campaign_id = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_2, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_3, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_4, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( ArtistRosterMainRep, global_participant_id=global_participant_id, vendor_id=vendor_id_2, subaccount_id=account.subaccount_id, ) create_reporting_model( ArtistRosterLocalRep, global_participant_id=global_participant_id, vendor_id=vendor_id_3, subaccount_id=account.subaccount_id, country_code="US", ) assert crm_campaign_id.global_participant_id result = handler.handle( GetCrmCampaignsRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_ids=[crm_campaign_id.global_participant_id], limit=50, search=None, ) ) assert len(result) == 1 crm_campaign_id_result = result[0] assert crm_campaign_id_result.vendor_id == account.vendor_id assert crm_campaign_id_result.id == crm_campaign_id.id @pytest.mark.db def test_get_crm_campaign_ids_for_list_from_non_sme_label( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, fake: FakerTyped, ) -> None: """ Verifies that the handler correctly returns multiple campaigns only for vendor_id, custom_list_id specified in the request and ignores other custom_list_id related records. """ vendor_id_2 = fake.integer() vendor_id_3 = fake.integer() vendor_id_4 = fake.integer() dt_now = timezone.now() crm_campaign_id_1 = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=None, created_at=dt_now + timedelta(seconds=1), ) crm_campaign_id_2 = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=None, custom_list_id=crm_campaign_id_1.custom_list_id, created_at=dt_now + timedelta(seconds=5), ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_2, subaccount_id=account.subaccount_id, global_participant_id=None, custom_list_id=crm_campaign_id_1.custom_list_id, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_3, subaccount_id=account.subaccount_id, global_participant_id=None, custom_list_id=crm_campaign_id_1.custom_list_id, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_4, subaccount_id=account.subaccount_id, global_participant_id=None, custom_list_id=crm_campaign_id_1.custom_list_id, ) assert crm_campaign_id_1.custom_list_id result = handler.handle( GetCrmCampaignsRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, custom_list_ids=[crm_campaign_id_1.custom_list_id], limit=50, search=None, ) ) assert len(result) == 2 crm_campaign_id_result_newer = result[0] crm_campaign_id_result_older = result[1] assert crm_campaign_id_result_newer.vendor_id == account.vendor_id assert crm_campaign_id_result_older.vendor_id == account.vendor_id assert crm_campaign_id_result_newer.id == crm_campaign_id_2.id assert crm_campaign_id_result_older.id == crm_campaign_id_1.id @pytest.mark.db def test_get_crm_campaign_id_for_gp_from_sme_label( self, handler: GetCrmCampaignsHandler, create_reporting_model: CreateReportingModel, identity_id: str, account: Account, fake: FakerTyped, ) -> None: """ Verifies that the handler correctly returns campaigns for vendor_id, global_participant_id specified in the request + campaigns from other vendors where this vendor_id is part of main/local rep owner concept. """ vendor_id_1 = fake.integer() vendor_id_2 = fake.integer() vendor_id_3 = fake.integer() vendor_id_4 = fake.integer() global_participant_id = fake.uuid4_string() crm_campaign_id = create_reporting_model( CrmCampaignDbt, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_1, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_2, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_3, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( CrmCampaignDbt, vendor_id=vendor_id_4, subaccount_id=account.subaccount_id, global_participant_id=global_participant_id, custom_list_id=None, ) create_reporting_model( ArtistRosterMainRep, global_participant_id=global_participant_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, ) assert crm_campaign_id.global_participant_id result = handler.handle( GetCrmCampaignsRequest( identity_id=identity_id, vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, global_participant_ids=[crm_campaign_id.global_participant_id], limit=50, search=None, ) ) assert len(result) == 5