Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix company not moving when moving a contact #7202

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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