Skip to content

Commit 37da37f

Browse files
authored
fix: fix company not moving when moving a contact (#7202)
1 parent fb2a94f commit 37da37f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

app/Domains/Contact/ManageContact/Services/MoveContactToAnotherVault.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Domains\Contact\ManageContact\Services;
44

5+
use App\Domains\Vault\ManageCompanies\Services\CreateCompany;
56
use App\Exceptions\NotEnoughPermissionException;
67
use App\Interfaces\ServiceInterface;
78
use App\Models\Contact;
@@ -50,6 +51,7 @@ public function execute(array $data): Contact
5051
$this->data = $data;
5152
$this->validate();
5253
$this->move();
54+
$this->moveCompanyInformation();
5355
$this->updateLastEditedDate();
5456

5557
return $this->contact;
@@ -78,6 +80,34 @@ private function move(): void
7880
$this->contact->save();
7981
}
8082

83+
/**
84+
* If the contact belongs to a company, we should move the company
85+
* information to the new vault as well.
86+
* If the company only has this contact, we should move the company.
87+
* However, if the company has other contacts, we should copy the company
88+
* and move the contact to the new company.
89+
*/
90+
private function moveCompanyInformation(): void
91+
{
92+
if ($this->contact->company) {
93+
if ($this->contact->company->contacts->count() === 1) {
94+
$this->contact->company->vault_id = $this->newVault->id;
95+
$this->contact->company->save();
96+
} else {
97+
$newCompany = (new CreateCompany())->execute([
98+
'account_id' => $this->author->account_id,
99+
'author_id' => $this->author->id,
100+
'vault_id' => $this->newVault->id,
101+
'name' => $this->contact->company->name,
102+
'type' => $this->contact->company->type,
103+
]);
104+
105+
$this->contact->company_id = $newCompany->id;
106+
$this->contact->save();
107+
}
108+
}
109+
}
110+
81111
private function updateLastEditedDate(): void
82112
{
83113
$this->contact->last_updated_at = Carbon::now();

tests/Unit/Domains/Contact/ManageContact/Services/MoveContactToAnotherVaultTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Domains\Contact\ManageContact\Services\MoveContactToAnotherVault;
66
use App\Exceptions\NotEnoughPermissionException;
77
use App\Models\Account;
8+
use App\Models\Company;
89
use App\Models\Contact;
910
use App\Models\User;
1011
use App\Models\Vault;
@@ -30,6 +31,58 @@ public function it_moves_a_contact_to_another_vault(): void
3031
$this->executeService($regis, $regis->account, $vault, $newVault, $contact);
3132
}
3233

34+
/** @test */
35+
public function it_moves_a_contact_to_another_vault_and_copy_the_company_information_if_there_are_multiple_contacts_in_it(): void
36+
{
37+
$regis = $this->createUser();
38+
$vault = $this->createVault($regis->account);
39+
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault);
40+
$newVault = $this->createVault($regis->account);
41+
$newVault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $newVault);
42+
$contact = Contact::factory()->create(['vault_id' => $vault->id]);
43+
$company = Company::factory()->create(['vault_id' => $vault->id]);
44+
Contact::factory()->count(2)->create(['vault_id' => $vault->id, 'company_id' => $company->id]);
45+
$contact->company_id = $company->id;
46+
$contact->save();
47+
48+
$this->executeService($regis, $regis->account, $vault, $newVault, $contact);
49+
50+
$this->assertDatabaseHas('companies', [
51+
'id' => $company->id,
52+
]);
53+
54+
$this->assertDatabaseMissing('contacts', [
55+
'id' => $contact->id,
56+
'company_id' => $company->id,
57+
]);
58+
}
59+
60+
/** @test */
61+
public function it_moves_a_contact_to_another_vault_and_move_the_company_information_if_there_are_no_other_contacts_in_it(): void
62+
{
63+
$regis = $this->createUser();
64+
$vault = $this->createVault($regis->account);
65+
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault);
66+
$newVault = $this->createVault($regis->account);
67+
$newVault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $newVault);
68+
$contact = Contact::factory()->create(['vault_id' => $vault->id]);
69+
$company = Company::factory()->create(['vault_id' => $vault->id]);
70+
$contact->company_id = $company->id;
71+
$contact->save();
72+
73+
$this->executeService($regis, $regis->account, $vault, $newVault, $contact);
74+
75+
$this->assertDatabaseMissing('companies', [
76+
'id' => $company->id,
77+
'vault_id' => $vault->id,
78+
]);
79+
80+
$this->assertDatabaseHas('companies', [
81+
'id' => $company->id,
82+
'vault_id' => $newVault->id,
83+
]);
84+
}
85+
3386
/** @test */
3487
public function it_fails_if_wrong_parameters_are_given(): void
3588
{

0 commit comments

Comments
 (0)