diff --git a/api/src/pcapi/core/offerers/api.py b/api/src/pcapi/core/offerers/api.py index 67764c889e8..7ab6a061bce 100644 --- a/api/src/pcapi/core/offerers/api.py +++ b/api/src/pcapi/core/offerers/api.py @@ -2727,18 +2727,29 @@ def create_offerer_address_from_address_api( city: str, latitude: float, longitude: float, + is_manual_edition: bool, ) -> geography_models.Address: - is_manual_edition: bool = False - address_info = api_adresse.get_address(street, postal_code, city) - location_data = LocationData( - city=address_info.city, - postal_code=address_info.postcode, - latitude=address_info.latitude, - longitude=address_info.longitude, - street=address_info.street, - insee_code=address_info.citycode, - ban_id=address_info.id, - ) + if not is_manual_edition: + address_info = api_adresse.get_address(street, postal_code, city) + location_data = LocationData( + city=address_info.city, + postal_code=address_info.postcode, + latitude=address_info.latitude, + longitude=address_info.longitude, + street=address_info.street, + insee_code=address_info.citycode, + ban_id=address_info.id, + ) + else: + location_data = LocationData( + city=city, + postal_code=postal_code, + latitude=latitude, + longitude=longitude, + street=street, + ban_id=None, + insee_code=None + ) return get_or_create_address(location_data, is_manual_edition=is_manual_edition) diff --git a/api/src/pcapi/core/offerers/schemas.py b/api/src/pcapi/core/offerers/schemas.py index 6815f5853d1..3877cac8681 100644 --- a/api/src/pcapi/core/offerers/schemas.py +++ b/api/src/pcapi/core/offerers/schemas.py @@ -117,3 +117,4 @@ class AddressBodyModel(BaseModel): longitude: float | str postalCode: VenuePostalCode street: VenueAddress + isManualEdition: bool = False diff --git a/api/src/pcapi/core/offers/api.py b/api/src/pcapi/core/offers/api.py index 026cd427b0f..cf010be0199 100644 --- a/api/src/pcapi/core/offers/api.py +++ b/api/src/pcapi/core/offers/api.py @@ -331,6 +331,7 @@ def get_offerer_address_from_address( city=address.city, latitude=float(address.latitude), longitude=float(address.longitude), + is_manual_edition=address.isManualEdition ) return offerers_api.get_or_create_offerer_address( venue.managingOffererId, diff --git a/api/tests/routes/pro/patch_offer_test.py b/api/tests/routes/pro/patch_offer_test.py index 046fc27e70c..344df247e15 100644 --- a/api/tests/routes/pro/patch_offer_test.py +++ b/api/tests/routes/pro/patch_offer_test.py @@ -132,6 +132,49 @@ def test_patch_offer_with_address(self, get_address_mock, label, offer_has_oa, a assert address.postalCode == "75102" assert address.latitude == Decimal("48.85660") assert address.longitude == Decimal("2.3522") + assert address.isManualEdition is False + + def test_patch_offer_with_manual_address_edition(self, client): + # Given + user_offerer = offerers_factories.UserOffererFactory(user__email="user@example.com") + venue = offerers_factories.VenueFactory(managingOfferer=user_offerer.offerer) + offer = offers_factories.OfferFactory( + subcategoryId=subcategories.ABO_MEDIATHEQUE.id, + venue=venue, + name="New name", + description="description", + ) + + # When + data = { + "name": "New name", + "externalTicketOfficeUrl": "http://example.net", + "mentalDisabilityCompliant": True, + "address": { + "street": "42, Rue qui n’existe pas", + "city": "Ville inconnue", + "postalCode": "00000", + "latitude": 1, + "longitude": 1, + "label": "label", + "isManualEdition": True, + }, + } + + response = client.with_session_auth("user@example.com").patch(f"/offers/{offer.id}", json=data) + + assert response.status_code == 200 + assert response.json["id"] == offer.id + updated_offer = Offer.query.get(offer.id) + address = updated_offer.offererAddress.address + assert updated_offer.offererAddress.label == "label" + assert address.street == "42, Rue qui n’existe pas" + assert address.city == "Ville inconnue" + assert address.postalCode == "00000" + assert address.latitude == Decimal("1") + assert address.longitude == Decimal("1") + assert address.isManualEdition is True + @pytest.mark.parametrize("label", ["", None, True]) @patch("pcapi.connectors.api_adresse.get_address") @@ -193,6 +236,7 @@ def test_patch_offer_with_address_twice(self, get_address_mock, label, client): assert address.postalCode == "75102" assert address.latitude == Decimal("48.85660") assert address.longitude == Decimal("2.3522") + assert address.isManualEdition is False def test_withdrawal_can_be_updated(self, client): offer = offers_factories.OfferFactory( diff --git a/api/tests/routes/pro/post_offer_test.py b/api/tests/routes/pro/post_offer_test.py index 1a8e830d884..095623939c6 100644 --- a/api/tests/routes/pro/post_offer_test.py +++ b/api/tests/routes/pro/post_offer_test.py @@ -210,11 +210,10 @@ def test_create_event_offer_with_manual_offerer_address(self, oa_label, client): "longitude": "2.308289", "postalCode": "75001", "street": "3 Rue de Valois", + "isManualEdition": True }, } - with patch("pcapi.connectors.api_adresse.get_address") as mocked_get_address: - mocked_get_address.side_effect = api_adresse.NoResultException - response = client.with_session_auth("user@example.com").post("/offers", json=data) + response = client.with_session_auth("user@example.com").post("/offers", json=data) # Then assert response.status_code == 201 @@ -222,7 +221,6 @@ def test_create_event_offer_with_manual_offerer_address(self, oa_label, client): offer = Offer.query.get(offer_id) assert offer.offererAddress.address.isManualEdition assert offer.offererAddress.label == oa_label - assert offer.offererAddress.address.inseeCode == "06029" assert not offer.offererAddress.address.banId def when_creating_new_thing_offer(self, client):