From 37da37f04c7f31f7262caf479b7eb8f99148bb2c Mon Sep 17 00:00:00 2001 From: Mazarin Date: Mon, 18 Mar 2024 10:41:20 -0400 Subject: [PATCH] fix: fix company not moving when moving a contact (#7202) --- .../Services/MoveContactToAnotherVault.php | 30 +++++++++++ .../MoveContactToAnotherVaultTest.php | 53 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/app/Domains/Contact/ManageContact/Services/MoveContactToAnotherVault.php b/app/Domains/Contact/ManageContact/Services/MoveContactToAnotherVault.php index 1fec33291e7..ca7dad6d07c 100644 --- a/app/Domains/Contact/ManageContact/Services/MoveContactToAnotherVault.php +++ b/app/Domains/Contact/ManageContact/Services/MoveContactToAnotherVault.php @@ -2,6 +2,7 @@ namespace App\Domains\Contact\ManageContact\Services; +use App\Domains\Vault\ManageCompanies\Services\CreateCompany; use App\Exceptions\NotEnoughPermissionException; use App\Interfaces\ServiceInterface; use App\Models\Contact; @@ -50,6 +51,7 @@ public function execute(array $data): Contact $this->data = $data; $this->validate(); $this->move(); + $this->moveCompanyInformation(); $this->updateLastEditedDate(); return $this->contact; @@ -78,6 +80,34 @@ private function move(): void $this->contact->save(); } + /** + * If the contact belongs to a company, we should move the company + * information to the new vault as well. + * If the company only has this contact, we should move the company. + * However, if the company has other contacts, we should copy the company + * and move the contact to the new company. + */ + private function moveCompanyInformation(): void + { + if ($this->contact->company) { + if ($this->contact->company->contacts->count() === 1) { + $this->contact->company->vault_id = $this->newVault->id; + $this->contact->company->save(); + } else { + $newCompany = (new CreateCompany())->execute([ + 'account_id' => $this->author->account_id, + 'author_id' => $this->author->id, + 'vault_id' => $this->newVault->id, + 'name' => $this->contact->company->name, + 'type' => $this->contact->company->type, + ]); + + $this->contact->company_id = $newCompany->id; + $this->contact->save(); + } + } + } + private function updateLastEditedDate(): void { $this->contact->last_updated_at = Carbon::now(); diff --git a/tests/Unit/Domains/Contact/ManageContact/Services/MoveContactToAnotherVaultTest.php b/tests/Unit/Domains/Contact/ManageContact/Services/MoveContactToAnotherVaultTest.php index 7f9973cfa5b..1d1b2d0e0b7 100644 --- a/tests/Unit/Domains/Contact/ManageContact/Services/MoveContactToAnotherVaultTest.php +++ b/tests/Unit/Domains/Contact/ManageContact/Services/MoveContactToAnotherVaultTest.php @@ -5,6 +5,7 @@ use App\Domains\Contact\ManageContact\Services\MoveContactToAnotherVault; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; +use App\Models\Company; use App\Models\Contact; use App\Models\User; use App\Models\Vault; @@ -30,6 +31,58 @@ public function it_moves_a_contact_to_another_vault(): void $this->executeService($regis, $regis->account, $vault, $newVault, $contact); } + /** @test */ + public function it_moves_a_contact_to_another_vault_and_copy_the_company_information_if_there_are_multiple_contacts_in_it(): void + { + $regis = $this->createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $newVault = $this->createVault($regis->account); + $newVault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $newVault); + $contact = Contact::factory()->create(['vault_id' => $vault->id]); + $company = Company::factory()->create(['vault_id' => $vault->id]); + Contact::factory()->count(2)->create(['vault_id' => $vault->id, 'company_id' => $company->id]); + $contact->company_id = $company->id; + $contact->save(); + + $this->executeService($regis, $regis->account, $vault, $newVault, $contact); + + $this->assertDatabaseHas('companies', [ + 'id' => $company->id, + ]); + + $this->assertDatabaseMissing('contacts', [ + 'id' => $contact->id, + 'company_id' => $company->id, + ]); + } + + /** @test */ + public function it_moves_a_contact_to_another_vault_and_move_the_company_information_if_there_are_no_other_contacts_in_it(): void + { + $regis = $this->createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $newVault = $this->createVault($regis->account); + $newVault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $newVault); + $contact = Contact::factory()->create(['vault_id' => $vault->id]); + $company = Company::factory()->create(['vault_id' => $vault->id]); + $contact->company_id = $company->id; + $contact->save(); + + $this->executeService($regis, $regis->account, $vault, $newVault, $contact); + + $this->assertDatabaseMissing('companies', [ + 'id' => $company->id, + 'vault_id' => $vault->id, + ]); + + $this->assertDatabaseHas('companies', [ + 'id' => $company->id, + 'vault_id' => $newVault->id, + ]); + } + /** @test */ public function it_fails_if_wrong_parameters_are_given(): void {