Skip to content

Commit

Permalink
(PC-26060)[BO] feat: disable all offers linked to provider when provi…
Browse files Browse the repository at this point in the history
…der is disabled
  • Loading branch information
ghaliela committed Dec 28, 2023
1 parent 2d2ef4d commit a8a0bf5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"pylint.importStrategy": "fromEnvironment",
"pylint.ignorePatterns": ["**/tests/**"],
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
},
"editor.formatOnSave": true,
"python.testing.unittestEnabled": false,
Expand Down
9 changes: 9 additions & 0 deletions api/src/pcapi/core/providers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,12 @@ def _get_siret(venue_id_at_offer_provider: str | None, siret: str | None) -> str
if siret is not None:
return siret
raise providers_exceptions.NoSiretSpecified()


def disable_offers_linked_to_provider(provider_id: int) -> None:
venue_providers = providers_models.VenueProvider.query.filter_by(providerId=provider_id).all()
for venue_provider in venue_providers:
update_venue_synchronized_offers_active_status_job.delay(
venue_provider.venueId, venue_provider.providerId, False
)
venue_provider.isActive = False
3 changes: 3 additions & 0 deletions api/src/pcapi/routes/backoffice/providers/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pcapi.core.offerers import models as offerers_models
from pcapi.core.offerers import repository as offerers_repository
from pcapi.core.permissions import models as perm_models
from pcapi.core.providers import api as providers_api
from pcapi.core.providers import models as providers_models
from pcapi.models import db
from pcapi.models.validation_status_mixin import ValidationStatus
Expand Down Expand Up @@ -176,6 +177,8 @@ def update_provider(provider_id: int) -> utils.BackofficeResponse:
try:
db.session.add(provider)
db.session.commit()
if not form.is_active.data:
providers_api.disable_offers_linked_to_provider(provider_id)
except sa.exc.IntegrityError:
db.session.rollback()
flash("Ce partenaire existe déjà", "warning")
Expand Down
32 changes: 32 additions & 0 deletions api/tests/routes/backoffice/providers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pcapi.core.offerers import api as offerers_api
from pcapi.core.offerers import factories as offerers_factories
from pcapi.core.offerers import models as offerers_models
from pcapi.core.offers import factories as offers_factories
from pcapi.core.permissions import models as perm_models
from pcapi.core.providers import factories as providers_factories
from pcapi.core.providers import models as providers_models
Expand Down Expand Up @@ -248,3 +249,34 @@ def test_update_provider_with_minimal_data(self, authenticated_client):
assert not updated_provider.apiKeys

assert offerer.name != form_data["name"]

def test_disable_offer_when_disabling_provider(self, authenticated_client):
provider = providers_factories.ProviderFactory()
offerer = offerers_factories.OffererFactory()
providers_factories.OffererProviderFactory(offerer=offerer, provider=provider)

venue_provider_1 = providers_factories.VenueProviderFactory(provider=provider)
offer_1_1 = offers_factories.OfferFactory(venue=venue_provider_1.venue, lastProvider=provider, isActive=True)

venue_provider_2 = providers_factories.VenueProviderFactory(provider=provider)
offer_2_1 = offers_factories.OfferFactory(venue=venue_provider_2.venue, lastProvider=provider, isActive=True)
offer_2_2 = offers_factories.OfferFactory(venue=venue_provider_2.venue, lastProvider=provider, isActive=True)

form_data = {
"name": "Individual Offer API consumer",
"enabled_for_pro": False,
}
response = self.post_to_endpoint(authenticated_client, form_data, provider_id=provider.id)
assert response.status_code == 303
redirected_response = authenticated_client.get(response.headers["location"])

created_provider_alert = html_parser.extract_alert(redirected_response.data)
assert created_provider_alert == f'Le partenaire {form_data["name"]} a été modifié.'

updated_provider = providers_models.Provider.query.get(provider.id)
assert updated_provider.isActive == False
assert venue_provider_1.isActive == False
assert offer_1_1.isActive == False
assert venue_provider_2.isActive == False
assert offer_2_1.isActive == False
assert offer_2_2.isActive == False

0 comments on commit a8a0bf5

Please sign in to comment.