Skip to content

Commit 59f576c

Browse files
committed
feat(an-web): add parse normalize to base widget actions
1 parent 91bcc77 commit 59f576c

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

action-network/web/normalize.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import re
55

6-
from typings import Donation, Form, Payload, Pressure, Plip
6+
from typings import Donation, Form, Payload, Pressure, Plip, BaseAction
77
from utils import only_digits, get_field, find_by_ddd
88

99

@@ -151,6 +151,19 @@ def plip(payload: Plip):
151151
)
152152
return clean_response(item)
153153

154+
def widget_action(payload: BaseAction):
155+
item = {
156+
"name": payload.first_name + " " + payload.last_name,
157+
"email": payload.email,
158+
"phone": normalize_phone(payload.phone_number) if payload.phone_number else None,
159+
"metadata": payload.custom_fields
160+
}
161+
162+
if item["phone"]:
163+
item["region"] = find_by_ddd(item["phone"])
164+
165+
return clean_response(item)
166+
154167

155168
def to_payload(data: Payload):
156169
"""to_payload"""
@@ -174,9 +187,14 @@ def to_payload(data: Payload):
174187
response = plip(payload=payload)
175188
response["action"] = "plip"
176189

190+
else:
191+
# Ação padronizada
192+
response = widget_action(payload=payload)
193+
response["action"] = payload.kind
194+
177195
response["widget_id"] = payload.widget_id
178196
response["mobilization_id"] = payload.mobilization_id
179-
response["community_id"] = payload.cached_community_id
197+
response["community_id"] = payload.cached_community_id or payload.community_id
180198
response["action_id"] = payload.id
181199
response["action_date"] = payload.created_at
182200

action-network/web/typings.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,24 @@ class Donation(WidgetAction):
8080
subscription: bool
8181

8282

83+
class BaseAction(BaseModel):
84+
id: int
85+
widget_id: int
86+
mobilization_id: int
87+
community_id: int
88+
activist_id: int
89+
first_name: str
90+
last_name: str
91+
email: str
92+
phone_number: Optional[str] = None
93+
custom_fields: Optional[any] = {}
94+
kind: str
95+
created_at: str
96+
97+
8398
class Data(BaseModel):
8499
"""Hasura Data Model"""
85-
new: Pressure | Plip | Donation | Form
100+
new: Pressure | Plip | Donation | Form | BaseAction
86101

87102

88103
class Event(BaseModel):

0 commit comments

Comments
 (0)