From ca5149134ea7f4036b48f7929c9840275894ebd1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 23 Sep 2019 17:17:58 +0200
Subject: [PATCH 1/2] cartads_cs: allow many subscribers to a single dossier
 (#36382)

---
 .../migrations/0007_auto_20190923_1711.py     | 27 +++++++++++++++++++
 .../migrations/0008_auto_20190923_1712.py     | 25 +++++++++++++++++
 passerelle/apps/cartads_cs/models.py          | 12 +++++++--
 3 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 passerelle/apps/cartads_cs/migrations/0007_auto_20190923_1711.py
 create mode 100644 passerelle/apps/cartads_cs/migrations/0008_auto_20190923_1712.py

diff --git a/passerelle/apps/cartads_cs/migrations/0007_auto_20190923_1711.py b/passerelle/apps/cartads_cs/migrations/0007_auto_20190923_1711.py
new file mode 100644
index 00000000..0e6f8d38
--- /dev/null
+++ b/passerelle/apps/cartads_cs/migrations/0007_auto_20190923_1711.py
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.12 on 2019-09-23 15:11
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('cartads_cs', '0006_cartadscs_client_name'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='CartaDSSubscriber',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('name_id', models.CharField(max_length=32, null=True)),
+            ],
+        ),
+        migrations.AddField(
+            model_name='cartadsdossier',
+            name='subscribers',
+            field=models.ManyToManyField(blank=True, to='cartads_cs.CartaDSSubscriber'),
+        ),
+    ]
diff --git a/passerelle/apps/cartads_cs/migrations/0008_auto_20190923_1712.py b/passerelle/apps/cartads_cs/migrations/0008_auto_20190923_1712.py
new file mode 100644
index 00000000..528027f2
--- /dev/null
+++ b/passerelle/apps/cartads_cs/migrations/0008_auto_20190923_1712.py
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.12 on 2019-09-23 15:12
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+def migrate_subscribers(apps, schema_editor):
+    CartaDSDossier = apps.get_model('cartads_cs', 'CartaDSDossier')
+    CartaDSSubscriber = apps.get_model('cartads_cs', 'CartaDSSubscriber')
+    for instance in CartaDSDossier.objects.all():
+        if instance.name_id:
+            subscriber, created = CartaDSSubscriber.objects.get_or_create(name_id=instance.name_id)
+            instance.subscribers.add(subscriber)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('cartads_cs', '0007_auto_20190923_1711'),
+    ]
+
+    operations = [
+        migrations.RunPython(migrate_subscribers, migrations.RunPython.noop),
+    ]
diff --git a/passerelle/apps/cartads_cs/models.py b/passerelle/apps/cartads_cs/models.py
index 51d529dc..a5363eba 100644
--- a/passerelle/apps/cartads_cs/models.py
+++ b/passerelle/apps/cartads_cs/models.py
@@ -75,6 +75,10 @@ class CartaDSFile(models.Model):
         ordering = ['id']
 
 
+class CartaDSSubscriber(models.Model):
+    name_id = models.CharField(max_length=32, null=True)
+
+
 class CartaDSDossier(models.Model):
     email = models.CharField(max_length=256)
     name_id = models.CharField(max_length=32, null=True)
@@ -91,6 +95,7 @@ class CartaDSDossier(models.Model):
     cartads_numero_dossier = models.CharField(max_length=50, null=True)
     cartads_steps_cache = JSONField(default={})
     last_update_datetime = models.DateTimeField(auto_now=True)
+    subscribers = models.ManyToManyField(CartaDSSubscriber, blank=True)
 
 
 class AbstractCartaDSCS(BaseResource):
@@ -650,7 +655,6 @@ class AbstractCartaDSCS(BaseResource):
                 objet_demande_id=objet_demande_id,
                 tracking_code=tracking_code,
                 email=email,
-                name_id=name_id,
                 )
         dossier.save()
         signer = Signer(salt='cart@ds_cs/dossier')
@@ -660,6 +664,8 @@ class AbstractCartaDSCS(BaseResource):
                     'slug': self.slug,
                     'endpoint': 'notification'})) + '/%s/' % signer.sign(str(dossier.id))
         dossier.save()
+        if name_id:
+            dossier.subscribers.add(CartaDSSubscriber.objects.get_or_create(name_id=name_id)[0])
         self.add_job('pack', dossier_id=dossier.id)
         return {'err': 0, 'dossier_id': dossier.id}
 
@@ -954,7 +960,9 @@ class AbstractCartaDSCS(BaseResource):
                   },
               })
     def files(self, request, name_id, status=None):
-        files = CartaDSDossier.objects.filter(name_id=name_id, cartads_numero_dossier__isnull=False)
+        files = CartaDSDossier.objects.filter(
+                cartads_numero_dossier__isnull=False,
+                subscribers__name_id__in=[name_id])
         if status:
             files = [x for x in files if
                      self.get_file_status(x).get('status_id') == status or
-- 
2.23.0

