Skip to content

Commit

Permalink
Merge pull request #1560 from bakaphp/development
Browse files Browse the repository at this point in the history
Fix receiver sync
  • Loading branch information
kaioken authored Jun 25, 2024
2 parents e904aa1 + a9e6f3f commit 63e5df7
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 12 deletions.
5 changes: 3 additions & 2 deletions app/Http/Controllers/ReceiverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function store(string $uuid, Request $request): JsonResponse

$tempSubSystem = $uuid == $app->get('subsystem-temp-uuid');
$zohoLeadTempSubSystem = $uuid == $app->get('zoho-lead-temp-uuid');
$isTempSystem = $tempSubSystem || $zohoLeadTempSubSystem;

Auth::loginUsingId($receiver->users_id);

Expand All @@ -51,7 +52,7 @@ public function store(string $uuid, Request $request): JsonResponse

$leadExternalId = $request->get('entity_id');

if ($receiver->rotation === null && ! $tempSubSystem) {
if ($receiver->rotation === null && ! $isTempSystem) {
return response()->json(['message' => 'Rotation not found'], 404);
}

Expand All @@ -64,7 +65,7 @@ public function store(string $uuid, Request $request): JsonResponse
}

if ($zohoLeadTempSubSystem) {
$syncLead = new SyncZohoLeadAction($app, $receiver->company, $leadExternalId);
$syncLead = new SyncZohoLeadAction($app, $receiver->company, $receiver, $leadExternalId);
$syncLead->execute();

return response()->json(['message' => 'Receiver processed']);
Expand Down
4 changes: 4 additions & 0 deletions graphql/schemas/Guild/organization.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ type Organization {

input OrganizationInput {
name: String!
email: String
address: String
city: String
state: String
zip: String
}

input OrganizationPeopleInput {
Expand Down
2 changes: 1 addition & 1 deletion graphql/schemas/Inventory/channel.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type Channel {
id: ID!
companies_id: Int!
users_id: Int!
companies: Company
companies: Company! @belongsTo(relation: "company")
users: User
name: String!
uuid: String!
Expand Down
4 changes: 2 additions & 2 deletions graphql/schemas/Inventory/product.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Product {
uuid: String!
name: String!
slug: String!
description: String!
description: String
short_description: String
html_description: String
warranty_terms: String
Expand Down Expand Up @@ -40,7 +40,7 @@ input ProductInput {
products_types_id: ID
sku: String @rules(apply: ["required_without:variants"])
name: String!
description: String!
description: String
slug: String
short_description: String
html_description: String
Expand Down
90 changes: 85 additions & 5 deletions src/Domains/Connectors/Zoho/Actions/SyncZohoLeadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,43 @@
use Baka\Contracts\AppInterface;
use Baka\Contracts\CompanyInterface;
use Baka\Support\Str;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Kanvas\Connectors\Zoho\Enums\CustomFieldEnum;
use Kanvas\Connectors\Zoho\ZohoService;
use Kanvas\Guild\Customers\DataTransferObject\Address;
use Kanvas\Guild\Customers\DataTransferObject\Contact;
use Kanvas\Guild\Customers\DataTransferObject\People;
use Kanvas\Guild\Leads\Actions\CreateLeadAction;
use Kanvas\Guild\Leads\DataTransferObject\Lead as DataTransferObjectLead;
use Kanvas\Guild\Leads\Models\Lead;
use Kanvas\Guild\Leads\Models\LeadReceiver;
use Kanvas\Guild\Leads\Models\LeadStatus;
use Kanvas\Guild\Pipelines\Models\Pipeline;
use Kanvas\Users\Models\UsersAssociatedApps;
use Spatie\LaravelData\DataCollection;

class SyncZohoLeadAction
{
public function __construct(
protected AppInterface $app,
protected CompanyInterface $company,
protected LeadReceiver $receiver,
protected string $zohoLeadId
) {
}

public function execute(): void
public function execute(): ?Lead
{
$zohoService = new ZohoService($this->app, $this->company);

try {
$zohoLead = $zohoService->getLeadById($this->zohoLeadId);
} catch (\Exception $e) {
} catch (Exception $e) {
Log::error('Error getting Zoho Lead', ['error' => $e->getMessage()]);

return;
return null;
}

$localLead = Lead::getByCustomField(
Expand All @@ -41,18 +53,86 @@ public function execute(): void
);

if (! $localLead) {
return ;
$table = (new Lead())->getTable();
$localLead = Lead::join(DB::connection('ecosystem')->getDatabaseName() . '.apps_custom_fields', 'apps_custom_fields.entity_id', '=', $table . '.id')
->where('apps_custom_fields.companies_id', $this->company->getId())
->where('apps_custom_fields.model_name', 'Gewaer\\Models\\Leads') //legacy
->where('apps_custom_fields.name', CustomFieldEnum::ZOHO_LEAD_ID->value)
->where('apps_custom_fields.value', $this->zohoLeadId)
->select($table . '.*')
->first();
}

$status = $zohoLead->Lead_Status;
$status = strtolower($zohoLead->Lead_Status);

$leadStatus = match (true) {
Str::contains($status, 'close') => LeadStatus::getByName('bad'),
Str::contains($status, 'won') => LeadStatus::getByName('complete'),
default => LeadStatus::getByName('active'),
};

$user = UsersAssociatedApps::fromApp($this->app)->where('email', $zohoLead->Owner['email'])->first();

if (! $localLead) {
//create lead
$pipelineStage = Pipeline::fromApp($this->app)->fromCompany($this->company)->where('is_default', 1)->first()->stages()->first();

$contact = [
[
'value' => $zohoLead->Email,
'contacts_types_id' => 1,
'weight' => 0,
],[
'value' => $zohoLead->Phone,
'contacts_types_id' => 2,
'weight' => 0,
],
];
$lead = new DataTransferObjectLead(
$this->app,
$this->company->defaultBranch,
$user ?? $this->company->user,
$zohoLead->Full_Name,
$pipelineStage->getId(),
new People(
$this->app,
$this->company->defaultBranch,
$user ?? $this->company->user,
$zohoLead->First_Name,
Contact::collect($contact, DataCollection::class),
Address::collect([], DataCollection::class),
$zohoLead->Last_Name
),
$user ? $user->getId() : 0,
0,
$leadStatus->getId(),
0,
$this->receiver->getId(),
null,
null,
null,
[
CustomFieldEnum::ZOHO_LEAD_ID->value => $this->zohoLeadId,
],
[],
true
);

return (new CreateLeadAction($lead))->execute();
}

if ($user) {
$localLead->leads_owner_id = $user->getId();
}
$localLead->people->firstname = $zohoLead->First_Name;
$localLead->people->lastname = $zohoLead->Last_Name;
$localLead->firstname = $zohoLead->First_Name;
$localLead->lastname = $zohoLead->Last_Name;
$localLead->title = $zohoLead->Full_Name . ' Opp';
$localLead->leads_status_id = $leadStatus->getId();
$localLead->disableWorkflows();
$localLead->saveOrFail();

return $localLead;
}
}
4 changes: 3 additions & 1 deletion src/Domains/Guild/Leads/Actions/CreateLeadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Baka\Contracts\CompanyInterface;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Kanvas\Apps\Models\Apps;
use Kanvas\Guild\Customers\Actions\CreatePeopleAction;
use Kanvas\Guild\Leads\DataTransferObject\Lead as LeadDataInput;
use Kanvas\Guild\Leads\Models\Lead;
Expand Down Expand Up @@ -63,6 +62,9 @@ public function execute(): Lead
$newLead->saveOrFail();

$newLead->setCustomFields($this->leadData->custom_fields);
if (! $this->leadData->runWorkflow) {
$newLead->disableWorkflows();
}
$newLead->saveCustomFields();

if ($this->leadData->files) {
Expand Down
1 change: 1 addition & 0 deletions src/Domains/Guild/Leads/DataTransferObject/Lead.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(
public readonly Organization|null $organization = null,
public readonly array $custom_fields = [],
public readonly array $files = [],
public readonly bool $runWorkflow = true
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public function __construct(
public readonly UserInterface $user,
public readonly AppInterface $app,
public readonly string $name,
public readonly ?string $address = null
public readonly ?string $email = null,
public readonly ?string $phone = null,
public readonly ?string $address = null,
public readonly ?string $city = null,
public readonly ?string $state = null,
public readonly ?string $zip = null,
) {
}
}

0 comments on commit 63e5df7

Please sign in to comment.