From e1577a4a76429ee6fa20b481e4c717133bbdcc57 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 2 Jan 2020 19:12:40 +0100 Subject: [PATCH] Make sure update and edit work for location, API or not. --- app/Api/V1/Requests/AccountStoreRequest.php | 2 +- app/Api/V1/Requests/AccountUpdateRequest.php | 2 +- app/Http/Requests/AccountFormRequest.php | 8 +--- app/Http/Requests/Request.php | 38 +++++++++++++------ .../Internal/Update/AccountUpdateService.php | 2 +- resources/views/v1/form/location.twig | 3 ++ 6 files changed, 35 insertions(+), 20 deletions(-) diff --git a/app/Api/V1/Requests/AccountStoreRequest.php b/app/Api/V1/Requests/AccountStoreRequest.php index 7dc729c0e1..6b764973ff 100644 --- a/app/Api/V1/Requests/AccountStoreRequest.php +++ b/app/Api/V1/Requests/AccountStoreRequest.php @@ -81,7 +81,7 @@ class AccountStoreRequest extends Request 'interest_period' => $this->string('interest_period'), ]; // append Location information. - $data = $this->appendLocationData($data); + $data = $this->appendLocationData($data, null); if ('liability' === $data['account_type']) { $data['opening_balance'] = bcmul($this->string('liability_amount'), '-1'); diff --git a/app/Api/V1/Requests/AccountUpdateRequest.php b/app/Api/V1/Requests/AccountUpdateRequest.php index 1b9169ba94..df7cbec7d9 100644 --- a/app/Api/V1/Requests/AccountUpdateRequest.php +++ b/app/Api/V1/Requests/AccountUpdateRequest.php @@ -81,7 +81,7 @@ class AccountUpdateRequest extends Request 'interest_period' => $this->nullableString('interest_period'), ]; - $data = $this->appendLocationData($data); + $data = $this->appendLocationData($data, null); if ('liability' === $data['account_type']) { $data['opening_balance'] = bcmul($this->nullableString('liability_amount'), '-1'); diff --git a/app/Http/Requests/AccountFormRequest.php b/app/Http/Requests/AccountFormRequest.php index 365fe14e25..aa442e2294 100644 --- a/app/Http/Requests/AccountFormRequest.php +++ b/app/Http/Requests/AccountFormRequest.php @@ -68,13 +68,9 @@ class AccountFormRequest extends Request 'interest' => $this->string('interest'), 'interest_period' => $this->string('interest_period'), 'include_net_worth' => '1', - - // new: location - 'longitude' => $this->string('location_longitude'), - 'latitude' => $this->string('location_latitude'), - 'zoom_level' => $this->integer('location_zoom_level'), - 'has_location' => $this->boolean('location_has_location'), ]; + + $data = $this->appendLocationData($data, 'location'); if (false === $this->boolean('include_net_worth')) { $data['include_net_worth'] = '0'; } diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php index 4a7c072b2b..4724ca3f09 100644 --- a/app/Http/Requests/Request.php +++ b/app/Http/Requests/Request.php @@ -347,39 +347,55 @@ class Request extends FormRequest /** * Read the submitted Request data and add new or updated Location data to the array. * - * @param array $data + * @param array $data + * + * @param string|null $prefix * * @return array */ - protected function appendLocationData(array $data): array + protected function appendLocationData(array $data, ?string $prefix): array { - Log::debug('Now in appendLocationData()'); + Log::debug(sprintf('Now in appendLocationData(%s)', $prefix), $data); $data['store_location'] = false; $data['update_location'] = false; $data['longitude'] = null; $data['latitude'] = null; $data['zoom_level'] = null; + $longitudeKey = null === $prefix ? 'longitude' : sprintf('%s_longitude', $prefix); + $latitudeKey = null === $prefix ? 'latitude' : sprintf('%s_latitude', $prefix); + $zoomLevelKey = null === $prefix ? 'zoom_level' : sprintf('%s_zoom_level', $prefix); + // for a POST (store, all fields must be present and accounted for: - if ('POST' === $this->method() && $this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) { + if ( + ('POST' === $this->method() && ($this->routeIs('accounts.store') || $this->routeIs('api.v1.accounts.store'))) + && ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey)) + ) { Log::debug('Method is POST and all fields present.'); $data['store_location'] = true; - $data['longitude'] = '' === $this->string('longitude') ? null : $this->string('longitude'); - $data['latitude'] = '' === $this->string('latitude') ? null : $this->string('latitude'); - $data['zoom_level'] = '' === $this->string('zoom_level') ? null : $this->integer('zoom_level'); + $data['longitude'] = '' === $this->string($longitudeKey) ? null : $this->string($longitudeKey); + $data['latitude'] = '' === $this->string($latitudeKey) ? null : $this->string($latitudeKey); + $data['zoom_level'] = '' === $this->string($zoomLevelKey) ? null : $this->integer($zoomLevelKey); } - if ('PUT' === $this->method() && $this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) { + if ( + ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey)) + && ( + ('PUT' === $this->method() && $this->routeIs('api.v1.accounts.update')) + || ('POST' === $this->method() && $this->routeIs('accounts.update')) + ) + ) { Log::debug('Method is PUT and all fields present.'); $data['update_location'] = true; - $data['longitude'] = '' === $this->string('longitude') ? null : $this->string('longitude'); - $data['latitude'] = '' === $this->string('latitude') ? null : $this->string('latitude'); - $data['zoom_level'] = '' === $this->string('zoom_level') ? null : $this->integer('zoom_level'); + $data['longitude'] = '' === $this->string($longitudeKey) ? null : $this->string($longitudeKey); + $data['latitude'] = '' === $this->string($latitudeKey) ? null : $this->string($latitudeKey); + $data['zoom_level'] = '' === $this->string($zoomLevelKey) ? null : $this->integer($zoomLevelKey); } if (null === $data['longitude'] || null === $data['latitude'] || null === $data['zoom_level']) { Log::debug('One of the fields is NULL, wont save.'); $data['store_location'] = false; $data['update_location'] = false; } + Log::debug(sprintf('Returning longitude: "%s", latitude: "%s", zoom level: "%s"', $data['longitude'], $data['latitude'], $data['zoom_level'])); return $data; diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php index 11244f273c..c454365eea 100644 --- a/app/Services/Internal/Update/AccountUpdateService.php +++ b/app/Services/Internal/Update/AccountUpdateService.php @@ -101,7 +101,7 @@ class AccountUpdateService $this->updateMetaData($account, $data); // update, delete or create location: - $updateLocation = $data['has_location'] ?? false; + $updateLocation = $data['update_location'] ?? false; // location must be updated? if (true === $updateLocation) { diff --git a/resources/views/v1/form/location.twig b/resources/views/v1/form/location.twig index dd4aa00ec1..82c431f721 100644 --- a/resources/views/v1/form/location.twig +++ b/resources/views/v1/form/location.twig @@ -72,6 +72,9 @@ if (typeof marker !== 'undefined') { marker.remove(); $('input[name="{{ haslocationvar }}"]').val('false'); + $('input[name="{{ latitudevar }}"]').val(''); + $('input[name="{{ longitudevar }}"]').val(''); + $('input[name="{{ zoomlevelvar }}"]').val(''); } }); };