Skip to content

Commit 575c95e

Browse files
authored
New fields to Customers (unit-finance#121)
* new fields to Customers fix to AuthorizedUser from json api * typo * jwtSubject added * added
1 parent 70a4aa4 commit 575c95e

File tree

3 files changed

+212
-15
lines changed

3 files changed

+212
-15
lines changed

e2e_tests/customer_test.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
from unit import Unit
44
from unit.models.customer import *
5+
from unit.models.codecs import DtoDecoder
56

67
token = os.environ.get('TOKEN')
78
client = Unit("https://api.s.unit.sh", token)
@@ -39,3 +40,170 @@ def test_list_customers():
3940
response = client.customers.list()
4041
for customer in response.data:
4142
assert customer.type == "individualCustomer" or customer.type == "businessCustomer"
43+
44+
45+
def test_business_customer():
46+
business_customer_api_response = {
47+
"type": "businessCustomer",
48+
"id": "1",
49+
"attributes": {
50+
"createdAt": "2020-05-10T12:28:37.698Z",
51+
"name": "Pied Piper",
52+
"address": {
53+
"street": "5230 Newell Rd",
54+
"street2": None,
55+
"city": "Palo Alto",
56+
"state": "CA",
57+
"postalCode": "94303",
58+
"country": "US"
59+
},
60+
"phone": {
61+
"countryCode": "1",
62+
"number": "1555555578"
63+
},
64+
"stateOfIncorporation": "DE",
65+
"ein": "123456789",
66+
"entityType": "Corporation",
67+
"contact": {
68+
"fullName": {
69+
"first": "Richard",
70+
"last": "Hendricks"
71+
},
72+
"email": "[email protected]",
73+
"phone": {
74+
"countryCode": "1",
75+
"number": "1555555578"
76+
}
77+
},
78+
"authorizedUsers": [
79+
{
80+
"fullName": {
81+
"first": "Jared",
82+
"last": "Dunn"
83+
},
84+
"email": "[email protected]",
85+
"phone": {
86+
"countryCode": "1",
87+
"number": "1555555590"
88+
}
89+
},
90+
{
91+
"fullName": {
92+
"first": "Jared",
93+
"last": "Dunn"
94+
},
95+
"email": "[email protected]",
96+
"phone": {
97+
"countryCode": "1",
98+
"number": "1555555590"
99+
}
100+
},{
101+
"fullName": {
102+
"first": "Jared",
103+
"last": "Dunn"
104+
},
105+
"email": "[email protected]",
106+
"phone": {
107+
"countryCode": "1",
108+
"number": "1555555590"
109+
}
110+
}
111+
],
112+
"status": "Active",
113+
"tags": {
114+
"userId": "106a75e9-de77-4e25-9561-faffe59d7814"
115+
}
116+
},
117+
"relationships": {
118+
"org": {
119+
"data": {
120+
"type": "org",
121+
"id": "1"
122+
}
123+
},
124+
"application": {
125+
"data": {
126+
"type": "businessApplication",
127+
"id": "1"
128+
}
129+
}
130+
}
131+
}
132+
133+
id = business_customer_api_response["id"]
134+
_type = business_customer_api_response["type"]
135+
136+
customer = DtoDecoder.decode(business_customer_api_response)
137+
138+
assert customer.id == id
139+
assert customer.type == _type
140+
assert customer.attributes["address"].street == "5230 Newell Rd"
141+
assert customer.attributes["name"] == "Pied Piper"
142+
assert customer.attributes["stateOfIncorporation"] == "DE"
143+
assert customer.attributes["contact"].full_name.first == "Richard"
144+
assert customer.attributes["authorizedUsers"][0].full_name.first == "Jared"
145+
assert customer.attributes["status"] == "Active"
146+
147+
def test_individual_customer():
148+
individual_customer_api_response = {
149+
"type": "individualCustomer",
150+
"id": "8",
151+
"attributes": {
152+
"createdAt": "2020-05-12T19:41:04.123Z",
153+
"fullName": {
154+
"first": "Peter",
155+
"last": "Parker"
156+
},
157+
"ssn": "721074426",
158+
"address": {
159+
"street": "20 Ingram St",
160+
"street2": None,
161+
"city": "Forest Hills",
162+
"state": "NY",
163+
"postalCode": "11375",
164+
"country": "US"
165+
},
166+
"dateOfBirth": "2001-08-10",
167+
"email": "[email protected]",
168+
"phone": {
169+
"countryCode": "1",
170+
"number": "1555555578"
171+
},
172+
"authorizedUsers": [],
173+
"status": "Active",
174+
"tags": {
175+
"userId": "106a75e9-de77-4e25-9561-faffe59d7814"
176+
}
177+
},
178+
"relationships": {
179+
"org": {
180+
"data": {
181+
"type": "org",
182+
"id": "1"
183+
}
184+
},
185+
"application": {
186+
"data": {
187+
"type": "individualApplication",
188+
"id": "8"
189+
}
190+
}
191+
}
192+
}
193+
194+
195+
id = individual_customer_api_response["id"]
196+
_type = individual_customer_api_response["type"]
197+
198+
customer = DtoDecoder.decode(individual_customer_api_response)
199+
200+
assert customer.id == id
201+
assert customer.type == _type
202+
assert customer.attributes["address"].street == "20 Ingram St"
203+
assert customer.attributes["fullName"].first == "Peter"
204+
assert customer.attributes["fullName"].last == "Parker"
205+
assert customer.attributes["email"] == "[email protected]"
206+
assert customer.attributes["phone"].number == "1555555578"
207+
assert customer.attributes["authorizedUsers"] == []
208+
assert customer.attributes["status"] == "Active"
209+

unit/models/__init__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,26 @@ def from_json_api(l: List):
178178

179179

180180
class AuthorizedUser(object):
181-
def __init__(self, full_name: FullName, email: str, phone: Phone):
181+
def __init__(self, full_name: FullName, email: str, phone: Phone, jwt_subject: Optional[str]):
182182
self.full_name = full_name
183183
self.email = email
184184
self.phone = phone
185+
self.jwt_subject = jwt_subject
185186

186187
@staticmethod
187-
def from_json_api(l: List):
188-
authorized_users = []
189-
for data in l:
190-
authorized_users.append(AuthorizedUser(data.get("fullName"), data.get("email"), data.get("phone")))
191-
return authorized_users
188+
def from_json_api(data):
189+
if data is None:
190+
return None
191+
192+
if data is []:
193+
return []
194+
195+
if type(data) is dict:
196+
return AuthorizedUser(FullName.from_json_api(data.get("fullName")), data.get("email"),
197+
Phone.from_json_api(data.get("phone")), data.get("jwtSubject"))
198+
199+
return [AuthorizedUser(FullName.from_json_api(d.get("fullName")), d.get("email"),
200+
Phone.from_json_api(d.get("phone")), d.get("jwtSubject")) for d in data]
192201

193202
class WireCounterparty(object):
194203
def __init__(self, routing_number: str, account_number: str, name: str, address: Address):

unit/models/customer.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
ArchiveReason = Literal["Inactive", "FraudACHActivity", "FraudCardActivity", "FraudCheckActivity",
55
"FraudApplicationHistory", "FraudAccountActivity", "FraudClientIdentified"]
66

7+
CustomerStatus = Literal["Active", "Archived"]
8+
9+
710
class IndividualCustomerDTO(object):
811
def __init__(self, id: str, created_at: datetime, full_name: FullName, date_of_birth: date, address: Address,
912
phone: Phone, email: str, ssn: Optional[str], passport: Optional[str], nationality: Optional[str],
10-
tags: Optional[Dict[str, str]], relationships: Optional[Dict[str, Relationship]]):
13+
tags: Optional[Dict[str, str]], relationships: Optional[Dict[str, Relationship]],
14+
authorized_users: Optional[List[AuthorizedUser]], status: CustomerStatus,
15+
archive_reason: Optional[ArchiveReason]):
1116
self.id = id
1217
self.type = 'individualCustomer'
1318
self.attributes = {"createdAt": created_at, "fullName": full_name, "dateOfBirth": date_of_birth,
1419
"address": address, "phone": phone, "email": email, "ssn": ssn, "passport": passport,
15-
"nationality": nationality, "tags": tags}
20+
"status": status, "archiveReason": archive_reason, "nationality": nationality,
21+
"authorizedUsers": authorized_users, "tags": tags}
1622
self.relationships = relationships
1723

1824
@staticmethod
@@ -22,20 +28,24 @@ def from_json_api(_id, _type, attributes, relationships):
2228
FullName.from_json_api(attributes["fullName"]), date_utils.to_date(attributes["dateOfBirth"]),
2329
Address.from_json_api(attributes["address"]), Phone.from_json_api(attributes["phone"]),
2430
attributes["email"], attributes.get("ssn"), attributes.get("passport"), attributes.get("nationality"),
25-
attributes.get("tags"), relationships
31+
attributes.get("tags"), relationships,
32+
AuthorizedUser.from_json_api(attributes.get("authorizedUsers")), attributes.get("status"),
33+
attributes.get("archiveReason")
2634
)
2735

2836

2937
class BusinessCustomerDTO(object):
3038
def __init__(self, id: str, created_at: datetime, name: str, address: Address, phone: Phone,
3139
state_of_incorporation: str, ein: str, entity_type: EntityType, contact: BusinessContact,
32-
authorized_users: [AuthorizedUser], dba: Optional[str], tags: Optional[Dict[str, str]],
33-
relationships: Optional[Dict[str, Relationship]]):
40+
authorized_users: Optional[List[AuthorizedUser]], dba: Optional[str], tags: Optional[Dict[str, str]],
41+
relationships: Optional[Dict[str, Relationship]], status: CustomerStatus,
42+
archive_reason: Optional[ArchiveReason]):
3443
self.id = id
3544
self.type = 'businessCustomer'
3645
self.attributes = {"createdAt": created_at, "name": name, "address": address, "phone": phone,
3746
"stateOfIncorporation": state_of_incorporation, "ein": ein, "entityType": entity_type,
38-
"contact": contact, "authorizedUsers": authorized_users, "dba": dba, "tags": tags}
47+
"contact": contact, "authorizedUsers": authorized_users, "dba": dba, "tags": tags,
48+
"status": status, "archiveReason": archive_reason}
3949
self.relationships = relationships
4050

4151
@staticmethod
@@ -45,21 +55,25 @@ def from_json_api(_id, _type, attributes, relationships):
4555
Address.from_json_api(attributes["address"]), Phone.from_json_api(attributes["phone"]),
4656
attributes["stateOfIncorporation"], attributes["ein"], attributes["entityType"],
4757
BusinessContact.from_json_api(attributes["contact"]),
48-
[AuthorizedUser.from_json_api(user) for user in attributes["authorizedUsers"]],
49-
attributes.get("dba"), attributes.get("tags"), relationships)
58+
AuthorizedUser.from_json_api(attributes.get("authorizedUsers")),
59+
attributes.get("dba"), attributes.get("tags"), relationships, attributes.get("status"),
60+
attributes.get("archiveReason"))
5061

5162
CustomerDTO = Union[IndividualCustomerDTO, BusinessCustomerDTO]
5263

5364

5465
class PatchIndividualCustomerRequest(UnitRequest):
5566
def __init__(self, customer_id: str, address: Optional[Address] = None, phone: Optional[Phone] = None,
56-
email: Optional[str] = None, dba: Optional[str] = None, tags: Optional[Dict[str, str]] = None):
67+
email: Optional[str] = None, dba: Optional[str] = None, tags: Optional[Dict[str, str]] = None,
68+
jwt_subject: Optional[str] = None, authorized_users: Optional[AuthorizedUser] = None):
5769
self.customer_id = customer_id
5870
self.address = address
5971
self.phone = phone
6072
self.email = email
6173
self.dba = dba
6274
self.tags = tags
75+
self.jwt_subject = jwt_subject
76+
self.authorized_users = authorized_users
6377

6478
def to_json_api(self) -> Dict:
6579
payload = {
@@ -84,6 +98,12 @@ def to_json_api(self) -> Dict:
8498
if self.tags:
8599
payload["data"]["attributes"]["tags"] = self.tags
86100

101+
if self.jwt_subject:
102+
payload["data"]["attributes"]["jwtSubject"] = self.jwt_subject
103+
104+
if self.authorized_users:
105+
payload["data"]["attributes"]["authorizedUsers"] = self.authorized_users
106+
87107
return payload
88108

89109
def __repr__(self):

0 commit comments

Comments
 (0)