Skip to content

Commit

Permalink
feat: add new people to a organization
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Jul 3, 2023
1 parent 54721ab commit 91eb421
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace App\GraphQL\Guild\Mutations\Organizations;

use Kanvas\Guild\Customers\Models\People;
use Kanvas\Guild\Organizations\Models\Organization;
use Kanvas\Guild\Organizations\Models\OrganizationPeople;

class PeopleOrganizationMutation
{
public function add(mixed $root, array $req): bool
{
$user = auth()->user();
$data = $req['input'];

$total = 0;

$organization = Organization::getByIdFromCompany((int) $data['organization_id'], $user->getCurrentCompany());
foreach ($data['peoples_id'] as $peopleId) {
$people = People::getByIdFromCompany($peopleId, $user->getCurrentCompany());

$organization->addPeople($people);
$total++;
}

return $total > 0;
}

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

$total = 0;
$organization = Organization::getByIdFromCompany((int) $data['organization_id'], $user->getCurrentCompany());

foreach ($data['peoples_id'] as $peopleId) {
$people = People::getByIdFromCompany($peopleId, $user->getCurrentCompany());

OrganizationPeople::where('organizations_id', $organization->getId())
->where('peoples_id', $people->getId())
->delete();

$total++;
}

return $total > 0;
}
}
17 changes: 11 additions & 6 deletions src/Guild/Organizations/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ public function relationships(): HasManyThrough
);
}

public function addPeople(People $people): void
/**
* @psalm-suppress MixedReturnStatement
*/
public function addPeople(People $people): OrganizationPeople
{
$newPeople = new OrganizationPeople();
$newPeople->organizations_id = $this->getId();
$newPeople->peoples_id = $people->getId();
$newPeople->created_at = date('Y-m-d H:i:s');
$newPeople->saveOrFail();
return OrganizationPeople::firstOrCreate([
'organizations_id' => $this->getId(),
'peoples_id' => $people->getId(),
], [
'created_at' => date('Y-m-d H:i:s'),

]);
}
}

0 comments on commit 91eb421

Please sign in to comment.