toulouse-maelis: log wcs demands related to duplicated invoice (#79157)
gitea/passerelle/pipeline/head Build started...
Details
gitea/passerelle/pipeline/head Build started...
Details
This commit is contained in:
parent
551916eb2e
commit
1d93b97101
|
@ -3806,6 +3806,22 @@ class ToulouseMaelis(BaseResource, HTTPResource):
|
|||
invoice = None
|
||||
if response:
|
||||
for item in response.get('factureLst') or []:
|
||||
# match related subscriptions
|
||||
subscriptions = []
|
||||
invoice_subscription_ids = []
|
||||
for line in item['lineInvoiceList'] or []:
|
||||
subscription_id = line.get('idIns')
|
||||
if subscription_id:
|
||||
invoice_subscription_ids.append(subscription_id)
|
||||
for subscription in Subscription.objects.filter(
|
||||
regie_id=item['regie']['code'], family_id=family_id, invoice__isnull=True
|
||||
):
|
||||
for line in subscription.maelis_data['basket']['lignes']:
|
||||
subscription_id = line.get('idIns')
|
||||
if subscription_id and subscription_id in invoice_subscription_ids:
|
||||
subscriptions.append(subscription)
|
||||
break
|
||||
|
||||
try:
|
||||
invoice = self.invoice_set.get(
|
||||
regie_id=item['regie']['code'], invoice_id=item['numInvoice']
|
||||
|
@ -3820,7 +3836,9 @@ class ToulouseMaelis(BaseResource, HTTPResource):
|
|||
maelis_data_update_date=now(),
|
||||
)
|
||||
self.logger.info("Ajout de %s sur la famille '%s'", repr(invoice), family_id)
|
||||
invoice.match_subscriptions()
|
||||
for subscription in subscriptions:
|
||||
subscription.invoice = invoice
|
||||
subscription.save()
|
||||
else:
|
||||
self.logger.error(
|
||||
"%s sur la famille '%s' existe déjà sur la famille '%s'",
|
||||
|
@ -3828,7 +3846,12 @@ class ToulouseMaelis(BaseResource, HTTPResource):
|
|||
family_id,
|
||||
invoice.family_id,
|
||||
)
|
||||
|
||||
for subscription in subscriptions:
|
||||
self.logger.error(
|
||||
"La demande '%s' de la famille '%s' n'a pas pu être liée à une facture et ne pourra pas être notifiée correctement par la suite.",
|
||||
subscription.wcs_form_number,
|
||||
family_id,
|
||||
)
|
||||
if not invoice:
|
||||
self.logger.error(
|
||||
"Pas de facture à la validation du panier '%s' sur la famille '%s'",
|
||||
|
@ -4360,25 +4383,6 @@ class Invoice(models.Model):
|
|||
obj.save()
|
||||
return True
|
||||
|
||||
def match_subscriptions(self):
|
||||
invoice_subscription_ids = []
|
||||
for line in self.maelis_data['lineInvoiceList'] or []:
|
||||
subscription_id = line.get('idIns')
|
||||
if subscription_id:
|
||||
invoice_subscription_ids.append(subscription_id)
|
||||
if not invoice_subscription_ids:
|
||||
return
|
||||
|
||||
for subscription in Subscription.objects.filter(
|
||||
regie_id=self.regie_id, family_id=self.family_id, invoice__isnull=True
|
||||
):
|
||||
for line in subscription.maelis_data['basket']['lignes']:
|
||||
subscription_id = line.get('idIns')
|
||||
if subscription_id and subscription_id in invoice_subscription_ids:
|
||||
subscription.invoice = self
|
||||
subscription.save()
|
||||
break
|
||||
|
||||
def set_trigger_subscriptions(self):
|
||||
for subscription in self.subscription_set.filter(wcs_trigger_payload__isnull=True):
|
||||
if subscription.trigger_status() == 'triggering':
|
||||
|
|
|
@ -9786,6 +9786,84 @@ def test_validate_basket_receiving_duplicated_invoice(activity_service, con, app
|
|||
)
|
||||
|
||||
|
||||
def test_validate_basket_receiving_duplicated_invoice_matching_demand(
|
||||
family_service, activity_service, con, app, caplog
|
||||
):
|
||||
family_service.add_soap_response('readFamily', get_xml_file('R_read_family_for_subscription.xml'))
|
||||
activity_service.add_soap_response('getPersonUnitInfo', get_xml_file('R_get_person_unit_info.xml'))
|
||||
activity_service.add_soap_response('addPersonUnitBasket', get_xml_file('R_add_person_unit_basket.xml'))
|
||||
activity_service.add_soap_response('getFamilyBasket', get_xml_file('R_get_family_basket.xml'))
|
||||
activity_service.add_soap_response('validateBasket', get_xml_file('R_validate_basket.xml'))
|
||||
url = get_endpoint('validate-basket')
|
||||
Link.objects.create(resource=con, family_id='1312', name_id='local')
|
||||
|
||||
# first subscription
|
||||
resp = app.post_json(
|
||||
get_endpoint('add-person-basket-subscription') + '?family_id=123',
|
||||
params={
|
||||
'person_id': '266145',
|
||||
'activity_id': 'A10053179798',
|
||||
'unit_id': 'A10053179809',
|
||||
'place_id': 'A10053179757',
|
||||
'start_date': '2023-02-01',
|
||||
'end_date': '2023-06-30',
|
||||
},
|
||||
)
|
||||
assert resp.json['err'] == 0
|
||||
assert con.subscription_set.count() == 0
|
||||
|
||||
# second subscription, providing a wcs demand
|
||||
resp = app.post_json(
|
||||
get_endpoint('add-person-basket-subscription') + '?NameID=local',
|
||||
params={
|
||||
'person_id': '266145',
|
||||
'activity_id': 'A10053179798',
|
||||
'unit_id': 'A10053179809',
|
||||
'place_id': 'A10053179757',
|
||||
'start_date': '2023-02-01',
|
||||
'end_date': '2023-06-30',
|
||||
'form_api_url': 'https://wcs.example.com/api/forms/exemple-inscription-loisirs-1/12/',
|
||||
'form_number': '13-12',
|
||||
},
|
||||
)
|
||||
assert resp.json['err'] == 0
|
||||
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.status() == 'pending_basket'
|
||||
assert subscription.trigger_status() == 'pending'
|
||||
assert [x['idIns'] for x in subscription.maelis_data['basket']['lignes']] == ['S10055641658']
|
||||
|
||||
# validate basket on first subscription
|
||||
resp = app.post_json(url + '?family_id=123', params={'basket_id': 'S10055641661'})
|
||||
assert resp.json['err'] == 0
|
||||
invoice = con.invoice_set.get(regie_id=109, invoice_id=18)
|
||||
assert invoice.subscription_set.count() == 0
|
||||
assert len(caplog.records) == 14
|
||||
assert caplog.records[-1].message == 'Ajout de <Invoice "109/18"> sur la famille \'123\''
|
||||
|
||||
# validate basket on second subscription and get a duplicated invoice
|
||||
resp = app.post_json(url + '?NameID=local', params={'basket_id': 'S10055641661'})
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.status() == 'pending_basket'
|
||||
assert subscription.trigger_status() == 'pending'
|
||||
assert resp.json['err'] == 0
|
||||
assert len(caplog.records) == 19
|
||||
assert caplog.records[-1].levelno == logging.ERROR
|
||||
assert (
|
||||
caplog.records[-1].message
|
||||
== "La demande '13-12' de la famille '1312' n'a pas pu être liée à une facture et ne pourra pas être notifiée correctement par la suite."
|
||||
)
|
||||
|
||||
# wcs will finaly be notified as if the basket was removed
|
||||
activity_service.add_soap_response('getFamilyBasket', get_xml_file('R_get_family_basket_empty.xml'))
|
||||
con.hourly()
|
||||
assert resp.json['err'] == 0
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.status() == 'removed'
|
||||
assert subscription.trigger_status() == 'triggered'
|
||||
assert subscription.wcs_trigger_payload['err_desc'] == "Le panier n'a pas été validé"
|
||||
|
||||
|
||||
def test_validate_basket_not_linked_error(con, app):
|
||||
url = get_endpoint('validate-basket')
|
||||
params = {'basket_id': 'S10055641661'}
|
||||
|
|
Loading…
Reference in New Issue