Skip to content

Commit 08b9e4d

Browse files
authored
Events Refactoring (unit-finance#247)
* implementation of CheckPayments * refactoring events * update event list filters
1 parent 74f8f30 commit 08b9e4d

File tree

4 files changed

+166
-320
lines changed

4 files changed

+166
-320
lines changed

e2e_tests/check_payment_test.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unit import Unit
55
from unit.models import Address
66
from unit.models.check_payment import CheckPaymentDTO, CreateCheckPaymentRequest, CheckPaymentCounterparty
7+
from unit.models.event import CheckPaymentPendingEvent
78

89
token = os.environ.get('TOKEN')
910
client = Unit("https://api.s.unit.sh", token)
@@ -109,7 +110,41 @@ def test_create_check_payment():
109110
Address("5230 Newell Rd", "Palo Alto", "CA", "94303",
110111
"US")), "test create checkPayment",
111112
relationships, send_date="2023-09-10", idempotency_key=generate_uuid())
112-
113113
res = client.check_payments.create(req)
114114
assert res
115115

116+
117+
def test_check_payment_event():
118+
data = {
119+
"id": "376",
120+
"type": "checkPayment.pending",
121+
"attributes": {
122+
"createdAt": "2021-06-06T07:21:39.509Z",
123+
"status": "Pending",
124+
"previousStatus": "New",
125+
"counterpartyMoved": True
126+
},
127+
"relationships": {
128+
"checkPayment": {
129+
"data": {
130+
"id": "122",
131+
"type": "checkPayment"
132+
}
133+
},
134+
"account": {
135+
"data": {
136+
"id": "10001",
137+
"type": "account"
138+
}
139+
},
140+
"customer": {
141+
"data": {
142+
"id": "10000",
143+
"type": "customer"
144+
}
145+
}
146+
}
147+
}
148+
149+
event = CheckPaymentPendingEvent.from_json_api(data["id"],data["type"],data["attributes"],data["relationships"])
150+
x = 6

unit/models/codecs.py

Lines changed: 10 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import json
2+
from datetime import datetime, date
3+
from typing import Dict
4+
5+
from unit.models import BeneficialOwnerDTO, RelationshipArray, Relationship, RawUnitObject
16
from unit.models.applicationForm import ApplicationFormDTO
27
from unit.models.application import IndividualApplicationDTO, BusinessApplicationDTO, ApplicationDocumentDTO,\
38
TrustApplicationDTO
@@ -16,7 +21,7 @@
1621
RecurringCreditAchPaymentDTO, RecurringCreditBookPaymentDTO, RecurringDebitAchPaymentDTO, BulkPaymentsDTO
1722
from unit.models.customerToken import CustomerTokenDTO, CustomerVerificationTokenDTO
1823
from unit.models.fee import FeeDTO
19-
from unit.models.event import *
24+
from unit.models.event import events_mapper
2025
from unit.models.counterparty import CounterpartyDTO, CounterpartyBalanceDTO
2126
from unit.models.webhook import WebhookDTO
2227
from unit.models.institution import InstitutionDTO
@@ -30,6 +35,7 @@
3035
from unit.models.account_end_of_day import AccountEndOfDayDTO
3136
from unit.models.check_deposit import CheckDepositDTO
3237
from unit.models.dispute import DisputeDTO
38+
from unit.utils import to_relationships
3339

3440
mappings = {
3541
"individualApplication": lambda _id, _type, attributes, relationships:
@@ -128,78 +134,6 @@
128134
"fee": lambda _id, _type, attributes, relationships:
129135
FeeDTO.from_json_api(_id, _type, attributes, relationships),
130136

131-
"account.closed": lambda _id, _type, attributes, relationships:
132-
AccountClosedEvent.from_json_api(_id, _type, attributes, relationships),
133-
134-
"account.frozen": lambda _id, _type, attributes, relationships:
135-
AccountFrozenEvent.from_json_api(_id, _type, attributes, relationships),
136-
137-
"application.awaitingDocuments": lambda _id, _type, attributes, relationships:
138-
ApplicationAwaitingDocumentsEvent.from_json_api(_id, _type, attributes, relationships),
139-
140-
"application.denied": lambda _id, _type, attributes, relationships:
141-
ApplicationDeniedEvent.from_json_api(_id, _type, attributes, relationships),
142-
143-
"application.pendingReview": lambda _id, _type, attributes, relationships:
144-
ApplicationPendingReviewEvent.from_json_api(_id, _type, attributes, relationships),
145-
146-
"card.activated": lambda _id, _type, attributes, relationships:
147-
CardActivatedEvent.from_json_api(_id, _type, attributes, relationships),
148-
149-
"card.statusChanged": lambda _id, _type, attributes, relationships:
150-
CardStatusChangedEvent.from_json_api(_id, _type, attributes, relationships),
151-
152-
"authorization.created": lambda _id, _type, attributes, relationships:
153-
AuthorizationCreatedEvent.from_json_api(_id, _type, attributes, relationships),
154-
155-
"authorizationRequest.declined": lambda _id, _type, attributes, relationships:
156-
AuthorizationRequestDeclinedEvent.from_json_api(_id, _type, attributes, relationships),
157-
158-
"authorizationRequest.pending": lambda _id, _type, attributes, relationships:
159-
AuthorizationRequestPendingEvent.from_json_api(_id, _type, attributes, relationships),
160-
161-
"authorizationRequest.approved": lambda _id, _type, attributes, relationships:
162-
AuthorizationRequestApprovedEvent.from_json_api(_id, _type, attributes, relationships),
163-
164-
"document.approved": lambda _id, _type, attributes, relationships:
165-
DocumentApprovedEvent.from_json_api(_id, _type, attributes, relationships),
166-
167-
"document.rejected": lambda _id, _type, attributes, relationships:
168-
DocumentRejectedEvent.from_json_api(_id, _type, attributes, relationships),
169-
170-
"document.approved": lambda _id, _type, attributes, relationships:
171-
DocumentApprovedEvent.from_json_api(_id, _type, attributes, relationships),
172-
173-
"checkDeposit.created": lambda _id, _type, attributes, relationships:
174-
CheckDepositCreatedEvent.from_json_api(_id, _type, attributes, relationships),
175-
176-
"checkDeposit.clearing": lambda _id, _type, attributes, relationships:
177-
CheckDepositClearingEvent.from_json_api(_id, _type, attributes, relationships),
178-
179-
"checkDeposit.sent": lambda _id, _type, attributes, relationships:
180-
CheckDepositSentEvent.from_json_api(_id, _type, attributes, relationships),
181-
182-
"payment.clearing": lambda _id, _type, attributes, relationships:
183-
PaymentClearingEvent.from_json_api(_id, _type, attributes, relationships),
184-
185-
"payment.sent": lambda _id, _type, attributes, relationships:
186-
PaymentSentEvent.from_json_api(_id, _type, attributes, relationships),
187-
188-
"payment.returned": lambda _id, _type, attributes, relationships:
189-
PaymentReturnedEvent.from_json_api(_id, _type, attributes, relationships),
190-
191-
"statements.created": lambda _id, _type, attributes, relationships:
192-
StatementsCreatedEvent.from_json_api(_id, _type, attributes, relationships),
193-
194-
"transaction.created": lambda _id, _type, attributes, relationships:
195-
TransactionCreatedEvent.from_json_api(_id, _type, attributes, relationships),
196-
197-
"customer.created": lambda _id, _type, attributes, relationships:
198-
CustomerCreatedEvent.from_json_api(_id, _type, attributes, relationships),
199-
200-
"account.reopened": lambda _id, _type, attributes, relationships:
201-
AccountReopenedEvent.from_json_api(_id, _type, attributes, relationships),
202-
203137
"webhook": lambda _id, _type, attributes, relationships:
204138
WebhookDTO.from_json_api(_id, _type, attributes, relationships),
205139

@@ -270,16 +204,7 @@
270204

271205
def split_json_api_single_response(payload: Dict):
272206
_id, _type, attributes = payload.get("id"), payload["type"], payload["attributes"]
273-
relationships = None
274-
275-
if payload.get("relationships"):
276-
relationships = dict()
277-
for k, v in payload.get("relationships").items():
278-
if isinstance(v["data"], list):
279-
relationships[k] = RelationshipArray(v["data"])
280-
else:
281-
relationships[k] = Relationship(v["data"]["type"], v["data"]["id"])
282-
207+
relationships = to_relationships(payload.get("relationships"))
283208
return _id, _type, attributes, relationships
284209

285210

@@ -304,6 +229,8 @@ def decode_limits(_id: str, _type: str, attributes: Dict):
304229
def mapping_wrapper(_id, _type, attributes, relationships):
305230
if _type in mappings:
306231
return mappings[_type](_id, _type, attributes, relationships)
232+
if "." in _type:
233+
return events_mapper(_id, _type, attributes, relationships)
307234
if "Transaction" in _type:
308235
return transactions_mapper(_id, _type, attributes, relationships)
309236
else:

0 commit comments

Comments
 (0)