From b7734cec4639073b59ef9346f6d2d3682550cf32 Mon Sep 17 00:00:00 2001 From: Mazarin Date: Thu, 9 Feb 2023 08:06:00 -0500 Subject: [PATCH] feat: list all addresses in vault (monicahq/chandler#422) --- .../Services/RemoveAddressFromContact.php | 4 + .../ContactModuleAddressController.php | 8 +- .../ModuleContactAddressesViewHelper.php | 16 +- .../ReportAddressesCitiesController.php | 24 + .../Controllers/ReportAddressesController.php | 23 + .../ReportAddressesCountriesController.php | 24 + .../ReportAddressIndexViewHelper.php | 60 +++ .../ReportCitiesShowViewHelper.php | 48 ++ .../ReportCountriesShowViewHelper.php | 48 ++ .../Web/ViewHelpers/ReportIndexViewHelper.php | 3 + app/Helpers/WikipediaHelper.php | 46 ++ .../Vault/Reports/Address/Cities/Index.vue | 123 ++++++ .../Vault/Reports/Address/Countries/Index.vue | 123 ++++++ .../js/Pages/Vault/Reports/Address/Index.vue | 118 +++++ resources/js/Pages/Vault/Reports/Index.vue | 5 + resources/js/Shared/Modules/Addresses.vue | 417 +++++++++++------- routes/web.php | 8 + .../ModuleContactAddressesViewHelperTest.php | 28 +- .../ReportAddressIndexViewHelperTest.php | 63 +++ .../ReportCitiesShowViewHelperTest.php | 69 +++ .../ReportCountriesShowViewHelperTest.php | 69 +++ .../ViewHelpers/ReportIndexViewHelperTest.php | 1 + tests/Unit/Helpers/WikipediaHelperTest.php | 25 ++ 23 files changed, 1178 insertions(+), 175 deletions(-) create mode 100644 app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCitiesController.php create mode 100644 app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesController.php create mode 100644 app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCountriesController.php create mode 100644 app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportAddressIndexViewHelper.php create mode 100644 app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCitiesShowViewHelper.php create mode 100644 app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCountriesShowViewHelper.php create mode 100644 app/Helpers/WikipediaHelper.php create mode 100644 resources/js/Pages/Vault/Reports/Address/Cities/Index.vue create mode 100644 resources/js/Pages/Vault/Reports/Address/Countries/Index.vue create mode 100644 resources/js/Pages/Vault/Reports/Address/Index.vue create mode 100644 tests/Unit/Domains/Vault/ManageReports/Web/ViewHelpers/ReportAddressIndexViewHelperTest.php create mode 100644 tests/Unit/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCitiesShowViewHelperTest.php create mode 100644 tests/Unit/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCountriesShowViewHelperTest.php create mode 100644 tests/Unit/Helpers/WikipediaHelperTest.php diff --git a/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php b/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php index 176fa9b6f29..868b38324aa 100644 --- a/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php +++ b/app/Domains/Contact/ManageContactAddresses/Services/RemoveAddressFromContact.php @@ -72,6 +72,10 @@ private function validate(): void private function remove(): void { $this->contact->addresses()->detach($this->address); + + if ($this->address->contacts()->count() === 0) { + $this->address->delete(); + } } private function createFeedItem(): void diff --git a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php index 24e6b66434e..4fd33c49685 100644 --- a/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php +++ b/app/Domains/Contact/ManageContactAddresses/Web/Controllers/ContactModuleAddressController.php @@ -8,6 +8,7 @@ use App\Domains\Vault\ManageAddresses\Services\CreateAddress; use App\Domains\Vault\ManageAddresses\Services\UpdateAddress; use App\Http\Controllers\Controller; +use App\Models\Address; use App\Models\Contact; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -31,7 +32,12 @@ public function store(Request $request, int $vaultId, int $contactId) 'longitude' => null, ]; - $address = (new CreateAddress())->execute($data); + if (! $request->input('existing_address')) { + $address = (new CreateAddress())->execute($data); + } else { + $address = Address::where('vault_id', $vaultId) + ->findOrFail($request->input('existing_address_id')); + } (new AssociateAddressToContact())->execute([ 'account_id' => Auth::user()->account_id, diff --git a/app/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelper.php b/app/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelper.php index 088c0c9ca45..b99f943cc4f 100644 --- a/app/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelper.php +++ b/app/Domains/Contact/ManageContactAddresses/Web/ViewHelpers/ModuleContactAddressesViewHelper.php @@ -12,12 +12,12 @@ class ModuleContactAddressesViewHelper { public static function data(Contact $contact, User $user): array { - $activeAddressesCollection = $contact->addresses() + $contactActiveAddressesCollection = $contact->addresses() ->wherePivot('is_past_address', false) ->get() ->map(fn (Address $address) => self::dto($contact, $address, $user)); - $inactiveAddressesCollection = $contact->addresses() + $contactInactiveAddressesCollection = $contact->addresses() ->wherePivot('is_past_address', true) ->get() ->map(fn (Address $address) => self::dto($contact, $address, $user)); @@ -31,10 +31,18 @@ public static function data(Contact $contact, User $user): array 'selected' => false, ]); + $vaultAddressesCollection = $contact->vault->addresses() + ->get() + ->map(fn (Address $address) => [ + 'id' => $address->id, + 'address' => MapHelper::getAddressAsString($address), + ]); + return [ - 'active_addresses' => $activeAddressesCollection, - 'inactive_addresses' => $inactiveAddressesCollection, + 'active_addresses' => $contactActiveAddressesCollection, + 'inactive_addresses' => $contactInactiveAddressesCollection, 'address_types' => $addressTypesCollection, + 'addresses_in_vault' => $vaultAddressesCollection, 'url' => [ 'store' => route('contact.address.store', [ 'vault' => $contact->vault_id, diff --git a/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCitiesController.php b/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCitiesController.php new file mode 100644 index 00000000000..a3af22a4019 --- /dev/null +++ b/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCitiesController.php @@ -0,0 +1,24 @@ + VaultIndexViewHelper::layoutData($vault), + 'data' => ReportCitiesShowViewHelper::data($vault, $city), + ]); + } +} diff --git a/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesController.php b/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesController.php new file mode 100644 index 00000000000..1a29e7033f5 --- /dev/null +++ b/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesController.php @@ -0,0 +1,23 @@ + VaultIndexViewHelper::layoutData($vault), + 'data' => ReportAddressIndexViewHelper::data($vault), + ]); + } +} diff --git a/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCountriesController.php b/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCountriesController.php new file mode 100644 index 00000000000..949a9538ab7 --- /dev/null +++ b/app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCountriesController.php @@ -0,0 +1,24 @@ + VaultIndexViewHelper::layoutData($vault), + 'data' => ReportCountriesShowViewHelper::data($vault, $country), + ]); + } +} diff --git a/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportAddressIndexViewHelper.php b/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportAddressIndexViewHelper.php new file mode 100644 index 00000000000..95715372181 --- /dev/null +++ b/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportAddressIndexViewHelper.php @@ -0,0 +1,60 @@ +addresses() + ->select('id', 'city') + ->whereNotNull('city') + ->withCount('contacts') + ->distinct('city') + ->get() + ->map(fn ($address) => [ + 'id' => $address->id, + 'name' => Str::ucfirst($address->city), + 'contacts' => $address->contacts_count, + 'url' => [ + 'index' => route('vault.reports.addresses.cities.show', [ + 'vault' => $vault->id, + 'city' => urlencode(utf8_encode($address->city)), + ]), + ], + ]) + ->unique('name'); + + // all the countries in the vault + $countries = $vault->addresses() + ->select('id', 'country') + ->whereNotNull('country') + ->withCount('contacts') + ->distinct('country') + ->get() + ->map(fn ($address) => [ + 'id' => $address->id, + 'name' => Str::ucfirst($address->country), + 'contacts' => $address->contacts_count, + 'url' => [ + 'index' => route('vault.reports.addresses.countries.show', [ + 'vault' => $vault->id, + 'country' => urlencode(utf8_encode($address->country)), + ]), + ], + ]) + ->unique('name'); + + return [ + 'cities' => $cities, + 'countries' => $countries, + ]; + } +} diff --git a/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCitiesShowViewHelper.php b/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCitiesShowViewHelper.php new file mode 100644 index 00000000000..fb85d95fc29 --- /dev/null +++ b/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCitiesShowViewHelper.php @@ -0,0 +1,48 @@ +addresses() + ->whereNotNull('city') + ->where('city', Str::ucfirst($city)) + ->orWhere('city', Str::lcfirst($city)) + ->with('contacts') + ->get() + ->map(fn ($address) => [ + 'id' => $address->id, + 'name' => Str::ucfirst($address->city), + 'address' => MapHelper::getAddressAsString($address), + 'contacts' => $address->contacts() + ->get() + ->map(fn (Contact $contact) => ContactCardHelper::data($contact)), + ]); + + $wikipediaInformation = WikipediaHelper::getInformation($city); + + return [ + 'city' => Str::ucfirst($city), + 'addresses' => $addresses, + 'wikipedia' => [ + 'url' => $wikipediaInformation['url'], + 'description' => $wikipediaInformation['description'], + 'thumbnail' => $wikipediaInformation['thumbnail'], + ], + 'url' => [ + 'addresses' => route('vault.reports.addresses.index', [ + 'vault' => $vault->id, + ]), + ], + ]; + } +} diff --git a/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCountriesShowViewHelper.php b/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCountriesShowViewHelper.php new file mode 100644 index 00000000000..c0d9a083c28 --- /dev/null +++ b/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCountriesShowViewHelper.php @@ -0,0 +1,48 @@ +addresses() + ->whereNotNull('country') + ->where('country', Str::ucfirst($country)) + ->orWhere('country', Str::lcfirst($country)) + ->with('contacts') + ->get() + ->map(fn ($address) => [ + 'id' => $address->id, + 'name' => Str::ucfirst($address->country), + 'address' => MapHelper::getAddressAsString($address), + 'contacts' => $address->contacts() + ->get() + ->map(fn (Contact $contact) => ContactCardHelper::data($contact)), + ]); + + $wikipediaInformation = WikipediaHelper::getInformation($country); + + return [ + 'country' => Str::ucfirst($country), + 'addresses' => $addresses, + 'wikipedia' => [ + 'url' => $wikipediaInformation['url'], + 'description' => $wikipediaInformation['description'], + 'thumbnail' => $wikipediaInformation['thumbnail'], + ], + 'url' => [ + 'addresses' => route('vault.reports.addresses.index', [ + 'vault' => $vault->id, + ]), + ], + ]; + } +} diff --git a/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportIndexViewHelper.php b/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportIndexViewHelper.php index 5101ba409e4..ddfa5cc54b4 100644 --- a/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportIndexViewHelper.php +++ b/app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportIndexViewHelper.php @@ -10,6 +10,9 @@ public static function data(Vault $vault): array { return [ 'url' => [ + 'addresses' => route('vault.reports.addresses.index', [ + 'vault' => $vault->id, + ]), 'mood_tracking_events' => route('vault.reports.mood_tracking_events.index', [ 'vault' => $vault->id, ]), diff --git a/app/Helpers/WikipediaHelper.php b/app/Helpers/WikipediaHelper.php new file mode 100644 index 00000000000..808c9603891 --- /dev/null +++ b/app/Helpers/WikipediaHelper.php @@ -0,0 +1,46 @@ + 'query', + 'prop' => 'description|pageimages', + 'titles' => $topic, + 'pithumbsize' => 400, + 'format' => 'json', + ]); + + $url = 'https://en.wikipedia.org/w/api.php?'.$query; + + $response = Http::get($url)->throw(); + + if ($response->json('query.pages.*.missing')[0] === true) { + return [ + 'url' => null, + 'description' => null, + 'thumbnail' => null, + ]; + } + + return [ + 'url' => 'https://en.wikipedia.org/wiki/'.Str::slug($topic), + 'description' => $response->json('query.pages.*.description')[0], + 'thumbnail' => $response->json('query.pages.*.thumbnail.source')[0], + ]; + } +} diff --git a/resources/js/Pages/Vault/Reports/Address/Cities/Index.vue b/resources/js/Pages/Vault/Reports/Address/Cities/Index.vue new file mode 100644 index 00000000000..ce68d7786d2 --- /dev/null +++ b/resources/js/Pages/Vault/Reports/Address/Cities/Index.vue @@ -0,0 +1,123 @@ + + + + + diff --git a/resources/js/Pages/Vault/Reports/Address/Countries/Index.vue b/resources/js/Pages/Vault/Reports/Address/Countries/Index.vue new file mode 100644 index 00000000000..d4c7682a804 --- /dev/null +++ b/resources/js/Pages/Vault/Reports/Address/Countries/Index.vue @@ -0,0 +1,123 @@ + + + + + diff --git a/resources/js/Pages/Vault/Reports/Address/Index.vue b/resources/js/Pages/Vault/Reports/Address/Index.vue new file mode 100644 index 00000000000..15c8a4a1f05 --- /dev/null +++ b/resources/js/Pages/Vault/Reports/Address/Index.vue @@ -0,0 +1,118 @@ + + + + + diff --git a/resources/js/Pages/Vault/Reports/Index.vue b/resources/js/Pages/Vault/Reports/Index.vue index de34173485e..c40c042463f 100644 --- a/resources/js/Pages/Vault/Reports/Index.vue +++ b/resources/js/Pages/Vault/Reports/Index.vue @@ -15,6 +15,11 @@ defineProps({

All the reports