Coverage for jbank/pain002.py: 0%

1 from datetime import datetime

2 from os.path import basename

3 from typing import Optional

4 from django.utils.timezone import now

5 from jbank.helpers import logger

6 from jbank.models import Payout, PayoutStatus, PAYOUT_PAID

7 from jbank.sepa import Pain002

8 from jutil.format import strip_media_root

9

10

11 def process_pain002_file_content(bcontent: bytes, filename: str, created: Optional[datetime] = None):

12 if not created:

13 created = now()

14 s = Pain002(bcontent)

15 p = Payout.objects.filter(msg_id=s.original_msg_id).first()

16

17 ps = PayoutStatus(

18 payout=p,

19 file_name=basename(filename),

20 file_path=strip_media_root(filename),

21 msg_id=s.msg_id,

22 original_msg_id=s.original_msg_id,

23 group_status=s.group_status,

24 status_reason=s.status_reason[:255],

25 created=created,

26 timestamp=s.credit_datetime,

27 )

28 ps.full_clean()

29 fields = (

30 "payout",

31 "file_name",

32 "response_code",

33 "response_text",

34 "msg_id",

35 "original_msg_id",

36 "group_status",

37 "status_reason",

38 )

39 params = {}

40 for k in fields:

41 params[k] = getattr(ps, k)

42 ps_old = PayoutStatus.objects.filter(**params).first()

43 if ps_old:

44 ps = ps_old

45 else:

46 ps.save()

47 logger.info("%s status updated %s", p, ps)

48 if p:

49 if ps.is_accepted:

50 p.state = PAYOUT_PAID

51 p.paid_date = s.credit_datetime

52 p.save(update_fields=["state", "paid_date"])

53 logger.info("%s marked as paid %s", p, ps)

54 return ps