toulouse-maelis: trigger wcs when basket subscription is paid (#76398)
gitea/passerelle/pipeline/head This commit looks good
Details
gitea/passerelle/pipeline/head This commit looks good
Details
This commit is contained in:
parent
347d09db89
commit
be4a65b6be
|
@ -4161,6 +4161,11 @@ class ToulouseMaelis(BaseResource, HTTPResource):
|
|||
invoice.lingo_data = post_data
|
||||
invoice.lingo_notification_date = now()
|
||||
invoice.save(update_fields=['updated', 'lingo_notification_date', 'lingo_data'])
|
||||
|
||||
if invoice.basket_generation_date is not None:
|
||||
# match paid invoice with subscriptions and trigger w.c.s.
|
||||
invoice.set_trigger_subscriptions()
|
||||
|
||||
self.add_job(
|
||||
'notify_invoice_paid_job',
|
||||
regie_id=regie_id,
|
||||
|
@ -4434,6 +4439,9 @@ class Subscription(models.Model):
|
|||
|
||||
def status(self):
|
||||
if self.invoice is not None:
|
||||
if self.invoice.status() in ['paid', 'notified']:
|
||||
# related invoice is paid
|
||||
return 'paid'
|
||||
if self.invoice.status() == 'cancelled':
|
||||
# related invoice is cancelled
|
||||
return 'cancelled'
|
||||
|
@ -4452,7 +4460,7 @@ class Subscription(models.Model):
|
|||
if self.wcs_trigger_date is not None:
|
||||
# wcs demand was triggered
|
||||
return 'triggered'
|
||||
if self.status() in ['cancelled', 'removed']:
|
||||
if self.status() in ['paid', 'cancelled', 'removed']:
|
||||
# wcs demand can be triggered
|
||||
return 'triggering'
|
||||
else:
|
||||
|
|
|
@ -10149,6 +10149,214 @@ def test_invoice_pdf_error(invoice_service, con, app):
|
|||
assert resp.json['err_desc'] == 'Fichier PDF non trouvé'
|
||||
|
||||
|
||||
def test_trigger_wcs_on_paid_subscriptions_cron(
|
||||
family_service, activity_service, invoice_service, wcs_service, con, app, freezer, 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'))
|
||||
invoice_service.add_soap_response('readInvoices', get_xml_file('R_read_invoices_regie_109.xml'))
|
||||
wcs_service.add(
|
||||
responses.POST,
|
||||
'https://wcs.example.com/api/forms/exemple-inscription-loisirs-1/12/hooks/update_subscription/',
|
||||
json={'err': 0},
|
||||
status=200,
|
||||
)
|
||||
Link.objects.create(resource=con, family_id='1312', name_id='local')
|
||||
|
||||
# subscribe providing a wcs demand
|
||||
freezer.move_to('2023-03-03 18:20:00')
|
||||
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
|
||||
assert resp.json['data']['controlResult']['controlOK'] is True
|
||||
assert resp.json['data']['basket']['codeRegie'] == 109
|
||||
assert len(resp.json['data']['basket']['lignes']) == 1
|
||||
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.created.strftime('%Y-%m-%d %H:%M:%S') == '2023-03-03 18:20:00'
|
||||
assert subscription.invoice is None
|
||||
assert subscription.status() == 'pending_basket'
|
||||
assert subscription.maelis_data == resp.json['data']
|
||||
assert [x['idIns'] for x in subscription.maelis_data['basket']['lignes']] == ['S10055641658']
|
||||
|
||||
# validate basket
|
||||
freezer.move_to('2023-03-03 18:30:00')
|
||||
resp = app.post_json(
|
||||
get_endpoint('validate-basket') + '?NameID=local', params={'basket_id': 'S10055641661'}
|
||||
)
|
||||
assert resp.json['err'] == 0
|
||||
assert len(resp.json['data']['factureLst']) == 1 # basket validation generate only one invoice
|
||||
assert resp.json['data']['factureLst'][0]['regie']['code'] == 109
|
||||
assert resp.json['data']['factureLst'][0]['numInvoice'] == '18'
|
||||
|
||||
invoice = con.invoice_set.get(regie_id=109, invoice_id=18)
|
||||
assert invoice.created.strftime('%Y-%m-%d %H:%M:%S') == '2023-03-03 18:30:00'
|
||||
assert invoice.basket_generation_date.strftime('%Y-%m-%d %H:%M:%S') == '2023-03-03 18:30:00'
|
||||
assert invoice.status() == 'created'
|
||||
assert invoice.maelis_data == resp.json['data']['factureLst'][0]
|
||||
assert 'S10055641658' in [x['idIns'] for x in invoice.maelis_data['lineInvoiceList']]
|
||||
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.status() == 'pending_invoice'
|
||||
assert subscription.trigger_status() == 'pending'
|
||||
|
||||
# get family invoices
|
||||
freezer.move_to('2023-03-03 18:35:00')
|
||||
resp = app.get(get_endpoint('regie/109/invoices') + '?family_id=1312')
|
||||
assert '1312-18' in [x['id'] for x in resp.json['data']]
|
||||
|
||||
invoice = con.invoice_set.get(regie_id=109, invoice_id=18)
|
||||
assert invoice.status() == 'created'
|
||||
|
||||
# pay invoice
|
||||
freezer.move_to('2023-03-03 18:38:00')
|
||||
resp = app.post_json(
|
||||
get_endpoint('regie/109/invoice/1312-18/pay/') + '?NameID=ignored',
|
||||
params={
|
||||
'transaction_date': '2023-03-03T18:38:00',
|
||||
'transaction_id': '1c5451752a064fc2bd7ea750998683e1',
|
||||
},
|
||||
)
|
||||
assert resp.json['err'] == 0
|
||||
|
||||
invoice = con.invoice_set.get(regie_id=109, invoice_id=18)
|
||||
assert invoice.status() == 'paid'
|
||||
job = Job.objects.get(method_name='notify_invoice_paid_job', natural_id='109/18')
|
||||
assert job.status == 'registered'
|
||||
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.invoice == invoice
|
||||
assert subscription.status() == 'paid'
|
||||
assert subscription.trigger_status() == 'triggering'
|
||||
assert subscription.wcs_trigger_payload['data']['subscription_status'] == 'paid'
|
||||
|
||||
# send trigger to wcs
|
||||
con.hourly()
|
||||
assert (
|
||||
'https://wcs.example.com/api/forms/exemple-inscription-loisirs-1/12/hooks/update_subscription/'
|
||||
in wcs_service.calls[-1].request.url
|
||||
)
|
||||
trigger_body = json.loads(wcs_service.calls[-1].request.body)
|
||||
assert trigger_body['err'] == 0
|
||||
assert trigger_body['data']['subscription_status'] == 'paid'
|
||||
assert trigger_body['data']['invoice_status'] == 'paid'
|
||||
assert trigger_body['data']['regie_text'] == 'DSBL'
|
||||
assert any(['trigger wcs' in x.message for x in caplog.records])
|
||||
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.wcs_trigger_date is not None
|
||||
assert subscription.trigger_status() == 'triggered'
|
||||
|
||||
|
||||
def test_trigger_wcs_on_paid_subscriptions_job(
|
||||
family_service, activity_service, invoice_service, wcs_service, con, app, freezer, 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'))
|
||||
invoice_service.add_soap_response('readInvoices', get_xml_file('R_read_invoices_regie_109.xml'))
|
||||
invoice_service.add_soap_response('payInvoices', get_xml_file('R_pay_invoices.xml'))
|
||||
wcs_service.add(
|
||||
responses.POST,
|
||||
'https://wcs.example.com/api/forms/exemple-inscription-loisirs-1/12/hooks/update_subscription/',
|
||||
json={'err': 0},
|
||||
status=200,
|
||||
)
|
||||
Link.objects.create(resource=con, family_id='1312', name_id='local')
|
||||
|
||||
# subscribe providing a wcs demand
|
||||
freezer.move_to('2023-03-03 18:20:00')
|
||||
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 [x['idIns'] for x in subscription.maelis_data['basket']['lignes']] == ['S10055641658']
|
||||
|
||||
# validate basket
|
||||
freezer.move_to('2023-03-03 18:30:00')
|
||||
resp = app.post_json(
|
||||
get_endpoint('validate-basket') + '?NameID=local', params={'basket_id': 'S10055641661'}
|
||||
)
|
||||
assert resp.json['err'] == 0
|
||||
|
||||
invoice = con.invoice_set.get(regie_id=109, invoice_id=18)
|
||||
assert invoice.status() == 'created'
|
||||
assert 'S10055641658' in [x['idIns'] for x in invoice.maelis_data['lineInvoiceList']]
|
||||
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.status() == 'pending_invoice'
|
||||
|
||||
# get family invoices
|
||||
freezer.move_to('2023-03-03 18:35:00')
|
||||
resp = app.get(get_endpoint('regie/109/invoices') + '?family_id=1312')
|
||||
assert '1312-18' in [x['id'] for x in resp.json['data']]
|
||||
|
||||
# pay invoice
|
||||
freezer.move_to('2023-03-03 18:38:00')
|
||||
resp = app.post_json(
|
||||
get_endpoint('regie/109/invoice/1312-18/pay/') + '?NameID=ignored',
|
||||
params={
|
||||
'transaction_date': '2023-03-03T18:38:00',
|
||||
'transaction_id': '1c5451752a064fc2bd7ea750998683e1',
|
||||
},
|
||||
)
|
||||
assert resp.json['err'] == 0
|
||||
|
||||
invoice = con.invoice_set.get(regie_id=109, invoice_id=18)
|
||||
assert invoice.status() == 'paid'
|
||||
job = Job.objects.get(method_name='notify_invoice_paid_job', natural_id='109/18')
|
||||
assert job.status == 'registered'
|
||||
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.trigger_status() == 'triggering'
|
||||
job = Job.objects.get(method_name='trigger_subscription_job', natural_id='13-12/%s' % subscription.pk)
|
||||
assert job.status == 'registered'
|
||||
|
||||
# send trigger to wcs
|
||||
con.jobs()
|
||||
assert any(
|
||||
[
|
||||
'https://wcs.example.com/api/forms/exemple-inscription-loisirs-1/12/hooks/update_subscription/'
|
||||
in x.request.url
|
||||
for x in wcs_service.calls
|
||||
]
|
||||
)
|
||||
|
||||
job = Job.objects.get(method_name='trigger_subscription_job', natural_id='13-12/%s' % subscription.pk)
|
||||
assert job.status == 'completed'
|
||||
subscription = con.subscription_set.get(wcs_form_number='13-12')
|
||||
assert subscription.trigger_status() == 'triggered'
|
||||
|
||||
|
||||
def test_trigger_wcs_on_cancelled_subscriptions_cron(
|
||||
family_service, activity_service, wcs_service, con, app, freezer, caplog
|
||||
):
|
||||
|
|
Loading…
Reference in New Issue