Skip to content

Commit c3ea1f0

Browse files
authored
Merge pull request #486 from os2display/358/change-area-and-facility-fields-to-id
Changed filter to filter through IDs instead of name on Area and Faci…
2 parents 25f774d + 1e4e56a commit c3ea1f0

2 files changed

Lines changed: 47 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
- [#486](https://github.com/os2display/display-api-service/pull/486)
8+
- Changed BRND feed area and facility filters to match on `områdeId` and `facilitetsId` instead of area/facility names.
9+
- Mapped area and facility IDs centrally in `parseBrndBooking()`.
10+
- ID filtering is only supported for BRND API v2.0; area/facility fields are hidden in admin for v1.0 feed sources.
711
- [#444](https://github.com/os2display/display-api-service/pull/444)
812
- Renamed 15 `changed_idx` indexes to `<table>_changed_idx` for cross-platform portability
913
(Postgres scopes index names schema-wide).

src/Feed/BrndFeedType.php

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class BrndFeedType implements FeedTypeInterface
2929
*/
3030
private const string BRND_API_TIMEZONE = 'Europe/Copenhagen';
3131

32+
private const string BRND_API_VERSION_WITH_ID_FILTERING = '2.0';
33+
3234
public function __construct(
3335
private readonly FeedService $feedService,
3436
private readonly ApiClient $apiClient,
@@ -40,7 +42,7 @@ public function getAdminFormOptions(FeedSource $feedSource): array
4042
{
4143
$feedEntryRecipients = $this->feedService->getFeedSourceConfigUrl($feedSource, 'sport-center');
4244

43-
return [
45+
$options = [
4446
[
4547
'key' => 'brnd-sport-center-id',
4648
'input' => 'input',
@@ -49,23 +51,28 @@ public function getAdminFormOptions(FeedSource $feedSource): array
4951
'label' => 'Sportcenter ID',
5052
'formGroupClasses' => 'mb-3',
5153
],
52-
[
54+
];
55+
56+
if ($this->supportsIdFiltering($feedSource)) {
57+
$options[] = [
5358
'key' => 'brnd-area',
5459
'input' => 'input',
5560
'type' => 'text',
5661
'name' => 'area',
57-
'label' => 'Område',
62+
'label' => 'Område ID',
5863
'formGroupClasses' => 'mb-3',
59-
],
60-
[
64+
];
65+
$options[] = [
6166
'key' => 'brnd-facility',
6267
'input' => 'input',
6368
'type' => 'text',
6469
'name' => 'facility',
65-
'label' => 'Facilitet',
70+
'label' => 'Facilitet ID',
6671
'formGroupClasses' => 'mb-3',
67-
],
68-
];
72+
];
73+
}
74+
75+
return $options;
6976
}
7077

7178
public function getData(Feed $feed): array
@@ -94,12 +101,13 @@ public function getData(Feed $feed): array
94101
return $result;
95102
}
96103

104+
$supportsIdFiltering = $this->supportsIdFiltering($feedSource);
97105
$areaFilterNormalized = self::normalizeFilterValue($areaFilter);
98106
$facilityFilterNormalized = self::normalizeFilterValue($facilityFilter);
99107

100108
$bookings = $this->apiClient->getInfomonitorBookingsDetails($feedSource, $sportCenterId);
101109

102-
$result['bookings'] = array_reduce($bookings, function (array $carry, mixed $booking) use ($areaFilterNormalized, $facilityFilterNormalized): array {
110+
$result['bookings'] = array_reduce($bookings, function (array $carry, mixed $booking) use ($areaFilterNormalized, $facilityFilterNormalized, $supportsIdFiltering): array {
103111
if (!is_array($booking)) {
104112
return $carry;
105113
}
@@ -111,18 +119,18 @@ public function getData(Feed $feed): array
111119
return $carry;
112120
}
113121

114-
// Bail out if area filter applies and booking area does not match.
115-
if ('' !== $areaFilterNormalized) {
116-
$bookingArea = self::normalizeFilterValue($parsedBooking['area'] ?? '');
117-
if ($bookingArea !== $areaFilterNormalized) {
122+
// Bail out if area filter applies and booking area ID does not match.
123+
if ($supportsIdFiltering && '' !== $areaFilterNormalized) {
124+
$bookingAreaId = self::normalizeFilterValue($parsedBooking['areaId'] ?? '');
125+
if ($bookingAreaId !== $areaFilterNormalized) {
118126
return $carry;
119127
}
120128
}
121129

122-
// Bail out if facility filter applies and booking facility does not match.
123-
if ('' !== $facilityFilterNormalized) {
124-
$bookingFacility = self::normalizeFilterValue($parsedBooking['facility'] ?? '');
125-
if ($bookingFacility !== $facilityFilterNormalized) {
130+
// Bail out if facility filter applies and booking facility ID does not match.
131+
if ($supportsIdFiltering && '' !== $facilityFilterNormalized) {
132+
$bookingFacilityId = self::normalizeFilterValue($parsedBooking['facilityId'] ?? '');
133+
if ($bookingFacilityId !== $facilityFilterNormalized) {
126134
return $carry;
127135
}
128136
}
@@ -142,13 +150,15 @@ public function getData(Feed $feed): array
142150

143151
private static function normalizeFilterValue(mixed $value): string
144152
{
153+
if (is_int($value) || is_float($value)) {
154+
return (string) $value;
155+
}
156+
145157
if (!is_string($value)) {
146158
return '';
147159
}
148160

149-
$value = trim($value);
150-
151-
return strtolower($value);
161+
return trim($value);
152162
}
153163

154164
private function parseBrndBooking(array $booking): array
@@ -195,6 +205,8 @@ private function parseBrndBooking(array $booking): array
195205
'complex' => $booking['anlæg'] ?? '',
196206
'area' => $booking['område'] ?? '',
197207
'facility' => $booking['facilitet'] ?? '',
208+
'areaId' => $booking['områdeId'] ?? '',
209+
'facilityId' => $booking['facilitetsId'] ?? '',
198210
'activity' => $booking['aktivitet'] ?? '',
199211
'team' => $booking['hold'] ?? '',
200212
'status' => $booking['status'] ?? '',
@@ -204,6 +216,17 @@ private function parseBrndBooking(array $booking): array
204216
];
205217
}
206218

219+
private function supportsIdFiltering(FeedSource $feedSource): bool
220+
{
221+
$secrets = $feedSource->getSecrets();
222+
223+
if (!is_array($secrets)) {
224+
return false;
225+
}
226+
227+
return self::BRND_API_VERSION_WITH_ID_FILTERING === ($secrets['api_version'] ?? '1.0');
228+
}
229+
207230
public function getConfigOptions(Request $request, FeedSource $feedSource, string $name): ?array
208231
{
209232
return null;

0 commit comments

Comments
 (0)