nerve/easyappointments.py

94 lines
2.8 KiB
Python
Raw Normal View History

2022-08-09 15:39:51 -04:00
import requests,json
class easyappointments:
def __init__(self,token,url):
self.token = token
self.url = url
self.headers = {"Authorization":"Bearer secrettoken"}
self.appointments = {}
self.services = {}
self.providers = {}
self.customers = {}
self.update_data()
def get_appointments(self):
data = requests.get(self.url+'/api/v1/appointments',headers=self.headers)
return data.json()
def get_services(self):
data = requests.get(self.url+'/api/v1/services',headers=self.headers)
return data.json()
def get_providers(self):
data = requests.get(self.url+'/api/v1/providers',headers=self.headers)
return data.json()
def get_customers(self):
data = requests.get(self.url+'/api/v1/customers',headers=self.headers)
return data.json()
def update_data(self):
appointments = self.get_appointments()
for a in appointments:
self.appointments[a['id']] = a
services = self.get_services()
for s in services:
self.services[s['id']]=s
providers = self.get_providers()
for p in providers:
self.providers[p['id']]=p
customers = self.get_customers()
for c in customers:
self.customers[c['id']]=c
def is_customer(self,mxid):
customers = self.get_customers()
for c in customers:
if mxid in c['notes']:
return c['id']
return False
def is_provider(self,mxid):
providers = self.get_providers()
for p in providers:
if mxid in p['notes']:
return p['id']
def get_upcoming_appointments(self,mxid):
#get services & customers
id = self.is_provider(mxid)
if id:
apts = []
data = self.get_appointments()
for a in data:
if a['providerId'] == id:
apts.append(a)
return apts
else:
return []
def list_upcoming_appointments(self,mxid):
apts = self.get_upcoming_appointments(mxid)
if apts == []:
return "No meetings recorded."
output = ""
for a in apts:
service = self.services[a['serviceId']]['name']
customer = self.customers[a['customerId']]['firstName']
output += f"## {service} with {customer}\nTime:{a['start']}\n"
return output
def register_customer(self,customer_data):
#customer data
"""
{
"firstName":"name",
"lastName":"name",
"email":"email",
"phone":"phone"
"notes":"matrix:mxid"
}
"""
id = requests.post(self.url+'/api/v1/customers',headers=self.headers,data=json.dumps(customer_data)).json()['id']
return id