diff --git a/app/Models/User.php b/app/Models/User.php
index 1ff9db19787..32e66eeeb8a 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
+use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable implements MustVerifyEmail
@@ -127,4 +128,20 @@ public function getNameAttribute($value): ?string
return $this->first_name.' '.$this->last_name;
}
+
+ /**
+ * Get the contact of the user in the given vault.
+ * All users have a contact in the vaults.
+ *
+ * @param Vault $vault
+ * @return Contact
+ */
+ public function getContactInVault(Vault $vault): Contact
+ {
+ $contact = DB::table('user_vault')->where('vault_id', $vault->id)
+ ->where('user_id', $this->id)
+ ->first();
+
+ return Contact::findOrFail($contact->contact_id);
+ }
}
diff --git a/domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelper.php b/domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelper.php
index 8ca814b0a03..1e07171ae4f 100644
--- a/domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelper.php
+++ b/domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelper.php
@@ -45,7 +45,14 @@ public static function data(Contact $contact, User $user): array
'template_pages' => $templatesPagesCollection,
'contact_information' => self::getContactInformation($templatePages, $contact, $user),
'modules' => $firstPage ? self::modules($firstPage, $contact, $user) : [],
+ 'options' => [
+ 'can_be_deleted' => $user->getContactInVault($contact->vault)->id !== $contact->id,
+ ],
'url' => [
+ 'update_template' => route('contact.blank', [
+ 'vault' => $contact->vault_id,
+ 'contact' => $contact->id,
+ ]),
'destroy' => route('contact.destroy', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
@@ -63,7 +70,14 @@ public static function dataForTemplatePage(Contact $contact, User $user, Templat
'template_pages' => self::getTemplatePagesList($templatePages, $contact, $templatePage),
'contact_information' => self::getContactInformation($templatePages, $contact, $user),
'modules' => self::modules($templatePage, $contact, $user),
+ 'options' => [
+ 'can_be_deleted' => $user->getContactInVault($contact->vault)->id !== $contact->id,
+ ],
'url' => [
+ 'update_template' => route('contact.blank', [
+ 'vault' => $contact->vault_id,
+ 'contact' => $contact->id,
+ ]),
'destroy' => route('contact.destroy', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
diff --git a/resources/js/Pages/Vault/Contact/Blank.vue b/resources/js/Pages/Vault/Contact/Blank.vue
index 9382e97dda3..0c29205e398 100644
--- a/resources/js/Pages/Vault/Contact/Blank.vue
+++ b/resources/js/Pages/Vault/Contact/Blank.vue
@@ -67,15 +67,13 @@
-
- This contact doesn't have an associated template. That means we don't know how to display this contact.
-
-
- Please choose one template below to tell Monica how a contact should be displayed.
+
+ Please choose one template below to tell Monica how this contact should be displayed. Templates let you
+ define which data should be diplayed on the contact page.
- However, it seems that there are no templates in the account yet. Please add at least template to your
- account first, then associate this template with this contact.
+ It seems that there are no templates in the account yet. Please add at least template to your account
+ first, then associate this template with this contact.
diff --git a/resources/js/Pages/Vault/Contact/Show.vue b/resources/js/Pages/Vault/Contact/Show.vue
index 9fa8af22520..edf8d494a5a 100644
--- a/resources/js/Pages/Vault/Contact/Show.vue
+++ b/resources/js/Pages/Vault/Contact/Show.vue
@@ -60,8 +60,13 @@
-
- -
+
+ -
+ Change template
+
+ -
Delete contact
diff --git a/routes/web.php b/routes/web.php
index 2ba7e49830d..a156e991b5e 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -99,7 +99,7 @@
Route::get('/edit', [ContactController::class, 'edit'])->name('contact.edit');
Route::post('', [ContactController::class, 'update'])->name('contact.update');
Route::delete('', [ContactController::class, 'destroy'])->name('contact.destroy');
- Route::get('no-template', [ContactNoTemplateController::class, 'show'])->name('contact.blank');
+ Route::get('update-template', [ContactNoTemplateController::class, 'show'])->name('contact.blank');
Route::put('template', [ContactTemplateController::class, 'update'])->name('contact.template.update');
Route::get('tabs/{slug}', [ContactPageController::class, 'show'])->name('contact.page.show');
diff --git a/tests/Unit/Domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelperTest.php b/tests/Unit/Domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelperTest.php
index 7a45aaee997..481748b74d2 100644
--- a/tests/Unit/Domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelperTest.php
+++ b/tests/Unit/Domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelperTest.php
@@ -8,6 +8,7 @@
use App\Models\Template;
use App\Models\TemplatePage;
use App\Models\User;
+use App\Models\Vault;
use function env;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
@@ -21,6 +22,16 @@ public function it_gets_the_data_needed_for_the_view(): void
{
$contact = Contact::factory()->create();
$user = User::factory()->create();
+ $vault = Vault::factory()->create([
+ 'account_id' => $user->account_id,
+ ]);
+ $contact = Contact::factory()->create([
+ 'vault_id' => $vault->id,
+ ]);
+ $vault->users()->save($user, [
+ 'permission' => 1,
+ 'contact_id' => $contact->id,
+ ]);
$template = Template::factory()->create();
TemplatePage::factory()->create([
'template_id' => $template->id,
@@ -37,7 +48,7 @@ public function it_gets_the_data_needed_for_the_view(): void
$array = ContactShowViewHelper::data($contact, $user);
$this->assertEquals(
- 5,
+ 6,
count($array)
);
@@ -45,6 +56,7 @@ public function it_gets_the_data_needed_for_the_view(): void
$this->assertArrayHasKey('template_pages', $array);
$this->assertArrayHasKey('contact_information', $array);
$this->assertArrayHasKey('modules', $array);
+ $this->assertArrayHasKey('options', $array);
$this->assertArrayHasKey('url', $array);
$this->assertEquals(
@@ -62,6 +74,7 @@ public function it_gets_the_data_needed_for_the_view(): void
);
$this->assertEquals(
[
+ 'update_template' => env('APP_URL').'/vaults/'.$contact->vault->id.'/contacts/'.$contact->id.'/update-template',
'destroy' => env('APP_URL').'/vaults/'.$contact->vault->id.'/contacts/'.$contact->id,
],
$array['url']
diff --git a/tests/Unit/Models/UserTest.php b/tests/Unit/Models/UserTest.php
index c84fa586581..4a9a7cf8ae7 100644
--- a/tests/Unit/Models/UserTest.php
+++ b/tests/Unit/Models/UserTest.php
@@ -2,6 +2,7 @@
namespace Tests\Unit\Models;
+use App\Models\Contact;
use App\Models\ContactTask;
use App\Models\Note;
use App\Models\User;
@@ -81,4 +82,28 @@ public function it_returns_the_name_attribute(): void
$rachel->name,
);
}
+
+ /** @test */
+ public function it_returns_the_contact_in_the_vault(): void
+ {
+ $rachel = User::factory()->create([
+ 'first_name' => 'Dwight',
+ 'last_name' => 'Schrute',
+ ]);
+ $vault = Vault::factory()->create([
+ 'account_id' => $rachel->account_id,
+ ]);
+ $contact = Contact::factory()->create([
+ 'vault_id' => $vault->id,
+ ]);
+ $vault->users()->save($rachel, [
+ 'permission' => 1,
+ 'contact_id' => $contact->id,
+ ]);
+
+ $this->assertEquals(
+ $contact->id,
+ $rachel->getContactInVault($vault)->id,
+ );
+ }
}