-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_new_expenses.py
63 lines (45 loc) · 1.41 KB
/
get_new_expenses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from abstra.workflows import *
from abstra.tables import *
import abstra.hooks as ah
import starkbank
from texts import user_not_registered, thread_title_pending_justification
EXPENSES_TABLE = 'expenses'
TEAM_TABLE = "team"
CARDS_TABLE = "corporate_cards"
# Use Abstra Hooks to create Python endpoints
body, _, _ = ah.get_request()
# get purchase info
purchase_id = body['purchase_id']
purchase_obj = starkbank.CorporatePurchase.get(purchase_id)
value = purchase_obj.amount
timestamp = purchase_obj.created
tags = purchase_obj.tags
card_id = purchase_obj.card_id
corporate_card_obj = starkbank.CorporateCard.get(card_id)
card_number = corporate_card_obj.number
# retriever user
try:
card_table_row = select_one(CARDS_TABLE, where={'card_number': card_number})
except IndexError:
print("Card not registered")
exit()
try:
employee_table_row = select_one(TEAM_TABLE, where={'id': card_table_row['team_id']})
except IndexError:
print(user_not_registered)
exit()
employee_email = employee_table_row['email']
employee_team_id = employee_table_row['id']
# insert new expense
data = {
'value': int(value * 100),
'expense_time': timestamp,
'justification': None,
'approval_status': None,
'team_id': employee_team_id,
'card_id': card_table_row['id'],
'tags': tags
}
insert(EXPENSES_TABLE, data)
set_data("employee_email", employee_email)
set_title(thread_title_pending_justification)