Make sure update and edit work for location, API or not.

This commit is contained in:
James Cole 2020-01-02 19:12:40 +01:00
parent f7d6a4c6f9
commit e1577a4a76
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
6 changed files with 35 additions and 20 deletions

View File

@ -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');

View File

@ -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');

View File

@ -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';
}

View File

@ -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;

View File

@ -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) {

View File

@ -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('');
}
});
};