-
Notifications
You must be signed in to change notification settings - Fork 0
/
reservation_system.py
147 lines (112 loc) · 5.41 KB
/
reservation_system.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
import uuid
from datetime import datetime
from typing import Dict, Optional
# Mock reservation data storage
reservations: Dict[str, dict] = {}
def validate_date_time(date_str: str, time_str: str) -> bool:
try:
datetime.strptime(f"{date_str} {time_str}", "%Y-%m-%d %H:%M")
return True
except ValueError:
return False
def validate_phone_number(phone_number: str) -> bool:
return phone_number.startswith("+") and len(phone_number) >= 10
def create_reservation_response(data: dict) -> str:
try:
name = data["name"]
party_size = int(data["party_size"])
date = data["date"]
time = data["time"]
phone_number = data["phone_number"]
if not validate_phone_number(phone_number):
return "Invalid phone number format. Please use E.164 format (e.g., +19185551234)."
if party_size < 1:
return "Party size must be at least 1 person."
if not validate_date_time(date, time):
return "Invalid date or time format. Use YYYY-MM-DD for date and HH:MM for time."
if phone_number in reservations:
return "A reservation already exists for this phone number."
reservations[phone_number] = {
"name": name,
"party_size": party_size,
"date": date,
"time": time
}
return "Reservation successfully created."
except KeyError as e:
return f"Missing required field: {str(e)}"
except Exception as e:
return f"Error creating reservation: {str(e)}"
def get_reservation_response(data: dict) -> str:
try:
phone_number = data["phone_number"]
if not validate_phone_number(phone_number):
return "Invalid phone number format. Please use E.164 format (e.g., +19185551234)."
reservation = reservations.get(phone_number)
if reservation:
return f"Reservation found: {reservation['name']} for {reservation['party_size']} people on {reservation['date']} at {reservation['time']}. Contact: {phone_number}"
return "No reservation found for this phone number."
except KeyError:
return "Phone number is required."
except Exception as e:
return f"Error retrieving reservation: {str(e)}"
def update_reservation_response(data: dict) -> str:
try:
phone_number = data["phone_number"]
if not validate_phone_number(phone_number):
return "Invalid phone number format. Please use E.164 format (e.g., +19185551234)."
if phone_number not in reservations:
return "No reservation found for this phone number."
current_reservation = reservations[phone_number]
if "date" in data and "time" in data:
if not validate_date_time(data["date"], data["time"]):
return "Invalid date or time format. Use YYYY-MM-DD for date and HH:MM for time."
if "party_size" in data and int(data["party_size"]) < 1:
return "Party size must be at least 1 person."
updated_reservation = {
"name": data.get("name", current_reservation["name"]),
"party_size": int(data.get("party_size", current_reservation["party_size"])),
"date": data.get("date", current_reservation["date"]),
"time": data.get("time", current_reservation["time"])
}
reservations[phone_number] = updated_reservation
return f"Reservation updated: {updated_reservation['name']} for {updated_reservation['party_size']} people on {updated_reservation['date']} at {updated_reservation['time']}. Contact: {phone_number}"
except KeyError:
return "Phone number is required."
except Exception as e:
return f"Error updating reservation: {str(e)}"
def cancel_reservation_response(data: dict) -> str:
try:
phone_number = data["phone_number"]
if not validate_phone_number(phone_number):
return "Invalid phone number format. Please use E.164 format (e.g., +19185551234)."
if phone_number in reservations:
reservation = reservations[phone_number]
del reservations[phone_number]
return "Reservation canceled successfully."
return "No reservation found for this phone number."
except KeyError:
return "Phone number is required."
except Exception as e:
return f"Error canceling reservation: {str(e)}"
def move_reservation_response(data: dict) -> str:
try:
phone_number = data["phone_number"]
new_date = data["new_date"]
new_time = data["new_time"]
if not validate_phone_number(phone_number):
return "Invalid phone number format. Please use E.164 format (e.g., +19185551234)."
if not validate_date_time(new_date, new_time):
return "Invalid date or time format. Use YYYY-MM-DD for date and HH:MM for time."
if phone_number in reservations:
reservation = reservations[phone_number]
old_date = reservation["date"]
old_time = reservation["time"]
reservation["date"] = new_date
reservation["time"] = new_time
return "Reservation moved successfully."
return "No reservation found for this phone number."
except KeyError as e:
return f"Missing required field: {str(e)}"
except Exception as e:
return f"Error moving reservation: {str(e)}"