From 846bd67c99872e8eafbd77ae9293968558d5230d Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sat, 16 Jul 2022 19:51:30 +0200
Subject: [PATCH 5/5] toulouse_maelis: relax constaint on family schema
 (#67326)

---
 passerelle/contrib/toulouse_maelis/models.py  |  4 ++--
 passerelle/contrib/toulouse_maelis/schemas.py | 15 +++++++++------
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/passerelle/contrib/toulouse_maelis/models.py b/passerelle/contrib/toulouse_maelis/models.py
index 27962ca2..fd2a2cce 100644
--- a/passerelle/contrib/toulouse_maelis/models.py
+++ b/passerelle/contrib/toulouse_maelis/models.py
@@ -297,17 +297,17 @@ class ToulouseMaelis(BaseResource, HTTPResource):
         return {'data': response}
 
     @endpoint(
         display_category=_('Family'),
         description='Création de la famille',
         name='create-family',
         perm='can_access',
         parameters={'NameID': {'description': _('Publik ID')}},
-        post={'request_body': {'schema': {'application/json': schemas.FAMILY_SCHEMA}}},
+        post={'request_body': {'schema': {'application/json': schemas.CREATE_FAMILY_SCHEMA}}},
     )
     def create_family(self, request, NameID, post_data):
         if self.link_set.filter(name_id=NameID).exists():
             raise APIError('User already linked to family', err_code='already-linked')
 
         response = self.call('Family', 'createFamily', **post_data)
         data = serialize_object(response)
         family_id = data.get('number')
@@ -320,17 +320,17 @@ class ToulouseMaelis(BaseResource, HTTPResource):
         return {'data': data}
 
     @endpoint(
         display_category=_('Family'),
         description='Modification de la famille',
         name='update-family',
         perm='can_access',
         parameters={'NameID': {'description': _('Publik ID')}},
-        post={'request_body': {'schema': {'application/json': schemas.FAMILY_SCHEMA}}},
+        post={'request_body': {'schema': {'application/json': schemas.UPDATE_FAMILY_SCHEMA}}},
     )
     def update_family(self, request, NameID, post_data):
         family_id = self.get_link(NameID).family_id
         self.replace_null_values(post_data)
 
         response = self.call('Family', 'updateFamily', dossierNumber=family_id, **post_data)
         data = serialize_object(response)
         family_id = data.get('number')
diff --git a/passerelle/contrib/toulouse_maelis/schemas.py b/passerelle/contrib/toulouse_maelis/schemas.py
index 1cb61f9b..19cc394d 100644
--- a/passerelle/contrib/toulouse_maelis/schemas.py
+++ b/passerelle/contrib/toulouse_maelis/schemas.py
@@ -9,16 +9,18 @@
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Affero General Public License for more details.
 #
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import copy
+
 BOOLEAN_TYPES = [
     {'type': 'boolean'},
     {
         'type': 'string',
         'pattern': '^([Oo][Uu][Ii]|[Nn][Oo][Nn]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|1|0)$',
         'pattern_description': 'Les valeurs "0", "1", "true", "false", "oui" ou "non" sont autorisées (insensibles à la casse).',
     },
 ]
@@ -259,26 +261,22 @@ RLINFO_SCHEMA = {
         'adresse': ADDRESS_SCHEMA,
         'contact': CONTACT_SCHEMA,
         'profession': PROFESSION_SCHEMA,
         'CAFInfo': CAFINFO_SCHEMA,
     },
 }
 
 
-FAMILY_SCHEMA = {
+CREATE_FAMILY_SCHEMA = {
     '$schema': 'http://json-schema.org/draft-04/schema#',
     'title': 'Family',
     'description': 'Informations pour créer ou mettre à jour une famille',
     'type': 'object',
-    'required': ['categorie', 'situation'],
-    "oneOf": [
-        {"required": ['rl1']},
-        {"required": ['rl2']},
-    ],
+    'required': ['rl1', 'categorie', 'situation'],
     'properties': {
         'categorie': {
             'description': 'Categorie (depuis référenciel)',
             'type': 'string',
         },
         'situation': {
             'description': 'Situation familiale (depuis référenciel)',
             'type': 'string',
@@ -300,16 +298,21 @@ FAMILY_SCHEMA = {
             'oneOf': [{'type': 'null'}, {'type': 'string'}],
         },
         'rl1': RLINFO_SCHEMA,
         'rl2': RLINFO_SCHEMA,
     },
     'unflatten': True,
 }
 
+
+UPDATE_FAMILY_SCHEMA = copy.deepcopy(CREATE_FAMILY_SCHEMA)
+UPDATE_FAMILY_SCHEMA['required'] = ['categorie', 'situation']
+
+
 # Schemas below describe parameters of Maelis wrapper around updateFamily endpoint
 
 UPDATE_COORDINATE_SCHEMA = {
     '$schema': 'http://json-schema.org/draft-04/schema#',
     'title': 'Update coordinate',
     'description': "Mise à jour des coordonnées d'un responsable légal",
     'type': 'object',
     'properties': {
-- 
2.30.2

