From bfb1c7335356588c5d554b4a7ec14f24c6a052e9 Mon Sep 17 00:00:00 2001 From: kaioken Date: Fri, 26 Jul 2024 15:03:47 -0400 Subject: [PATCH 1/9] feat: download all zoho leads --- .../Actions/DownloadAllZohoLeadAction.php | 48 ++++++++++++++++ .../Zoho/Actions/SyncZohoLeadAction.php | 5 +- .../Actions/CreateLeadReceiverAction.php | 37 ++++++++++++ .../Leads/DataTransferObject/LeadReceiver.php | 29 ++++++++++ src/Domains/Guild/Support/Setup.php | 21 +++++++ .../Connectors/Integration/Zoho/LeadTest.php | 56 +++++++++++++++++++ 6 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php create mode 100644 src/Domains/Guild/Leads/Actions/CreateLeadReceiverAction.php create mode 100644 src/Domains/Guild/Leads/DataTransferObject/LeadReceiver.php create mode 100644 tests/Connectors/Integration/Zoho/LeadTest.php diff --git a/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php b/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php new file mode 100644 index 000000000..0f6bb4c06 --- /dev/null +++ b/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php @@ -0,0 +1,48 @@ +app, $this->company); + + $localLeadsIds = []; + + for ($page = 1; $page <= $totalPages; $page++) { + $leads = $zohoClient->leads->getList(['page' => $page, 'per_page' => $leadsPerPage]); + foreach ($leads as $lead) { + $syncZohoLead = new SyncZohoLeadAction( + $this->app, + $this->company, + $this->receiver, + $lead->getId() + ); + + $localLead = $syncZohoLead->execute($lead); + + if (! $localLead) { + continue; + } + $localLeadsIds[] = $localLead->getId(); + } + } + + return $localLeadsIds; + } +} diff --git a/src/Domains/Connectors/Zoho/Actions/SyncZohoLeadAction.php b/src/Domains/Connectors/Zoho/Actions/SyncZohoLeadAction.php index 49e571f82..535d4c226 100644 --- a/src/Domains/Connectors/Zoho/Actions/SyncZohoLeadAction.php +++ b/src/Domains/Connectors/Zoho/Actions/SyncZohoLeadAction.php @@ -23,6 +23,7 @@ use Kanvas\Guild\Pipelines\Models\Pipeline; use Kanvas\Users\Models\UsersAssociatedApps; use Spatie\LaravelData\DataCollection; +use Webleit\ZohoCrmApi\Models\Record; class SyncZohoLeadAction { @@ -34,12 +35,12 @@ public function __construct( ) { } - public function execute(): ?Lead + public function execute(?Record $zohoLead = null): ?Lead { $zohoService = new ZohoService($this->app, $this->company); try { - $zohoLead = $zohoService->getLeadById($this->zohoLeadId); + $zohoLead = $zohoLead === null ? $zohoService->getLeadById($this->zohoLeadId) : $zohoLead; } catch (Exception $e) { Log::error('Error getting Zoho Lead', ['error' => $e->getMessage()]); diff --git a/src/Domains/Guild/Leads/Actions/CreateLeadReceiverAction.php b/src/Domains/Guild/Leads/Actions/CreateLeadReceiverAction.php new file mode 100644 index 000000000..064302fb0 --- /dev/null +++ b/src/Domains/Guild/Leads/Actions/CreateLeadReceiverAction.php @@ -0,0 +1,37 @@ + $this->leadReceiver->branch->getId(), + 'companies_id' => $this->leadReceiver->branch->company->getId(), + 'apps_id' => $this->leadReceiver->app->getId(), + 'name' => $this->leadReceiver->name, + ], [ + 'users_id' => $this->leadReceiver->user->getId(), + 'agents_id' => $this->leadReceiver->agent->getId(), + 'is_default' => (int) $this->leadReceiver->isDefault, + 'rotations_id' => $this->leadReceiver->rotation ? $this->leadReceiver->rotation->getId() : 0, + 'source_name' => $this->leadReceiver->source, + ]); + } +} diff --git a/src/Domains/Guild/Leads/DataTransferObject/LeadReceiver.php b/src/Domains/Guild/Leads/DataTransferObject/LeadReceiver.php new file mode 100644 index 000000000..fcb343789 --- /dev/null +++ b/src/Domains/Guild/Leads/DataTransferObject/LeadReceiver.php @@ -0,0 +1,29 @@ + 'Default Receiver', ]); + foreach ($this->leadStatus as $key => $value) { + LeadStatus::firstOrCreate([ + 'name' => $value, + ]); + } + return LeadType::fromCompany($this->company)->count() == count($this->leadTypes) && LeadReceiver::fromCompany($this->company)->count() > 0 && LeadSource::fromApp($this->app)->fromCompany($this->company)->count() == count($this->leadSources) && Pipeline::fromCompany($this->company)->count() > 0 && + LeadStatus::count() > 0 && PipelineStage::where('pipelines_id', $defaultPipeline->getId())->count() == count($this->defaultStages); } } diff --git a/tests/Connectors/Integration/Zoho/LeadTest.php b/tests/Connectors/Integration/Zoho/LeadTest.php new file mode 100644 index 000000000..dbdfb98b0 --- /dev/null +++ b/tests/Connectors/Integration/Zoho/LeadTest.php @@ -0,0 +1,56 @@ +user(); + + $app->set(FlagEnum::APP_GLOBAL_ZOHO->value, 1); + $app->set(CustomFieldEnum::CLIENT_ID->value, getenv('TEST_ZOHO_CLIENT_ID')); + $app->set(CustomFieldEnum::CLIENT_SECRET->value, getenv('TEST_ZOHO_CLIENT_SECRET')); + $app->set(CustomFieldEnum::REFRESH_TOKEN->value, getenv('TEST_ZOHO_CLIENT_REFRESH_TOKEN')); + + $receiver = (new CreateLeadReceiverAction( + new LeadReceiver( + app: $app, + branch: $company->branches()->first(), + user: $user, + agent: $user, + name: 'test', + source: 'test', + isDefault: true + ) + ))->execute(); + + $companySetup = new Setup($app, $user, $company); + $companySetup->run(); + + $downloadAllLeads = new DownloadAllZohoLeadAction( + $app, + $company, + $receiver + ); + + $leads = $downloadAllLeads->execute(totalPages: 1, leadsPerPage: 1); + + $this->assertIsArray($leads); + $this->assertCount(1, $leads); + } +} From d47a39621a8336ab30fdaad6883457e478a45ccd Mon Sep 17 00:00:00 2001 From: kaioken Date: Fri, 26 Jul 2024 15:27:05 -0400 Subject: [PATCH 2/9] refact: use yield --- .../Zoho/Actions/DownloadAllZohoLeadAction.php | 16 +++++++++++----- tests/Connectors/Integration/Zoho/LeadTest.php | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php b/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php index 0f6bb4c06..5ed7eab31 100644 --- a/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php +++ b/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php @@ -6,11 +6,14 @@ use Baka\Contracts\AppInterface; use Baka\Contracts\CompanyInterface; +use Generator; use Kanvas\Connectors\Zoho\Client; use Kanvas\Guild\Leads\Models\LeadReceiver; class DownloadAllZohoLeadAction { + protected int $totalLeadsProcessed = 0; + public function __construct( protected AppInterface $app, protected CompanyInterface $company, @@ -18,12 +21,10 @@ public function __construct( ) { } - public function execute($totalPages = 50, $leadsPerPage = 200): array + public function execute($totalPages = 50, $leadsPerPage = 200): Generator { $zohoClient = Client::getInstance($this->app, $this->company); - $localLeadsIds = []; - for ($page = 1; $page <= $totalPages; $page++) { $leads = $zohoClient->leads->getList(['page' => $page, 'per_page' => $leadsPerPage]); foreach ($leads as $lead) { @@ -39,10 +40,15 @@ public function execute($totalPages = 50, $leadsPerPage = 200): array if (! $localLead) { continue; } - $localLeadsIds[] = $localLead->getId(); + + $this->totalLeadsProcessed++; + yield $localLead->getId(); } } + } - return $localLeadsIds; + public function getTotalLeadsProcessed(): int + { + return $this->totalLeadsProcessed; } } diff --git a/tests/Connectors/Integration/Zoho/LeadTest.php b/tests/Connectors/Integration/Zoho/LeadTest.php index dbdfb98b0..7f9ed0a23 100644 --- a/tests/Connectors/Integration/Zoho/LeadTest.php +++ b/tests/Connectors/Integration/Zoho/LeadTest.php @@ -50,7 +50,7 @@ public function testDownloadAllLeads(): void $leads = $downloadAllLeads->execute(totalPages: 1, leadsPerPage: 1); - $this->assertIsArray($leads); - $this->assertCount(1, $leads); + $this->assertIsArray(iterator_to_array($leads)); + $this->assertEquals(1, $downloadAllLeads->getTotalLeadsProcessed()); } } From 3fd8127c781aca14011507f51c178e97eb784b0a Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 27 Jul 2024 08:08:37 -0400 Subject: [PATCH 3/9] refact: move lead creation to action --- .../Zoho/Actions/SyncLeadToZohoAction.php | 186 ++++++++++++++++++ .../Zoho/Workflows/ZohoLeadActivity.php | 174 +--------------- 2 files changed, 193 insertions(+), 167 deletions(-) create mode 100644 src/Domains/Connectors/Zoho/Actions/SyncLeadToZohoAction.php diff --git a/src/Domains/Connectors/Zoho/Actions/SyncLeadToZohoAction.php b/src/Domains/Connectors/Zoho/Actions/SyncLeadToZohoAction.php new file mode 100644 index 000000000..1f4c916f0 --- /dev/null +++ b/src/Domains/Connectors/Zoho/Actions/SyncLeadToZohoAction.php @@ -0,0 +1,186 @@ +lead); + $zohoData = $zohoLead->toArray(); + $lead = $this->lead; + $company = $lead->company; + $usesAgentsModule = $company->get(CustomFieldEnum::ZOHO_HAS_AGENTS_MODULE->value); + + $zohoCrm = Client::getInstance($this->app, $company); + + if (! $zohoLeadId = $lead->get(CustomFieldEnum::ZOHO_LEAD_ID->value)) { + if ($usesAgentsModule) { + $this->assignAgent($this->app, $zohoLead, $lead, $company, $zohoData); + } + + $zohoLead = $zohoCrm->leads->create($zohoData); + $zohoLeadId = $zohoLead->getId(); + + $zohoData['Lead_Status'] = 'New Lead'; + + $lead->set( + CustomFieldEnum::ZOHO_LEAD_ID->value, + $zohoLeadId + ); + } else { + $zohoLeadInfo = $zohoCrm->leads->get((string) $zohoLeadId)->getData(); + if (! empty($zohoLeadInfo)) { + $zohoLead = $zohoCrm->leads->update( + (string) $zohoLeadId, + $zohoData + ); + } else { + $lead->close(); + } + } + + $this->uploadAttachments($zohoCrm->leads, $lead); + + return $zohoLead; + } + + protected function assignAgent( + AppInterface $app, + ZohoLead $zohoLead, + Lead $lead, + Companies $company, + array &$zohoData + ): void { + $memberNumber = (string) $zohoLead->getMemberNumber(); + + if (empty($memberNumber) && $lead->user()->exists()) { + $memberNumber = (string) $lead->user()->firstOrFail()->get('member_number_' . $company->getId()); + } + + if (! empty($memberNumber)) { + $zohoMemberField = $company->get(CustomFieldEnum::ZOHO_MEMBER_FIELD->value) ?? 'Member_ID'; + $zohoData[$zohoMemberField] = $memberNumber; + } + + $zohoService = new ZohoService($app, $company); + + try { + $agent = $zohoService->getAgentByMemberNumber($memberNumber); + } catch (Throwable $e) { + $agent = null; + } + + try { + $agentInfo = Agent::getByMemberNumber($memberNumber, $company); + } catch (Throwable $e) { + $agentInfo = null; + } + + $defaultLeadSource = $company->get(CustomFieldEnum::ZOHO_DEFAULT_LEAD_SOURCE->value); + if (! empty($defaultLeadSource)) { + $zohoData['Lead_Source'] = $defaultLeadSource; //$lead->receiver ? $lead->receiver->name : $defaultLeadSource; + } + + if (is_object($agent)) { + try { + ///lead owner should match lead routing + $leadRoutingEmailCleanUp = preg_replace('/[^a-zA-Z0-9@._-]/', '', $agent->Lead_Routing); + $zohoData['Owner'] = $zohoService->getAgentByEmail($leadRoutingEmailCleanUp)->Owner['id']; + } catch (Throwable $e) { + //send fail notification and assign to default lead routing email + $zohoData['Owner'] = (int) ($app->get(CustomFieldEnum::DEFAULT_OWNER->value) ?? $agent->Owner['id']); + } + + if ($agent->Sponsor) { + $zohoData['Sponsor'] = (string) $agent->Sponsor; + } + + if ($agentInfo) { + $lead->users_id = $agentInfo->users_id; + $lead->saveOrFail(); + } + + if ($agentInfo && $agentInfo->get('over_write_owner')) { + $zohoData['Owner'] = (int) $agentInfo->get('over_write_owner'); + } + if (empty($defaultLeadSource)) { + $zohoData['Lead_Source'] = $agent->name ?? $agent->Name; + } + } elseif ($agentInfo instanceof Agent) { + $zohoData['Owner'] = (int) $agentInfo->owner_linked_source_id; + if (empty($defaultLeadSource)) { + $zohoData['Lead_Source'] = $agentInfo->name; + } + + if ($agentInfo->user && ! empty($agentInfo->user->get('sponsor'))) { + $zohoData['Sponsor'] = (string) $agentInfo->user->get('sponsor'); + } + } + + if ($company->get(CustomFieldEnum::ZOHO_USE_AGENT_NAME->value) && ! empty($agentInfo->name)) { + $zohoData['Agent_Name'] = $agentInfo->name; + } + + //if value is 0 or empty, remove it + if (empty($zohoData['Owner'])) { + unset($zohoData['Owner']); + } + } + + protected function uploadAttachments(ZohoLeadModule $zohoLead, Lead $lead): void + { + $lead->load('files'); + if (! $lead->files()->count()) { + return; + } + + $syncFiles = $lead->get(CustomFieldEnum::ZOHO_LEAD_SYNC_FILES->value) ?? []; + + foreach ($lead->files()->get() as $file) { + if (isset($syncFiles[$file->id])) { + continue; + } + + try { + $fileContent = file_get_contents($file->url); + + $zohoLead->uploadAttachment( + (string) $lead->get(CustomFieldEnum::ZOHO_LEAD_ID->value), + $file->name, + $fileContent + ); + + $syncFiles[$file->id] = $file->id; + } catch (Throwable $e) { + //do nothing + } + } + + $lead->set( + CustomFieldEnum::ZOHO_LEAD_SYNC_FILES->value, + $syncFiles + ); + } +} diff --git a/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php b/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php index 5893a99d2..9b9763768 100644 --- a/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php +++ b/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php @@ -8,15 +8,10 @@ use Baka\Traits\KanvasJobsTrait; use Illuminate\Database\Eloquent\Model; use Kanvas\Companies\Models\Companies; -use Kanvas\Connectors\Zoho\Client; +use Kanvas\Connectors\Zoho\Actions\SyncLeadToZohoAction; use Kanvas\Connectors\Zoho\DataTransferObject\ZohoLead; -use Kanvas\Connectors\Zoho\Enums\CustomFieldEnum; -use Kanvas\Connectors\Zoho\ZohoService; -use Kanvas\Guild\Agents\Models\Agent; use Kanvas\Guild\Leads\Models\Lead; use Kanvas\Workflow\Contracts\WorkflowActivityInterface; -use Throwable; -use Webleit\ZohoCrmApi\Modules\Leads as ZohoLeadModule; use Workflow\Activity; /** @@ -25,7 +20,7 @@ class ZohoLeadActivity extends Activity implements WorkflowActivityInterface { use KanvasJobsTrait; - public $tries = 10; + public $tries = 5; /** * @param Lead $lead @@ -35,171 +30,16 @@ public function execute(Model $lead, AppInterface $app, array $params): array $this->overwriteAppService($app); $zohoLead = ZohoLead::fromLead($lead); $zohoData = $zohoLead->toArray(); - $company = Companies::getById($lead->companies_id); - $usesAgentsModule = $company->get(CustomFieldEnum::ZOHO_HAS_AGENTS_MODULE->value); + //$company = Companies::getById($lead->companies_id); - $zohoCrm = Client::getInstance($app, $company); - $status = 'created'; - - if (! $zohoLeadId = $lead->get(CustomFieldEnum::ZOHO_LEAD_ID->value)) { - if ($usesAgentsModule) { - $this->assignAgent($app, $zohoLead, $lead, $company, $zohoData); - } - - $zohoLead = $zohoCrm->leads->create($zohoData); - $zohoLeadId = $zohoLead->getId(); - - $zohoData['Lead_Status'] = 'New Lead'; - - $lead->set( - CustomFieldEnum::ZOHO_LEAD_ID->value, - $zohoLeadId - ); - } else { - $zohoLeadInfo = $zohoCrm->leads->get((string) $zohoLeadId)->getData(); - if (! empty($zohoLeadInfo)) { - $status = 'updated'; - $zohoLead = $zohoCrm->leads->update( - (string) $zohoLeadId, - $zohoData - ); - } else { - $lead->close(); - - return [ - 'zohoLeadId' => $zohoLeadId, - 'zohoRequest' => 'Lead not found in Zoho', - 'leadId' => $lead->getId(), - 'status' => 'closed', - ]; - } - } - - $this->uploadAttachments($zohoCrm->leads, $lead); + $syncLeadWithZoho = new SyncLeadToZohoAction($app, $lead->company, $lead); + $zohoLead = $syncLeadWithZoho->execute(); return [ - 'zohoLeadId' => $zohoLeadId, + 'zohoLeadId' => $zohoLead->getId(), 'zohoRequest' => $zohoData, 'leadId' => $lead->getId(), - 'status' => $status, + 'status' => $lead->status()->get()->name, ]; } - - protected function assignAgent( - AppInterface $app, - ZohoLead $zohoLead, - Lead $lead, - Companies $company, - array &$zohoData - ): void { - $memberNumber = (string) $zohoLead->getMemberNumber(); - - if (empty($memberNumber) && $lead->user()->exists()) { - $memberNumber = (string) $lead->user()->firstOrFail()->get('member_number_' . $company->getId()); - } - - if (! empty($memberNumber)) { - $zohoMemberField = $company->get(CustomFieldEnum::ZOHO_MEMBER_FIELD->value) ?? 'Member_ID'; - $zohoData[$zohoMemberField] = $memberNumber; - } - - $zohoService = new ZohoService($app, $company); - - try { - $agent = $zohoService->getAgentByMemberNumber($memberNumber); - } catch (Throwable $e) { - $agent = null; - } - - try { - $agentInfo = Agent::getByMemberNumber($memberNumber, $company); - } catch (Throwable $e) { - $agentInfo = null; - } - - $defaultLeadSource = $company->get(CustomFieldEnum::ZOHO_DEFAULT_LEAD_SOURCE->value); - if (! empty($defaultLeadSource)) { - $zohoData['Lead_Source'] = $defaultLeadSource; //$lead->receiver ? $lead->receiver->name : $defaultLeadSource; - } - - if (is_object($agent)) { - try { - ///lead owner should match lead routing - $leadRoutingEmailCleanUp = preg_replace('/[^a-zA-Z0-9@._-]/', '', $agent->Lead_Routing); - $zohoData['Owner'] = $zohoService->getAgentByEmail($leadRoutingEmailCleanUp)->Owner['id']; - } catch (Throwable $e) { - //send fail notification and assign to default lead routing email - $zohoData['Owner'] = (int) ($app->get(CustomFieldEnum::DEFAULT_OWNER->value) ?? $agent->Owner['id']); - } - - if ($agent->Sponsor) { - $zohoData['Sponsor'] = (string) $agent->Sponsor; - } - - if ($agentInfo) { - $lead->users_id = $agentInfo->users_id; - $lead->saveOrFail(); - } - - if ($agentInfo && $agentInfo->get('over_write_owner')) { - $zohoData['Owner'] = (int) $agentInfo->get('over_write_owner'); - } - if (empty($defaultLeadSource)) { - $zohoData['Lead_Source'] = $agent->name ?? $agent->Name; - } - } elseif ($agentInfo instanceof Agent) { - $zohoData['Owner'] = (int) $agentInfo->owner_linked_source_id; - if (empty($defaultLeadSource)) { - $zohoData['Lead_Source'] = $agentInfo->name; - } - - if ($agentInfo->user && ! empty($agentInfo->user->get('sponsor'))) { - $zohoData['Sponsor'] = (string) $agentInfo->user->get('sponsor'); - } - } - - if ($company->get(CustomFieldEnum::ZOHO_USE_AGENT_NAME->value) && ! empty($agentInfo->name)) { - $zohoData['Agent_Name'] = $agentInfo->name; - } - - //if value is 0 or empty, remove it - if (empty($zohoData['Owner'])) { - unset($zohoData['Owner']); - } - } - - protected function uploadAttachments(ZohoLeadModule $zohoLead, Lead $lead): void - { - $lead->load('files'); - if (! $lead->files()->count()) { - return; - } - - $syncFiles = $lead->get(CustomFieldEnum::ZOHO_LEAD_SYNC_FILES->value) ?? []; - - foreach ($lead->files()->get() as $file) { - if (isset($syncFiles[$file->id])) { - continue; - } - - try { - $fileContent = file_get_contents($file->url); - - $zohoLead->uploadAttachment( - (string) $lead->get(CustomFieldEnum::ZOHO_LEAD_ID->value), - $file->name, - $fileContent - ); - - $syncFiles[$file->id] = $file->id; - } catch (Throwable $e) { - //do nothing - } - } - - $lead->set( - CustomFieldEnum::ZOHO_LEAD_SYNC_FILES->value, - $syncFiles - ); - } } From 624d612fc12d6cb89cc05f6801b01784730dbe13 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 27 Jul 2024 08:15:31 -0400 Subject: [PATCH 4/9] refact: move lead creation to action --- src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php b/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php index 9b9763768..29d755b36 100644 --- a/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php +++ b/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php @@ -32,7 +32,7 @@ public function execute(Model $lead, AppInterface $app, array $params): array $zohoData = $zohoLead->toArray(); //$company = Companies::getById($lead->companies_id); - $syncLeadWithZoho = new SyncLeadToZohoAction($app, $lead->company, $lead); + $syncLeadWithZoho = new SyncLeadToZohoAction($app, $lead); $zohoLead = $syncLeadWithZoho->execute(); return [ From 772015088b91ae207143281abd234d2441a303b8 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 27 Jul 2024 09:45:01 -0400 Subject: [PATCH 5/9] fix: relate lead to contact --- src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php b/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php index 29d755b36..ce532c067 100644 --- a/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php +++ b/src/Domains/Connectors/Zoho/Workflows/ZohoLeadActivity.php @@ -39,7 +39,7 @@ public function execute(Model $lead, AppInterface $app, array $params): array 'zohoLeadId' => $zohoLead->getId(), 'zohoRequest' => $zohoData, 'leadId' => $lead->getId(), - 'status' => $lead->status()->get()->name, + 'status' => $lead->status()->first()->name, ]; } } From 1b733aec891256b745f46e1f764a195cfee5dfae Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 27 Jul 2024 10:14:48 -0400 Subject: [PATCH 6/9] refact: downlaod all leads --- .../Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php | 2 +- tests/Connectors/Integration/Zoho/LeadTest.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php b/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php index 5ed7eab31..f708e8b29 100644 --- a/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php +++ b/src/Domains/Connectors/Zoho/Actions/DownloadAllZohoLeadAction.php @@ -42,7 +42,7 @@ public function execute($totalPages = 50, $leadsPerPage = 200): Generator } $this->totalLeadsProcessed++; - yield $localLead->getId(); + yield $localLead; } } } diff --git a/tests/Connectors/Integration/Zoho/LeadTest.php b/tests/Connectors/Integration/Zoho/LeadTest.php index 7f9ed0a23..739ea4182 100644 --- a/tests/Connectors/Integration/Zoho/LeadTest.php +++ b/tests/Connectors/Integration/Zoho/LeadTest.php @@ -11,6 +11,7 @@ use Kanvas\Guild\Enums\FlagEnum; use Kanvas\Guild\Leads\Actions\CreateLeadReceiverAction; use Kanvas\Guild\Leads\DataTransferObject\LeadReceiver; +use Kanvas\Guild\Leads\Models\Lead; use Kanvas\Guild\Support\Setup; use Tests\TestCase; @@ -50,6 +51,10 @@ public function testDownloadAllLeads(): void $leads = $downloadAllLeads->execute(totalPages: 1, leadsPerPage: 1); + foreach ($leads as $lead) { + $this->assertInstanceOf(Lead::class, $lead); + } + $this->assertIsArray(iterator_to_array($leads)); $this->assertEquals(1, $downloadAllLeads->getTotalLeadsProcessed()); } From 316a7d79926d5fa79b12f98b5e6020b07e45be8f Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 27 Jul 2024 10:19:44 -0400 Subject: [PATCH 7/9] feat: add new command --- .../Zoho/ZohoLeadsDownloadCommand.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php diff --git a/app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php b/app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php new file mode 100644 index 000000000..b0d5541b0 --- /dev/null +++ b/app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php @@ -0,0 +1,52 @@ +argument('app_id')); + $this->overwriteAppService($app); + $company = Companies::getById((int) $this->argument('company_id')); + $leadReceiver = LeadReceiver::getByIdFromCompanyApp((int) $this->argument('receiver_id'), $company, $app); + $page = (int) $this->argument('page'); + $leadsPerPage = (int) $this->argument('leadsPerPage'); + + $downloadAllLeads = new DownloadAllZohoLeadAction($app, $company, $leadReceiver); + $downloadAllLeads->execute($page, $leadsPerPage); + + $this->info($downloadAllLeads->getTotalLeadsProcessed() . ' leads downloaded from Zoho to ' . $leadReceiver->name); + + return; + } +} From bbf9e31323c6feddaa01a61b57148c64227d978c Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 27 Jul 2024 10:21:07 -0400 Subject: [PATCH 8/9] feat: add new command --- tests/Connectors/Integration/Zoho/LeadTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Connectors/Integration/Zoho/LeadTest.php b/tests/Connectors/Integration/Zoho/LeadTest.php index 739ea4182..31c84306d 100644 --- a/tests/Connectors/Integration/Zoho/LeadTest.php +++ b/tests/Connectors/Integration/Zoho/LeadTest.php @@ -55,7 +55,6 @@ public function testDownloadAllLeads(): void $this->assertInstanceOf(Lead::class, $lead); } - $this->assertIsArray(iterator_to_array($leads)); $this->assertEquals(1, $downloadAllLeads->getTotalLeadsProcessed()); } } From 7bc823b160673498ae9ef5bc0ae91ef6a4181a40 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 27 Jul 2024 10:24:08 -0400 Subject: [PATCH 9/9] feat: add new command --- .../Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php b/app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php index b0d5541b0..15da2638d 100644 --- a/app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php +++ b/app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace App\Console\Commands\Connectors\Shopify; +namespace App\Console\Commands\Connectors\Zoho; use Baka\Traits\KanvasJobsTrait; use Illuminate\Console\Command; @@ -11,7 +11,7 @@ use Kanvas\Connectors\Zoho\Actions\DownloadAllZohoLeadAction; use Kanvas\Guild\Leads\Models\LeadReceiver; -class ShopifyInventoryDownloadCommand extends Command +class ZohoLeadsDownloadCommand extends Command { use KanvasJobsTrait; /**