-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.sql
421 lines (328 loc) · 15.9 KB
/
query.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
-- APP_LOG_COUNT
SELECT count(*) FROM app_logs;
-- APP_LOG_PAGINATE
SELECT * FROM app_logs ORDER BY id DESC offset $1 LIMIT $2;
-- APP_LOG_INSERT
INSERT INTO app_logs (level,message) VALUES ($1,$2);
-- TRIGGERED_INSERT
INSERT INTO triggered (schedule_id) VALUES ($1);
-- TRIGGERED_DELETE
DELETE FROM triggered WHERE schedule_id=$1;
-- USERS_COUNT
SELECT count(*) FROM users;
-- USERS_PAGINATE
select * from users where fullname ilike $1 or email ilike $1 or phone ilike $1 order by id desc offset $2 limit $3;
-- USER_EXISTS_WITH_ID
SELECT count(*) FROM users WHERE id=$1;
-- USER_EXISTS_WITH_EMAIL
SELECT count(*) FROM users WHERE email=$1;
-- USER_GET_WITH_ID
SELECT id, fullname, email, is_admin, password FROM users WHERE id=$1 AND deleted_at isnull;
-- USER_GET_WITH_EMAIL
SELECT id, fullname, email, is_admin, password FROM users WHERE email=$1 AND deleted_at isnull;
-- USER_INSERT
INSERT INTO users (fullname,email,password,phone) VALUES ($1,$2,$3,$4) RETURNING id,fullname,email,phone;
-- USER_UPDATE_PASS
UPDATE users SET password=$1, updated_at=$2 WHERE id=$3;
-- USER_LAST_LOGIN
UPDATE users SET last_login=$1 WHERE id=$2;
-- USER_DELETE
UPDATE users SET active=$1, deleted_at=$2, updated_at=$3 WHERE id=$4;
-- GROUPS_COUNT
SELECT count(*) FROM groups WHERE user_id=$1 AND deleted_at isnull;
-- GROUPS_PAGINATE
SELECT g.*, p.name as parent, u.fullname FROM groups g
JOIN users u ON u.id=g.user_id
LEFT JOIN groups p ON p.id=g.uid
WHERE (g.name ilike $1 OR p.name ilike $1 OR u.fullname ilike $1) AND g.user_id=$2 AND g.deleted_at isnull AND p.deleted_at isnull
ORDER BY g.id DESC offset $3 LIMIT $4;
-- GROUPS
SELECT id,uid,name,active,created_at,updated_at FROM groups WHERE user_id=$1 AND deleted_at isnull;
-- GROUPS_WITH_ID
SELECT id,uid,name,active,created_at,updated_at FROM groups WHERE user_id=$1 AND id=$2 AND deleted_at isnull;
-- GROUP_INSERT
INSERT INTO groups (uid, user_id, name, active) VALUES (CASE WHEN $1 = 0 THEN NULL ELSE $1 END, $2, $3, $4) RETURNING id,uid,name,active;
-- GROUP_NAME_EXISTS_WITH_USER
SELECT count(*) FROM groups WHERE name=$1 AND user_id=$2 AND deleted_at isnull;
-- GROUP_ID_EXISTS_WITH_USER
SELECT count(*) FROM groups WHERE id=$1 AND user_id=$2 AND deleted_at isnull;
-- GROUP_DELETE
UPDATE groups SET deleted_at=$1, updated_at=$2 WHERE id=$3 AND user_id=$4;
-- REQUESTS_COUNT
SELECT count(*) FROM requests WHERE user_id=$1 AND deleted_at isnull;
-- REQUESTS_PAGINATE
SELECT r.*, u.fullname FROM requests r
JOIN users u ON u.id=r.user_id
WHERE (r.url ilike $1 OR r.method ilike $1 OR r.content::text ilike $1 OR u.fullname ilike $1) AND r.user_id=$2 AND r.deleted_at isnull
ORDER BY r.id DESC offset $3 LIMIT $4;
-- REQUESTS
SELECT * FROM requests WHERE user_id=$1 AND deleted_at isnull;
-- REQUESTS_WITH_ID
SELECT * FROM requests WHERE user_id=$1 AND id=$2 AND deleted_at isnull;
-- REQUEST_INSERT
INSERT INTO requests (user_id,url,method,content,active) VALUES ($1,$2,$3,$4,$5) RETURNING id,user_id,url,method,content,active;
-- REQUEST_URL_EXISTS_WITH_USER
SELECT count(*) FROM requests WHERE url=$1 AND user_id=$2 AND deleted_at isnull;
-- REQUEST_ID_EXISTS_WITH_USER
SELECT count(*) FROM requests WHERE id=$1 AND user_id=$2 AND deleted_at isnull;
-- REQUEST_DELETE
UPDATE requests SET deleted_at=$1, updated_at=$2 WHERE id=$3 AND user_id=$4;
-- REQUEST_HEADERS_COUNT
SELECT count(rh.*) FROM request_headers rh JOIN requests r ON r.id=rh.request_id WHERE r.user_id=$1 AND rh.deleted_at isnull;
-- REQUEST_HEADERS_PAGINATE
SELECT rh.*, r.url FROM request_headers rh
JOIN requests r ON r.id=rh.request_id
WHERE (rh.key ilike $1 OR rh.value ilike $1 OR r.url ilike $1) AND r.user_id=$2 AND r.deleted_at isnull AND rh.deleted_at isnull
ORDER BY rh.id DESC offset $3 LIMIT $4;
-- REQUEST_HEADERS
SELECT rh.* FROM request_headers rh JOIN requests r ON r.id=rh.request_id WHERE r.user_id=$1 AND rh.deleted_at isnull AND r.deleted_at isnull;
-- REQUEST_HEADERS_WITH_ID
SELECT rh.* FROM request_headers rh JOIN requests r ON r.id=rh.request_id WHERE r.user_id=$1 AND rh.id=$2 AND rh.deleted_at isnull AND r.deleted_at isnull;
-- REQUEST_HEADER_INSERT
INSERT INTO request_headers (request_id,key,value,active) VALUES ($1,$2,$3,$4) RETURNING id,request_id,key,value,active;
-- REQUEST_HEADER_EXISTS_WITH_USER
SELECT count(*) FROM request_headers rh JOIN requests r ON r.id=rh.request_id
WHERE rh.key=$1 AND r.user_id=$2 AND r.id=$3 AND rh.deleted_at isnull;
-- REQUEST_HEADER_ID_EXISTS_WITH_USER
SELECT count(*) FROM request_headers rh JOIN requests r ON r.id=rh.request_id WHERE rh.id=$1 AND r.user_id=$2 AND rh.deleted_at isnull AND r.deleted_at isnull;
-- REQUEST_HEADER_DELETE
UPDATE request_headers SET deleted_at=$1, updated_at=$2 FROM requests
WHERE requests.id=request_headers.request_id AND request_headers.id=$3 AND requests.user_id=$4;
-- SCHEDULES_COUNT
SELECT count(*) FROM schedules WHERE user_id=$1 AND deleted_at isnull;
-- SCHEDULES_PAGINATE
SELECT s.*, g.name, r.url, n.title FROM schedules s
JOIN groups g ON g.id=s.group_id
JOIN requests r ON r.id=s.request_id
JOIN notifications n ON n.id=s.notification_id
WHERE s.user_id=$1 AND s.deleted_at isnull AND (s.timing ilike $2 OR g.name ilike $2 OR r.url ilike $2 OR n.title ilike $2)
ORDER BY s.id DESC offset $3 LIMIT $4;
-- SCHEDULES
SELECT s.*, g.name, r.url, n.title FROM schedules s
JOIN groups g ON g.id=s.group_id
JOIN requests r ON r.id=s.request_id
JOIN notifications n ON n.id=s.notification_id
WHERE s.user_id=$1 AND s.deleted_at isnull;
-- SCHEDULES_WITH_ID
SELECT * FROM schedules WHERE user_id=$1 AND id=$2 AND deleted_at isnull;
-- SCHEDULES_INSERT
INSERT INTO schedules (user_id,group_id,request_id,notification_id,timing,timeout,retries,active)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING id,user_id,group_id,request_id,notification_id,timing,timeout,retries,running,active;
-- SCHEDULES_ID_EXISTS_WITH_USER
SELECT count(*) FROM schedules WHERE id=$1 AND user_id=$2 AND deleted_at isnull;
-- SCHEDULES_TIMING_EXISTS_WITH_USER
SELECT count(*) FROM schedules WHERE user_id=$1 AND request_id=$2 AND timing=$3 AND deleted_at isnull;
-- SCHEDULES_DELETE
UPDATE schedules SET deleted_at=$1, updated_at=$2 WHERE id=$3 AND user_id=$4;
-- SCHEDULE_LOGS_COUNT
SELECT count(sl.*) FROM schedule_logs sl JOIN schedules s ON s.id=sl.schedule_id WHERE s.user_id=$1 AND s.deleted_at isnull;
-- SCHEDULE_LOGS_PAGINATE
SELECT sl.*, s.timing FROM schedule_logs sl
JOIN schedules s ON s.id=sl.schedule_id
WHERE s.user_id=$1 AND s.deleted_at isnull AND s.timing ilike $2
ORDER BY sl.id DESC offset $3 LIMIT $4;
-- SCHEDULE_LOGS
SELECT * FROM schedule_logs sl JOIN schedules s ON s.id=sl.schedule_id WHERE sl.schedule_id=$1 AND s.user_id=$2;
-- WEBHOOKS_COUNT
SELECT count(w.*) FROM webhooks w JOIN schedules s ON s.id=w.schedule_id WHERE s.user_id=$1 AND w.deleted_at isnull;
-- WEBHOOKS_PAGINATE
SELECT w.*, s.timing, r.url FROM webhooks w
JOIN schedules s ON s.id=w.schedule_id
JOIN requests r ON r.id=w.request_id
WHERE s.user_id=$1 AND w.deleted_at isnull AND (s.timing ilike $2 OR r.url ilike $2)
ORDER BY w.id DESC offset $3 LIMIT $4;
-- WEBHOOKS
SELECT w.*, s.timing, r.url FROM webhooks w
JOIN schedules s ON s.id=w.schedule_id
JOIN requests r ON r.id=w.request_id
WHERE s.user_id=$1 AND w.deleted_at isnull;
-- WEBHOOKS_WITH_ID
SELECT w.* FROM webhooks w JOIN schedules s ON s.id=w.schedule_id WHERE w.id=$1 AND s.user_id=$2 AND w.deleted_at isnull;
-- WEBHOOK_INSERT
INSERT INTO webhooks (schedule_id,request_id, active) VALUES ($1,$2,$3) RETURNING id,schedule_id,request_id,active;
-- WEBHOOK_ID_EXISTS_WITH_USER
SELECT count(*) FROM webhooks w JOIN schedules s ON s.id=w.schedule_id WHERE w.id=$1 AND s.user_id=$2 AND w.deleted_at isnull;
-- WEBHOOK_UNIQ_EXISTS_WITH_USER
SELECT count(*) FROM webhooks w JOIN schedules s ON s.id=w.schedule_id WHERE w.schedule_id=$1 AND w.request_id=$2 AND s.user_id=$3 AND w.deleted_at isnull;
-- WEBHOOK_DELETE
UPDATE webhooks SET deleted_at=$1, updated_at=$2 FROM schedules
WHERE schedules.id=webhooks.schedule_id AND webhooks.id=$3 AND schedules.user_id=$4;
-- NOTIFICATIONS_COUNT
SELECT count(*) FROM notifications WHERE user_id=$1 AND deleted_at isnull;
-- NOTIFICATIONS_PAGINATE
SELECT n.*, u.fullname FROM notifications n
JOIN users u ON u.id=n.user_id
WHERE (n.title ilike $1 OR n.content ilike $1 OR u.fullname ilike $1) AND n.user_id=$2 AND n.deleted_at isnull
ORDER BY n.id DESC offset $3 LIMIT $4;
-- NOTIFICATIONS
SELECT * FROM notifications WHERE user_id=$1 AND deleted_at isnull;
-- NOTIFICATIONS_WITH_ID
SELECT * FROM notifications WHERE user_id=$1 AND id=$2 AND deleted_at isnull;
-- NOTIFICATION_INSERT
INSERT INTO notifications (user_id,title,content,is_mail,is_message,active) VALUES ($1,$2,$3,$4,$5,$6) RETURNING id,user_id,title,content,is_mail,is_message,active;
-- NOTIFICATION_TITLE_EXISTS_WITH_USER
SELECT count(*) FROM notifications WHERE user_id=$1 AND title=$2 AND deleted_at isnull;
-- NOTIFICATION_ID_EXISTS_WITH_USER
SELECT count(*) FROM notifications WHERE id=$1 AND user_id=$2 AND deleted_at isnull;
-- NOTIFICATION_DELETE
UPDATE notifications SET deleted_at=$1, updated_at=$2 WHERE id=$3 AND user_id=$4;
-- NOTIFICATION_EMAILS_COUNT
SELECT count(ne.*) FROM notify_emails ne JOIN notifications n ON n.id=ne.notification_id WHERE n.user_id=$1 AND ne.deleted_at isnull;
-- NOTIFICATION_EMAILS_PAGINATE
SELECT ne.*, n.title FROM notify_emails ne
JOIN notifications n ON n.id=ne.notification_id
WHERE (n.title ilike $1) AND n.user_id=$2 AND ne.deleted_at isnull AND n.deleted_at isnull
ORDER BY ne.id DESC offset $3 LIMIT $4;
-- NOTIFICATION_EMAILS
SELECT ne.*, n.title FROM notify_emails ne JOIN notifications n ON n.id=ne.notification_id WHERE n.user_id=$1 AND ne.deleted_at isnull AND n.deleted_at isnull;
-- NOTIFICATION_EMAILS_WITH_ID
SELECT ne.* FROM notify_emails ne JOIN notifications n ON n.id=ne.notification_id WHERE n.user_id=$1 AND ne.id=$2 AND ne.deleted_at isnull AND n.deleted_at isnull;
-- NOTIFICATION_EMAIL_INSERT
INSERT INTO notify_emails (notification_id,email,active) VALUES ($1,$2,$3) RETURNING id,notification_id,email,active;
-- NOTIFICATION_EMAIL_EXISTS_WITH_USER
SELECT count(*) FROM notify_emails ne JOIN notifications n ON n.id=ne.notification_id
WHERE n.user_id=$1 AND ne.email=$2 AND n.id=$3 AND ne.deleted_at isnull AND n.deleted_at isnull;
-- NOTIFICATION_EMAIL_ID_EXISTS_WITH_USER
SELECT count(*) FROM notify_emails ne JOIN notifications n ON n.id=ne.notification_id
WHERE ne.id=$1 AND n.user_id=$2 AND ne.deleted_at isnull AND n.deleted_at isnull;
-- NOTIFICATION_EMAIL_DELETE
UPDATE notify_emails SET deleted_at=$1, updated_at=$2 FROM notifications
WHERE notifications.id=notify_emails.notification_id AND notify_emails.id=$3 AND notifications.user_id=$4;
-- NOTIFICATION_MESSAGES_COUNT
SELECT count(nm.*) FROM notify_messages nm JOIN notifications n ON n.id=nm.notification_id WHERE n.user_id=$1 AND nm.deleted_at isnull;
-- NOTIFICATION_MESSAGES_PAGINATE
SELECT nm.*, n.title FROM notify_messages nm
JOIN notifications n ON n.id=nm.notification_id
WHERE (n.title ilike $1) AND n.user_id=$2 AND nm.deleted_at isnull AND n.deleted_at isnull
ORDER BY nm.id DESC offset $3 LIMIT $4;
-- NOTIFICATION_MESSAGES
SELECT nm.*, n.title FROM notify_messages nm JOIN notifications n ON n.id=nm.notification_id WHERE n.user_id=$1 AND nm.deleted_at isnull AND n.deleted_at isnull;
-- NOTIFICATION_MESSAGE_WITH_ID
SELECT nm.* FROM notify_messages nm JOIN notifications n ON n.id=nm.notification_id WHERE n.user_id=$1 AND nm.id=$2 AND ns.deleted_at isnull AND n.deleted_at isnull;
-- NOTIFICATION_MESSAGE_INSERT
INSERT INTO notify_messages (notification_id,phone,active) VALUES ($1,$2,$3) RETURNING id,notification_id,phone,active;
-- NOTIFICATION_MESSAGE_PHONE_EXISTS_WITH_USER
SELECT count(*) FROM notify_messages nm JOIN notifications n ON n.id=nm.notification_id
WHERE n.user_id=$1 AND nm.phone=$2 AND n.id=$3 AND nm.deleted_at isnull AND n.deleted_at isnull;
-- NOTIFICATION_MESSAGE_ID_EXISTS_WITH_USER
SELECT count(*) FROM notify_messages nm JOIN notifications n ON n.id=nm.notification_id
WHERE nm.id=$1 AND n.user_id=$2 AND nm.deleted_at isnull AND n.deleted_at isnull;
-- NOTIFICATION_MESSAGE_DELETE
UPDATE notify_messages SET deleted_at=$1, updated_at=$2 FROM notifications
WHERE notifications.id=notify_messages.notification_id AND notify_messages.id=$3 AND notifications.user_id=$4;
-- SCHEDULE_MAPS
WITH schedule_lists AS (
SELECT
s.*,
json_build_object(
'id', u.id,
'fullname', u.fullname,
'email', u.email,
'phone', u.phone
) as user,
json_build_object(
'id', g.id,
'uid', g.uid,
'name', g.name,
'active', g.active,
'parent', (
SELECT json_build_object(
'id', p.id,
'name', p.name
)
FROM groups p
WHERE p.id = g.uid
)
) as group,
json_build_object(
'id', r.id,
'user_id', r.user_id,
'url', r.url,
'method', r.method,
'content', r.content,
'active', r.active,
'headers', (
SELECT json_agg(
json_build_object(
'id', rh.id,
'key', rh.key,
'value', rh.value,
'active', rh.active
)
)
FROM request_headers rh
WHERE rh.request_id = r.id
)
) as request,
json_build_object(
'id', n.id,
'user_id', n.user_id,
'title', n.title,
'content', n.content,
'is_mail', n.is_mail,
'is_message', n.is_mail,
'active', n.active,
'emails', (
SELECT json_agg(
json_build_object(
'id', ne.id,
'email', ne.email,
'active', ne.active
)
)
FROM notify_emails ne
WHERE ne.notification_id = n.id
),
'messages', (
SELECT json_agg(
json_build_object(
'id', nm.id,
'phone', nm.phone,
'active', nm.active
)
)
FROM notify_messages nm
WHERE nm.notification_id = n.id
)
) as notification,
json_agg(
json_build_object(
'id', w.id,
'schedule_id', w.schedule_id,
'request_id', w.request_id,
'active', w.active,
'requests', (
SELECT json_agg(
json_build_object(
'id', r.id,
'url', r.url,
'content', r.content,
'active', r.active,
'headers', (
SELECT json_agg(
json_build_object(
'id', rh.id,
'key', rh.key,
'value', rh.value,
'active', rh.active
)
)
FROM request_headers rh
WHERE rh.request_id = r.id
)
)
)
FROM requests r
WHERE r.id = w.request_id
)
)
) as webhooks
FROM schedules as s
JOIN users AS u ON u.id = s.user_id
JOIN groups AS g ON g.id = s.group_id
JOIN requests AS r ON r.id = s.request_id
JOIN notifications n on n.id=s.notification_id
LEFT JOIN webhooks w on w.schedule_id=s.id
GROUP BY s.id, u.id, g.id, r.id, n.id
)
SELECT * FROM schedule_lists WHERE deleted_at isnull;