HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall/buyercall/blueprints/widgets/utils/interaction_manager.py
from buyercall.blueprints.channels.models import Channel
from buyercall.blueprints.partnership.models import PartnershipAccount, Partnership
from buyercall.blueprints.sources.models import Source
from buyercall.blueprints.workflows.models import Workflow
from buyercall.blueprints.workflows.utils.action_manager import ActionManager
from buyercall.blueprints.workflows.utils.workflow_identifier import WorkflowIdentifier


class InteractionManager:
    def run(self, interaction_type, contact, channel, is_existing_contact, data, partnership_account_id):
        # Get workflows by partnership account
        workflows = Workflow.get_by_partnership_account(partnership_account_id)

        partnership_account = PartnershipAccount.get_by_id(partnership_account_id)
        partnership = Partnership.get_by_id(partnership_account.partnership_id)
        # Get sources by partnership account
        sources = Source.get_all_by_partnership_account(partnership_account_id)
        sources = [str(s.sid) for s in sources]
        # Get channels by partnership account
        channels = Channel.get_all_by_partnership_account(partnership_account_id)
        channel_sids = [str(c.sid) for c in channels]
        channels_type_names = [str(c.channel_type.name) for c in channels]

        agents_assigned = []
        valid_workflows = []
        if interaction_type == 'email':
            # Get valid workflows
            valid_workflows = WorkflowIdentifier(workflows, sources, channel_sids, channels_type_names).identify(
                source=str(Source.get_sid_from_id(channel.source)), channel=str(channel.sid),
                is_existing_contact=is_existing_contact, channel_type=interaction_type)

            # Get actions
            actions = []
            for wf in valid_workflows:
                actions.extend(wf.get_actions())
                agents_assigned.extend(wf.get_agents_assigned_in_actions())
            agents_assigned = list(set(agents_assigned))
            resp = ActionManager().run(contact=contact, actions=actions, data=data)

        elif interaction_type == 'chat':
            # Get valid workflows
            valid_workflows = WorkflowIdentifier(workflows, sources, channel_sids, channels_type_names).identify(
                source=str(Source.get_sid_from_id(channel.source)), channel=str(channel.sid),
                is_existing_contact=is_existing_contact, channel_type=interaction_type)

            # Get actions
            actions = []
            for wf in valid_workflows:
                actions.extend(wf.get_actions())
                agents_assigned.extend(wf.get_agents_assigned_in_actions())
            agents_assigned = list(set(agents_assigned))
            resp = ActionManager().run(contact=contact, actions=actions, data=data)

        else:
            pass
            # TODO Other interaction types

        print('valid_workflows : ', valid_workflows)

        if interaction_type in ['email']:
            payload = data.get('message', '')
            # Send to node server
            from buyercall.blueprints.agents.webhooks.inbox_interaction import InboxConnector
            from flask import current_app
            endpoint = current_app.config.get('SOCKET_ENDPOINT')
            resp = InboxConnector(endpoint).send_interaction(contact_id=str(contact.sid),
                                                             source_id=str(Source.get_sid_from_id(channel.source)),
                                                             interaction_type=interaction_type.upper(),
                                                             agent_ids=[str(agent) for agent in agents_assigned],
                                                             channel_id=str(channel.sid),
                                                             partnership_id=str(partnership.sid),
                                                             partnership_account_id=str(partnership_account.sid),
                                                             from_lead=True, payload=payload)

        return agents_assigned