From 1c9067daab71247034bfcac10717811d67c1044c Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Fri, 13 Jan 2023 11:48:37 -0500 Subject: [PATCH 01/10] Update 2020_04_26_215133_create_addresses_table.php --- .../migrations/2020_04_26_215133_create_addresses_table.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/migrations/2020_04_26_215133_create_addresses_table.php b/database/migrations/2020_04_26_215133_create_addresses_table.php index f45ace039..795f2530b 100644 --- a/database/migrations/2020_04_26_215133_create_addresses_table.php +++ b/database/migrations/2020_04_26_215133_create_addresses_table.php @@ -14,7 +14,7 @@ public function up() Schema::create('addresses', function (Blueprint $table) { $table->id(); $table->uuid('uuid')->nullable(); - $table->unsignedBigInteger('contact_id'); + $table->unsignedBigInteger('vault_id'); $table->unsignedBigInteger('address_type_id')->nullable(); $table->boolean('is_past_address')->default(false); $table->string('street')->nullable(); @@ -27,7 +27,7 @@ public function up() $table->datetime('lived_from_at')->nullable(); $table->datetime('lived_until_at')->nullable(); $table->timestamps(); - $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade'); + $table->foreign('vault_id')->references('id')->on('vaults')->onDelete('cascade'); $table->foreign('address_type_id')->references('id')->on('address_types')->onDelete('set null'); }); } From eb9a3f4ecda15142596852b288fc4d3528c6118c Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Fri, 13 Jan 2023 17:31:09 -0500 Subject: [PATCH 02/10] refactor: manage address vault wide --- .../Services/CreateContactAddress.php | 35 +------ .../Jobs/FetchAddressGeocoding.php | 4 +- .../Services/CreateAddress.php | 91 +++++++++++++++++++ .../Services/DestroyAddress.php} | 25 +---- .../Services/GetGPSCoordinate.php | 2 +- .../Services/UpdateAddress.php} | 35 +------ .../ContactModuleAddressController.php | 2 +- .../ContactModuleAddressImageController.php | 2 +- .../ModuleContactAddressesViewHelper.php | 2 +- app/Models/Address.php | 24 +---- app/Models/Contact.php | 20 ++-- app/Models/Vault.php | 10 ++ database/factories/AddressFactory.php | 3 +- ...20_04_26_215133_create_addresses_table.php | 13 ++- .../Services/CreateAddressTest.php} | 86 +++--------------- .../Services/DestroyAddressTest.php} | 52 +++-------- .../Services/GetGPSCoordinateTest.php | 6 +- .../Services/UpdateAddressTest.php} | 75 ++++++++------- .../ModuleContactAddressesViewHelperTest.php | 4 +- tests/Unit/Models/ContactTest.php | 22 ++--- tests/Unit/Models/VaultTest.php | 12 +++ 21 files changed, 239 insertions(+), 286 deletions(-) rename app/Domains/Contact/{ManageContactAddresses => ManageContactAddress}/Services/CreateContactAddress.php (65%) rename app/Domains/{Contact/ManageContactAddresses => Vault/ManageAddresses}/Jobs/FetchAddressGeocoding.php (86%) create mode 100644 app/Domains/Vault/ManageAddresses/Services/CreateAddress.php rename app/Domains/{Contact/ManageContactAddresses/Services/DestroyContactAddress.php => Vault/ManageAddresses/Services/DestroyAddress.php} (60%) rename app/Domains/{Contact/ManageContactAddresses => Vault/ManageAddresses}/Services/GetGPSCoordinate.php (97%) rename app/Domains/{Contact/ManageContactAddresses/Services/UpdateContactAddress.php => Vault/ManageAddresses/Services/UpdateAddress.php} (68%) rename app/Domains/{Contact/ManageContactAddresses => Vault/ManageAddresses}/Web/Controllers/ContactModuleAddressController.php (98%) rename app/Domains/{Contact/ManageContactAddresses => Vault/ManageAddresses}/Web/Controllers/ContactModuleAddressImageController.php (93%) rename app/Domains/{Contact/ManageContactAddresses => Vault/ManageAddresses}/Web/ViewHelpers/ModuleContactAddressesViewHelper.php (97%) rename tests/Unit/Domains/{Contact/ManageContactAddresses/Services/UpdateContactAddressTest.php => Vault/ManageAddresses/Services/CreateAddressTest.php} (53%) rename tests/Unit/Domains/{Contact/ManageContactAddresses/Services/DestroyContactAddressTest.php => Vault/ManageAddresses/Services/DestroyAddressTest.php} (61%) rename tests/Unit/Domains/{Contact/ManageContactAddresses => Vault/ManageAddresses}/Services/GetGPSCoordinateTest.php (94%) rename tests/Unit/Domains/{Contact/ManageContactAddresses/Services/CreateContactAddressTest.php => Vault/ManageAddresses/Services/UpdateAddressTest.php} (75%) rename tests/Unit/Domains/{Contact/ManageContactAddresses => Vault/ManageAddresses}/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php (98%) diff --git a/app/Domains/Contact/ManageContactAddresses/Services/CreateContactAddress.php b/app/Domains/Contact/ManageContactAddress/Services/CreateContactAddress.php similarity index 65% rename from app/Domains/Contact/ManageContactAddresses/Services/CreateContactAddress.php rename to app/Domains/Contact/ManageContactAddress/Services/CreateContactAddress.php index 4bab92938..c799dfbad 100644 --- a/app/Domains/Contact/ManageContactAddresses/Services/CreateContactAddress.php +++ b/app/Domains/Contact/ManageContactAddress/Services/CreateContactAddress.php @@ -1,8 +1,8 @@ 'required|integer|exists:accounts,id', 'vault_id' => 'required|integer|exists:vaults,id', 'author_id' => 'required|integer|exists:users,id', - 'contact_id' => 'required|integer|exists:contacts,id', 'address_type_id' => 'nullable|integer|exists:address_types,id', 'line_1' => 'nullable|string|max:255', 'line_2' => 'nullable|string|max:255', @@ -35,9 +34,6 @@ public function rules(): array 'country' => 'nullable|string|max:255', 'latitude' => 'nullable|numeric', 'longitude' => 'nullable|numeric', - 'lived_from_at' => 'nullable|date_format:Y-m-d', - 'lived_until_at' => 'nullable|date_format:Y-m-d', - 'is_past_address' => 'nullable|boolean', ]; } @@ -52,12 +48,11 @@ public function permissions(): array 'author_must_belong_to_account', 'vault_must_belong_to_account', 'author_must_be_vault_editor', - 'contact_must_belong_to_vault', ]; } /** - * Create a contact address. + * Create an address. * * @param array $data * @return Address @@ -72,7 +67,7 @@ public function execute(array $data): Address } $this->address = Address::create([ - 'contact_id' => $data['contact_id'], + 'vault_id' => $data['vault_id'], 'address_type_id' => $this->valueOrNull($data, 'address_type_id'), 'line_1' => $this->valueOrNull($data, 'line_1'), 'line_2' => $this->valueOrNull($data, 'line_2'), @@ -82,18 +77,10 @@ public function execute(array $data): Address 'country' => $this->valueOrNull($data, 'country'), 'latitude' => $this->valueOrNull($data, 'latitude'), 'longitude' => $this->valueOrNull($data, 'longitude'), - 'lived_from_at' => $this->valueOrNull($data, 'lived_from_at'), - 'lived_until_at' => $this->valueOrNull($data, 'lived_until_at'), - 'is_past_address' => $this->valueOrFalse($data, 'is_past_address'), ]); - $this->contact->last_updated_at = Carbon::now(); - $this->contact->save(); - $this->geocodeAddress(); - $this->createFeedItem(); - return $this->address; } @@ -101,16 +88,4 @@ private function geocodeAddress(): void { FetchAddressGeocoding::dispatch($this->address)->onQueue('low'); } - - private function createFeedItem(): void - { - $feedItem = ContactFeedItem::create([ - 'author_id' => $this->author->id, - 'contact_id' => $this->contact->id, - 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_CREATED, - 'description' => MapHelper::getAddressAsString($this->address), - ]); - - $this->address->feedItem()->save($feedItem); - } } diff --git a/app/Domains/Contact/ManageContactAddresses/Jobs/FetchAddressGeocoding.php b/app/Domains/Vault/ManageAddresses/Jobs/FetchAddressGeocoding.php similarity index 86% rename from app/Domains/Contact/ManageContactAddresses/Jobs/FetchAddressGeocoding.php rename to app/Domains/Vault/ManageAddresses/Jobs/FetchAddressGeocoding.php index 6e4b27d86..ed74132c6 100644 --- a/app/Domains/Contact/ManageContactAddresses/Jobs/FetchAddressGeocoding.php +++ b/app/Domains/Vault/ManageAddresses/Jobs/FetchAddressGeocoding.php @@ -1,8 +1,8 @@ 'required|integer|exists:accounts,id', + 'vault_id' => 'required|integer|exists:vaults,id', + 'author_id' => 'required|integer|exists:users,id', + 'address_type_id' => 'nullable|integer|exists:address_types,id', + 'line_1' => 'nullable|string|max:255', + 'line_2' => 'nullable|string|max:255', + 'city' => 'nullable|string|max:255', + 'province' => 'nullable|string|max:255', + 'postal_code' => 'nullable|string|max:255', + 'country' => 'nullable|string|max:255', + 'latitude' => 'nullable|numeric', + 'longitude' => 'nullable|numeric', + ]; + } + + /** + * Get the permissions that apply to the user calling the service. + * + * @return array + */ + public function permissions(): array + { + return [ + 'author_must_belong_to_account', + 'vault_must_belong_to_account', + 'author_must_be_vault_editor', + ]; + } + + /** + * Create an address. + * + * @param array $data + * @return Address + */ + public function execute(array $data): Address + { + $this->validateRules($data); + + if ($this->valueOrNull($data, 'address_type_id')) { + $this->account()->addressTypes() + ->findOrFail($data['address_type_id']); + } + + $this->address = Address::create([ + 'vault_id' => $data['vault_id'], + 'address_type_id' => $this->valueOrNull($data, 'address_type_id'), + 'line_1' => $this->valueOrNull($data, 'line_1'), + 'line_2' => $this->valueOrNull($data, 'line_2'), + 'city' => $this->valueOrNull($data, 'city'), + 'province' => $this->valueOrNull($data, 'province'), + 'postal_code' => $this->valueOrNull($data, 'postal_code'), + 'country' => $this->valueOrNull($data, 'country'), + 'latitude' => $this->valueOrNull($data, 'latitude'), + 'longitude' => $this->valueOrNull($data, 'longitude'), + ]); + + $this->geocodeAddress(); + + return $this->address; + } + + private function geocodeAddress(): void + { + FetchAddressGeocoding::dispatch($this->address)->onQueue('low'); + } +} diff --git a/app/Domains/Contact/ManageContactAddresses/Services/DestroyContactAddress.php b/app/Domains/Vault/ManageAddresses/Services/DestroyAddress.php similarity index 60% rename from app/Domains/Contact/ManageContactAddresses/Services/DestroyContactAddress.php rename to app/Domains/Vault/ManageAddresses/Services/DestroyAddress.php index 246f88b45..ef9df5b1e 100644 --- a/app/Domains/Contact/ManageContactAddresses/Services/DestroyContactAddress.php +++ b/app/Domains/Vault/ManageAddresses/Services/DestroyAddress.php @@ -1,6 +1,6 @@ 'required|integer|exists:accounts,id', 'vault_id' => 'required|integer|exists:vaults,id', 'author_id' => 'required|integer|exists:users,id', - 'contact_id' => 'required|integer|exists:contacts,id', 'address_id' => 'required|integer|exists:addresses,id', ]; } @@ -39,13 +38,12 @@ public function permissions(): array return [ 'author_must_belong_to_account', 'vault_must_belong_to_account', - 'contact_must_belong_to_vault', 'author_must_be_vault_editor', ]; } /** - * Delete a contact address. + * Delete an address. * * @param array $data */ @@ -53,24 +51,9 @@ public function execute(array $data): void { $this->validateRules($data); - $this->address = $this->contact->addresses() + $this->address = $this->vault->addresses() ->findOrFail($data['address_id']); $this->address->delete(); - - $this->createFeedItem(); - - $this->contact->last_updated_at = Carbon::now(); - $this->contact->save(); - } - - private function createFeedItem(): void - { - ContactFeedItem::create([ - 'author_id' => $this->author->id, - 'contact_id' => $this->contact->id, - 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_DESTROYED, - 'description' => MapHelper::getAddressAsString($this->address), - ]); } } diff --git a/app/Domains/Contact/ManageContactAddresses/Services/GetGPSCoordinate.php b/app/Domains/Vault/ManageAddresses/Services/GetGPSCoordinate.php similarity index 97% rename from app/Domains/Contact/ManageContactAddresses/Services/GetGPSCoordinate.php rename to app/Domains/Vault/ManageAddresses/Services/GetGPSCoordinate.php index 855b247f8..e15391b61 100644 --- a/app/Domains/Contact/ManageContactAddresses/Services/GetGPSCoordinate.php +++ b/app/Domains/Vault/ManageAddresses/Services/GetGPSCoordinate.php @@ -1,6 +1,6 @@ 'required|integer|exists:accounts,id', 'vault_id' => 'required|integer|exists:vaults,id', 'author_id' => 'required|integer|exists:users,id', - 'contact_id' => 'required|integer|exists:contacts,id', 'address_id' => 'required|integer|exists:addresses,id', 'address_type_id' => 'nullable|integer|exists:address_types,id', 'line_1' => 'nullable|string|max:255', @@ -38,9 +37,6 @@ public function rules(): array 'country' => 'nullable|string|max:255', 'latitude' => 'nullable|numeric', 'longitude' => 'nullable|numeric', - 'lived_from_at' => 'nullable|date_format:Y-m-d', - 'lived_until_at' => 'nullable|date_format:Y-m-d', - 'is_past_address' => 'nullable|boolean', ]; } @@ -54,13 +50,12 @@ public function permissions(): array return [ 'author_must_belong_to_account', 'vault_must_belong_to_account', - 'contact_must_belong_to_vault', 'author_must_be_vault_editor', ]; } /** - * Update a contact address. + * Update an address. * * @param array $data * @return Address @@ -83,7 +78,7 @@ private function validate(): void ->findOrFail($this->data['address_type_id']); } - $this->address = $this->contact->addresses() + $this->address = $this->vault->addresses() ->findOrFail($this->data['address_id']); } @@ -98,33 +93,13 @@ private function update(): void $this->address->country = $this->valueOrNull($this->data, 'country'); $this->address->latitude = $this->valueOrNull($this->data, 'latitude'); $this->address->longitude = $this->valueOrNull($this->data, 'longitude'); - $this->address->lived_from_at = $this->valueOrNull($this->data, 'lived_from_at'); - $this->address->lived_until_at = $this->valueOrNull($this->data, 'lived_until_at'); - $this->address->is_past_address = $this->valueOrFalse($this->data, 'is_past_address'); $this->address->save(); $this->geocodeAddress(); - - $this->createFeedItem(); - - $this->contact->last_updated_at = Carbon::now(); - $this->contact->save(); } private function geocodeAddress(): void { FetchAddressGeocoding::dispatch($this->address)->onQueue('low'); } - - private function createFeedItem(): void - { - $feedItem = ContactFeedItem::create([ - 'author_id' => $this->author->id, - 'contact_id' => $this->contact->id, - 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_UPDATED, - 'description' => MapHelper::getAddressAsString($this->address), - ]); - - $this->address->feedItem()->save($feedItem); - } } diff --git a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php b/app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressController.php similarity index 98% rename from app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php rename to app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressController.php index 2790884fb..318f17050 100644 --- a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php +++ b/app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressController.php @@ -1,6 +1,6 @@ */ protected $fillable = [ - 'contact_id', + 'vault_id', 'address_type_id', 'line_1', 'line_2', @@ -29,28 +29,6 @@ class Address extends Model 'country', 'latitude', 'longitude', - 'lived_from_at', - 'lived_until_at', - 'is_past_address', - ]; - - /** - * The attributes that should be mutated to dates. - * - * @var array - */ - protected $dates = [ - 'lived_from_at', - 'lived_until_at', - ]; - - /** - * The attributes that should be cast to native types. - * - * @var array - */ - protected $casts = [ - 'is_past_address' => 'boolean', ]; /** diff --git a/app/Models/Contact.php b/app/Models/Contact.php index 271172fe9..a1d1d3a79 100644 --- a/app/Models/Contact.php +++ b/app/Models/Contact.php @@ -221,16 +221,6 @@ public function contactInformations(): HasMany return $this->hasMany(ContactInformation::class); } - /** - * Get the address records associated with the contact. - * - * @return HasMany - */ - public function addresses(): HasMany - { - return $this->hasMany(Address::class); - } - /** * Get the note records associated with the contact. * @@ -396,6 +386,16 @@ public function lifeEvents(): BelongsToMany return $this->belongsToMany(LifeEvent::class, 'life_event_participants', 'contact_id', 'life_event_id'); } + /** + * Get the addresses associated with the contact. + * + * @return BelongsToMany + */ + public function addresses(): BelongsToMany + { + return $this->belongsToMany(Address::class, 'contact_address', 'contact_id'); + } + /** * Get the name of the contact, according to the user preference. * diff --git a/app/Models/Vault.php b/app/Models/Vault.php index 7739dd6f6..e3f38f545 100644 --- a/app/Models/Vault.php +++ b/app/Models/Vault.php @@ -241,4 +241,14 @@ public function lifeEvents(): HasMany { return $this->hasMany(LifeEvent::class); } + + /** + * Get the address records associated with the vault. + * + * @return HasMany + */ + public function addresses(): HasMany + { + return $this->hasMany(Address::class); + } } diff --git a/database/factories/AddressFactory.php b/database/factories/AddressFactory.php index b0a82ca3e..f545618e4 100644 --- a/database/factories/AddressFactory.php +++ b/database/factories/AddressFactory.php @@ -5,6 +5,7 @@ use App\Models\Address; use App\Models\AddressType; use App\Models\Contact; +use App\Models\Vault; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -22,7 +23,7 @@ class AddressFactory extends Factory public function definition() { return [ - 'contact_id' => Contact::factory(), + 'vault_id' => Vault::factory(), 'address_type_id' => AddressType::factory(), 'line_1' => $this->faker->streetAddress(), 'line_2' => $this->faker->streetAddress(), diff --git a/database/migrations/2020_04_26_215133_create_addresses_table.php b/database/migrations/2020_04_26_215133_create_addresses_table.php index 9f65a0686..c25e8515b 100644 --- a/database/migrations/2020_04_26_215133_create_addresses_table.php +++ b/database/migrations/2020_04_26_215133_create_addresses_table.php @@ -16,7 +16,6 @@ public function up() $table->uuid('uuid')->nullable(); $table->unsignedBigInteger('vault_id'); $table->unsignedBigInteger('address_type_id')->nullable(); - $table->boolean('is_past_address')->default(false); $table->string('line_1')->nullable(); $table->string('line_2')->nullable(); $table->string('city')->nullable(); @@ -25,12 +24,19 @@ public function up() $table->string('country')->nullable(); $table->double('latitude')->nullable(); $table->double('longitude')->nullable(); - $table->datetime('lived_from_at')->nullable(); - $table->datetime('lived_until_at')->nullable(); $table->timestamps(); $table->foreign('vault_id')->references('id')->on('vaults')->onDelete('cascade'); $table->foreign('address_type_id')->references('id')->on('address_types')->onDelete('set null'); }); + + Schema::create('contact_address', function (Blueprint $table) { + $table->unsignedBigInteger('contact_id'); + $table->unsignedBigInteger('address_id'); + $table->boolean('is_past_address')->default(false); + $table->timestamps(); + $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade'); + $table->foreign('address_id')->references('id')->on('addresses')->onDelete('cascade'); + }); } /** @@ -39,5 +45,6 @@ public function up() public function down() { Schema::dropIfExists('addresses'); + Schema::dropIfExists('contact_address'); } }; diff --git a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/UpdateContactAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php similarity index 53% rename from tests/Unit/Domains/Contact/ManageContactAddresses/Services/UpdateContactAddressTest.php rename to tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php index f38101dc4..8537908d4 100644 --- a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/UpdateContactAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php @@ -1,9 +1,9 @@ createUser(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $type = AddressType::factory()->create(['account_id' => $regis->account_id]); - $address = Address::factory()->create([ - 'address_type_id' => $type->id, - 'contact_id' => $contact->id, - ]); - $this->executeService($regis, $regis->account, $vault, $contact, $type, $address); + $this->executeService($regis, $regis->account, $vault, $type); } /** @test */ @@ -46,7 +41,7 @@ public function it_fails_if_wrong_parameters_are_given(): void ]; $this->expectException(ValidationException::class); - (new UpdateContactAddress())->execute($request); + (new CreateAddress())->execute($request); } /** @test */ @@ -58,32 +53,9 @@ public function it_fails_if_user_doesnt_belong_to_account(): void $account = Account::factory()->create(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); - $type = AddressType::factory()->create(['account_id' => $regis->account_id]); - $address = Address::factory()->create([ - 'address_type_id' => $type->id, - 'contact_id' => $contact->id, - ]); - - $this->executeService($regis, $account, $vault, $contact, $type, $address); - } - - /** @test */ - public function it_fails_if_contact_doesnt_belong_to_vault(): void - { - $this->expectException(ModelNotFoundException::class); - - $regis = $this->createUser(); - $vault = $this->createVault($regis->account); - $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(); $type = AddressType::factory()->create(['account_id' => $regis->account_id]); - $address = Address::factory()->create([ - 'address_type_id' => $type->id, - 'contact_id' => $contact->id, - ]); - $this->executeService($regis, $regis->account, $vault, $contact, $type, $address); + $this->executeService($regis, $account, $vault, $type); } /** @test */ @@ -94,14 +66,9 @@ public function it_fails_if_user_doesnt_have_right_permission_in_initial_vault() $regis = $this->createUser(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_VIEW, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $type = AddressType::factory()->create(['account_id' => $regis->account_id]); - $address = Address::factory()->create([ - 'address_type_id' => $type->id, - 'contact_id' => $contact->id, - ]); - $this->executeService($regis, $regis->account, $vault, $contact, $type, $address); + $this->executeService($regis, $regis->account, $vault, $type); } /** @test */ @@ -112,32 +79,12 @@ public function it_fails_if_type_is_not_in_the_account(): void $regis = $this->createUser(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $type = AddressType::factory()->create(); - $address = Address::factory()->create([ - 'address_type_id' => $type->id, - 'contact_id' => $contact->id, - ]); - $this->executeService($regis, $regis->account, $vault, $contact, $type, $address); + $this->executeService($regis, $regis->account, $vault, $type); } - /** @test */ - public function it_fails_if_address_does_not_exist(): void - { - $this->expectException(ModelNotFoundException::class); - - $regis = $this->createUser(); - $vault = $this->createVault($regis->account); - $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); - $type = AddressType::factory()->create(['account_id' => $regis->account_id]); - $address = Address::factory()->create(); - - $this->executeService($regis, $regis->account, $vault, $contact, $type, $address); - } - - private function executeService(User $author, Account $account, Vault $vault, Contact $contact, AddressType $type, Address $address): void + private function executeService(User $author, Account $account, Vault $vault, AddressType $type): void { Queue::fake(); @@ -145,9 +92,7 @@ private function executeService(User $author, Account $account, Vault $vault, Co 'account_id' => $account->id, 'vault_id' => $vault->id, 'author_id' => $author->id, - 'contact_id' => $contact->id, 'address_type_id' => $type->id, - 'address_id' => $address->id, 'line_1' => '25 grand rue', 'line_2' => 'Apartment 3', 'city' => 'paris', @@ -158,10 +103,10 @@ private function executeService(User $author, Account $account, Vault $vault, Co 'longitude' => 12345, ]; - $address = (new UpdateContactAddress())->execute($request); + $address = (new CreateAddress())->execute($request); $this->assertDatabaseHas('addresses', [ - 'contact_id' => $contact->id, + 'vault_id' => $vault->id, 'address_type_id' => $type->id, 'line_1' => '25 grand rue', 'line_2' => 'Apartment 3', @@ -181,10 +126,5 @@ private function executeService(User $author, Account $account, Vault $vault, Co Queue::assertPushed(FetchAddressGeocoding::class, function ($job) use ($address) { return $job->address->id === $address->id; }); - - $this->assertDatabaseHas('contact_feed_items', [ - 'contact_id' => $contact->id, - 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_UPDATED, - ]); } } diff --git a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/DestroyContactAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php similarity index 61% rename from tests/Unit/Domains/Contact/ManageContactAddresses/Services/DestroyContactAddressTest.php rename to tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php index 99d7c224b..462751f37 100644 --- a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/DestroyContactAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php @@ -1,8 +1,8 @@ createUser(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $address = Address::factory()->create([ - 'contact_id' => $contact->id, + 'vault_id' => $vault->id, ]); - $this->executeService($regis, $regis->account, $vault, $contact, $address); + $this->executeService($regis, $regis->account, $vault, $address); } /** @test */ @@ -41,7 +40,7 @@ public function it_fails_if_wrong_parameters_are_given(): void ]; $this->expectException(ValidationException::class); - (new DestroyContactAddress())->execute($request); + (new DestroyAddress())->execute($request); } /** @test */ @@ -53,28 +52,11 @@ public function it_fails_if_user_doesnt_belong_to_account(): void $account = Account::factory()->create(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $address = Address::factory()->create([ - 'contact_id' => $contact->id, - ]); - - $this->executeService($regis, $account, $vault, $contact, $address); - } - - /** @test */ - public function it_fails_if_contact_doesnt_belong_to_vault(): void - { - $this->expectException(ModelNotFoundException::class); - - $regis = $this->createUser(); - $vault = $this->createVault($regis->account); - $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(); - $address = Address::factory()->create([ - 'contact_id' => $contact->id, + 'vault_id' => $vault->id, ]); - $this->executeService($regis, $regis->account, $vault, $contact, $address); + $this->executeService($regis, $account, $vault, $address); } /** @test */ @@ -85,12 +67,11 @@ public function it_fails_if_user_doesnt_have_right_permission_in_initial_vault() $regis = $this->createUser(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_VIEW, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $address = Address::factory()->create([ - 'contact_id' => $contact->id, + 'vault_id' => $vault->id, ]); - $this->executeService($regis, $regis->account, $vault, $contact, $address); + $this->executeService($regis, $regis->account, $vault, $address); } /** @test */ @@ -101,31 +82,24 @@ public function it_fails_if_address_does_not_exist(): void $regis = $this->createUser(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $address = Address::factory()->create(); - $this->executeService($regis, $regis->account, $vault, $contact, $address); + $this->executeService($regis, $regis->account, $vault, $address); } - private function executeService(User $author, Account $account, Vault $vault, Contact $contact, Address $address): void + private function executeService(User $author, Account $account, Vault $vault, Address $address): void { $request = [ 'account_id' => $account->id, 'vault_id' => $vault->id, 'author_id' => $author->id, - 'contact_id' => $contact->id, 'address_id' => $address->id, ]; - (new DestroyContactAddress())->execute($request); + (new DestroyAddress())->execute($request); $this->assertDatabaseMissing('addresses', [ 'id' => $address->id, ]); - - $this->assertDatabaseHas('contact_feed_items', [ - 'contact_id' => $contact->id, - 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_DESTROYED, - ]); } } diff --git a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/GetGPSCoordinateTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/GetGPSCoordinateTest.php similarity index 94% rename from tests/Unit/Domains/Contact/ManageContactAddresses/Services/GetGPSCoordinateTest.php rename to tests/Unit/Domains/Vault/ManageAddresses/Services/GetGPSCoordinateTest.php index d645e6ed9..9b362371c 100644 --- a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/GetGPSCoordinateTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/GetGPSCoordinateTest.php @@ -1,8 +1,8 @@ $address->id, ]; - (new GetGPSCoordinate)->execute($request); + (new GetGPSCoordinate())->execute($request); } /** @test */ diff --git a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/CreateContactAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php similarity index 75% rename from tests/Unit/Domains/Contact/ManageContactAddresses/Services/CreateContactAddressTest.php rename to tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php index 60662b2b4..8b37e8213 100644 --- a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/CreateContactAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php @@ -1,9 +1,9 @@ createUser(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $type = AddressType::factory()->create(['account_id' => $regis->account_id]); + $address = Address::factory()->create([ + 'address_type_id' => $type->id, + 'vault_id' => $vault->id, + ]); - $this->executeService($regis, $regis->account, $vault, $contact, $type); + $this->executeService($regis, $regis->account, $vault, $type, $address); } /** @test */ @@ -42,7 +45,7 @@ public function it_fails_if_wrong_parameters_are_given(): void ]; $this->expectException(ValidationException::class); - (new CreateContactAddress())->execute($request); + (new UpdateAddress())->execute($request); } /** @test */ @@ -54,55 +57,64 @@ public function it_fails_if_user_doesnt_belong_to_account(): void $account = Account::factory()->create(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); $type = AddressType::factory()->create(['account_id' => $regis->account_id]); + $address = Address::factory()->create([ + 'address_type_id' => $type->id, + 'vault_id' => $vault->id, + ]); - $this->executeService($regis, $account, $vault, $contact, $type); + $this->executeService($regis, $account, $vault, $type, $address); } /** @test */ - public function it_fails_if_contact_doesnt_belong_to_vault(): void + public function it_fails_if_user_doesnt_have_right_permission_in_initial_vault(): void { - $this->expectException(ModelNotFoundException::class); + $this->expectException(NotEnoughPermissionException::class); $regis = $this->createUser(); $vault = $this->createVault($regis->account); - $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_VIEW, $vault); $type = AddressType::factory()->create(['account_id' => $regis->account_id]); + $address = Address::factory()->create([ + 'address_type_id' => $type->id, + 'vault_id' => $vault->id, + ]); - $this->executeService($regis, $regis->account, $vault, $contact, $type); + $this->executeService($regis, $regis->account, $vault, $type, $address); } /** @test */ - public function it_fails_if_user_doesnt_have_right_permission_in_initial_vault(): void + public function it_fails_if_type_is_not_in_the_account(): void { - $this->expectException(NotEnoughPermissionException::class); + $this->expectException(ModelNotFoundException::class); $regis = $this->createUser(); $vault = $this->createVault($regis->account); - $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_VIEW, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); - $type = AddressType::factory()->create(['account_id' => $regis->account_id]); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $type = AddressType::factory()->create(); + $address = Address::factory()->create([ + 'address_type_id' => $type->id, + 'vault_id' => $vault->id, + ]); - $this->executeService($regis, $regis->account, $vault, $contact, $type); + $this->executeService($regis, $regis->account, $vault, $type, $address); } /** @test */ - public function it_fails_if_type_is_not_in_the_account(): void + public function it_fails_if_address_does_not_exist(): void { $this->expectException(ModelNotFoundException::class); $regis = $this->createUser(); $vault = $this->createVault($regis->account); $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); - $contact = Contact::factory()->create(['vault_id' => $vault->id]); - $type = AddressType::factory()->create(); + $type = AddressType::factory()->create(['account_id' => $regis->account_id]); + $address = Address::factory()->create(); - $this->executeService($regis, $regis->account, $vault, $contact, $type); + $this->executeService($regis, $regis->account, $vault, $type, $address); } - private function executeService(User $author, Account $account, Vault $vault, Contact $contact, AddressType $type): void + private function executeService(User $author, Account $account, Vault $vault, AddressType $type, Address $address): void { Queue::fake(); @@ -110,8 +122,8 @@ private function executeService(User $author, Account $account, Vault $vault, Co 'account_id' => $account->id, 'vault_id' => $vault->id, 'author_id' => $author->id, - 'contact_id' => $contact->id, 'address_type_id' => $type->id, + 'address_id' => $address->id, 'line_1' => '25 grand rue', 'line_2' => 'Apartment 3', 'city' => 'paris', @@ -122,10 +134,10 @@ private function executeService(User $author, Account $account, Vault $vault, Co 'longitude' => 12345, ]; - $address = (new CreateContactAddress())->execute($request); + $address = (new UpdateAddress())->execute($request); $this->assertDatabaseHas('addresses', [ - 'contact_id' => $contact->id, + 'vault_id' => $vault->id, 'address_type_id' => $type->id, 'line_1' => '25 grand rue', 'line_2' => 'Apartment 3', @@ -145,10 +157,5 @@ private function executeService(User $author, Account $account, Vault $vault, Co Queue::assertPushed(FetchAddressGeocoding::class, function ($job) use ($address) { return $job->address->id === $address->id; }); - - $this->assertDatabaseHas('contact_feed_items', [ - 'contact_id' => $contact->id, - 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_CREATED, - ]); } } diff --git a/tests/Unit/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php similarity index 98% rename from tests/Unit/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php rename to tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php index eb3e44913..7208a1a48 100644 --- a/tests/Unit/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php @@ -1,15 +1,15 @@ assertTrue($contact->contactInformations()->exists()); } - /** @test */ - public function it_has_many_addresses(): void - { - $contact = Contact::factory()->create(); - Address::factory()->count(2)->create([ - 'contact_id' => $contact->id, - ]); - - $this->assertTrue($contact->addresses()->exists()); - } - /** @test */ public function it_has_many_notes(): void { @@ -293,6 +282,17 @@ public function it_has_many_life_events(): void $this->assertTrue($contact->lifeEvents()->exists()); } + /** @test */ + public function it_has_many_addresses(): void + { + $contact = Contact::factory()->create([]); + $address = Address::factory()->create(); + + $contact->addresses()->sync([$contact->id => ['is_past_address' => false]]); + + $this->assertTrue($contact->addresses()->exists()); + } + /** @test */ public function it_gets_the_name(): void { diff --git a/tests/Unit/Models/VaultTest.php b/tests/Unit/Models/VaultTest.php index 21bb725f5..6fb92507b 100644 --- a/tests/Unit/Models/VaultTest.php +++ b/tests/Unit/Models/VaultTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit\Models; +use App\Models\Address; use App\Models\Company; use App\Models\Contact; use App\Models\ContactImportantDateType; @@ -177,4 +178,15 @@ public function it_has_many_life_events(): void $this->assertTrue($vault->lifeEvents()->exists()); } + + /** @test */ + public function it_has_many_addresses(): void + { + $vault = Vault::factory()->create(); + Address::factory()->count(2)->create([ + 'vault_id' => $vault->id, + ]); + + $this->assertTrue($vault->addresses()->exists()); + } } From c181123648d48171974f7a2f05ceb8f8f991c399 Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Fri, 13 Jan 2023 20:50:31 -0500 Subject: [PATCH 03/10] wip --- .../Services/CreateContactAddress.php | 91 ----------- .../Services/AssociateAddressToContact.php | 94 +++++++++++ .../Services/RemoveAddressFromContact.php | 91 +++++++++++ .../Jobs/FetchAddressGeocoding.php | 2 +- .../AssociateAddressToContactTest.php | 150 ++++++++++++++++++ .../Services/RemoveAddressFromContactTest.php | 150 ++++++++++++++++++ .../Services/CreateAddressTest.php | 6 +- 7 files changed, 489 insertions(+), 95 deletions(-) delete mode 100644 app/Domains/Contact/ManageContactAddress/Services/CreateContactAddress.php create mode 100644 app/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContact.php create mode 100644 app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php create mode 100644 tests/Unit/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContactTest.php create mode 100644 tests/Unit/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContactTest.php diff --git a/app/Domains/Contact/ManageContactAddress/Services/CreateContactAddress.php b/app/Domains/Contact/ManageContactAddress/Services/CreateContactAddress.php deleted file mode 100644 index c799dfbad..000000000 --- a/app/Domains/Contact/ManageContactAddress/Services/CreateContactAddress.php +++ /dev/null @@ -1,91 +0,0 @@ - 'required|integer|exists:accounts,id', - 'vault_id' => 'required|integer|exists:vaults,id', - 'author_id' => 'required|integer|exists:users,id', - 'address_type_id' => 'nullable|integer|exists:address_types,id', - 'line_1' => 'nullable|string|max:255', - 'line_2' => 'nullable|string|max:255', - 'city' => 'nullable|string|max:255', - 'province' => 'nullable|string|max:255', - 'postal_code' => 'nullable|string|max:255', - 'country' => 'nullable|string|max:255', - 'latitude' => 'nullable|numeric', - 'longitude' => 'nullable|numeric', - ]; - } - - /** - * Get the permissions that apply to the user calling the service. - * - * @return array - */ - public function permissions(): array - { - return [ - 'author_must_belong_to_account', - 'vault_must_belong_to_account', - 'author_must_be_vault_editor', - ]; - } - - /** - * Create an address. - * - * @param array $data - * @return Address - */ - public function execute(array $data): Address - { - $this->validateRules($data); - - if ($this->valueOrNull($data, 'address_type_id')) { - $this->account()->addressTypes() - ->findOrFail($data['address_type_id']); - } - - $this->address = Address::create([ - 'vault_id' => $data['vault_id'], - 'address_type_id' => $this->valueOrNull($data, 'address_type_id'), - 'line_1' => $this->valueOrNull($data, 'line_1'), - 'line_2' => $this->valueOrNull($data, 'line_2'), - 'city' => $this->valueOrNull($data, 'city'), - 'province' => $this->valueOrNull($data, 'province'), - 'postal_code' => $this->valueOrNull($data, 'postal_code'), - 'country' => $this->valueOrNull($data, 'country'), - 'latitude' => $this->valueOrNull($data, 'latitude'), - 'longitude' => $this->valueOrNull($data, 'longitude'), - ]); - - $this->geocodeAddress(); - - return $this->address; - } - - private function geocodeAddress(): void - { - FetchAddressGeocoding::dispatch($this->address)->onQueue('low'); - } -} diff --git a/app/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContact.php b/app/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContact.php new file mode 100644 index 000000000..8e281858f --- /dev/null +++ b/app/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContact.php @@ -0,0 +1,94 @@ + 'required|integer|exists:accounts,id', + 'vault_id' => 'required|integer|exists:vaults,id', + 'author_id' => 'required|integer|exists:users,id', + 'contact_id' => 'required|integer|exists:contacts,id', + 'address_id' => 'required|integer|exists:addresses,id', + 'is_past_address' => 'required|boolean', + ]; + } + + /** + * Get the permissions that apply to the user calling the service. + * + * @return array + */ + public function permissions(): array + { + return [ + 'author_must_belong_to_account', + 'vault_must_belong_to_account', + 'author_must_be_vault_editor', + 'contact_must_belong_to_vault', + ]; + } + + /** + * Associate an address to a contact. + * + * @param array $data + * @return Address + */ + public function execute(array $data): Address + { + $this->data = $data; + $this->validate(); + $this->match(); + $this->createFeedItem(); + + return $this->address; + } + + private function validate(): void + { + $this->validateRules($this->data); + + $this->address = $this->vault->addresses()->findOrFail($this->data['address_id']); + } + + private function match(): void + { + $this->contact->addresses()->attach($this->address, + ['is_past_address' => $this->data['is_past_address']] + ); + } + + private function createFeedItem(): void + { + $this->contact->last_updated_at = Carbon::now(); + $this->contact->save(); + + $feedItem = ContactFeedItem::create([ + 'author_id' => $this->author->id, + 'contact_id' => $this->contact->id, + 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_CREATED, + 'description' => MapHelper::getAddressAsString($this->address), + ]); + + $this->address->feedItem()->save($feedItem); + } +} diff --git a/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php b/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php new file mode 100644 index 000000000..2b23e41c6 --- /dev/null +++ b/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php @@ -0,0 +1,91 @@ + 'required|integer|exists:accounts,id', + 'vault_id' => 'required|integer|exists:vaults,id', + 'author_id' => 'required|integer|exists:users,id', + 'contact_id' => 'required|integer|exists:contacts,id', + 'address_id' => 'required|integer|exists:addresses,id', + ]; + } + + /** + * Get the permissions that apply to the user calling the service. + * + * @return array + */ + public function permissions(): array + { + return [ + 'author_must_belong_to_account', + 'vault_must_belong_to_account', + 'author_must_be_vault_editor', + 'contact_must_belong_to_vault', + ]; + } + + /** + * Remove an address from a contact. + * + * @param array $data + * @return Address + */ + public function execute(array $data): Address + { + $this->data = $data; + $this->validate(); + $this->remove(); + $this->createFeedItem(); + + return $this->address; + } + + private function validate(): void + { + $this->validateRules($this->data); + + $this->address = $this->vault->addresses()->findOrFail($this->data['address_id']); + } + + private function remove(): void + { + $this->contact->addresses()->detach($this->address); + } + + private function createFeedItem(): void + { + $this->contact->last_updated_at = Carbon::now(); + $this->contact->save(); + + $feedItem = ContactFeedItem::create([ + 'author_id' => $this->author->id, + 'contact_id' => $this->contact->id, + 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_DESTROYED, + 'description' => MapHelper::getAddressAsString($this->address), + ]); + + $this->address->feedItem()->save($feedItem); + } +} diff --git a/app/Domains/Vault/ManageAddresses/Jobs/FetchAddressGeocoding.php b/app/Domains/Vault/ManageAddresses/Jobs/FetchAddressGeocoding.php index ed74132c6..6f754ba19 100644 --- a/app/Domains/Vault/ManageAddresses/Jobs/FetchAddressGeocoding.php +++ b/app/Domains/Vault/ManageAddresses/Jobs/FetchAddressGeocoding.php @@ -2,7 +2,7 @@ namespace App\Domains\Vault\ManageAddresses\Jobs; -use App\Domains\Contact\ManageContactAddress\Services\GetGPSCoordinate; +use App\Domains\Vault\ManageAddresses\Services\GetGPSCoordinate; use App\Models\Address; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; diff --git a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContactTest.php b/tests/Unit/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContactTest.php new file mode 100644 index 000000000..6adca30d5 --- /dev/null +++ b/tests/Unit/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContactTest.php @@ -0,0 +1,150 @@ +createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $contact = Contact::factory()->create([ + 'vault_id' => $vault->id, + ]); + $address = Address::factory()->create([ + 'vault_id' => $vault->id, + ]); + + $this->executeService($regis, $regis->account, $vault, $contact, $address); + } + + /** @test */ + public function it_fails_if_wrong_parameters_are_given(): void + { + $request = [ + 'title' => 'Ross', + ]; + + $this->expectException(ValidationException::class); + (new AssociateAddressToContact())->execute($request); + } + + /** @test */ + public function it_fails_if_user_doesnt_belong_to_account(): void + { + $this->expectException(ModelNotFoundException::class); + + $regis = $this->createUser(); + $account = Account::factory()->create(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $contact = Contact::factory()->create([ + 'vault_id' => $vault->id, + ]); + $address = Address::factory()->create([ + 'vault_id' => $vault->id, + ]); + + $this->executeService($regis, $account, $vault, $contact, $address); + } + + /** @test */ + public function it_fails_if_user_doesnt_have_right_permission_in_initial_vault(): void + { + $this->expectException(NotEnoughPermissionException::class); + + $regis = $this->createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_VIEW, $vault); + $contact = Contact::factory()->create([ + 'vault_id' => $vault->id, + ]); + $address = Address::factory()->create([ + 'vault_id' => $vault->id, + ]); + + $this->executeService($regis, $regis->account, $vault, $contact, $address); + } + + /** @test */ + public function it_fails_if_contact_is_not_part_of_the_vault(): void + { + $this->expectException(ModelNotFoundException::class); + + $regis = $this->createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $contact = Contact::factory()->create(); + $address = Address::factory()->create([ + 'vault_id' => $vault->id, + ]); + + $this->executeService($regis, $regis->account, $vault, $contact, $address); + } + + /** @test */ + public function it_fails_if_address_does_not_exist(): void + { + $this->expectException(ModelNotFoundException::class); + + $regis = $this->createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $contact = Contact::factory()->create([ + 'vault_id' => $vault->id, + ]); + $address = Address::factory()->create(); + + $this->executeService($regis, $regis->account, $vault, $contact, $address); + } + + private function executeService(User $author, Account $account, Vault $vault, Contact $contact, Address $address): void + { + Queue::fake(); + + $request = [ + 'account_id' => $account->id, + 'vault_id' => $vault->id, + 'author_id' => $author->id, + 'contact_id' => $contact->id, + 'address_id' => $address->id, + 'is_past_address' => false, + ]; + + $address = (new AssociateAddressToContact())->execute($request); + + $this->assertDatabaseHas('contact_address', [ + 'contact_id' => $contact->id, + 'address_id' => $address->id, + 'is_past_address' => false, + ]); + + $this->assertInstanceOf( + Address::class, + $address + ); + + $this->assertDatabaseHas('contact_feed_items', [ + 'contact_id' => $contact->id, + 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_CREATED, + ]); + } +} diff --git a/tests/Unit/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContactTest.php b/tests/Unit/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContactTest.php new file mode 100644 index 000000000..64ed6accf --- /dev/null +++ b/tests/Unit/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContactTest.php @@ -0,0 +1,150 @@ +createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $contact = Contact::factory()->create([ + 'vault_id' => $vault->id, + ]); + $address = Address::factory()->create([ + 'vault_id' => $vault->id, + ]); + + $this->executeService($regis, $regis->account, $vault, $contact, $address); + } + + /** @test */ + public function it_fails_if_wrong_parameters_are_given(): void + { + $request = [ + 'title' => 'Ross', + ]; + + $this->expectException(ValidationException::class); + (new RemoveAddressFromContact())->execute($request); + } + + /** @test */ + public function it_fails_if_user_doesnt_belong_to_account(): void + { + $this->expectException(ModelNotFoundException::class); + + $regis = $this->createUser(); + $account = Account::factory()->create(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $contact = Contact::factory()->create([ + 'vault_id' => $vault->id, + ]); + $address = Address::factory()->create([ + 'vault_id' => $vault->id, + ]); + + $this->executeService($regis, $account, $vault, $contact, $address); + } + + /** @test */ + public function it_fails_if_user_doesnt_have_right_permission_in_initial_vault(): void + { + $this->expectException(NotEnoughPermissionException::class); + + $regis = $this->createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_VIEW, $vault); + $contact = Contact::factory()->create([ + 'vault_id' => $vault->id, + ]); + $address = Address::factory()->create([ + 'vault_id' => $vault->id, + ]); + + $this->executeService($regis, $regis->account, $vault, $contact, $address); + } + + /** @test */ + public function it_fails_if_contact_is_not_part_of_the_vault(): void + { + $this->expectException(ModelNotFoundException::class); + + $regis = $this->createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $contact = Contact::factory()->create(); + $address = Address::factory()->create([ + 'vault_id' => $vault->id, + ]); + + $this->executeService($regis, $regis->account, $vault, $contact, $address); + } + + /** @test */ + public function it_fails_if_address_does_not_exist(): void + { + $this->expectException(ModelNotFoundException::class); + + $regis = $this->createUser(); + $vault = $this->createVault($regis->account); + $vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); + $contact = Contact::factory()->create([ + 'vault_id' => $vault->id, + ]); + $address = Address::factory()->create(); + + $this->executeService($regis, $regis->account, $vault, $contact, $address); + } + + private function executeService(User $author, Account $account, Vault $vault, Contact $contact, Address $address): void + { + Queue::fake(); + + $contact->addresses()->attach($address); + + $request = [ + 'account_id' => $account->id, + 'vault_id' => $vault->id, + 'author_id' => $author->id, + 'contact_id' => $contact->id, + 'address_id' => $address->id, + ]; + + $address = (new RemoveAddressFromContact())->execute($request); + + $this->assertDatabaseMissing('contact_address', [ + 'contact_id' => $contact->id, + 'address_id' => $address->id, + ]); + + $this->assertInstanceOf( + Address::class, + $address + ); + + $this->assertDatabaseHas('contact_feed_items', [ + 'contact_id' => $contact->id, + 'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_DESTROYED, + ]); + } +} diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php index 8537908d4..b33699153 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php @@ -3,7 +3,7 @@ namespace Tests\Unit\Domains\Vault\ManageAddresses\Services; use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; -use App\Domains\Contact\ManageContactAddress\Services\CreateAddress; +use App\Domains\Contact\ManageContactAddress\Services\AssociateAddressToContact; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; @@ -41,7 +41,7 @@ public function it_fails_if_wrong_parameters_are_given(): void ]; $this->expectException(ValidationException::class); - (new CreateAddress())->execute($request); + (new AssociateAddressToContact())->execute($request); } /** @test */ @@ -103,7 +103,7 @@ private function executeService(User $author, Account $account, Vault $vault, Ad 'longitude' => 12345, ]; - $address = (new CreateAddress())->execute($request); + $address = (new AssociateAddressToContact())->execute($request); $this->assertDatabaseHas('addresses', [ 'vault_id' => $vault->id, From c707b6c6a2613627bb892dcc08941f7d91dfd17e Mon Sep 17 00:00:00 2001 From: djaiss Date: Sat, 14 Jan 2023 01:53:38 +0000 Subject: [PATCH 04/10] chore: php linting with pint --- .../Services/AssociateAddressToContact.php | 2 +- .../Services/RemoveAddressFromContact.php | 2 +- app/Domains/Vault/ManageAddresses/Services/CreateAddress.php | 3 --- app/Domains/Vault/ManageAddresses/Services/DestroyAddress.php | 3 --- app/Domains/Vault/ManageAddresses/Services/UpdateAddress.php | 3 --- database/factories/AddressFactory.php | 1 - .../Vault/ManageAddresses/Services/CreateAddressTest.php | 4 +--- .../Vault/ManageAddresses/Services/DestroyAddressTest.php | 2 -- .../Vault/ManageAddresses/Services/UpdateAddressTest.php | 4 +--- .../Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php | 2 +- 10 files changed, 5 insertions(+), 21 deletions(-) diff --git a/app/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContact.php b/app/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContact.php index 8e281858f..47a6c7a95 100644 --- a/app/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContact.php +++ b/app/Domains/Contact/ManageContactAddresses/Services/AssociateAddressToContact.php @@ -2,7 +2,6 @@ namespace App\Domains\Contact\ManageContactAddresses\Services; -use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; use App\Helpers\MapHelper; use App\Interfaces\ServiceInterface; use App\Models\Address; @@ -13,6 +12,7 @@ class AssociateAddressToContact extends BaseService implements ServiceInterface { private Address $address; + private array $data; /** diff --git a/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php b/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php index 2b23e41c6..176fa9b6f 100644 --- a/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php +++ b/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php @@ -2,7 +2,6 @@ namespace App\Domains\Contact\ManageContactAddresses\Services; -use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; use App\Helpers\MapHelper; use App\Interfaces\ServiceInterface; use App\Models\Address; @@ -13,6 +12,7 @@ class RemoveAddressFromContact extends BaseService implements ServiceInterface { private Address $address; + private array $data; /** diff --git a/app/Domains/Vault/ManageAddresses/Services/CreateAddress.php b/app/Domains/Vault/ManageAddresses/Services/CreateAddress.php index a0657a326..2a9610a0a 100644 --- a/app/Domains/Vault/ManageAddresses/Services/CreateAddress.php +++ b/app/Domains/Vault/ManageAddresses/Services/CreateAddress.php @@ -3,12 +3,9 @@ namespace App\Domains\Vault\ManageAddresses\Services; use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; -use App\Helpers\MapHelper; use App\Interfaces\ServiceInterface; use App\Models\Address; -use App\Models\ContactFeedItem; use App\Services\BaseService; -use Carbon\Carbon; class CreateAddress extends BaseService implements ServiceInterface { diff --git a/app/Domains/Vault/ManageAddresses/Services/DestroyAddress.php b/app/Domains/Vault/ManageAddresses/Services/DestroyAddress.php index ef9df5b1e..019e4dfc0 100644 --- a/app/Domains/Vault/ManageAddresses/Services/DestroyAddress.php +++ b/app/Domains/Vault/ManageAddresses/Services/DestroyAddress.php @@ -2,12 +2,9 @@ namespace App\Domains\Vault\ManageAddresses\Services; -use App\Helpers\MapHelper; use App\Interfaces\ServiceInterface; use App\Models\Address; -use App\Models\ContactFeedItem; use App\Services\BaseService; -use Carbon\Carbon; class DestroyAddress extends BaseService implements ServiceInterface { diff --git a/app/Domains/Vault/ManageAddresses/Services/UpdateAddress.php b/app/Domains/Vault/ManageAddresses/Services/UpdateAddress.php index b023edb86..432538ee2 100644 --- a/app/Domains/Vault/ManageAddresses/Services/UpdateAddress.php +++ b/app/Domains/Vault/ManageAddresses/Services/UpdateAddress.php @@ -3,12 +3,9 @@ namespace App\Domains\Vault\ManageAddresses\Services; use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; -use App\Helpers\MapHelper; use App\Interfaces\ServiceInterface; use App\Models\Address; -use App\Models\ContactFeedItem; use App\Services\BaseService; -use Carbon\Carbon; class UpdateAddress extends BaseService implements ServiceInterface { diff --git a/database/factories/AddressFactory.php b/database/factories/AddressFactory.php index f545618e4..5041ba307 100644 --- a/database/factories/AddressFactory.php +++ b/database/factories/AddressFactory.php @@ -4,7 +4,6 @@ use App\Models\Address; use App\Models\AddressType; -use App\Models\Contact; use App\Models\Vault; use Illuminate\Database\Eloquent\Factories\Factory; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php index b33699153..4ed7799e3 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php @@ -2,14 +2,12 @@ namespace Tests\Unit\Domains\Vault\ManageAddresses\Services; -use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; use App\Domains\Contact\ManageContactAddress\Services\AssociateAddressToContact; +use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; use App\Models\AddressType; -use App\Models\Contact; -use App\Models\ContactFeedItem; use App\Models\User; use App\Models\Vault; use Illuminate\Database\Eloquent\ModelNotFoundException; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php index 462751f37..879ce7dc5 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php @@ -6,8 +6,6 @@ use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; -use App\Models\Contact; -use App\Models\ContactFeedItem; use App\Models\User; use App\Models\Vault; use Illuminate\Database\Eloquent\ModelNotFoundException; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php index 8b37e8213..690f77679 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php @@ -2,14 +2,12 @@ namespace Tests\Unit\Domains\Vault\ManageAddresses\Services; -use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; use App\Domains\Contact\ManageContactAddress\Services\UpdateAddress; +use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; use App\Models\AddressType; -use App\Models\Contact; -use App\Models\ContactFeedItem; use App\Models\User; use App\Models\Vault; use Illuminate\Database\Eloquent\ModelNotFoundException; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php index 7208a1a48..63d0ff138 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php @@ -7,9 +7,9 @@ use App\Models\AddressType; use App\Models\Contact; use App\Models\User; +use function env; use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\TestCase; -use function env; class ModuleContactAddressesViewHelperTest extends TestCase { From 321b3ab3fdf58f0fc685d6affef71e95bfd8f5ac Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Fri, 13 Jan 2023 21:13:35 -0500 Subject: [PATCH 05/10] wip --- .../ContactModuleAddressController.php | 34 +++++++++++-------- .../ContactModuleAddressImageController.php | 2 +- .../ModuleContactAddressesViewHelper.php | 2 +- app/Models/Address.php | 9 ++--- ...20_04_26_215133_create_addresses_table.php | 1 - .../Actions/ActionFeedAddressTest.php | 1 - .../Services/CreateAddressTest.php | 6 ++-- .../Services/DestroyAddressTest.php | 2 +- .../Services/GetGPSCoordinateTest.php | 2 +- .../Services/UpdateAddressTest.php | 2 +- .../ModuleContactAddressesViewHelperTest.php | 13 +++---- tests/Unit/Models/AddressTest.php | 7 ++-- 12 files changed, 41 insertions(+), 40 deletions(-) rename app/Domains/{Vault/ManageAddresses => Contact/ManageContactAddresses}/Web/Controllers/ContactModuleAddressController.php (76%) rename app/Domains/{Vault/ManageAddresses => Contact/ManageContactAddresses}/Web/Controllers/ContactModuleAddressImageController.php (93%) rename app/Domains/{Vault/ManageAddresses => Contact/ManageContactAddresses}/Web/ViewHelpers/ModuleContactAddressesViewHelper.php (97%) diff --git a/app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressController.php b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php similarity index 76% rename from app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressController.php rename to app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php index 318f17050..924530adc 100644 --- a/app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressController.php +++ b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php @@ -1,11 +1,13 @@ Auth::user()->account_id, 'author_id' => Auth::id(), 'vault_id' => $vaultId, - 'contact_id' => $contactId, 'address_type_id' => $request->input('address_type_id') == 0 ? null : $request->input('address_type_id'), 'line_1' => $request->input('line_1'), 'line_2' => $request->input('line_2'), @@ -27,14 +28,21 @@ public function store(Request $request, int $vaultId, int $contactId) 'province' => $request->input('province'), 'postal_code' => $request->input('postal_code'), 'country' => $request->input('country'), - 'is_past_address' => $request->input('is_past_address'), 'latitude' => null, 'longitude' => null, - 'lived_from_at' => null, - 'lived_until_at' => null, ]; - $address = (new CreateContactAddress())->execute($data); + $address = (new CreateAddress())->execute($data); + + (new AssociateAddressToContact())->execute([ + 'account_id' => Auth::user()->account_id, + 'vault_id' => $vaultId, + 'author_id' => Auth::id(), + 'contact_id' => $contactId, + 'address_id' => $address->id, + 'is_past_address' => $request->input('is_past_address'), + ]); + $contact = Contact::find($contactId); return response()->json([ @@ -48,7 +56,6 @@ public function update(Request $request, int $vaultId, int $contactId, int $addr 'account_id' => Auth::user()->account_id, 'author_id' => Auth::id(), 'vault_id' => $vaultId, - 'contact_id' => $contactId, 'address_id' => $addressId, 'address_type_id' => $request->input('address_type_id') == 0 ? null : $request->input('address_type_id'), 'line_1' => $request->input('line_1'), @@ -57,14 +64,11 @@ public function update(Request $request, int $vaultId, int $contactId, int $addr 'province' => $request->input('province'), 'postal_code' => $request->input('postal_code'), 'country' => $request->input('country'), - 'is_past_address' => $request->input('is_past_address'), 'latitude' => null, 'longitude' => null, - 'lived_from_at' => null, - 'lived_until_at' => null, ]; - $address = (new UpdateContactAddress())->execute($data); + $address = (new UpdateAddress())->execute($data); $contact = Contact::find($contactId); return response()->json([ @@ -82,7 +86,7 @@ public function destroy(Request $request, int $vaultId, int $contactId, int $add 'address_id' => $addressId, ]; - (new DestroyContactAddress())->execute($data); + (new RemoveAddressFromContact())->execute($data); return response()->json([ 'data' => true, diff --git a/app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressImageController.php b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressImageController.php similarity index 93% rename from app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressImageController.php rename to app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressImageController.php index 3f1e0e16a..bce3ab157 100644 --- a/app/Domains/Vault/ManageAddresses/Web/Controllers/ContactModuleAddressImageController.php +++ b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressImageController.php @@ -1,6 +1,6 @@ belongsTo(Contact::class); + return $this->belongsToMany(Contact::class, 'contact_address'); } /** diff --git a/database/migrations/2020_04_26_215133_create_addresses_table.php b/database/migrations/2020_04_26_215133_create_addresses_table.php index c25e8515b..c19c3da82 100644 --- a/database/migrations/2020_04_26_215133_create_addresses_table.php +++ b/database/migrations/2020_04_26_215133_create_addresses_table.php @@ -13,7 +13,6 @@ public function up() { Schema::create('addresses', function (Blueprint $table) { $table->id(); - $table->uuid('uuid')->nullable(); $table->unsignedBigInteger('vault_id'); $table->unsignedBigInteger('address_type_id')->nullable(); $table->string('line_1')->nullable(); diff --git a/tests/Unit/Domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedAddressTest.php b/tests/Unit/Domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedAddressTest.php index 76977bd89..df437c89e 100644 --- a/tests/Unit/Domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedAddressTest.php +++ b/tests/Unit/Domains/Contact/ManageContactFeed/Web/ViewHelpers/Actions/ActionFeedAddressTest.php @@ -35,7 +35,6 @@ public function it_gets_the_data_needed_for_the_view(): void 'province' => 'quebec', 'postal_code' => 'h1k 12k', 'country' => 'Canada', - 'is_past_address' => false, 'address_type_id' => $addressType->id, 'longitude' => '-74.005941', 'latitude' => '40.712784', diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php index b33699153..a00f6317e 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php @@ -3,7 +3,7 @@ namespace Tests\Unit\Domains\Vault\ManageAddresses\Services; use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; -use App\Domains\Contact\ManageContactAddress\Services\AssociateAddressToContact; +use App\Domains\Vault\ManageAddresses\Services\CreateAddress; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; @@ -41,7 +41,7 @@ public function it_fails_if_wrong_parameters_are_given(): void ]; $this->expectException(ValidationException::class); - (new AssociateAddressToContact())->execute($request); + (new CreateAddress())->execute($request); } /** @test */ @@ -103,7 +103,7 @@ private function executeService(User $author, Account $account, Vault $vault, Ad 'longitude' => 12345, ]; - $address = (new AssociateAddressToContact())->execute($request); + $address = (new CreateAddress())->execute($request); $this->assertDatabaseHas('addresses', [ 'vault_id' => $vault->id, diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php index 462751f37..7f036f1d2 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/DestroyAddressTest.php @@ -2,7 +2,7 @@ namespace Tests\Unit\Domains\Vault\ManageAddresses\Services; -use App\Domains\Contact\ManageContactAddress\Services\DestroyAddress; +use App\Domains\Vault\ManageAddresses\Services\DestroyAddress; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/GetGPSCoordinateTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/GetGPSCoordinateTest.php index 9b362371c..3ef5e290d 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/GetGPSCoordinateTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/GetGPSCoordinateTest.php @@ -2,7 +2,7 @@ namespace Tests\Unit\Domains\Vault\ManageAddresses\Services; -use App\Domains\Contact\ManageContactAddress\Services\GetGPSCoordinate; +use App\Domains\Vault\ManageAddresses\Services\GetGPSCoordinate; use App\Exceptions\EnvVariablesNotSetException; use App\Models\Address; use Illuminate\Foundation\Testing\DatabaseTransactions; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php index 8b37e8213..762308fbd 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php @@ -3,7 +3,7 @@ namespace Tests\Unit\Domains\Vault\ManageAddresses\Services; use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; -use App\Domains\Contact\ManageContactAddress\Services\UpdateAddress; +use App\Domains\Vault\ManageAddresses\Services\UpdateAddress; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php index 7208a1a48..66a89a015 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php @@ -21,14 +21,10 @@ public function it_gets_the_data_needed_for_the_view(): void $contact = Contact::factory()->create(); $user = User::factory()->create(); - Address::factory()->create([ - 'contact_id' => $contact->id, - 'is_past_address' => false, - ]); - Address::factory()->create([ - 'contact_id' => $contact->id, - 'is_past_address' => true, - ]); + $address = Address::factory()->create(); + $address->contacts()->attach($contact, ['is_past_address' => true]); + $address = Address::factory()->create(); + $address->contacts()->attach($contact, ['is_past_address' => false]); $addressType = AddressType::factory()->create([ 'account_id' => $contact->vault->account_id, @@ -94,7 +90,6 @@ public function it_gets_the_data_transfer_object(): void 'province' => 'quebec', 'postal_code' => 'h1k 12k', 'country' => 'Canada', - 'is_past_address' => false, 'address_type_id' => $addressType->id, ]); diff --git a/tests/Unit/Models/AddressTest.php b/tests/Unit/Models/AddressTest.php index d38301cfa..145d8e041 100644 --- a/tests/Unit/Models/AddressTest.php +++ b/tests/Unit/Models/AddressTest.php @@ -3,6 +3,7 @@ namespace Tests\Unit\Models; use App\Models\Address; +use App\Models\Contact; use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\TestCase; @@ -11,11 +12,13 @@ class AddressTest extends TestCase use DatabaseTransactions; /** @test */ - public function it_has_one_contact() + public function it_has_many_contacts() { $address = Address::factory()->create(); + $contact = Contact::factory()->create(); + $address->contacts()->attach($contact->id); - $this->assertTrue($address->contact()->exists()); + $this->assertTrue($address->contacts()->exists()); } /** @test */ From 639de6b313ba53609e4b5374e674b9590e32e2e6 Mon Sep 17 00:00:00 2001 From: djaiss Date: Sat, 14 Jan 2023 02:15:43 +0000 Subject: [PATCH 06/10] chore: php linting with pint --- .../Web/Controllers/ContactModuleAddressController.php | 1 - .../Vault/ManageAddresses/Services/CreateAddressTest.php | 1 - .../Vault/ManageAddresses/Services/UpdateAddressTest.php | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php index 924530adc..fd6597acb 100644 --- a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php +++ b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php @@ -6,7 +6,6 @@ use App\Domains\Contact\ManageContactAddresses\Services\RemoveAddressFromContact; use App\Domains\Contact\ManageContactAddresses\Web\ViewHelpers\ModuleContactAddressesViewHelper; use App\Domains\Vault\ManageAddresses\Services\CreateAddress; -use App\Domains\Vault\ManageAddresses\Services\DestroyAddress; use App\Domains\Vault\ManageAddresses\Services\UpdateAddress; use App\Http\Controllers\Controller; use App\Models\Contact; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php index 1bd60e19f..c3e384d72 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/CreateAddressTest.php @@ -4,7 +4,6 @@ use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; use App\Domains\Vault\ManageAddresses\Services\CreateAddress; -use App\Domains\Contact\ManageContactAddress\Services\AssociateAddressToContact; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php index a7d1e0246..3ce79837c 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Services/UpdateAddressTest.php @@ -2,8 +2,8 @@ namespace Tests\Unit\Domains\Vault\ManageAddresses\Services; -use App\Domains\Vault\ManageAddresses\Services\UpdateAddress; use App\Domains\Vault\ManageAddresses\Jobs\FetchAddressGeocoding; +use App\Domains\Vault\ManageAddresses\Services\UpdateAddress; use App\Exceptions\NotEnoughPermissionException; use App\Models\Account; use App\Models\Address; From 39160badc071b0acd5756202b69195eb83f2c69d Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Sun, 15 Jan 2023 20:10:46 -0500 Subject: [PATCH 07/10] wip --- .../ContactModuleAddressController.php | 11 ++++++- .../ModuleContactAddressesViewHelper.php | 30 ++++++++----------- app/Models/Address.php | 2 +- app/Models/Contact.php | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php index fd6597acb..fe03bb2f7 100644 --- a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php +++ b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php @@ -11,6 +11,7 @@ use App\Models\Contact; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\DB; class ContactModuleAddressController extends Controller { @@ -67,9 +68,17 @@ public function update(Request $request, int $vaultId, int $contactId, int $addr 'longitude' => null, ]; - $address = (new UpdateAddress())->execute($data); + (new UpdateAddress())->execute($data); $contact = Contact::find($contactId); + // update pivot table + $contact->addresses()->where('address_id', $addressId)->update([ + 'is_past_address' => $request->input('is_past_address'), + ]); + + // get the address with pivot information + $address = $contact->addresses()->where('address_id', $addressId)->first(); + return response()->json([ 'data' => ModuleContactAddressesViewHelper::dto($contact, $address, Auth::user()), ], 200); diff --git a/app/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelper.php b/app/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelper.php index 2ee968f70..e4dd212d7 100644 --- a/app/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelper.php +++ b/app/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelper.php @@ -4,6 +4,7 @@ use App\Helpers\MapHelper; use App\Models\Address; +use App\Models\AddressType; use App\Models\Contact; use App\Models\User; @@ -11,29 +12,24 @@ class ModuleContactAddressesViewHelper { public static function data(Contact $contact, User $user): array { - $activeAddressesCollection = $contact->addresses()->where('is_past_address', false)->get(); - $inactiveAddressesCollection = $contact->addresses()->where('is_past_address', true)->get(); + $activeAddressesCollection = $contact->addresses() + ->wherePivot('is_past_address', false) + ->get() + ->map(fn (Address $address) => self::dto($contact, $address, $user)); - // get collections - $activeAddressesCollection = $activeAddressesCollection->map(function ($address) use ($contact, $user) { - return self::dto($contact, $address, $user); - }); - $inactiveAddressesCollection = $inactiveAddressesCollection->map(function ($address) use ($contact, $user) { - return self::dto($contact, $address, $user); - }); + $inactiveAddressesCollection = $contact->addresses() + ->wherePivot('is_past_address', true) + ->get() + ->map(fn (Address $address) => self::dto($contact, $address, $user)); - $addressTypes = $contact->vault->account + $addressTypesCollection = $contact->vault->account ->addressTypes() - ->get(); - - $addressTypesCollection = collect(); - foreach ($addressTypes as $addressType) { - $addressTypesCollection->push([ + ->get() + ->map(fn (AddressType $addressType) => [ 'id' => $addressType->id, 'name' => $addressType->name, 'selected' => false, ]); - } return [ 'active_addresses' => $activeAddressesCollection, @@ -52,7 +48,7 @@ public static function dto(Contact $contact, Address $address, User $user): arra { return [ 'id' => $address->id, - 'is_past_address' => $address->is_past_address, + 'is_past_address' => (bool) $address->pivot->is_past_address, 'line_1' => $address->line_1, 'line_2' => $address->line_2, 'city' => $address->city, diff --git a/app/Models/Address.php b/app/Models/Address.php index f3e00fed8..af8f7fe11 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -39,7 +39,7 @@ class Address extends Model */ public function contacts(): BelongsToMany { - return $this->belongsToMany(Contact::class, 'contact_address'); + return $this->belongsToMany(Contact::class, 'contact_address')->withPivot('is_past_address'); } /** diff --git a/app/Models/Contact.php b/app/Models/Contact.php index a1d1d3a79..d678f59b7 100644 --- a/app/Models/Contact.php +++ b/app/Models/Contact.php @@ -393,7 +393,7 @@ public function lifeEvents(): BelongsToMany */ public function addresses(): BelongsToMany { - return $this->belongsToMany(Address::class, 'contact_address', 'contact_id'); + return $this->belongsToMany(Address::class, 'contact_address', 'contact_id')->withPivot('is_past_address')->withTimestamps(); } /** From 6fd3515f2a8fee1fbce908cabe48dd8dee96e103 Mon Sep 17 00:00:00 2001 From: djaiss Date: Mon, 16 Jan 2023 01:11:46 +0000 Subject: [PATCH 08/10] chore: php linting with pint --- .../Web/Controllers/ContactModuleAddressController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php index fe03bb2f7..24e6b6643 100644 --- a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php +++ b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php @@ -11,7 +11,6 @@ use App\Models\Contact; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\DB; class ContactModuleAddressController extends Controller { From 67660b138217c48a01f48fe93526c64b5d1d195a Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Sun, 15 Jan 2023 20:12:48 -0500 Subject: [PATCH 09/10] Update ModuleContactAddressesViewHelperTest.php --- .../ViewHelpers/ModuleContactAddressesViewHelperTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php b/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php index 160dc5b22..86d39126c 100644 --- a/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php +++ b/tests/Unit/Domains/Vault/ManageAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelperTest.php @@ -93,11 +93,14 @@ public function it_gets_the_data_transfer_object(): void 'address_type_id' => $addressType->id, ]); - $collection = ModuleContactAddressesViewHelper::dto($contact, $activeAddress, $user); + $activeAddress->contacts()->attach($contact, ['is_past_address' => false]); + $address = $contact->addresses()->where('address_id', $activeAddress->id)->first(); + + $collection = ModuleContactAddressesViewHelper::dto($contact, $address, $user); $this->assertEquals( [ - 'id' => $activeAddress->id, + 'id' => $address->id, 'is_past_address' => false, 'line_1' => '123 main st', 'line_2' => 'Apartment 4', From ab753a8131795abbc34fede387f3ceee4436f23d Mon Sep 17 00:00:00 2001 From: Regis Freyd Date: Sun, 15 Jan 2023 20:16:52 -0500 Subject: [PATCH 10/10] Update ContactTest.php --- tests/Unit/Models/ContactTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Models/ContactTest.php b/tests/Unit/Models/ContactTest.php index beb832340..70ea6d23e 100644 --- a/tests/Unit/Models/ContactTest.php +++ b/tests/Unit/Models/ContactTest.php @@ -288,7 +288,7 @@ public function it_has_many_addresses(): void $contact = Contact::factory()->create([]); $address = Address::factory()->create(); - $contact->addresses()->sync([$contact->id => ['is_past_address' => false]]); + $contact->addresses()->sync([$address->id => ['is_past_address' => false]]); $this->assertTrue($contact->addresses()->exists()); }