-
Notifications
You must be signed in to change notification settings - Fork 7
BRND Booking #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
BRND Booking #313
Changes from all commits
2b85f26
45b3f94
e90f378
0519fee
5d60da1
5a27c01
ac26287
a90f6c7
a56cc6e
64f6dad
13597d1
fb5d3c0
e2b4b67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,183 @@ | ||||||
| <?php | ||||||
|
|
||||||
| declare(strict_types=1); | ||||||
|
|
||||||
| namespace App\Feed; | ||||||
|
|
||||||
| use App\Entity\Tenant\Feed; | ||||||
| use App\Entity\Tenant\FeedSource; | ||||||
| use App\Feed\SourceType\Brnd\ApiClient; | ||||||
| use App\Feed\SourceType\Brnd\SecretsDTO; | ||||||
| use App\Service\FeedService; | ||||||
| use Psr\Cache\CacheItemPoolInterface; | ||||||
| use Symfony\Component\HttpFoundation\Request; | ||||||
|
|
||||||
| /** | ||||||
| * Brnd Bookingsystem Feed. | ||||||
| * | ||||||
| * @see https://brndapi.brnd.com/swagger/index.html | ||||||
| */ | ||||||
| class BrndFeedType implements FeedTypeInterface | ||||||
| { | ||||||
| public const int CACHE_TTL = 3600; | ||||||
|
|
||||||
| final public const string SUPPORTED_FEED_TYPE = FeedOutputModels::BRND_BOOKING_OUTPUT; | ||||||
|
|
||||||
| public function __construct( | ||||||
| private readonly FeedService $feedService, | ||||||
| private readonly ApiClient $apiClient, | ||||||
| private readonly CacheItemPoolInterface $feedsCache, | ||||||
| ) {} | ||||||
|
|
||||||
| public function getAdminFormOptions(FeedSource $feedSource): array | ||||||
| { | ||||||
| $feedEntryRecipients = $this->feedService->getFeedSourceConfigUrl($feedSource, 'sport-center'); | ||||||
|
|
||||||
| return [ | ||||||
| [ | ||||||
| 'key' => 'brnd-sport-center-id', | ||||||
| 'input' => 'input', | ||||||
| 'type' => 'text', | ||||||
| 'name' => 'sport_center_id', | ||||||
| 'label' => 'Sportcenter ID', | ||||||
| 'formGroupClasses' => 'mb-3', | ||||||
| ], | ||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| public function getData(Feed $feed): array | ||||||
| { | ||||||
| $result = [ | ||||||
| 'title' => 'BRND Booking', | ||||||
| 'bookings' => [], | ||||||
| ]; | ||||||
|
|
||||||
| $configuration = $feed->getConfiguration(); | ||||||
| $feedSource = $feed->getFeedSource(); | ||||||
|
|
||||||
| if (null == $feedSource) { | ||||||
| return $result; | ||||||
| } | ||||||
|
|
||||||
| $secrets = new SecretsDTO($feedSource); | ||||||
|
|
||||||
| $baseUri = $secrets->apiBaseUri; | ||||||
| $sportCenterId = $configuration['sport_center_id'] ?? null; | ||||||
|
|
||||||
| if ('' === $baseUri || null === $sportCenterId || '' === $sportCenterId) { | ||||||
| return $result; | ||||||
| } | ||||||
|
|
||||||
| $feedSource = $feed->getFeedSource(); | ||||||
|
|
||||||
| if (null === $feedSource) { | ||||||
| return $result; | ||||||
| } | ||||||
|
|
||||||
| $bookings = $this->apiClient->getInfomonitorBookingsDetails($feedSource, $sportCenterId); | ||||||
|
|
||||||
| $result['bookings'] = array_map([$this, 'parseBrndBooking'], $bookings); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to filter out entries that do not meet some minimum requirements (startTime, endTime, activity, ?) I would replace array_map with array_reduce and add some validation checks and ignore broken entries. |
||||||
|
|
||||||
| return $result; | ||||||
| } | ||||||
|
|
||||||
| private function parseBrndBooking(array $booking): array | ||||||
| { | ||||||
| // Parse start time | ||||||
| $startDateTime = null; | ||||||
| if (!empty($booking['dato']) && isset($booking['starttid']) && is_string($booking['starttid'])) { | ||||||
| // Trim starttid to 6 digits after dot for microseconds | ||||||
| $starttid = preg_replace('/\.(\d{6})\d+$/', '.$1', $booking['starttid']); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace danish variable name with english. |
||||||
| $dateOnly = substr($booking['dato'], 0, 10); | ||||||
| $dateTimeString = $dateOnly.' '.$starttid; | ||||||
| $startDateTime = \DateTimeImmutable::createFromFormat('m/d/Y H:i:s.u', $dateTimeString); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if this throws an exception or returns false? |
||||||
| } | ||||||
|
|
||||||
| // Parse end time | ||||||
| $endDateTime = null; | ||||||
| if (!empty($booking['dato']) && isset($booking['sluttid']) && is_string($booking['sluttid'])) { | ||||||
| $sluttid = preg_replace('/\.(\d{6})\d+$/', '.$1', $booking['sluttid']); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace danish variable name with english. |
||||||
| $dateOnly = substr($booking['dato'], 0, 10); | ||||||
| $dateTimeString = $dateOnly.' '.$sluttid; | ||||||
| $endDateTime = \DateTimeImmutable::createFromFormat('m/d/Y H:i:s.u', $dateTimeString); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if this throws an exception or returns false? |
||||||
| } | ||||||
|
|
||||||
| return [ | ||||||
| 'bookingcode' => $booking['ansøgning'] ?? '', | ||||||
| 'remarks' => $booking['bemærkninger'] ?? '', | ||||||
| 'startTime' => $startDateTime ? $startDateTime->getTimestamp() : null, | ||||||
| 'endTime' => $endDateTime ? $endDateTime->getTimestamp() : null, | ||||||
| 'complex' => $booking['anlæg'] ?? '', | ||||||
| 'area' => $booking['område'] ?? '', | ||||||
| 'facility' => $booking['facilitet'] ?? '', | ||||||
| 'activity' => $booking['aktivitet'] ?? '', | ||||||
| 'team' => $booking['hold'] ?? '', | ||||||
| 'status' => $booking['status'] ?? '', | ||||||
| 'checkIn' => $booking['checK_IN'] ?? '', | ||||||
| 'bookingBy' => $booking['ansøgt_af'] ?? '', | ||||||
| 'changingRooms' => $booking['omklædningsrum'] ?? '', | ||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| public function getConfigOptions(Request $request, FeedSource $feedSource, string $name): ?array | ||||||
| { | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| public function getRequiredSecrets(): array | ||||||
| { | ||||||
| return [ | ||||||
| 'api_base_uri' => [ | ||||||
| 'type' => 'string', | ||||||
| 'exposeValue' => true, | ||||||
| ], | ||||||
| 'company_id' => [ | ||||||
| 'type' => 'string', | ||||||
| 'exposeValue' => true, | ||||||
| ], | ||||||
| 'api_auth_key' => [ | ||||||
| 'type' => 'string', | ||||||
| 'exposeValue' => true, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should hide this value, otherwise everybody in the admin can see the value.
Suggested change
It will still be possible to set the value, it will just not be visible in the admin after being set. |
||||||
| ], | ||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| public function getRequiredConfiguration(): array | ||||||
| { | ||||||
| return ['sport_center_id']; | ||||||
| } | ||||||
|
|
||||||
| public function getSupportedFeedOutputType(): string | ||||||
| { | ||||||
| return self::SUPPORTED_FEED_TYPE; | ||||||
| } | ||||||
|
|
||||||
| public function getSchema(): array | ||||||
| { | ||||||
| return [ | ||||||
| '$schema' => 'http://json-schema.org/draft-04/schema#', | ||||||
| 'type' => 'object', | ||||||
| 'properties' => [ | ||||||
| 'api_base_uri' => [ | ||||||
| 'type' => 'string', | ||||||
| 'format' => 'uri', | ||||||
| ], | ||||||
| 'company_id' => [ | ||||||
| 'type' => 'string', | ||||||
| ], | ||||||
| 'api_auth_key' => [ | ||||||
| 'type' => 'string', | ||||||
| ], | ||||||
| ], | ||||||
| 'required' => ['api_base_uri', 'company_id', 'api_auth_key'], | ||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| public static function getIdKey(FeedSource $feedSource): string | ||||||
| { | ||||||
| $ulid = $feedSource->getId(); | ||||||
| assert(null !== $ulid); | ||||||
|
|
||||||
| return $ulid->toBase32(); | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure this function does not throw an exception.
It should always return $result, so the client does not have to handle the exceptions.