From 02043840e266d8048196499f4882970b8a75d2ae Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Wed, 31 Jan 2024 15:50:02 +0100 Subject: [PATCH 1/2] Added digital post test command --- CHANGELOG.md | 2 + modules/os2forms_digital_post/README.md | 11 +- .../os2forms_digital_post/drush.services.yml | 9 ++ .../Commands/DigitalPostTestCommands.php | 152 ++++++++++++++++++ .../src/Helper/DigitalPostHelper.php | 15 +- 5 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 modules/os2forms_digital_post/drush.services.yml create mode 100644 modules/os2forms_digital_post/src/Drush/Commands/DigitalPostTestCommands.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d63471..88fd808 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] - CprFetchData adding ajax error fix +- [#84](https://github.com/OS2Forms/os2forms/pull/84) + Added digital post test command. ## [3.14.0] diff --git a/modules/os2forms_digital_post/README.md b/modules/os2forms_digital_post/README.md index 04747c9..578e70e 100644 --- a/modules/os2forms_digital_post/README.md +++ b/modules/os2forms_digital_post/README.md @@ -58,7 +58,7 @@ graphical overview of jobs in the queue. ## Beskedfordeler -Thie digital post module depends on [Beskedfordeler for +The digital post module depends on [Beskedfordeler for Drupal](https://github.com/itk-dev/beskedfordeler-drupalon) to get get information on how or why not a digital post has been delivered (cf. [BeskedfordelerEventSubscriber](src/EventSubscriber/BeskedfordelerEventSubscriber.php)). @@ -66,3 +66,12 @@ information on how or why not a digital post has been delivered (cf. See the [documentation for Beskedfordeler for Drupal](https://github.com/itk-dev/beskedfordeler-drupal#beskedfordeler) for details on how to set up the Beskedfordeler module. + +## Testing digital post + +This module contains a simple Drush command for sending digital post to a list +of recipients: + +``` shell +drush os2forms-digital-post:test:send --help +``` diff --git a/modules/os2forms_digital_post/drush.services.yml b/modules/os2forms_digital_post/drush.services.yml new file mode 100644 index 0000000..ac0d5f8 --- /dev/null +++ b/modules/os2forms_digital_post/drush.services.yml @@ -0,0 +1,9 @@ +services: + Drupal\os2forms_digital_post\Drush\Commands\DigitalPostTestCommands: + arguments: + - '@Drupal\os2forms_digital_post\Helper\DigitalPostHelper' + - '@token' + - '@plugin.manager.entity_print.print_engine' + - '@Drupal\os2forms_digital_post\Helper\Settings' + tags: + - { name: drush.command } diff --git a/modules/os2forms_digital_post/src/Drush/Commands/DigitalPostTestCommands.php b/modules/os2forms_digital_post/src/Drush/Commands/DigitalPostTestCommands.php new file mode 100644 index 0000000..653783f --- /dev/null +++ b/modules/os2forms_digital_post/src/Drush/Commands/DigitalPostTestCommands.php @@ -0,0 +1,152 @@ + $recipients + * @phpstan-param array $options + * + * @command os2forms-digital-post:test:send + * @usage os2forms-digital-post:test:send --help + */ + public function send(array $recipients, array $options = [ + 'subject' => 'os2forms_digital_post', + 'message' => 'This is a test message from os2forms_digital_post sent on [current-date:html_datetime].', + 'digital-post-type' => SF1601::TYPE_AUTOMATISK_VALG, + 'dump-digital-post-settings' => FALSE, + ]): void { + $io = new SymfonyStyle($this->input(), $this->output()); + + if ($options['dump-digital-post-settings']) { + $this->dumpDigitalPostSettings($io); + } + + $subject = $this->token->replace($options['subject']); + $message = $this->token->replace($options['message']); + $senderLabel = $this->token->replace('os2forms_digital_post'); + $messageLabel = $this->token->replace('os2forms_digital_post [current-date:html_datetime]'); + + $html = sprintf('

%s

%s', $subject, $message); + + /** @var \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PdfEngineBase $printer */ + $printer = $this->entityPrintPluginManager->createInstance('dompdf'); + $printer->addPage($html); + $content = $printer->getBlob(); + + $document = new Document( + $content, + Document::MIME_TYPE_PDF, + 'os2forms_digital_post.pdf' + ); + + $type = $options['digital-post-type']; + if (!in_array($type, SF1601::TYPES, TRUE)) { + $quote = static fn ($value) => var_export($value, TRUE); + throw new InvalidArgumentException(sprintf('Invalid type: %s. Must be one of %s.', $quote($type), implode(', ', array_map($quote, SF1601::TYPES)))); + } + + $io->section('Digital post'); + $io->definitionList( + ['Type' => $type], + ['Subject' => $subject], + ['Message' => $message] + ); + + foreach ($recipients as $recipient) { + try { + $io->writeln(sprintf('Recipient: %s', $recipient)); + $recipientLookupResult = $this->digitalPostHelper->lookupRecipient($recipient); + $actions = []; + + $meMoMessage = $this->digitalPostHelper->getMeMoHelper()->buildMessage($recipientLookupResult, $senderLabel, + $messageLabel, $document, $actions); + $forsendelse = $this->digitalPostHelper->getForsendelseHelper()->buildForsendelse($recipientLookupResult, + $messageLabel, $document); + + $this->digitalPostHelper->sendDigitalPost( + $type, + $meMoMessage, + $forsendelse + ); + + $io->success(sprintf('Digital post sent to %s', $recipient)); + } + catch (\Throwable $throwable) { + $io->error(sprintf('Error sending digital post to %s:', $recipient)); + $io->writeln($throwable->getMessage()); + + if ($throwable instanceof ClientExceptionInterface) { + $response = $throwable->getResponse(); + $io->section('Response'); + $io->writeln( + Yaml::encode([ + 'headers' => $response->getHeaders(FALSE), + 'content' => $response->getContent(FALSE), + ]), + ); + } + } + } + } + + /** + * Dump digital post settings. + */ + private function dumpDigitalPostSettings(SymfonyStyle $io): void { + $io->section('Digital post settings'); + $io->writeln([ + Yaml::encode([ + 'testMode' => $this->digitalPostSettings->getTestMode(), + 'sender' => $this->digitalPostSettings->getSender(), + 'certificate' => $this->digitalPostSettings->getCertificate(), + 'processing' => $this->digitalPostSettings->getProcessing(), + ]), + '', + ]); + } + +} diff --git a/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php b/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php index f237ace..78972ef 100644 --- a/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php +++ b/modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php @@ -47,15 +47,15 @@ public function __construct( * The MeMo message. * @param \Oio\Fjernprint\ForsendelseI|null $forsendelse * The forsendelse if any. - * @param \Drupal\webform\WebformSubmissionInterface $submission - * The submission. + * @param null|\Drupal\webform\WebformSubmissionInterface $submission + * A submission used for hooking up with Beskedfordeler. * * @return array * [The response, The kombi post message]. * * @phpstan-return array */ - public function sendDigitalPost(string $type, Message $message, ?ForsendelseI $forsendelse, WebformSubmissionInterface $submission): array { + public function sendDigitalPost(string $type, Message $message, ?ForsendelseI $forsendelse, WebformSubmissionInterface $submission = NULL): array { $senderSettings = $this->settings->getSender(); $options = [ 'test_mode' => (bool) $this->settings->getTestMode(), @@ -66,7 +66,10 @@ public function sendDigitalPost(string $type, Message $message, ?ForsendelseI $f $transactionId = Serializer::createUuid(); $response = $service->kombiPostAfsend($transactionId, $type, $message, $forsendelse); - $this->beskedfordelerHelper->createMessage($submission->id(), $message, (string) $response->getContent()); + $content = (string) $response->getContent(); + if (NULL !== $submission) { + $this->beskedfordelerHelper->createMessage($submission->id(), $message, $content); + } return [$response, $service->getLastKombiMeMoMessage()]; } @@ -92,7 +95,7 @@ public function lookupCpr(string $cpr): CprLookupResult { } $lookupResult = $instance->lookup($cpr); if (!$lookupResult->isSuccessful()) { - throw new RuntimeException('Cannot lookup CPR'); + throw new RuntimeException('Cannot look up CPR'); } return $lookupResult; @@ -108,7 +111,7 @@ public function lookupCvr(string $cvr): CompanyLookupResult { } $lookupResult = $instance->lookup($cvr); if (!$lookupResult->isSuccessful()) { - throw new RuntimeException('Cannot lookup CVR'); + throw new RuntimeException('Cannot look up CVR'); } return $lookupResult; From 5241fd00b6a4cfff92d54d10ac786052779ad882 Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Fri, 8 Mar 2024 13:41:30 +0100 Subject: [PATCH 2/2] Update modules/os2forms_digital_post/README.md Co-authored-by: Jeppe Kuhlmann Andersen <78410897+jekuaitk@users.noreply.github.com> --- modules/os2forms_digital_post/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/os2forms_digital_post/README.md b/modules/os2forms_digital_post/README.md index 578e70e..a487b13 100644 --- a/modules/os2forms_digital_post/README.md +++ b/modules/os2forms_digital_post/README.md @@ -59,7 +59,7 @@ graphical overview of jobs in the queue. ## Beskedfordeler The digital post module depends on [Beskedfordeler for -Drupal](https://github.com/itk-dev/beskedfordeler-drupalon) to get get +Drupal](https://github.com/itk-dev/beskedfordeler-drupal) to get get information on how or why not a digital post has been delivered (cf. [BeskedfordelerEventSubscriber](src/EventSubscriber/BeskedfordelerEventSubscriber.php)).