diff --git a/CHANGELOG.md b/CHANGELOG.md index 8146cb76..cd5369e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- Updated `os2forms_fbs_handler` to use latest endpoints and operations. + ## [3.21.0] 2024-12-17 - Updated `os2web_audit`. diff --git a/modules/os2forms_fbs_handler/src/Client/FBS.php b/modules/os2forms_fbs_handler/src/Client/FBS.php index 660a83a0..44b8f6a7 100644 --- a/modules/os2forms_fbs_handler/src/Client/FBS.php +++ b/modules/os2forms_fbs_handler/src/Client/FBS.php @@ -75,34 +75,22 @@ public function isLoggedIn(): bool { * @param string $cpr * The users personal security number. * - * @return \Drupal\os2forms_fbs_handler\Client\Model\Patron|null - * NULL if not else the Patron. + * @return string|null + * NULL if not else the PatronId. * * @throws \GuzzleHttp\Exception\GuzzleException * @throws \JsonException */ - public function doUserExists(string $cpr): ?Patron { + public function authenticatePatron(string $cpr): ?string { // Check if session has been created with FBS and if not creates it. if (!$this->isLoggedIn()) { $this->login(); } - // Try pre-authenticate the user/parent. - $json = $this->request('/external/{agency_id}/patrons/preauthenticated/v9', $cpr); + // Authenticate the patron. + $json = $this->request('/external/{agency_id}/patrons/preauthenticated/v10', $cpr); if ($json->authenticateStatus === $this::AUTHENTICATE_STATUS_VALID) { - return new Patron( - $json->patron->patronId, - (bool) $json->patron->receiveSms, - (bool) $json->patron->receivePostalMail, - $json->patron->notificationProtocols, - $json->patron->phoneNumber, - is_null($json->patron->onHold) ? $json->patron->onHold : (array) $json->patron->onHold, - $json->patron->preferredLanguage, - (bool) $json->patron->guardianVisibility, - $json->patron->emailAddress, - (bool) $json->patron->receiveEmail, - $json->patron->preferredPickupBranch - ); + return $json->patronId; } return NULL; @@ -123,17 +111,60 @@ public function doUserExists(string $cpr): ?Patron { * @throws \JsonException */ public function createPatronWithGuardian(Patron $patron, Guardian $guardian) { - $uri = '/external/{agency_id}/patrons/withGuardian/v1'; + $uri = '/external/{agency_id}/patrons/withGuardian/v4'; $payload = [ - 'cprNumber' => $patron->cpr, + 'personId' => $patron->personId, 'pincode' => $patron->pincode, 'preferredPickupBranch' => $patron->preferredPickupBranch, 'name' => 'Unknown Name', - 'email' => $patron->emailAddress, + 'emailAddresses' => $patron->emailAddresses, 'guardian' => $guardian->toArray(), ]; - return $this->request($uri, $payload,); + return $this->request($uri, $payload); + } + + /** + * Get patron information. + * + * @param string $patronId + * The patron to update. + * + * @return \Drupal\os2forms_fbs_handler\Client\Model\Patron + * Patron object + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \JsonException + */ + public function getPatron(string $patronId): ?Patron { + $uri = '/external/{agency_id}/patrons/' . $patronId . '/v4'; + + $json = $this->request($uri, [], RequestMethodInterface::METHOD_GET); + + if ($json->authenticateStatus === "VALID") { + return new Patron( + $json->patron->patronId, + (bool) $json->patron->receiveSms, + (bool) $json->patron->receivePostalMail, + $json->patron->notificationProtocols, + $json->patron->phoneNumber, + is_null($json->patron->onHold) ? $json->patron->onHold : (array) $json->patron->onHold, + $json->patron->preferredLanguage, + (bool) $json->patron->guardianVisibility, + $json->patron->defaultInterestPeriod, + (bool) $json->patron->resident, + [ + [ + 'emailAddress' => $json->patron->emailAddress, + 'receiveNotification' => $json->patron->receiveEmail, + ], + ], + (bool) $json->patron->receiveEmail, + $json->patron->preferredPickupBranch + ); + } + + return NULL; } /** @@ -149,19 +180,27 @@ public function createPatronWithGuardian(Patron $patron, Guardian $guardian) { * @throws \JsonException */ public function updatePatron(Patron $patron): bool { - $uri = '/external/{agency_id}/patrons/' . $patron->patronId . '/v6'; + $uri = '/external/{agency_id}/patrons/' . $patron->patronId . '/v8'; $payload = [ - 'patronid' => $patron->patronId, - 'patron' => $patron->toArray(), + 'patron' => [ + 'preferredPickupBranch' => $patron->preferredPickupBranch, + 'emailAddresses' => $patron->emailAddresses, + 'guardianVisibility' => $patron->guardianVisibility, + 'receivePostalMail' => $patron->receiveEmail, + 'phoneNumbers' => [ + [ + 'receiveNotification' => TRUE, + 'phoneNumber' => $patron->phoneNumber, + ], + ], + ], 'pincodeChange' => [ 'pincode' => $patron->pincode, - 'libraryCardNumber' => $patron->cpr, + 'libraryCardNumber' => $patron->personId, ], ]; - $json = $this->request($uri, $payload, Request::METHOD_PUT); - - return $json->authenticateStatus === $this::AUTHENTICATE_STATUS_VALID; + return $this->request($uri, $payload, RequestMethodInterface::METHOD_PUT); } /** @@ -179,7 +218,7 @@ public function updatePatron(Patron $patron): bool { * @throws \JsonException */ public function createGuardian(Patron $patron, Guardian $guardian): int { - $uri = '/external/{agency_id}/patrons/withGuardian/v1'; + $uri = '/external/{agency_id}/patrons/withGuardian/v2'; $payload = [ 'patronId' => $patron->patronId, 'guardian' => $guardian->toArray(), @@ -199,7 +238,7 @@ public function createGuardian(Patron $patron, Guardian $guardian): int { * The type of request to send (Default: POST). * * @return mixed - * Json response from FBS. + * Json response from FBS or TRUE on updatePatron response. * * @throws \GuzzleHttp\Exception\GuzzleException * @throws \JsonException @@ -230,6 +269,10 @@ private function request(string $uri, array|string $data, string $method = Reque $response = $this->client->request($method, $url, $options); + if ($response->getStatusCode() === 204) { + return TRUE; + } + return json_decode($response->getBody(), FALSE, 512, JSON_THROW_ON_ERROR); } diff --git a/modules/os2forms_fbs_handler/src/Client/Model/Guardian.php b/modules/os2forms_fbs_handler/src/Client/Model/Guardian.php index 150db93e..371d3bba 100644 --- a/modules/os2forms_fbs_handler/src/Client/Model/Guardian.php +++ b/modules/os2forms_fbs_handler/src/Client/Model/Guardian.php @@ -25,7 +25,7 @@ public function __construct( */ public function toArray(): array { return [ - 'cprNumber' => $this->cpr, + 'personIdentifier' => $this->cpr, 'name' => $this->name, 'email' => $this->email, ]; diff --git a/modules/os2forms_fbs_handler/src/Client/Model/Patron.php b/modules/os2forms_fbs_handler/src/Client/Model/Patron.php index 4847c944..e5e7d562 100644 --- a/modules/os2forms_fbs_handler/src/Client/Model/Patron.php +++ b/modules/os2forms_fbs_handler/src/Client/Model/Patron.php @@ -19,11 +19,13 @@ public function __construct( public readonly ?array $onHold = NULL, public readonly ?string $preferredLanguage = NULL, public readonly ?bool $guardianVisibility = NULL, + public readonly ?int $defaultInterestPeriod = NULL, + public readonly ?bool $resident = NULL, // Allow these properties below to be updatable. - public ?string $emailAddress = NULL, + public ?array $emailAddresses = NULL, public ?bool $receiveEmail = NULL, public ?string $preferredPickupBranch = NULL, - public ?string $cpr = NULL, + public ?string $personId = NULL, public ?string $pincode = NULL, ) { } @@ -36,16 +38,19 @@ public function __construct( */ public function toArray(): array { return [ + 'patronId' => $this->patronId, 'receiveEmail' => $this->receiveEmail, 'receiveSms' => $this->receiveSms, 'receivePostalMail' => $this->receivePostalMail, - 'emailAddress' => $this->emailAddress, + 'emailAddresses' => $this->emailAddresses, 'notificationProtocols' => $this->notificationProtocols, 'phoneNumber' => $this->phoneNumber, 'preferredPickupBranch' => $this->preferredPickupBranch, 'onHold' => $this->onHold, 'preferredLanguage' => $this->preferredLanguage, 'guardianVisibility' => $this->guardianVisibility, + 'defaultInterestPeriod' => $this->defaultInterestPeriod, + 'resident' => $this->resident, ]; } diff --git a/modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php b/modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php index 3afe322c..179d0a1a 100644 --- a/modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php +++ b/modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php @@ -87,9 +87,6 @@ public function process(Job $job): JobResult { $data = $webformSubmission->getData(); - // Checker child patron exists. - $patron = $fbs->doUserExists($data['barn_cpr']); - // Create Guardian. $guardian = new Guardian( $data['cpr'], @@ -97,26 +94,43 @@ public function process(Job $job): JobResult { $data['email'] ); + // Check if child patron exists. + $patronId = $fbs->authenticatePatron($data['barn_cpr']); + // If "yes" update the child patron and create the guardian (the // guardian is not another patron user). - if (!is_null($patron)) { - // Create Patron object with updated values. - $patron->preferredPickupBranch = $data['afhentningssted']; - $patron->emailAddress = $data['barn_mail']; - $patron->receiveEmail = TRUE; - $patron->cpr = $data['barn_cpr']; - $patron->pincode = $data['pinkode']; - - $fbs->updatePatron($patron); - $fbs->createGuardian($patron, $guardian); + if (!is_null($patronId)) { + // Fetch patron. + $patron = $fbs->getPatron($patronId); + + if (!is_null($patron)) { + // Create Patron object with updated values. + $patron->preferredPickupBranch = $data['afhentningssted']; + $patron->emailAddresses = [ + [ + 'emailAddress' => $data['barn_mail'], + 'receiveNotification' => TRUE, + ], + ]; + $patron->receiveEmail = TRUE; + $patron->pincode = $data['pinkode']; + + $fbs->updatePatron($patron); + $fbs->createGuardian($patron, $guardian); + } } else { // If "no" create child patron and guardian. $patron = new Patron(); $patron->preferredPickupBranch = $data['afhentningssted']; - $patron->emailAddress = $data['barn_mail']; + $patron->emailAddresses = [ + [ + 'emailAddress' => $data['barn_mail'], + 'receiveNotification' => TRUE, + ], + ]; $patron->receiveEmail = TRUE; - $patron->cpr = $data['barn_cpr']; + $patron->personId = $data['barn_cpr']; $patron->pincode = $data['pinkode']; $fbs->createPatronWithGuardian($patron, $guardian);