diff --git a/app/GraphQL/Guild/Mutations/Organizations/PeopleOrganizationMutation.php b/app/GraphQL/Guild/Mutations/Organizations/PeopleOrganizationMutation.php new file mode 100644 index 000000000..f9215df6f --- /dev/null +++ b/app/GraphQL/Guild/Mutations/Organizations/PeopleOrganizationMutation.php @@ -0,0 +1,51 @@ +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; + } +} diff --git a/src/Guild/Organizations/Models/Organization.php b/src/Guild/Organizations/Models/Organization.php index f328ff035..c68dc0cd6 100644 --- a/src/Guild/Organizations/Models/Organization.php +++ b/src/Guild/Organizations/Models/Organization.php @@ -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'), + + ]); } }