ibm-apiconnect / devportal-addons

Example addon modules and themes for the API Connect Developer Portal
GNU General Public License v2.0
1 stars 2 forks source link

Deprecating Notification Module for APIC v 2018 giving error #10

Closed rohanbcbst closed 4 years ago

rohanbcbst commented 4 years ago

Hi,

I was trying to reuse the module shared for APIC v 2018 for sending notifications to consumer org owner in case of the deprecation of the API which they are subscribed to. I am getting below error:

Aug 18 16:36:01 rnt00110 33a3a234a738[2508]: [ queue stderr] e85a0e:e85a0e:c89c1f 2020-08-18 16:36:01: snapshot: WATCHDOG: [NOTICE] [product] Product WebSvcGetFamily_v15 updated | user: anonymous | uri: http://bcbst.dev.portal.sbox.apic.bcbst.com/ | referer: #015 Aug 18 16:36:01 rnt00110 33a3a234a738[2508]: [ queue stderr] e85a0e:e85a0e:c89c1f 2020-08-18 16:36:01: snapshot: WATCHDOG: [ERROR] [php] Error: Cannot use object of type Drupal\Core\StringTranslation\TranslatableMarkup as array in Drupal\mail_subscribers\Service\MailService->sendEmail() (line 574 of /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/modules/mail_subscribers/src/Service/MailService.php) #0 /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/modules/mail_subscribers/src/Service/MailService.php(105): Drupal\mail_subscribers\Service\MailService->sendEmail(Array, Array, Array, 'websvcgetfamily...') Aug 18 16:36:01 rnt00110 33a3a234a738[2508]: [ queue stderr] e85a0e:e85a0e:c89c1f 2020-08-18 16:36:01: snapshot: #1 /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/sites/bcbst.dev.portal.sbox.apic.bcbst.com/modules/deprecated_notification/deprecated_notification.module(37): Drupal\mail_subscribers\Service\MailService->mailProductMembers(Array, Array, 'en') Aug 18 16:36:01 rnt00110 33a3a234a738[2508]: [ queue stderr] e85a0e:e85a0e:c89c1f 2020-08-18 16:36:01: snapshot: #2 [internal function]: deprecated_notification_product_update(Object(Drupal\node\Entity\Node), Array) Aug 18 16:36:01 rnt00110 33a3a234a738[2508]: [ queue stdout] e85a0e:e85a0e:c89c1f 2020-08-18 16:36:01: snapshot: Error: Cannot use object of type Drupal\Core\StringTranslation\TranslatableMarkup as array in Drupal\mail_subscribers\Service\MailService->sendEmail() (line 574 of /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/modules/mail_subscribers/src/Service/MailService.php).

chrisdudley commented 4 years ago

Please reproduce on latest fp12 code. Currently looks like you’re trying to send a translatable array and that won’t work, it only does plaintext emails so it needs normal plaintext strings.

Could well be a bug in the same module.

rohanbcbst commented 4 years ago

This module was developed specifically for 2018.4.1.12 and won't work in 2018.4.1.10 . As mail_subscribers itself is a module., can't I just update that ?

chrisdudley commented 4 years ago

Nope. You update apic as a whole.

In this case I think it’s a bug in the sample module in this repos. It needs to use a plaintext string for the message subject and content. You can change that in your copy of the module. I’ll fix the one here tomorrow.

rohanbcbst commented 4 years ago

Thanks Chris , so I understand there's some issue with this code below and you will update it tomorrow.

Is it possible if you can provide a quick fix for this code piece as I have a demo tomorrow morning. I am not a Drupal/PHP developer and I am trying to get this piece working

function deprecated_notification_product_update(NodeInterface $node, $data) { if (isset($data['state']) && $data['state'] === 'deprecated') { // do something such as send an email or invoke a REST API // the example below is using some of the code from the mail_subscribers module to email all the consumerorg owners // that are subscribed to any plan of this product

$mailService = \Drupal::service('mail_subscribers.mail_service');
$product_ref = $data['catalog_product']['info']['name'] . ':' . $data['catalog_product']['info']['version'];

$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$site_config = \Drupal::config('system.site');
$from['name'] = $site_config->get('name');
$from['mail'] = $site_config->get('mail');

$message['subject'] = t('Product deprecated');
$message['message'] = t('Product @product has been deprecated.', ['@product' => $node->getTitle()]);
$message['priority'] = 0;
$message['receipt'] = TRUE;
$message['headers'] = '';
$message['direct'] = TRUE;
$message['carbon_copy'] = '';

// this is the target product to email the subscribers of
$message['product'] = $product_ref;

$rc = $mailService->mailProductMembers($message, $from, $langcode);

// $rc should include the number of emails sent

}

rohanbcbst commented 4 years ago

I will update it in my local module and test.

chrisdudley commented 4 years ago

Change the subject and message content lines to this:

$message['subject'] = “Product deprecated”; $message['message'] = “Product $node->getTitle() has been deprecated.”;

chrisdudley commented 4 years ago

(Sorry phone trouble - didn’t mean to close it!)

rohanbcbst commented 4 years ago

Thanks so much! I will try this now! Appreciate your help!

rohanbcbst commented 4 years ago

Hi Chris,

I tried what you mentioned and its still not working. I did following changes:

Double quotes “ Developer Portal was giving "The website encountered an unexpected error. Please try again later" and portal basically became unresponsive. Had to recreate the portal.

I used " this and then syntax started working and added some logger statements, but still email was not sent out.

Tested Final code for the module:

Module.txt

Logged Output in Syslog:

Log.txt

Specifically , these 3 statements: Aug 19 16:08:58 rnt00110 33a3a234a738[2508]: [ queue stderr] b3cff0:b3cff0:b3cff0 2020-08-19 16:08:58: run_site_task: Illegal string offset 'format' MailService.php:574 [warning] Aug 19 16:08:58 rnt00110 33a3a234a738[2508]: [ queue stderr] b3cff0:b3cff0:b3cff0 2020-08-19 16:08:58: run_site_task: Illegal string offset 'value' MailService.php:588 [warning] Aug 19 16:08:58 rnt00110 33a3a234a738[2508]: [ queue stderr] b3cff0:b3cff0:b3cff0 2020-08-19 16:08:58: run_site_task: WATCHDOG: [INFO] [mail_subscribers] Sent email to members subscribing to product demotest:1.0.0 | user: anonymous | uri: http://testprovider.democatalog.portal.sbox.apic.bcbst.com/ | referer: #015

rc is also printed as 0 as shown in the log.

Thanks, Rohan

chrisdudley commented 4 years ago

Oops - another bug, now fixed: https://github.com/ibm-apiconnect/devportal-addons/blob/master/apic_v2018/modules/deprecated_notification/deprecated_notification.module

rohanbcbst commented 4 years ago

Hi Chris,

I am still not getting emails.

Tested Final code for the module:

Module.txt

Logged Output in Syslog: Log.txt

Is there a way I can see debug logs for mail_subscribers module. I don't see any useful information in the log now ? RC is still 0

Thanks, Rohan

rohanbcbst commented 4 years ago

See, if you find anything in these logs.

chrisdudley commented 4 years ago

Apart from a minor issue that it didn't display the product name correctly because the Node wasn't loaded - which i've fixed - I didn't see anything wrong there. Are you sure you actually have a subscriber to a plan of that product? If there are no subscribers then no emails will be sent.

You can check by calling \Drupal::service('mail_subscribers.mail_service')->getProductSubscribingMembers($product_ref . ':'); and see what it says.

You can also try setting $message['carbon_copy'] = TRUE; as that will send a copy to the site admin, and at least you could check that the SMTP config works.

rohanbcbst commented 4 years ago

I guess, still there's a problem with the changed statements:

Aug 21 21:33:03 rnt00110 8e0bc576a4bc[2508]: [ php-fpm stderr] dadf8b:4db11c:2aa997 2020-08-21 21:33:03: WARNING: [pool www] child 402 said into stderr: "#27 {main}. | user: anonymous | uri: https://devprovorg.sandbox.portal.sbox.apic.bcbst.com/devprovorg/sandbox/admin/modules?q=admin%2Fmodules | referer: https://portal.sbox.apic.bcbst.com/devprovorg/sandbox/admin/modules#015" Aug 21 21:33:03 rnt00110 8e0bc576a4bc[2508]: [ php-fpm stderr] dadf8b:4db11c:2aa997 2020-08-21 21:33:03: WARNING: [pool www] child 402 said into stderr: "NOTICE: PHP message: ParseError: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/sites/devprovorg.sandbox.portal.sbox.apic.bcbst.com/modules/deprecated_notification/deprecated_notification.module on line 28 #0 /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/core/lib/Drupal/Core/Extension/ModuleHandler.php(128): Drupal\Core\Extension\Extension->load()" Aug 21 21:33:03 rnt00110 8e0bc576a4bc[2508]: [ php-fpm stderr] dadf8b:4db11c:2aa997 2020-08-21 21:33:03: WARNING: [pool www] child 402 said into stderr: "#1 /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/core/lib/Drupal/Core/Extension/ModuleInstaller.php(202): Drupal\Core\Extension\ModuleHandler->load('deprecated_noti...')" Aug 21 21:33:03 rnt00110 33a3a234a738[2508]: [ director stderr] dcdf39:8f4a47:ba3dd7 2020-08-21 21:33:03: 96037ff0 \/ Entering: site_content_refresh:getUuidRecord Aug 21 21:33:03 rnt00110 8e0bc576a4bc[2508]: [ php-fpm stderr] dadf8b:4db11c:2aa997 2020-08-21 21:33:03: WARNING: [pool www] child 402 said into stderr: "#2 /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php(83): Drupal\Core\Extension\ModuleInstaller->install(Array, true)" Aug 21 21:33:03 rnt00110 8e0bc576a4bc[2508]: [ php-fpm stderr] dadf8b:4db11c:2aa997 2020-08-21 21:33:03: WARNING: [pool www] child 402 said into stderr: "#3 /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/core/modules/system/src/Form/ModulesListForm.php(477): Drupal\Core\ProxyClass\Exte..."

rohanbcbst commented 4 years ago

Chris,

This line was not working : $message['message']['value'] = "Product $data['catalog_product']['info']['title'] has been deprecated."; and was breaking the portal with the error as posted above:

Aug 21 21:33:03 rnt00110 8e0bc576a4bc[2508]: [ php-fpm stderr] dadf8b:4db11c:2aa997 2020-08-21 21:33:03: WARNING: [pool www] child 402 said into stderr: "NOTICE: PHP message: ParseError: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/sites/devprovorg.sandbox.portal.sbox.apic.bcbst.com/modules/deprecated_notification/deprecated_notification.module on line 28 #0 /web/platforms/devportal-8.x-2018.4.1.10-20200219-1834/core/lib/Drupal/Core/Extension/ModuleHandler.php(128): Drupal\Core\Extension\Extension->load()"

I replaced this line with : $message['message']['value'] = "Product $product_ref has been deprecated.";

This error stopped coming. I see rc was giving 1 as the result. Even then neither the site admin nor the consumer org owner received the email. Please find below the module and log

Log.txt Module.txt

Please suggest the next steps. i have kept all the necessary logs. \Drupal::service('mail_subscribers.mail_service')->getProductSubscribingMembers($product_ref . ':'); is not giving the subscriber details.

chrisdudley commented 4 years ago

https://github.com/ibm-apiconnect/devportal-addons/commit/366123791b0d9f5827e6f951da2c0a95fd76693b

That change fixed it for me and it now works and sends emails for me.

Please reopen if still having issues.