-
Notifications
You must be signed in to change notification settings - Fork 4
/
powerapp-ans-booking-app.sql
341 lines (304 loc) · 9.95 KB
/
powerapp-ans-booking-app.sql
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
-- SQL Script to create required Tables, Views and Stored Procedures for PowerApp ANS Booking App.
-- Create Buildings Table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ans_booking_buildings](
[id] [int] IDENTITY(100001,1) NOT NULL,
[building_name] [varchar](50) NOT NULL,
[address_line1] [varchar](50) NULL,
[address_line2] [varchar](50) NULL,
[address_line3] [varchar](50) NULL,
[address_city] [varchar](20) NULL,
[address_country] [varchar](20) NULL,
[address_postcode] [varchar](10) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_buildings] ADD PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO
-- Create Notes Table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ans_booking_notes](
[id] [int] IDENTITY(100001,1) NOT NULL,
[note_name] [varchar](50) NOT NULL,
[text] [varchar](2048) NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_notes] ADD PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO
INSERT INTO [dbo].[ans_booking_notes] (note_name, text)
VALUES ('terms', 'We want you and your colleagues to stay safe. Before using this application to book a workspace, you must accept our terms of use below.1. You must only use the workspace that you have booked. 2. You must only use the workspace during the booking period. 3. Social Distancing rules must be adhered to at all times.4. Wash your hands frequently or use a hand sanitizer provided.');
INSERT INTO [dbo].[ans_booking_notes] (note_name, text)
VALUES ('booking_notes', 'We want you and your colleagues to stay safe, please follow the rules below.1. You must only use the workspace that you have booked. 2. You must only use the workspace during the booking period. 3. Social Distancing rules must be adhered to at all times.4. Wash your hands frequently or use a hand sanitizer provided.')
-- Create Reservations Table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ans_booking_reservations](
[id] [int] IDENTITY(100001,1) NOT NULL,
[spot_id] [int] NOT NULL,
[reservation_date] [date] NOT NULL,
[space_option_id] [int] NOT NULL,
[outlook_id] [varchar](200) NULL,
[user_id] [int] NOT NULL,
[reservation_state] [bit] NULL,
[reservation_start_time] [time](0) NOT NULL,
[reservation_end_time] [time](0) NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_reservations] ADD PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_reservations] ADD CONSTRAINT [reservation_state_cs] DEFAULT ((1)) FOR [reservation_state]
GO
-- Create Space Options Table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ans_booking_space_options](
[id] [int] IDENTITY(100001,1) NOT NULL,
[space_id] [int] NOT NULL,
[option_name] [varchar](50) NOT NULL,
[option_start_time] [time](0) NULL,
[option_end_time] [time](0) NULL,
[options_day_sunday] [bit] NULL,
[options_day_monday] [bit] NULL,
[options_day_tuesday] [bit] NULL,
[options_day_wednesday] [bit] NULL,
[options_day_thursday] [bit] NULL,
[options_day_friday] [bit] NULL,
[options_day_saturday] [bit] NULL,
[space_option_enabled] [bit] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_space_options] ADD PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO
-- Create Spaces Table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ans_booking_spaces](
[id] [int] IDENTITY(100001,1) NOT NULL,
[building_id] [int] NOT NULL,
[space_name] [varchar](50) NOT NULL,
[space_capacity] [int] NULL,
[floor_number] [varchar](50) NULL,
[space_enabled] [bit] NULL,
[space_notes] [varchar](2048) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_spaces] ADD PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO
-- Create Spots Table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ans_booking_spots](
[id] [int] IDENTITY(100001,1) NOT NULL,
[space_id] [int] NOT NULL,
[spot_name] [varchar](50) NOT NULL,
[spot_sanitised] [bit] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_spots] ADD PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_spots] ADD CONSTRAINT [spot_sanitised_cs] DEFAULT ((0)) FOR [spot_sanitised]
GO
-- Create Users Table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ans_booking_users](
[id] [int] IDENTITY(100001,1) NOT NULL,
[full_name] [varchar](50) NOT NULL,
[email] [varchar](100) NOT NULL,
[accepted_terms] [bit] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ans_booking_users] ADD PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
ALTER TABLE [dbo].[ans_booking_users] ADD CONSTRAINT [uc_email] UNIQUE NONCLUSTERED
(
[email] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO
-- Create Reservations View
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[ans_bookings_reservations_view]
AS
SELECT res.[id]
,spot.[spot_name]
,spot.[id] AS spot_id
,spot.[spot_sanitised]
,space.[space_name]
,space.[id] AS space_id
,building.[building_name]
,building.[id] AS building_id
,res.[reservation_date]
,res.[reservation_start_time] AS [option_start_time]
,res.[reservation_end_time] AS [option_end_time]
,res.[reservation_state]
,res.[outlook_id]
,users.[full_name]
,users.[email]
,users.[id] AS user_id
FROM ((((([dbo].[ans_booking_reservations] res
INNER JOIN [dbo].[ans_booking_spots] spot ON res.spot_id = spot.id)
INNER JOIN [dbo].[ans_booking_space_options] opt ON res.space_option_id = opt.id)
INNER JOIN [dbo].[ans_booking_spaces] space ON spot.space_id = space.id)
INNER JOIN [dbo].[ans_booking_buildings] building ON space.building_id = building.id)
INNER JOIN [dbo].[ans_booking_users] users ON res.user_id = users.id)
GO
-- Create Stored Procedures Update Space Capcity
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ans_booking_spaces_update_capacity_sp]
@space_id INT
AS
BEGIN
DECLARE @capacity INT
SET @capacity = (SELECT COUNT(*) FROM [dbo].[ans_booking_spots] WHERE space_id = @space_id)
UPDATE [dbo].[ans_booking_spaces]
SET space_capacity = @capacity
WHERE id = @space_id
END;
GO
-- Create Stored Procedures Update Spot Availability
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ans_booking_spots_availability_sp]
@space_id INT,
@start TIME(0),
@end TIME(0),
@date DATE
AS
BEGIN
SELECT
spot.id,
spot.spot_name,
spot.spot_sanitised
FROM [ans_booking_spots] spot
LEFT JOIN [ans_booking_spaces] spaces ON spot.space_id = spaces.id
WHERE spot.space_id = @space_id
EXCEPT
SELECT
res.spot_id,
spot.spot_name,
spot.spot_sanitised
FROM
[ans_booking_reservations] res
LEFT JOIN [ans_booking_space_options] opt ON res.space_option_id = opt.id
LEFT JOIN [ans_booking_spots] spot ON res.spot_id = spot.id
WHERE res.reservation_date = @date
AND spot.space_id = @space_id
AND res.reservation_state = 1
AND (@end >= opt.option_start_time AND @start <= opt.option_end_time);
END;
GO
-- Create Stored Procedures Check Spot Status
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ans_booking_spots_check_status_sp]
@current_time TIME(0),
@current_date DATE
AS
BEGIN
SELECT *
FROM [dbo].[ans_bookings_reservations_view] res
WHERE res.reservation_date = @current_date AND @current_time >= DATEADD(MINUTE, -15, res.option_start_time) AND @current_time <= DATEADD(MINUTE, -10, res.option_start_time)
END;
GO
-- Create Stored Procedures Create Spot Reservation
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ans_booking_spots_reservation_sp]
@spot_id INT,
@option_id INT,
@date DATE,
@user_id INT,
@outlook_id VARCHAR(200)
AS
BEGIN
DECLARE @available_spot_id INT;
SET @available_spot_id = (
SELECT
spot_id
FROM
[dbo].[ans_booking_reservations]
WHERE
reservation_date = @date
AND spot_id = @spot_id
AND space_option_id = @option_id
)
SELECT @available_spot_id;
IF @available_spot_id IS NULL
DECLARE @reservation_start_time TIME(0)
DECLARE @reservation_end_time TIME(0)
SET @reservation_start_time = (SELECT option_start_time FROM [dbo].[ans_booking_space_options] WHERE id = @option_id)
SET @reservation_end_time = (SELECT option_end_time FROM [dbo].[ans_booking_space_options] WHERE id = @option_id)
BEGIN
INSERT INTO [dbo].[ans_booking_reservations] (spot_id, user_id, reservation_date, reservation_start_time, reservation_end_time, space_option_id, outlook_id)
VALUES(@spot_id, @user_id, @date, @reservation_start_time, @reservation_end_time, @option_id, @outlook_id)
END;
END;
GO
-- Create Stored Procedures Update Date Spot Sanitised
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ans_booking_spots_update_not_sanitised_sp]
@current_time TIME(0),
@current_date DATE
AS
BEGIN
UPDATE [dbo].[ans_booking_spots]
SET [dbo].[ans_booking_spots].[spot_sanitised] = 0
FROM (([dbo].[ans_booking_reservations] res
INNER JOIN [dbo].[ans_booking_space_options] opt ON res.space_option_id = opt.id)
INNER JOIN [dbo].[ans_booking_spots] spot ON res.spot_id = spot.id)
WHERE @current_time >= DATEADD(MINUTE, -15, opt.option_end_time) AND @current_time <= DATEADD(MINUTE, -10, opt.option_end_time) AND res.reservation_date = @current_date
END;
GO