diff --git a/CHANGELOG.md b/CHANGELOG.md index 719d61a3..8c4dabae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret. ## [Under udvikling] +* Opdaterede `os2forms` core modulet. + * Skiftede til FBS modulet i `os2forms` core. + * Sikrede at Maestro notifikationshandleren gemmer besked format. * Slog `NemID CPR Fetch data`-elementet fra. * Opdaterede `os2forms_get_organized`. * Normaliserer white spaces i filnavne. diff --git a/composer.lock b/composer.lock index 83fdca50..1f52282f 100644 --- a/composer.lock +++ b/composer.lock @@ -9254,16 +9254,16 @@ }, { "name": "os2forms/os2forms", - "version": "3.21.0", + "version": "3.21.1", "source": { "type": "git", "url": "https://github.com/OS2Forms/os2forms.git", - "reference": "8f4bbfafa7315c4804255fdc323d8fb6e9b78dfd" + "reference": "b6f79ec64c0c5b2794aa21dbb9b8728e1a25fa6b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/OS2Forms/os2forms/zipball/8f4bbfafa7315c4804255fdc323d8fb6e9b78dfd", - "reference": "8f4bbfafa7315c4804255fdc323d8fb6e9b78dfd", + "url": "https://api.github.com/repos/OS2Forms/os2forms/zipball/b6f79ec64c0c5b2794aa21dbb9b8728e1a25fa6b", + "reference": "b6f79ec64c0c5b2794aa21dbb9b8728e1a25fa6b", "shasum": "" }, "require": { @@ -9380,9 +9380,9 @@ "description": "Drupal 8 OS2Form module provides advanced webform functionality for Danish Municipalities", "support": { "issues": "https://github.com/OS2Forms/os2forms/issues", - "source": "https://github.com/OS2Forms/os2forms/tree/3.21.0" + "source": "https://github.com/OS2Forms/os2forms/tree/3.21.1" }, - "time": "2024-12-17T10:12:18+00:00" + "time": "2025-01-06T10:11:14+00:00" }, { "name": "os2forms/os2forms_forloeb_profile", diff --git a/web/modules/custom/os2forms_fbs_handler/config/install/advancedqueue.advancedqueue_queue.os2forms_fbs_handler.yml b/web/modules/custom/os2forms_fbs_handler/config/install/advancedqueue.advancedqueue_queue.os2forms_fbs_handler.yml deleted file mode 100644 index 29d43d9d..00000000 --- a/web/modules/custom/os2forms_fbs_handler/config/install/advancedqueue.advancedqueue_queue.os2forms_fbs_handler.yml +++ /dev/null @@ -1,13 +0,0 @@ -status: true -dependencies: - enforced: - module: - - os2forms_fbs_handler -id: os2forms_fbs_handler -label: os2forms_fbs_handler -backend: database -backend_configuration: - lease_time: 300 -processor: cron -processing_time: 90 -locked: false diff --git a/web/modules/custom/os2forms_fbs_handler/os2forms_fbs_handler.info.yml b/web/modules/custom/os2forms_fbs_handler/os2forms_fbs_handler.info.yml deleted file mode 100644 index d7112b24..00000000 --- a/web/modules/custom/os2forms_fbs_handler/os2forms_fbs_handler.info.yml +++ /dev/null @@ -1,8 +0,0 @@ -name: 'FBS Handler' -type: module -description: 'Provides integration to FBS.' -package: 'OS2Forms' -core_version_requirement: ^9 || ^10 -dependencies: - - 'webform:webform' - - 'advancedqueue:advancedqueue' diff --git a/web/modules/custom/os2forms_fbs_handler/src/Client/FBS.php b/web/modules/custom/os2forms_fbs_handler/src/Client/FBS.php deleted file mode 100644 index a72b8895..00000000 --- a/web/modules/custom/os2forms_fbs_handler/src/Client/FBS.php +++ /dev/null @@ -1,276 +0,0 @@ - $this->username, - 'password' => $this->password, - ]; - - $json = $this->request($uri, $payload); - if (isset($json->sessionKey)) { - $this->sessionKey = $json->sessionKey; - - return TRUE; - } - - return FALSE; - } - - /** - * Check is user is logged in. - * - * @return bool - * TRUE if logged in else FALSE. - */ - public function isLoggedIn(): bool { - return isset($this->sessionKey); - } - - /** - * Check if user exists. - * - * @param string $cpr - * The users personal security number. - * - * @return string|null - * NULL if not else the PatronId. - * - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException - */ - public function authenticatePatron(string $cpr): ?string { - // Check if session have been created with FBS and if not create it. - if (!$this->isLoggedIn()) { - $this->login(); - } - - // Authenticate the patron. - $json = $this->request('/external/{agency_id}/patrons/preauthenticated/v10', $cpr); - if ($json->authenticateStatus === 'VALID') { - return $json->patronId; - } - - return NULL; - } - - /** - * Create new patron with guardian attached. - * - * @param \Drupal\os2forms_fbs_handler\Client\Model\Patron $patron - * The patron to create. - * @param \Drupal\os2forms_fbs_handler\Client\Model\Guardian $guardian - * The guardian to attach to the parton. - * - * @return mixed - * JSON response from FBS. - * - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException - */ - public function createPatronWithGuardian(Patron $patron, Guardian $guardian) { - $uri = '/external/{agency_id}/patrons/withGuardian/v4'; - $payload = [ - 'personId' => $patron->personId, - 'pincode' => $patron->pincode, - 'preferredPickupBranch' => $patron->preferredPickupBranch, - 'name' => 'Unknown Name', - 'emailAddresses' => $patron->emailAddresses, - 'guardian' => $guardian->toArray(), - ]; - - return $this->request($uri, $payload); - } - - /** - * Update 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; - } - - /** - * Update patron information. - * - * @param \Drupal\os2forms_fbs_handler\Client\Model\Patron $patron - * The patron to update. - * - * @return bool - * TRUE if success else FALSE. - * - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException - */ - public function updatePatron(Patron $patron): bool { - $uri = '/external/{agency_id}/patrons/' . $patron->patronId . '/v8'; - $payload = [ - '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->personId, - ], - ]; - - return $this->request($uri, $payload, RequestMethodInterface::METHOD_PUT); - } - - /** - * Create guardian for patron. - * - * @param \Drupal\os2forms_fbs_handler\Client\Model\Patron $patron - * Patron to create guardian for. - * @param \Drupal\os2forms_fbs_handler\Client\Model\Guardian $guardian - * The guardian to create. - * - * @return int - * Guardian identifier. - * - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException - */ - public function createGuardian(Patron $patron, Guardian $guardian): int { - $uri = '/external/{agency_id}/patrons/withGuardian/v2'; - $payload = [ - 'patronId' => $patron->patronId, - 'guardian' => $guardian->toArray(), - ]; - - return $this->request($uri, $payload, RequestMethodInterface::METHOD_PUT); - } - - /** - * Send request to FSB. - * - * @param string $uri - * The uri/path to send request to. - * @param array|string $data - * The json or string to send to FBS. - * @param string $method - * The type of request to send (Default: POST). - * - * @return mixed - * Json response from FBS or TRUE on updatePatron response. - * - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws \JsonException - */ - private function request(string $uri, array|string $data, string $method = RequestMethodInterface::METHOD_POST): mixed { - $url = rtrim($this->endpoint, '/\\'); - $url = $url . str_replace('{agency_id}', $this->agencyId, $uri); - - $options = [ - 'headers' => [ - 'Content-type' => 'application/json; charset=utf-8', - ], - ]; - - // The API designer at FBS don't always use JSON. So in some cases only a - // string should be sent. - if (is_array($data)) { - $options['json'] = $data; - } - else { - $options['body'] = $data; - } - - // If already logged in lets add the session key to the request headers. - if ($this->isLoggedIn()) { - $options['headers']['X-Session'] = $this->sessionKey; - } - - $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/web/modules/custom/os2forms_fbs_handler/src/Client/Model/Guardian.php b/web/modules/custom/os2forms_fbs_handler/src/Client/Model/Guardian.php deleted file mode 100644 index 371d3bba..00000000 --- a/web/modules/custom/os2forms_fbs_handler/src/Client/Model/Guardian.php +++ /dev/null @@ -1,34 +0,0 @@ - $this->cpr, - 'name' => $this->name, - 'email' => $this->email, - ]; - } - -} diff --git a/web/modules/custom/os2forms_fbs_handler/src/Client/Model/Patron.php b/web/modules/custom/os2forms_fbs_handler/src/Client/Model/Patron.php deleted file mode 100644 index e5e7d562..00000000 --- a/web/modules/custom/os2forms_fbs_handler/src/Client/Model/Patron.php +++ /dev/null @@ -1,57 +0,0 @@ - $this->patronId, - 'receiveEmail' => $this->receiveEmail, - 'receiveSms' => $this->receiveSms, - 'receivePostalMail' => $this->receivePostalMail, - '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/web/modules/custom/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php b/web/modules/custom/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php deleted file mode 100644 index 017c8003..00000000 --- a/web/modules/custom/os2forms_fbs_handler/src/Plugin/AdvancedQueue/JobType/FbsCreateUser.php +++ /dev/null @@ -1,154 +0,0 @@ -submissionLogger = $loggerFactory->get('webform_submission'); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('logger.factory'), - $container->get('http_client') - ); - } - - /** - * {@inheritdoc} - */ - public function process(Job $job): JobResult { - try { - $payload = $job->getPayload(); - - /** @var \Drupal\webform\WebformSubmissionInterface $webformSubmission */ - $webformSubmission = WebformSubmission::load($payload['submissionId']); - $logger_context = [ - 'handler_id' => 'os2forms_fbs', - 'channel' => 'webform_submission', - 'webform_submission' => $webformSubmission, - 'operation' => 'response from queue', - ]; - $config = $payload['configuration']; - - try { - $fbs = new FBS($this->client, $config['endpoint_url'], $config['agency_id'], $config['username'], $config['password']); - - // Log into FBS and obtain session. - $fbs->login(); - - $data = $webformSubmission->getData(); - - // Create Guardian. - $guardian = new Guardian( - $data['cpr'], - $data['navn'], - $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($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->emailAddresses = [ - [ - 'emailAddress' => $data['barn_mail'], - 'receiveNotification' => TRUE, - ], - ]; - $patron->receiveEmail = TRUE; - $patron->personId = $data['barn_cpr']; - $patron->pincode = $data['pinkode']; - - $fbs->createPatronWithGuardian($patron, $guardian); - } - - $this->submissionLogger->notice($this->t('The submission #@serial was successfully delivered', ['@serial' => $webformSubmission->serial()]), $logger_context); - - return JobResult::success(); - } - catch (\Exception | GuzzleException $e) { - $this->submissionLogger->error($this->t('The submission #@serial failed (@message)', [ - '@serial' => $webformSubmission->serial(), - '@message' => $e->getMessage(), - ]), $logger_context); - - return JobResult::failure($e->getMessage()); - } - } - catch (\Exception $e) { - return JobResult::failure($e->getMessage()); - } - } - -} diff --git a/web/modules/custom/os2forms_fbs_handler/src/Plugin/WebformHandler/FbsWebformHandler.php b/web/modules/custom/os2forms_fbs_handler/src/Plugin/WebformHandler/FbsWebformHandler.php deleted file mode 100644 index f857dd07..00000000 --- a/web/modules/custom/os2forms_fbs_handler/src/Plugin/WebformHandler/FbsWebformHandler.php +++ /dev/null @@ -1,212 +0,0 @@ -setConfiguration($configuration); - $this->loggerFactory = $loggerFactory; - $this->configFactory = $configFactory; - $this->renderer = $renderer; - $this->entityTypeManager = $entityTypeManager; - $this->conditionsValidator = $conditionsValidator; - $this->tokenManager = $tokenManager; - $this->submissionLogger = $loggerFactory->get('webform_submission'); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('logger.factory'), - $container->get('config.factory'), - $container->get('renderer'), - $container->get('entity_type.manager'), - $container->get('webform_submission.conditions_validator'), - $container->get('webform.token_manager') - ); - } - - /** - * {@inheritdoc} - */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { - if (is_null($this->getQueue())) { - $form['queue_message'] = [ - '#theme' => 'status_messages', - '#message_list' => [ - 'warning' => [$this->t('Cannot get queue @queue_id', ['@queue_id' => $this->queueId])], - ], - ]; - } - - $translation_options = ['context' => 'FBS configuration']; - - $form['wrapper'] = [ - '#type' => 'fieldset', - '#title' => $this->t('FBS configuration', [], $translation_options), - '#tree' => TRUE, - ]; - - $form['wrapper']['agency_id'] = [ - '#type' => 'textfield', - '#title' => $this->t('ISIL', [], $translation_options), - '#description' => $this->t('The library\'s ISIL number (e.g. "DK-775100" for Aarhus libraries', [], $translation_options), - '#required' => TRUE, - '#default_value' => $this->configuration['agency_id'] ?? '', - ]; - - $form['wrapper']['endpoint_url'] = [ - '#type' => 'url', - '#title' => $this->t('FBS endpoint URL', [], $translation_options), - '#description' => $this->t('The URL for the FBS REST service, usually something like https://et.cicero-fbs.com/rest'), - '#required' => TRUE, - '#default_value' => $this->configuration['endpoint_url'] ?? 'https://cicero-fbs.com/rest/', - ]; - - $form['wrapper']['username'] = [ - '#type' => 'textfield', - '#title' => $this->t('Username', [], $translation_options), - '#description' => $this->t('FBS username to allow connection to FBS'), - '#required' => TRUE, - '#default_value' => $this->configuration['username'] ?? '', - ]; - - $form['wrapper']['password'] = [ - '#type' => 'password', - '#title' => $this->t('Password', [], $translation_options), - '#description' => $this->t('Password to access the API'), - '#required' => TRUE, - '#default_value' => $this->configuration['password'] ?? '', - ]; - - return $this->setSettingsParents($form); - } - - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { - parent::submitConfigurationForm($form, $form_state); - $this->configuration['agency_id'] = $form_state - ->getValue(['wrapper', 'agency_id']); - $this->configuration['endpoint_url'] = $form_state - ->getValue(['wrapper', 'endpoint_url']); - $this->configuration['username'] = $form_state - ->getValue(['wrapper', 'username']); - $this->configuration['password'] = $form_state - ->getValue(['wrapper', 'password']); - } - - /** - * {@inheritdoc} - */ - public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE): void { - $logger_context = [ - 'handler_id' => 'os2forms_fbs', - 'channel' => 'webform_submission', - 'webform_submission' => $webform_submission, - 'operation' => 'submission queued', - ]; - - // Validate fields required in the job and FBS client. - $data = $webform_submission->getData(); - $fields = [ - 'afhentningssted', - 'barn_cpr', - 'barn_mail', - 'cpr', - 'email', - 'navn', - 'pinkode', - ]; - foreach ($fields as $field) { - if (!isset($data[$field])) { - $this->submissionLogger->error($this->t('Missing field in submission @field to queue for processing', ['@field' => $field]), $logger_context); - return; - } - } - - /** @var \Drupal\advancedqueue\Entity\Queue $queue */ - $queue = $this->getQueue(); - $job = Job::create(FbsCreateUser::class, [ - 'submissionId' => $webform_submission->id(), - 'configuration' => $this->configuration, - ]); - $queue->enqueueJob($job); - - $this->submissionLogger->notice($this->t('Added submission #@serial to queue for processing', ['@serial' => $webform_submission->serial()]), $logger_context); - } - - /** - * Get queue. - */ - private function getQueue(): ?Queue { - $queueStorage = $this->entityTypeManager->getStorage('advancedqueue_queue'); - /** @var ?\Drupal\advancedqueue\Entity\Queue $queue */ - $queue = $queueStorage->load($this->queueId); - - return $queue; - } - -}