Return default user setting if not configured for API endpoint /user/settings/{settingKey} (fixes #1169)

This commit is contained in:
Bernd Bestel 2020-12-10 18:02:24 +01:00
parent 48aa9fd138
commit 4b1766ead0
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
2 changed files with 11 additions and 3 deletions

View File

@ -217,5 +217,6 @@
- Fixed that the endpoint `/stock/volatile` didn't include products which expire today (thanks @fipwmaqzufheoxq92ebc) - Fixed that the endpoint `/stock/volatile` didn't include products which expire today (thanks @fipwmaqzufheoxq92ebc)
- Fixed that the endpoint `/objects/{entity}` did not include Userfields for Userentities (so the effective endpoint `/objects/userobjects`) - Fixed that the endpoint `/objects/{entity}` did not include Userfields for Userentities (so the effective endpoint `/objects/userobjects`)
- Fixed that the endpoint `/stock/consume` returned the response code `200` and an empty response body when `stock_entry_id` was set (consuming a specific stock entry) but invalid (now returns the response code `400`) (thanks @fipwmaqzufheoxq92ebc) - Fixed that the endpoint `/stock/consume` returned the response code `200` and an empty response body when `stock_entry_id` was set (consuming a specific stock entry) but invalid (now returns the response code `400`) (thanks @fipwmaqzufheoxq92ebc)
- Fixed that the endpoint `/user/settings/{settingKey}` didn't return the default setting if it was not configured for the current user (same behavior as the endpoint `/user/settings` now)
- Endpoint `/calendar/ical`: Fixed that "Track date only"-chores were always set to happen at 12am (are treated as all-day events now) - Endpoint `/calendar/ical`: Fixed that "Track date only"-chores were always set to happen at 12am (are treated as all-day events now)
- Fixed (again) that CORS was broken - Fixed (again) that CORS was broken

View File

@ -53,14 +53,22 @@ class UsersService extends BaseService
public function GetUserSetting($userId, $settingKey) public function GetUserSetting($userId, $settingKey)
{ {
$settingRow = $this->getDatabase()->user_settings()->where('user_id = :1 AND key = :2', $userId, $settingKey)->fetch(); $settingRow = $this->getDatabase()->user_settings()->where('user_id = :1 AND key = :2', $userId, $settingKey)->fetch();
if ($settingRow !== null) if ($settingRow !== null)
{ {
return $settingRow->value; return $settingRow->value;
} }
else else
{ {
return null; // Use the configured default values for a missing setting, otherwise return NULL
global $GROCY_DEFAULT_USER_SETTINGS;
if (array_key_exists($settingKey, $GROCY_DEFAULT_USER_SETTINGS))
{
return $GROCY_DEFAULT_USER_SETTINGS[$settingKey];
}
else
{
return null;
}
} }
} }
@ -69,7 +77,6 @@ class UsersService extends BaseService
$settings = []; $settings = [];
$settingRows = $this->getDatabase()->user_settings()->where('user_id = :1', $userId)->fetchAll(); $settingRows = $this->getDatabase()->user_settings()->where('user_id = :1', $userId)->fetchAll();
foreach ($settingRows as $settingRow) foreach ($settingRows as $settingRow)
{ {
$settings[$settingRow->key] = $settingRow->value; $settings[$settingRow->key] = $settingRow->value;