Repository: mozmeao/basket

Issue:
Process `newsletters:update` FxA event to restore newsletters in Sync signup
Basket currently processes FxA `verified` events to subscribe newsletters to users. This flow was changed for sync when we switched newsletter options with choose what to sync option in https://github.com/mozilla/fxa/pull/1206.

In https://jira.mozilla.com/browse/FXA-1462, we proposed to add the newsletter subscription at the end of the sync signup flow. To support this, FxA can emit a new event when the user selects the newsletters. This event should emit values similar to the `verified` event.

Ex. Newsletter sqs update event
- `event`: The string "newsletters:update"
- `uid`: The userid of the account being that was created.
- `email`: The primary email address that was verified for the account.
- `locale`: The accept-language header supplies by the user at account creation.
- `country`: The estimated country (based on IP) of the user
- `countryCode`: The country code of `country`
- `newsletters`: Array of newsletter subscriptions for a user
- `userAgent`: The user agent of device that completed the account verified


Discussion:
Looks good. Thanks @vbudhram !

Resolving patch:
diff --git a/basket/news/management/commands/process_fxa_queue.py b/basket/news/management/commands/process_fxa_queue.py
--- a/basket/news/management/commands/process_fxa_queue.py
+++ b/basket/news/management/commands/process_fxa_queue.py
@@ -10,14 +10,21 @@
 import sentry_sdk
 from django_statsd.clients import statsd
 
-from basket.news.tasks import fxa_delete, fxa_email_changed, fxa_login, fxa_verified
+from basket.news.tasks import (
+    fxa_delete,
+    fxa_email_changed,
+    fxa_login,
+    fxa_newsletters_update,
+    fxa_verified,
+)
 
 
 FXA_EVENT_TYPES = {
     'delete': fxa_delete,
-    'verified': fxa_verified,
-    'primaryEmailChanged': fxa_email_changed,
     'login': fxa_login,
+    'newsletters:update': fxa_newsletters_update,
+    'primaryEmailChanged': fxa_email_changed,
+    'verified': fxa_verified,
 }
 
 
diff --git a/basket/news/tasks.py b/basket/news/tasks.py
--- a/basket/news/tasks.py
+++ b/basket/news/tasks.py
@@ -319,7 +319,7 @@ def fxa_verified_sfdc(data):
 
     newsletters = newsletters or []
     newsletters.append(settings.FXA_REGISTER_NEWSLETTER)
-    new_data['newsletters'] = ','.join(newsletters)
+    new_data['newsletters'] = newsletters
 
     user_data = get_fxa_user_data(fxa_id, email)
     # don't overwrite the user's language if already set
@@ -347,6 +347,23 @@ def fxa_verified_sfmc(data):
         raise RetryTask(str(e))
 
 
+@et_task
+def fxa_newsletters_update(data):
+    email = data['email']
+    fxa_id = data['uid']
+    new_data = {
+        'email': email,
+        'newsletters': data['newsletters'],
+        'source_url': settings.FXA_REGISTER_SOURCE_URL,
+        'country': data.get('countryCode', ''),
+        'fxa_lang': data.get('locale'),
+        'fxa_id': fxa_id,
+        'optin': True,
+        'format': 'H',
+    }
+    upsert_contact(SUBSCRIBE, new_data, get_fxa_user_data(fxa_id, email))
+
+
 @et_task
 def fxa_login(data):
     email = data['email']


Test patch:
diff --git a/basket/news/tests/test_tasks.py b/basket/news/tests/test_tasks.py
--- a/basket/news/tests/test_tasks.py
+++ b/basket/news/tests/test_tasks.py
@@ -1283,7 +1283,7 @@ def test_success(self, fxa_data_mock, upsert_mock, apply_mock):
         fxa_verified(data)
         upsert_mock.assert_called_with(SUBSCRIBE, {
             'email': data['email'],
-            'newsletters': settings.FXA_REGISTER_NEWSLETTER,
+            'newsletters': [settings.FXA_REGISTER_NEWSLETTER],
             'source_url': settings.FXA_REGISTER_SOURCE_URL,
             'country': '',
             'fxa_lang': data['locale'],
@@ -1312,7 +1312,11 @@ def test_with_newsletters(self, fxa_data_mock, upsert_mock, apply_mock):
         fxa_verified(data)
         upsert_mock.assert_called_with(SUBSCRIBE, {
             'email': data['email'],
-            'newsletters': 'test-pilot,take-action-for-the-internet,' + settings.FXA_REGISTER_NEWSLETTER,
+            'newsletters': [
+                'test-pilot',
+                'take-action-for-the-internet',
+                settings.FXA_REGISTER_NEWSLETTER
+            ],
             'source_url': settings.FXA_REGISTER_SOURCE_URL,
             'country': '',
             'lang': 'en-US',
@@ -1346,7 +1350,7 @@ def test_with_subscribe_and_metrics(self, fxa_data_mock, upsert_mock, apply_mock
         fxa_verified(data)
         upsert_mock.assert_called_with(SUBSCRIBE, {
             'email': data['email'],
-            'newsletters': settings.FXA_REGISTER_NEWSLETTER,
+            'newsletters': [settings.FXA_REGISTER_NEWSLETTER],
             'source_url': settings.FXA_REGISTER_SOURCE_URL + '?utm_campaign=bowling',
             'country': 'DE',
             'lang': 'en-US',
@@ -1376,7 +1380,7 @@ def test_with_createDate(self, fxa_data_mock, upsert_mock, apply_mock):
         fxa_verified(data)
         upsert_mock.assert_called_with(SUBSCRIBE, {
             'email': data['email'],
-            'newsletters': settings.FXA_REGISTER_NEWSLETTER,
+            'newsletters': [settings.FXA_REGISTER_NEWSLETTER],
             'source_url': settings.FXA_REGISTER_SOURCE_URL,
             'country': '',
             'lang': 'en-US',