Skip to content

Commit

Permalink
fix: fix company not moving when moving a contact (#7202)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Mar 18, 2024
1 parent fb2a94f commit 37da37f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function execute(array $data): Contact
$this->data = $data;
$this->validate();
$this->move();
$this->moveCompanyInformation();
$this->updateLastEditedDate();

return $this->contact;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down

0 comments on commit 37da37f

Please sign in to comment.