44
55use Drupal \os2forms_fbs_handler \Client \Model \Guardian ;
66use Drupal \os2forms_fbs_handler \Client \Model \Patron ;
7+ use Fig \Http \Message \RequestMethodInterface ;
78use GuzzleHttp \Client ;
89use Symfony \Component \HttpFoundation \Request ;
910
@@ -75,34 +76,22 @@ public function isLoggedIn(): bool {
7576 * @param string $cpr
7677 * The users personal security number.
7778 *
78- * @return \Drupal\os2forms_fbs_handler\Client\Model\Patron |null
79- * NULL if not else the Patron .
79+ * @return string |null
80+ * NULL if not else the PatronId .
8081 *
8182 * @throws \GuzzleHttp\Exception\GuzzleException
8283 * @throws \JsonException
8384 */
84- public function doUserExists (string $ cpr ): ?Patron {
85+ public function authenticatePatron (string $ cpr ): ?string {
8586 // Check if session has been created with FBS and if not creates it.
8687 if (!$ this ->isLoggedIn ()) {
8788 $ this ->login ();
8889 }
8990
90- // Try pre-authenticate the user/parent .
91- $ json = $ this ->request ('/external/{agency_id}/patrons/preauthenticated/v9 ' , $ cpr );
91+ // Authenticate the patron .
92+ $ json = $ this ->request ('/external/{agency_id}/patrons/preauthenticated/v10 ' , $ cpr );
9293 if ($ json ->authenticateStatus === $ this ::AUTHENTICATE_STATUS_VALID ) {
93- return new Patron (
94- $ json ->patron ->patronId ,
95- (bool ) $ json ->patron ->receiveSms ,
96- (bool ) $ json ->patron ->receivePostalMail ,
97- $ json ->patron ->notificationProtocols ,
98- $ json ->patron ->phoneNumber ,
99- is_null ($ json ->patron ->onHold ) ? $ json ->patron ->onHold : (array ) $ json ->patron ->onHold ,
100- $ json ->patron ->preferredLanguage ,
101- (bool ) $ json ->patron ->guardianVisibility ,
102- $ json ->patron ->emailAddress ,
103- (bool ) $ json ->patron ->receiveEmail ,
104- $ json ->patron ->preferredPickupBranch
105- );
94+ return $ json ->patronId ;
10695 }
10796
10897 return NULL ;
@@ -123,17 +112,60 @@ public function doUserExists(string $cpr): ?Patron {
123112 * @throws \JsonException
124113 */
125114 public function createPatronWithGuardian (Patron $ patron , Guardian $ guardian ) {
126- $ uri = '/external/{agency_id}/patrons/withGuardian/v1 ' ;
115+ $ uri = '/external/{agency_id}/patrons/withGuardian/v4 ' ;
127116 $ payload = [
128- 'cprNumber ' => $ patron ->cpr ,
117+ 'personId ' => $ patron ->personId ,
129118 'pincode ' => $ patron ->pincode ,
130119 'preferredPickupBranch ' => $ patron ->preferredPickupBranch ,
131120 'name ' => 'Unknown Name ' ,
132- 'email ' => $ patron ->emailAddress ,
121+ 'emailAddresses ' => $ patron ->emailAddresses ,
133122 'guardian ' => $ guardian ->toArray (),
134123 ];
135124
136- return $ this ->request ($ uri , $ payload ,);
125+ return $ this ->request ($ uri , $ payload );
126+ }
127+
128+ /**
129+ * Get patron information.
130+ *
131+ * @param string $patronId
132+ * The patron to update.
133+ *
134+ * @return \Drupal\os2forms_fbs_handler\Client\Model\Patron
135+ * Patron object
136+ *
137+ * @throws \GuzzleHttp\Exception\GuzzleException
138+ * @throws \JsonException
139+ */
140+ public function getPatron (string $ patronId ): ?Patron {
141+ $ uri = '/external/{agency_id}/patrons/ ' . $ patronId . '/v4 ' ;
142+
143+ $ json = $ this ->request ($ uri , [], RequestMethodInterface::METHOD_GET );
144+
145+ if ($ json ->authenticateStatus === "VALID " ) {
146+ return new Patron (
147+ $ json ->patron ->patronId ,
148+ (bool ) $ json ->patron ->receiveSms ,
149+ (bool ) $ json ->patron ->receivePostalMail ,
150+ $ json ->patron ->notificationProtocols ,
151+ $ json ->patron ->phoneNumber ,
152+ is_null ($ json ->patron ->onHold ) ? $ json ->patron ->onHold : (array ) $ json ->patron ->onHold ,
153+ $ json ->patron ->preferredLanguage ,
154+ (bool ) $ json ->patron ->guardianVisibility ,
155+ $ json ->patron ->defaultInterestPeriod ,
156+ (bool ) $ json ->patron ->resident ,
157+ [
158+ [
159+ 'emailAddress ' => $ json ->patron ->emailAddress ,
160+ 'receiveNotification ' => $ json ->patron ->receiveEmail ,
161+ ],
162+ ],
163+ (bool ) $ json ->patron ->receiveEmail ,
164+ $ json ->patron ->preferredPickupBranch
165+ );
166+ }
167+
168+ return NULL ;
137169 }
138170
139171 /**
@@ -149,19 +181,27 @@ public function createPatronWithGuardian(Patron $patron, Guardian $guardian) {
149181 * @throws \JsonException
150182 */
151183 public function updatePatron (Patron $ patron ): bool {
152- $ uri = '/external/{agency_id}/patrons/ ' . $ patron ->patronId . '/v6 ' ;
184+ $ uri = '/external/{agency_id}/patrons/ ' . $ patron ->patronId . '/v8 ' ;
153185 $ payload = [
154- 'patronid ' => $ patron ->patronId ,
155- 'patron ' => $ patron ->toArray (),
186+ 'patron ' => [
187+ 'preferredPickupBranch ' => $ patron ->preferredPickupBranch ,
188+ 'emailAddresses ' => $ patron ->emailAddresses ,
189+ 'guardianVisibility ' => $ patron ->guardianVisibility ,
190+ 'receivePostalMail ' => $ patron ->receiveEmail ,
191+ 'phoneNumbers ' => [
192+ [
193+ 'receiveNotification ' => TRUE ,
194+ 'phoneNumber ' => $ patron ->phoneNumber ,
195+ ],
196+ ],
197+ ],
156198 'pincodeChange ' => [
157199 'pincode ' => $ patron ->pincode ,
158- 'libraryCardNumber ' => $ patron ->cpr ,
200+ 'libraryCardNumber ' => $ patron ->personId ,
159201 ],
160202 ];
161203
162- $ json = $ this ->request ($ uri , $ payload , Request::METHOD_PUT );
163-
164- return $ json ->authenticateStatus === $ this ::AUTHENTICATE_STATUS_VALID ;
204+ return $ this ->request ($ uri , $ payload , RequestMethodInterface::METHOD_PUT );
165205 }
166206
167207 /**
@@ -179,7 +219,7 @@ public function updatePatron(Patron $patron): bool {
179219 * @throws \JsonException
180220 */
181221 public function createGuardian (Patron $ patron , Guardian $ guardian ): int {
182- $ uri = '/external/{agency_id}/patrons/withGuardian/v1 ' ;
222+ $ uri = '/external/{agency_id}/patrons/withGuardian/v2 ' ;
183223 $ payload = [
184224 'patronId ' => $ patron ->patronId ,
185225 'guardian ' => $ guardian ->toArray (),
@@ -199,10 +239,12 @@ public function createGuardian(Patron $patron, Guardian $guardian): int {
199239 * The type of request to send (Default: POST).
200240 *
201241 * @return mixed
202- * Json response from FBS.
242+ * Json response from FBS or TRUE on updatePatron response .
203243 *
204244 * @throws \GuzzleHttp\Exception\GuzzleException
205245 * @throws \JsonException
246+ *
247+ * @phpstan-param array<mixed>|string $data
206248 */
207249 private function request (string $ uri , array |string $ data , string $ method = Request::METHOD_POST ): mixed {
208250 $ url = rtrim ($ this ->endpoint , '/ \\' );
@@ -230,6 +272,10 @@ private function request(string $uri, array|string $data, string $method = Reque
230272
231273 $ response = $ this ->client ->request ($ method , $ url , $ options );
232274
275+ if ($ response ->getStatusCode () === 204 ) {
276+ return TRUE ;
277+ }
278+
233279 return json_decode ($ response ->getBody (), FALSE , 512 , JSON_THROW_ON_ERROR );
234280 }
235281
0 commit comments