2022-08-09 15:39:51 -04:00
|
|
|
import requests,json
|
|
|
|
|
|
|
|
class easyappointments:
|
|
|
|
def __init__(self,token,url):
|
|
|
|
self.token = token
|
|
|
|
self.url = url
|
2022-09-28 11:02:35 -04:00
|
|
|
self.headers = {"Authorization":f"Bearer {token}"}
|
2022-08-09 15:39:51 -04:00
|
|
|
self.appointments = {}
|
|
|
|
self.services = {}
|
|
|
|
self.providers = {}
|
|
|
|
self.customers = {}
|
2022-09-28 11:02:35 -04:00
|
|
|
try:
|
|
|
|
self.update_data()
|
|
|
|
except:
|
|
|
|
print("Failed to connect to easyappointments!")
|
2022-08-09 15:39:51 -04:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2022-09-28 11:02:35 -04:00
|
|
|
def get_availabilities(self,service,provider,date):
|
|
|
|
try:
|
|
|
|
data = requests.get(self.url+f"/api/v1/availabilities?serviceId={service}&providerId={provider}&date={date}",headers=self.headers)
|
|
|
|
return data.json()
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2022-08-09 15:39:51 -04:00
|
|
|
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']
|
2022-09-28 11:02:35 -04:00
|
|
|
|
2022-08-09 15:39:51 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
def is_provider(self,mxid):
|
|
|
|
providers = self.get_providers()
|
|
|
|
for p in providers:
|
|
|
|
if mxid in p['notes']:
|
|
|
|
return p['id']
|
2022-09-28 11:02:35 -04:00
|
|
|
return -1
|
2022-08-09 15:39:51 -04:00
|
|
|
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 []
|
|
|
|
|
2022-09-28 11:02:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-09 15:39:51 -04:00
|
|
|
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
|
|
|
|
|
2022-09-28 11:02:35 -04:00
|
|
|
|
|
|
|
|
2022-08-09 15:39:51 -04:00
|
|
|
def register_customer(self,customer_data):
|
|
|
|
#customer data
|
|
|
|
"""
|
|
|
|
{
|
|
|
|
"firstName":"name",
|
|
|
|
"lastName":"name",
|
|
|
|
"email":"email",
|
|
|
|
"phone":"phone"
|
|
|
|
"notes":"matrix:mxid"
|
|
|
|
}
|
|
|
|
"""
|
2022-09-28 11:02:35 -04:00
|
|
|
try:
|
|
|
|
id = requests.post(self.url+'/api/v1/customers',headers=self.headers,data=json.dumps(customer_data)).json()['id']
|
|
|
|
self.update_data()
|
|
|
|
return id
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def register_appointment(self,appointment_data):
|
|
|
|
#appointment data
|
|
|
|
"""{
|
|
|
|
"start":"YY-MM-DD HH:MM:SS",
|
|
|
|
"customerId":9,
|
|
|
|
"providerId":4,
|
|
|
|
"serviceId:4
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
print("Registering appointment:")
|
|
|
|
print(json.dumps(appointment_data))
|
|
|
|
result = requests.post(
|
|
|
|
self.url+'/api/v1/appointments',
|
|
|
|
headers = self.headers,
|
|
|
|
data=json.dumps(appointment_data)
|
|
|
|
)
|
|
|
|
id = result.json()['id']
|
|
|
|
if id:
|
|
|
|
return id
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2022-08-09 15:39:51 -04:00
|
|
|
|
|
|
|
|
2022-09-28 11:02:35 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
cdat = {
|
|
|
|
"firstName":"Test",
|
|
|
|
"lastName":"",
|
|
|
|
"email":"test@testing.xa",
|
|
|
|
"phone":"888-888-8888",
|
|
|
|
"notes":"matrix:@lol:testing"
|
|
|
|
}
|
|
|
|
adat = {
|
|
|
|
"start":"2022-09-28 09:00:00",
|
|
|
|
"customerId":8,
|
|
|
|
"providerId":4,
|
|
|
|
"serviceId":2
|
|
|
|
}
|
2022-08-09 15:39:51 -04:00
|
|
|
|
2022-09-28 11:02:35 -04:00
|
|
|
easy = easyappointments('secrettoken', 'http://localhost:8787/easyappointments/index.php')
|