Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
feat: list all addresses in vault (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Feb 9, 2023
1 parent 4342452 commit ee17e9a
Show file tree
Hide file tree
Showing 23 changed files with 1,178 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Domains\Vault\ManageReports\Web\Controllers;

use App\Domains\Vault\ManageReports\Web\ViewHelpers\ReportCitiesShowViewHelper;
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Vault;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ReportAddressesCitiesController extends Controller
{
public function show(Request $request, int $vaultId, string $city)
{
$vault = Vault::findOrFail($vaultId);
$city = utf8_decode(urldecode($city));

return Inertia::render('Vault/Reports/Address/Cities/Index', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => ReportCitiesShowViewHelper::data($vault, $city),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Domains\Vault\ManageReports\Web\Controllers;

use App\Domains\Vault\ManageReports\Web\ViewHelpers\ReportAddressIndexViewHelper;
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Vault;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ReportAddressesController extends Controller
{
public function index(Request $request, int $vaultId)
{
$vault = Vault::findOrFail($vaultId);

return Inertia::render('Vault/Reports/Address/Index', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => ReportAddressIndexViewHelper::data($vault),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Domains\Vault\ManageReports\Web\Controllers;

use App\Domains\Vault\ManageReports\Web\ViewHelpers\ReportCountriesShowViewHelper;
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Vault;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ReportAddressesCountriesController extends Controller
{
public function show(Request $request, int $vaultId, string $country)
{
$vault = Vault::findOrFail($vaultId);
$country = utf8_decode(urldecode($country));

return Inertia::render('Vault/Reports/Address/Countries/Index', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => ReportCountriesShowViewHelper::data($vault, $country),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Domains\Vault\ManageReports\Web\ViewHelpers;

use App\Models\Vault;
use Illuminate\Support\Str;

class ReportAddressIndexViewHelper
{
public static function data(Vault $vault): array
{
// all the cities in the vault
// the distinct method does not do a case-insensitive search, so we need
// to do it manually. there would be a way to do it with raw SQL but
// it wouldn't work on sqlite
$cities = $vault->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,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Domains\Vault\ManageReports\Web\ViewHelpers;

use App\Helpers\ContactCardHelper;
use App\Helpers\MapHelper;
use App\Helpers\WikipediaHelper;
use App\Models\Contact;
use App\Models\Vault;
use Illuminate\Support\Str;

class ReportCitiesShowViewHelper
{
public static function data(Vault $vault, string $city): array
{
$addresses = $vault->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,
]),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Domains\Vault\ManageReports\Web\ViewHelpers;

use App\Helpers\ContactCardHelper;
use App\Helpers\MapHelper;
use App\Helpers\WikipediaHelper;
use App\Models\Contact;
use App\Models\Vault;
use Illuminate\Support\Str;

class ReportCountriesShowViewHelper
{
public static function data(Vault $vault, string $country): array
{
$addresses = $vault->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,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]),
Expand Down
46 changes: 46 additions & 0 deletions app/Helpers/WikipediaHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class WikipediaHelper
{
/**
* Return the information about the given city or country from Wikipedia.
* All API calls are documented here:
* https://www.mediawiki.org/w/api.php?action=help&modules=query.
*
* @param string $topic
* @return array
*/
public static function getInformation(string $topic): array
{
$query = http_build_query([
'action' => '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],
];
}
}
Loading

0 comments on commit ee17e9a

Please sign in to comment.