Skip to content

Commit a0de500

Browse files
authored
Merge branch 'develop' into f/module_cleanup
2 parents 16a7837 + fc33762 commit a0de500

File tree

9 files changed

+154
-52
lines changed

9 files changed

+154
-52
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ before starting to add changes. Use example [placed in the end of the page](#exa
1313

1414
- Removed modules ldap_auth, logging_alerts, maillog
1515

16+
## [3.21.2] 2025-01-07
17+
18+
- Adds missing `http-message-util` requirement and use statement.
19+
- Runs code-analysis on `os2forms_fbs_handler` module.
20+
21+
## [3.21.1] 2025-01-06
22+
23+
- Updated Maestro notification handler assignment message format.
24+
- Updated `os2forms_fbs_handler` to use latest endpoints and operations.
25+
1626
## [3.21.0] 2024-12-17
1727

1828
- Updated `os2web_audit`.
@@ -325,7 +335,9 @@ before starting to add changes. Use example [placed in the end of the page](#exa
325335
- Security in case of vulnerabilities.
326336
```
327337

328-
[Unreleased]: https://github.com/OS2Forms/os2forms/compare/3.21.0...HEAD
338+
[Unreleased]: https://github.com/OS2Forms/os2forms/compare/3.21.2...HEAD
339+
[3.21.2]: https://github.com/OS2Forms/os2forms/compare/3.21.1...3.21.2
340+
[3.21.1]: https://github.com/OS2Forms/os2forms/compare/3.21.0...3.21.1
329341
[3.21.0]: https://github.com/OS2Forms/os2forms/compare/3.20.1...3.21.0
330342
[3.20.1]: https://github.com/OS2Forms/os2forms/compare/3.20.0...3.20.1
331343
[3.20.0]: https://github.com/OS2Forms/os2forms/compare/3.19.0...3.20.0

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"drupal/webform_validation": "^2.0",
6666
"drupal/webform_views": "^5.0@alpha",
6767
"drupal/workflow_participants": "^3.0@RC",
68+
"fig/http-message-util": "^1.1",
6869
"http-interop/http-factory-guzzle": "^1.0.0",
6970
"itk-dev/beskedfordeler-drupal": "^1.0",
7071
"itk-dev/serviceplatformen": "^1.5",

modules/os2forms_fbs_handler/src/Client/FBS.php

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\os2forms_fbs_handler\Client\Model\Guardian;
66
use Drupal\os2forms_fbs_handler\Client\Model\Patron;
7+
use Fig\Http\Message\RequestMethodInterface;
78
use GuzzleHttp\Client;
89
use 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

modules/os2forms_fbs_handler/src/Client/Model/Guardian.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public function __construct(
2222
*
2323
* @return array
2424
* Array with field required by FBS calls.
25+
*
26+
* @phpstan-return array<string, string>
2527
*/
2628
public function toArray(): array {
2729
return [
28-
'cprNumber' => $this->cpr,
30+
'personIdentifier' => $this->cpr,
2931
'name' => $this->name,
3032
'email' => $this->email,
3133
];

modules/os2forms_fbs_handler/src/Client/Model/Patron.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ final class Patron {
99

1010
/**
1111
* Default constructor.
12+
*
13+
* @phpstan-param array<mixed>|null $notificationProtocols
14+
* @phpstan-param array<mixed>|null $onHold
15+
* @phpstan-param array<mixed>|null $emailAddresses
1216
*/
1317
public function __construct(
1418
public readonly ?string $patronId = NULL,
@@ -19,11 +23,13 @@ public function __construct(
1923
public readonly ?array $onHold = NULL,
2024
public readonly ?string $preferredLanguage = NULL,
2125
public readonly ?bool $guardianVisibility = NULL,
26+
public readonly ?int $defaultInterestPeriod = NULL,
27+
public readonly ?bool $resident = NULL,
2228
// Allow these properties below to be updatable.
23-
public ?string $emailAddress = NULL,
29+
public ?array $emailAddresses = NULL,
2430
public ?bool $receiveEmail = NULL,
2531
public ?string $preferredPickupBranch = NULL,
26-
public ?string $cpr = NULL,
32+
public ?string $personId = NULL,
2733
public ?string $pincode = NULL,
2834
) {
2935
}
@@ -33,19 +39,24 @@ public function __construct(
3339
*
3440
* @return array
3541
* Array with field required by FBS calls.
42+
*
43+
* @phpstan-return array<string, string>
3644
*/
3745
public function toArray(): array {
3846
return [
47+
'patronId' => $this->patronId,
3948
'receiveEmail' => $this->receiveEmail,
4049
'receiveSms' => $this->receiveSms,
4150
'receivePostalMail' => $this->receivePostalMail,
42-
'emailAddress' => $this->emailAddress,
51+
'emailAddresses' => $this->emailAddresses,
4352
'notificationProtocols' => $this->notificationProtocols,
4453
'phoneNumber' => $this->phoneNumber,
4554
'preferredPickupBranch' => $this->preferredPickupBranch,
4655
'onHold' => $this->onHold,
4756
'preferredLanguage' => $this->preferredLanguage,
4857
'guardianVisibility' => $this->guardianVisibility,
58+
'defaultInterestPeriod' => $this->defaultInterestPeriod,
59+
'resident' => $this->resident,
4960
];
5061
}
5162

modules/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ final class FbsCreateUser extends JobTypeBase implements ContainerFactoryPluginI
3535

3636
/**
3737
* {@inheritdoc}
38+
*
39+
* @phpstan-param array<mixed> $configuration
3840
*/
3941
public function __construct(
4042
array $configuration,
@@ -50,6 +52,8 @@ public function __construct(
5052

5153
/**
5254
* {@inheritdoc}
55+
*
56+
* @phpstan-param array<mixed> $configuration
5357
*/
5458
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
5559
return new static(
@@ -87,36 +91,50 @@ public function process(Job $job): JobResult {
8791

8892
$data = $webformSubmission->getData();
8993

90-
// Checker child patron exists.
91-
$patron = $fbs->doUserExists($data['barn_cpr']);
92-
9394
// Create Guardian.
9495
$guardian = new Guardian(
9596
$data['cpr'],
9697
$data['navn'],
9798
$data['email']
9899
);
99100

101+
// Check if child patron exists.
102+
$patronId = $fbs->authenticatePatron($data['barn_cpr']);
103+
100104
// If "yes" update the child patron and create the guardian (the
101105
// guardian is not another patron user).
102-
if (!is_null($patron)) {
103-
// Create Patron object with updated values.
104-
$patron->preferredPickupBranch = $data['afhentningssted'];
105-
$patron->emailAddress = $data['barn_mail'];
106-
$patron->receiveEmail = TRUE;
107-
$patron->cpr = $data['barn_cpr'];
108-
$patron->pincode = $data['pinkode'];
109-
110-
$fbs->updatePatron($patron);
111-
$fbs->createGuardian($patron, $guardian);
106+
if (!is_null($patronId)) {
107+
// Fetch patron.
108+
$patron = $fbs->getPatron($patronId);
109+
110+
if (!is_null($patron)) {
111+
// Create Patron object with updated values.
112+
$patron->preferredPickupBranch = $data['afhentningssted'];
113+
$patron->emailAddresses = [
114+
[
115+
'emailAddress' => $data['barn_mail'],
116+
'receiveNotification' => TRUE,
117+
],
118+
];
119+
$patron->receiveEmail = TRUE;
120+
$patron->pincode = $data['pinkode'];
121+
122+
$fbs->updatePatron($patron);
123+
$fbs->createGuardian($patron, $guardian);
124+
}
112125
}
113126
else {
114127
// If "no" create child patron and guardian.
115128
$patron = new Patron();
116129
$patron->preferredPickupBranch = $data['afhentningssted'];
117-
$patron->emailAddress = $data['barn_mail'];
130+
$patron->emailAddresses = [
131+
[
132+
'emailAddress' => $data['barn_mail'],
133+
'receiveNotification' => TRUE,
134+
],
135+
];
118136
$patron->receiveEmail = TRUE;
119-
$patron->cpr = $data['barn_cpr'];
137+
$patron->personId = $data['barn_cpr'];
120138
$patron->pincode = $data['pinkode'];
121139

122140
$fbs->createPatronWithGuardian($patron, $guardian);

0 commit comments

Comments
 (0)