@@ -0,0 +1,3319 @@
+from django.core.management import BaseCommand%0A%0Afrom corehq.apps.analytics.utils import (%0A    get_blocked_hubspot_domains,%0A    get_first_conversion_status_for_emails,%0A    ALLOWED_CONVERSIONS,%0A)%0Afrom corehq.apps.users.models import WebUser, Invitation%0A%0A%0Aclass Command(BaseCommand):%0A    help = %22Audit a web user's HubSpot status in HubSpot and CommCare HQ%22%0A%0A    def add_arguments(self, parser):%0A        parser.add_argument('email')%0A%0A    def handle(self, email, **options):%0A        web_user = WebUser.get_by_username(email)%0A        if not web_user:%0A            self.stdout.write(self.style.ERROR(%0A                %22User not does not exist on CommCare HQ.%22%0A            ))%0A            return%0A%0A        blocked_domains = set(get_blocked_hubspot_domains())%0A%0A        user_domains = set(web_user.get_domains())%0A        blocked_memberships = user_domains.intersection(blocked_domains)%0A%0A        if not blocked_memberships:%0A            self.show_status_of_possibly_unblocked_user(email, blocked_domains)%0A        else:%0A            self.show_status_of_blocked_user(email, blocked_memberships)%0A%0A        self.display_first_conversion_status(email)%0A%0A    def show_status_of_possibly_unblocked_user(self, email, blocked_domains):%0A        self.stdout.write(%0A            f%22%5Cn%7Bemail%7D is not a member of any projects actively blocking %22%0A            f%22data from HubSpot.%22%0A        )%0A        blocked_domains_invited = Invitation.objects.filter(%0A            domain__in=list(blocked_domains),%0A            is_accepted=True,%0A            email=email,%0A        ).values_list('email', flat=True)%0A        if blocked_domains_invited:%0A            self.stdout.write(%0A                %22However, it looks like they have previously accepted %22%0A                %22invitations to the following project(s) blocking HubSpot data:%22%0A            )%0A            self.stdout.write(%22%5Cn%22.join(blocked_domains_invited))%0A        else:%0A            self.stdout.write(%0A                %22They also have never accepted an invitation from a project %22%0A                %22that is actively blocking HubSpot data.%22%0A            )%0A        self.stdout.write(%22%5Cn%22)%0A%0A    def show_status_of_blocked_user(self, email, blocked_memberships):%0A        self.stdout.write(%0A            f%22%5Cn%7Bemail%7D is a member of the following project(s) actively %22%0A            f%22blocking data from HubSpot:%22%0A        )%0A        self.stdout.write(%22%5Cn%22.join(blocked_memberships))%0A        self.stdout.write(%22%5Cn%22)%0A%0A    def display_first_conversion_status(self, email):%0A        hubspot_contact_info = get_first_conversion_status_for_emails(%5Bemail%5D)%0A        if hubspot_contact_info:%0A            first_conversion = hubspot_contact_info%5Bemail%5D%0A            self.stdout.write(%0A                f%22%5Cn%7Bemail%7D found in HubSpot with First Conversion %22%0A                f%22(clustered) as '%7Bfirst_conversion%7D'.%22%0A            )%0A            if first_conversion in ALLOWED_CONVERSIONS:%0A                self.stdout.write(%0A                    %22This is fully acceptable according to our policy.%22%0A                )%0A            else:%0A                self.stdout.write(%0A                    %22If the user is part of a project blocking HubSpot data, %22%0A                    %22then this is not allowed.%22%0A                )%0A        else:%0A            self.stdout.write(%0A                f%22%7Bemail%7D not found in HubSpot.%22%0A            )%0A        self.stdout.write(%22%5Cn%22)%0A

create a management command that allows us to audit a single user's status in hubspot analytics and cross reference this to their membership status with a domain blocking hubspot data