Skip to content

Commit

Permalink
feat : update people
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Jul 2, 2023
1 parent d2edf75 commit 5a77eb5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
30 changes: 30 additions & 0 deletions app/GraphQL/Guild/Mutations/Peoples/PeopleManagementMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use Kanvas\Apps\Models\Apps;
use Kanvas\Guild\Customers\Actions\CreatePeopleAction;
use Kanvas\Guild\Customers\Actions\UpdatePeopleAction;
use Kanvas\Guild\Customers\DataTransferObject\Address;
use Kanvas\Guild\Customers\DataTransferObject\Contact;
use Kanvas\Guild\Customers\DataTransferObject\People;
use Kanvas\Guild\Customers\Models\People as ModelsPeople;
use Kanvas\Guild\Customers\Repositories\PeoplesRepository;
use Kanvas\Guild\Leads\DataTransferObject\Lead;

class PeopleManagementMutation
Expand Down Expand Up @@ -42,4 +44,32 @@ public function create(mixed $root, array $req): ModelsPeople

return $createPeople->execute();
}

public function update(mixed $root, array $req): ModelsPeople
{
$user = auth()->user();
$data = $req['input'];

$people = PeoplesRepository::getById((int) $req['id'], $user->getCurrentCompany());

$peopleData = People::from([
'app' => app(Apps::class),
'branch' => $user->getCurrentBranch(),
'user' => $user,
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'contacts' => Contact::collection($data['contacts']),
'address' => Address::collection($data['address']),
'id' => $people->getId(),
'dob' => $data['dob'] ?? null,
'facebook_contact_id' => $data['facebook_contact_id'] ?? null,
'google_contact_id' => $data['google_contact_id'] ?? null,
'apple_contact_id' => $data['apple_contact_id'] ?? null,
'linkedin_contact_id' => $data['linkedin_contact_id'] ?? null,
]);

$updatePeople = new UpdatePeopleAction($people, $peopleData);

return $updatePeople->execute();
}
}
4 changes: 2 additions & 2 deletions src/Guild/Customers/Actions/CreatePeopleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function execute(): People
$people = People::create($attributes);
}

if ($this->peopleData->contacts instanceof DataCollection && $this->peopleData->contacts->count()) {
if ($this->peopleData->contacts->count()) {
$contacts = [];
foreach ($this->peopleData->contacts as $contact) {
$contacts[] = new Contact([
Expand All @@ -59,7 +59,7 @@ public function execute(): People
$people->contacts()->saveMany($contacts);
}

if ($this->peopleData->address instanceof DataCollection && $this->peopleData->address->count()) {
if ($this->peopleData->address->count()) {
$addresses = [];
foreach ($this->peopleData->address as $address) {
$addresses[] = new Address([
Expand Down
77 changes: 77 additions & 0 deletions src/Guild/Customers/Actions/UpdatePeopleAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Kanvas\Guild\Customers\Actions;

use Kanvas\Guild\Customers\DataTransferObject\People as PeopleDataInput;
use Kanvas\Guild\Customers\Models\Address;
use Kanvas\Guild\Customers\Models\Contact;
use Kanvas\Guild\Customers\Models\People;
use Spatie\LaravelData\DataCollection;

class UpdatePeopleAction
{
/**
* __construct.
*/
public function __construct(
protected People $people,
protected readonly PeopleDataInput $peopleData
) {
}

/**
* execute.
*/
public function execute(): People
{
$attributes = [
'name' => $this->peopleData->firstname . ' ' . $this->peopleData->lastname,
'dob' => $this->peopleData->dob,
'google_contact_id' => $this->peopleData->google_contact_id,
'facebook_contact_id' => $this->peopleData->facebook_contact_id,
'apple_contact_id' => $this->peopleData->apple_contact_id,
];

//@todo how to avoid duplicated? should it be use or frontend?
$this->people->update($attributes);

if ($this->peopleData->contacts->count()) {
$contacts = [];
$this->people->contacts()->delete();
foreach ($this->peopleData->contacts as $contact) {
$contacts[] = new Contact([
'contacts_types_id' => $contact->contacts_types_id,
'value' => $contact->value,
'weight' => $contact->weight,
]);
}

$this->people->contacts()->saveMany($contacts);
}

if ($this->peopleData->address->count()) {
$addresses = [];
$this->people->address()->delete();
foreach ($this->peopleData->address as $address) {
$addresses[] = new Address([
'address' => $address->address,
'address_2' => $address->address_2,
'city' => $address->city,
'state' => $address->state,
'zip' => $address->zipcode,
//'country' => $address->country,
'is_default' => $address->is_default,
'city_id' => $address->city_id ?? 0,
'state_id' => $address->state_id ?? 0,
'countries_id' => $address->country_id ?? 0,
]);
}

$this->people->address()->saveMany($addresses);
}

return $this->people;
}
}

0 comments on commit 5a77eb5

Please sign in to comment.