-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotion_insurance_tracker.py
169 lines (154 loc) · 4.6 KB
/
notion_insurance_tracker.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import datetime
from notion_api_methods import *
database_id_insurance = '7f213122bd5d41db995bb770c0d130c1'
database_id_pivot = '8242f0300dda4be398b210b1e92285ab'
def query_insurance_database(date, type) -> bool:
payload = {
"page_size": 10,
"filter": {
"and": [
{
"property": "Date",
"date": {
"equals": date.isoformat()
}
},
{
"property": "Type",
"select": {
"equals": type
},
}
]
}
}
# check if page already exists
existing_appointment = query_database_pages(database_id_insurance, payload)
if not existing_appointment:
return False
return True
def get_latest_pivot_table_year(date, type) -> str:
# check date is before september
if date.month < 9:
# get previous year
date = date.replace(year=date.year - 1)
date = date.isoformat()
year = date[:4]
# TODO: fix this query for the date is between start and end
payload = {
"filter": {
"and": [
{
"property": "Timeframe",
"date": {
"past_year": {}
}
},
{
"property": "Type",
"select": {
"equals": type
},
}
]
# "and": [
# {
# "property": "Timeframe",
# "date": {
# "before": date
# }
# },
# {
# "property": "Timeframe",
# "date": {
# "after": date
# }
# }
# ]
}
}
pivot_pages = query_database_pages(database_id_pivot, payload)
if len(pivot_pages) == 0:
# create new stats page for the year
newPageData = {
"parent": {"database_id": database_id_pivot},
"properties": {
"Type": {
"type": "select",
"select": {
"name": type
}
},
"ID": {
"title": [
{
"text": {
"content": f'{type} {year}-{int(year) + 1}'
}
}
]
},
"Timeframe": {
"date": {
"start": datetime.date(int(year), 9, 1).isoformat(),
"end": datetime.date(int(year) + 1, 8, 31).isoformat()
}
}
}
}
pivot_page_id = create_page(newPageData)["id"]
else:
pivot_page_id = pivot_pages[0]['id']
return pivot_page_id
def add_new_appointment(type, cost=None, date=None, emoji=''):
"""Add a new appointment to the database"""
if date is None:
date = datetime.date.today()
# check if page already exists
if query_insurance_database(date, type):
print(f"Appointment already exists for {date.isoformat()}")
return
new_appointment_data = {
"parent": {"database_id": database_id_insurance},
"properties": {
"": {
"title": [
{
"type": "mention",
"mention": {
"type": "date",
"date": {
"start": date.isoformat(),
}
}
}
]
},
"Type": {
"type": "select",
"select": {
"name": type
}
},
"Cost": {
"number": cost
},
"Date": {
"date": {
"start": date.isoformat()
}
},
"Linked to Pivot Table": {
"relation": [
{
"id": get_latest_pivot_table_year(date, type)
}
]
}
},
"icon": {
"type": "emoji",
"emoji": emoji
} if emoji else None
}
create_page(new_appointment_data)