-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathschema.sql
2681 lines (2137 loc) · 111 KB
/
schema.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
CREATE EXTENSION IF NOT EXISTS "pg_net" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "pgsodium" WITH SCHEMA "pgsodium";
ALTER SCHEMA "public" OWNER TO "postgres";
COMMENT ON SCHEMA "public" IS 'standard public schema';
CREATE EXTENSION IF NOT EXISTS "pg_graphql" WITH SCHEMA "graphql";
CREATE EXTENSION IF NOT EXISTS "pg_stat_statements" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "pgcrypto" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "pgjwt" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "pgtap" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "plpgsql_check" WITH SCHEMA "extensions";
CREATE EXTENSION IF NOT EXISTS "supabase_vault" WITH SCHEMA "vault";
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA "extensions";
CREATE TYPE "public"."app_permission" AS ENUM (
'dictionaries',
'notes',
'projects',
'verses.set',
'moderator.set',
'user_projects',
'project_source',
'coordinator.set',
'languages',
'user_languages',
'translator.set'
);
ALTER TYPE "public"."app_permission" OWNER TO "postgres";
CREATE TYPE "public"."app_role" AS ENUM (
'admin',
'coordinator',
'moderator',
'translator'
);
ALTER TYPE "public"."app_role" OWNER TO "postgres";
CREATE TYPE "public"."book_code" AS ENUM (
'gen',
'exo',
'lev',
'num',
'deu',
'jos',
'jdg',
'rut',
'1sa',
'2sa',
'1ki',
'2ki',
'1ch',
'2ch',
'ezr',
'neh',
'est',
'job',
'psa',
'pro',
'ecc',
'sng',
'isa',
'jer',
'lam',
'ezk',
'dan',
'hos',
'jol',
'amo',
'oba',
'jon',
'mic',
'nam',
'hab',
'zep',
'hag',
'zec',
'mal',
'mat',
'mrk',
'luk',
'jhn',
'act',
'rom',
'1co',
'2co',
'gal',
'eph',
'php',
'col',
'1th',
'2th',
'1ti',
'2ti',
'tit',
'phm',
'heb',
'jas',
'1pe',
'2pe',
'1jn',
'2jn',
'3jn',
'jud',
'rev',
'obs'
);
ALTER TYPE "public"."book_code" OWNER TO "postgres";
CREATE TYPE "public"."project_role" AS ENUM (
'coordinator',
'moderator',
'translator'
);
ALTER TYPE "public"."project_role" OWNER TO "postgres";
CREATE TYPE "public"."project_type" AS ENUM (
'obs',
'bible'
);
ALTER TYPE "public"."project_type" OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."admin_only"() RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
access INT;
BEGIN
SELECT
COUNT(*) INTO access
FROM
PUBLIC.users
WHERE
users.id = auth.uid() AND users.is_admin;
RETURN access > 0;
END;
$$;
ALTER FUNCTION "public"."admin_only"() OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."alphabet_change_handler"() RETURNS "trigger"
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
old_letter_exists BOOLEAN;
new_letter_exists BOOLEAN;
letter_count INT;
BEGIN
-- If the record was undeleted, check if the letter exists in the alphabet
IF OLD.deleted_at IS NOT NULL AND NEW.deleted_at IS NULL THEN
SELECT EXISTS(
SELECT 1 FROM PUBLIC.projects
WHERE jsonb_exists(dictionaries_alphabet, upper(NEW.title::VARCHAR(1)))
AND projects.id = NEW.project_id
) INTO new_letter_exists;
-- If the letter does not exist, add it to the project alphabet
IF NOT new_letter_exists THEN
UPDATE PUBLIC.projects
SET dictionaries_alphabet = dictionaries_alphabet || jsonb_build_array(upper(NEW.title::VARCHAR(1)))
WHERE projects.id = NEW.project_id;
END IF;
RETURN NEW;
END IF;
-- If the word was updated or soft deleted
IF OLD.title <> NEW.title OR (OLD.deleted_at IS NULL AND NEW.deleted_at IS NOT NULL) THEN
-- Check if there are other words starting with the same letter as the old word
SELECT EXISTS(
SELECT 1 FROM PUBLIC.dictionaries
WHERE upper(title::VARCHAR(1)) = upper(OLD.title::VARCHAR(1))
AND project_id = OLD.project_id AND deleted_at IS NULL
) INTO old_letter_exists;
-- If not, remove the letter from the project alphabet
IF NOT old_letter_exists THEN
UPDATE PUBLIC.projects
SET dictionaries_alphabet = dictionaries_alphabet - upper(OLD.title::VARCHAR(1))
WHERE projects.id = OLD.project_id;
END IF;
-- If the word was updated (not soft deleted), check if there are other words starting with the same letter as the new word
IF NEW.deleted_at IS NULL AND OLD.title <> NEW.title THEN
SELECT COUNT(id) > 1 FROM PUBLIC.dictionaries
WHERE upper(title::VARCHAR(1)) = upper(NEW.title::VARCHAR(1))
AND project_id = NEW.project_id AND deleted_at IS NULL
INTO new_letter_exists;
-- If not, add the letter to the project alphabet
IF NOT new_letter_exists THEN
UPDATE PUBLIC.projects
SET dictionaries_alphabet = dictionaries_alphabet || jsonb_build_array(upper(NEW.title::VARCHAR(1)))
WHERE projects.id = NEW.project_id;
END IF;
END IF;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION "public"."alphabet_change_handler"() OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."alphabet_insert_handler"() RETURNS "trigger"
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
new_letter_exists BOOLEAN;
BEGIN
-- Check if the letter exists in the alphabet
SELECT EXISTS(
SELECT 1 FROM PUBLIC.projects
WHERE jsonb_exists(dictionaries_alphabet, upper(NEW.title::VARCHAR(1)))
AND projects.id = NEW.project_id
) INTO new_letter_exists;
-- If the letter does not exist, add it to the project alphabet
IF NOT new_letter_exists THEN
UPDATE PUBLIC.projects
SET dictionaries_alphabet = dictionaries_alphabet || jsonb_build_array(upper(NEW.title::VARCHAR(1)))
WHERE projects.id = NEW.project_id;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION "public"."alphabet_insert_handler"() OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."assign_moderator"("user_id" "uuid", "project_id" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
usr RECORD;
BEGIN
IF authorize(auth.uid(), assign_moderator.project_id) NOT IN ('admin', 'coordinator') THEN
RETURN FALSE;
END IF;
SELECT id, is_moderator INTO usr FROM PUBLIC.project_translators WHERE project_translators.project_id = assign_moderator.project_id AND project_translators.user_id = assign_moderator.user_id;
IF usr.id IS NULL THEN
RETURN FALSE;
END IF;
UPDATE PUBLIC.project_translators SET is_moderator = TRUE WHERE project_translators.id = usr.id;
RETURN TRUE;
END;
$$;
ALTER FUNCTION "public"."assign_moderator"("user_id" "uuid", "project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."authorize"("user_id" "uuid", "project_id" bigint) RETURNS "text"
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
bind_permissions INT;
priv RECORD;
BEGIN
SELECT u.is_admin as is_admin,
pc.project_id*1 IS NOT NULL as is_coordinator,
pt.project_id*1 IS NOT NULL as is_translator,
pt.is_moderator IS TRUE as is_moderator
FROM public.users as u
LEFT JOIN public.project_coordinators as pc
ON (u.id = pc.user_id AND pc.project_id = authorize.project_id)
LEFT JOIN public.project_translators as pt
ON (u.id = pt.user_id AND pt.project_id = authorize.project_id)
WHERE u.id = authorize.user_id AND u.blocked IS NULL INTO priv;
IF priv.is_admin THEN
return 'admin';
END IF;
IF priv.is_coordinator THEN
return 'coordinator';
END IF;
IF priv.is_moderator THEN
return 'moderator';
END IF;
IF priv.is_translator THEN
return 'translator';
END IF;
return 'user';
END;
$$;
ALTER FUNCTION "public"."authorize"("user_id" "uuid", "project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."block_user"("user_id" "uuid") RETURNS "text"
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
blocked_user RECORD;
BEGIN
IF NOT PUBLIC.admin_only() THEN
RETURN FALSE;
END IF;
SELECT blocked, is_admin INTO blocked_user FROM PUBLIC.users WHERE id = block_user.user_id;
IF blocked_user.is_admin = TRUE THEN
RETURN FALSE;
END IF;
IF blocked_user.blocked IS NULL THEN
UPDATE PUBLIC.users SET blocked = NOW() WHERE id = block_user.user_id;
ELSE
UPDATE PUBLIC.users SET blocked = NULL WHERE id = block_user.user_id;
END IF;
RETURN TRUE;
END;
$$;
ALTER FUNCTION "public"."block_user"("user_id" "uuid") OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."can_translate"("translator_id" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
access INT;
BEGIN
SELECT
COUNT(*) INTO access
FROM
PUBLIC.project_translators
WHERE
user_id = auth.uid() AND id = can_translate.translator_id;
RETURN access > 0;
END;
$$;
ALTER FUNCTION "public"."can_translate"("translator_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."change_finish_chapter"("chapter_id" bigint, "project_id" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
chap RECORD;
BEGIN
IF authorize(auth.uid(), change_finish_chapter.project_id) NOT IN ('admin', 'coordinator')THEN RETURN FALSE;
END IF;
SELECT finished_at,started_at INTO chap FROM PUBLIC.chapters WHERE change_finish_chapter.chapter_id = chapters.id AND change_finish_chapter.project_id = chapters.project_id;
IF chap.started_at IS NULL
THEN RETURN FALSE;
END IF;
IF chap.finished_at IS NULL THEN
UPDATE PUBLIC.chapters SET finished_at = NOW() WHERE change_finish_chapter.chapter_id = chapters.id;
ELSE
UPDATE PUBLIC.chapters SET finished_at = NULL WHERE change_finish_chapter.chapter_id = chapters.id;
END IF;
RETURN true;
END;
$$;
ALTER FUNCTION "public"."change_finish_chapter"("chapter_id" bigint, "project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."change_start_chapter"("chapter_id" bigint, "project_id" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
chap RECORD;
BEGIN
IF authorize(auth.uid(), change_start_chapter.project_id) NOT IN ('admin', 'coordinator')THEN RETURN FALSE;
END IF;
SELECT started_at,finished_at INTO chap FROM PUBLIC.chapters WHERE change_start_chapter.chapter_id = chapters.id AND change_start_chapter.project_id = chapters.project_id;
IF chap.finished_at IS NOT NULL
THEN RETURN FALSE;
END IF;
IF chap.started_at IS NULL THEN
UPDATE PUBLIC.chapters SET started_at = NOW() WHERE change_start_chapter.chapter_id = chapters.id;
ELSE
UPDATE PUBLIC.chapters SET started_at = NULL WHERE change_start_chapter.chapter_id = chapters.id;
END IF;
RETURN true;
END;
$$;
ALTER FUNCTION "public"."change_start_chapter"("chapter_id" bigint, "project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."chapter_assign"("chapter" integer, "translators" bigint[], "project_id" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
verse_row RECORD;
num_verse INT;
x INT;
BEGIN
IF authorize(auth.uid(), chapter_assign.project_id) NOT IN ('admin', 'coordinator') THEN
RETURN FALSE;
END IF;
UPDATE PUBLIC.verses
SET project_translator_id = NULL WHERE verses.chapter_id = chapter AND verses.num >200;
num_verse = 201;
FOREACH x IN ARRAY translators LOOP
UPDATE PUBLIC.verses
SET project_translator_id = x WHERE chapter_id = chapter AND num = num_verse;
num_verse = num_verse + 1;
END LOOP;
RETURN TRUE;
END;
$$;
ALTER FUNCTION "public"."chapter_assign"("chapter" integer, "translators" bigint[], "project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."check_agreement"() RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
BEGIN
UPDATE PUBLIC.users SET agreement = TRUE WHERE users.id = auth.uid();
RETURN TRUE;
END;
$$;
ALTER FUNCTION "public"."check_agreement"() OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."check_confession"() RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
BEGIN
UPDATE PUBLIC.users SET confession = TRUE WHERE users.id = auth.uid();
RETURN TRUE;
END;
$$;
ALTER FUNCTION "public"."check_confession"() OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."compile_book"("book_id" bigint, "project_id" bigint) RETURNS TABLE("num" smallint, "text" "jsonb", "id" bigint)
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
chapter JSONB;
chapter_row RECORD;
BEGIN
IF authorize(auth.uid(), project_id) NOT IN ('admin', 'coordinator', 'moderator') Query THEN
RETURN QUERY SELECT NULL::SMALLINT AS num, '{}'::JSONB AS "text", NULL::BIGINT AS id;
END IF;
FOR chapter_row IN SELECT c.id AS chapter_id, c.num as chapter_num FROM PUBLIC.chapters c JOIN PUBLIC.verses v ON c.id = v.chapter_id WHERE c.book_id = compile_book.book_id AND c.started_at IS NOT NULL GROUP BY c.id, c.num LOOP
SELECT jsonb_object_agg(verses.num, verses."text" ORDER BY verses.num ASC) FROM PUBLIC.verses WHERE verses.project_id = compile_book.project_id AND verses.chapter_id = chapter_row.chapter_id AND verses.num < 201 INTO chapter;
UPDATE PUBLIC.chapters
SET "text"= chapter
WHERE chapters.id = chapter_row.chapter_id AND chapters.started_at IS NOT NULL;
END LOOP;
RETURN QUERY SELECT chapters.num,chapters.text,chapters.id FROM chapters WHERE chapters.id = ANY(ARRAY(SELECT chapters.id FROM PUBLIC.chapters WHERE chapters.book_id = compile_book.book_id));
END;
$$;
ALTER FUNCTION "public"."compile_book"("book_id" bigint, "project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."correct_sorting_on_deletion"() RETURNS "trigger"
LANGUAGE "plpgsql" SECURITY DEFINER
AS $_$
DECLARE
parent_sorting INT;
user_id UUID;
project_id INT8;
BEGIN
IF TG_TABLE_NAME = 'personal_notes' THEN
SELECT OLD.user_id INTO user_id;
IF OLD.parent_id IS NULL THEN
IF NEW.sorting IS NULL THEN
EXECUTE format('
UPDATE PUBLIC.%I
SET sorting = sorting - 1
WHERE user_id = $1 AND parent_id IS NULL AND sorting > $2',
TG_TABLE_NAME)
USING user_id, OLD.sorting;
END IF;
ELSE
SELECT sorting INTO parent_sorting
FROM PUBLIC.personal_notes
WHERE id = OLD.parent_id;
IF NEW.sorting IS NULL THEN
EXECUTE format('
UPDATE PUBLIC.%I
SET sorting = sorting - 1
WHERE user_id = $1 AND parent_id = $2 AND sorting > $3',
TG_TABLE_NAME)
USING user_id, OLD.parent_id, OLD.sorting - parent_sorting;
END IF;
END IF;
ELSE -- TG_TABLE_NAME = 'team_notes'
SELECT OLD.project_id INTO project_id;
IF OLD.parent_id IS NULL THEN
IF NEW.sorting IS NULL THEN
EXECUTE format('
UPDATE PUBLIC.%I
SET sorting = sorting - 1
WHERE project_id = $1 AND parent_id IS NULL AND sorting > $2',
TG_TABLE_NAME)
USING project_id, OLD.sorting;
END IF;
ELSE
SELECT sorting INTO parent_sorting
FROM PUBLIC.team_notes
WHERE id = OLD.parent_id;
IF NEW.sorting IS NULL THEN
EXECUTE format('
UPDATE PUBLIC.%I
SET sorting = sorting - 1
WHERE project_id = $1 AND parent_id = $2 AND sorting > $3',
TG_TABLE_NAME)
USING project_id, OLD.parent_id, OLD.sorting - parent_sorting;
END IF;
END IF;
END IF;
RETURN OLD;
END;
$_$;
ALTER FUNCTION "public"."correct_sorting_on_deletion"() OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."create_brief"("project_id" bigint, "is_enable" boolean, "data_collection" "json") RETURNS bigint
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
brief_id BIGINT;
BEGIN
IF authorize(auth.uid(), create_brief.project_id) NOT IN ('admin', 'coordinator') THEN
RETURN false;
END IF;
INSERT INTO PUBLIC.briefs (project_id, data_collection, is_enable) VALUES (project_id, data_collection, is_enable) RETURNING id INTO brief_id;
RETURN brief_id;
END;
$$;
ALTER FUNCTION "public"."create_brief"("project_id" bigint, "is_enable" boolean, "data_collection" "json") OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."create_chapters"("book_id" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
book RECORD;
chapter RECORD;
BEGIN
-- 1. Получаем список json глав и стихов для книги
SELECT id, chapters, project_id FROM PUBLIC.books WHERE id = create_chapters.book_id into book;
IF authorize(auth.uid(), book.project_id) NOT IN ('admin', 'coordinator') THEN
RETURN FALSE;
END IF;
FOR chapter IN SELECT * FROM json_each_text(book.chapters)
LOOP
INSERT INTO
PUBLIC.chapters (num, book_id, verses, project_id)
VALUES
(chapter.key::int2 , book.id, chapter.value::int4, book.project_id);
END LOOP;
-- 2. Наверное не вариант сразу создавать все стихи и все главы
-- 3. Создадим все главы книги. И сделаем какую-нить функцию которая потом создаст все стихи
RETURN true;
END;
$$;
ALTER FUNCTION "public"."create_chapters"("book_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."create_verses"("chapter_id" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
chapter RECORD;
start_verse int;
method_type text;
BEGIN
-- 1. Get the number of verses
SELECT chapters.id AS id,
chapters.verses AS verses,
chapters.project_id AS project_id,
steps.id AS step_id
FROM PUBLIC.chapters
JOIN PUBLIC.steps ON (steps.project_id = chapters.project_id)
WHERE chapters.id = create_verses.chapter_id
ORDER BY steps.sorting ASC
LIMIT 1
INTO chapter;
IF authorize(auth.uid(), chapter.project_id) NOT IN ('admin', 'coordinator')
THEN
RETURN FALSE;
END IF;
method_type = (SELECT type FROM projects WHERE id = chapter.project_id);
IF method_type = 'obs'
THEN
start_verse = 0;
ELSE
start_verse = 1;
END IF;
FOR i IN start_verse..chapter.verses LOOP
INSERT INTO
PUBLIC.verses (num, chapter_id, current_step, project_id)
VALUES
(i , chapter.id, chapter.step_id, chapter.project_id);
END LOOP;
FOR i IN 201..220 LOOP
INSERT INTO
PUBLIC.verses (num, chapter_id, current_step, project_id)
VALUES
(i , chapter.id, chapter.step_id, chapter.project_id);
END LOOP;
IF method_type = 'obs'
THEN
INSERT INTO
PUBLIC.verses (num, chapter_id, current_step, project_id)
VALUES
(200 , chapter.id, chapter.step_id, chapter.project_id);
ELSE
RETURN true;
END IF;
RETURN true;
END;
$$;
ALTER FUNCTION "public"."create_verses"("chapter_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."divide_verses"("divider" character varying, "project_id" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
verse_row record;
BEGIN
IF authorize(auth.uid(), divide_verses.project_id) NOT IN ('admin', 'coordinator') THEN
RETURN FALSE;
END IF;
FOR verse_row IN SELECT * FROM jsonb_to_recordset(divider::jsonb) AS x(project_translator_id INT,id INT)
LOOP
UPDATE PUBLIC.verses SET project_translator_id = verse_row.project_translator_id WHERE verse_row.id = id;
END LOOP;
RETURN TRUE;
END;
$$;
ALTER FUNCTION "public"."divide_verses"("divider" character varying, "project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."find_books_with_chapters_and_verses"("project_code" "text") RETURNS TABLE("book_code" "public"."book_code", "chapter_num" smallint, "verse_num" smallint, "verse_text" "text")
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
project_id bigint;
BEGIN
SELECT id INTO project_id FROM public.projects WHERE code = project_code;
IF project_id IS NULL THEN
RETURN;
END IF;
IF authorize(auth.uid(), project_id) NOT IN ('user', 'admin', 'coordinator', 'moderator') THEN
RETURN;
END IF;
RETURN QUERY
SELECT
b.code AS book_code,
c.num AS chapter_num,
v.num AS verse_num,
v.text AS verse_text
FROM
public.books b
INNER JOIN
public.chapters c ON b.id = c.book_id
INNER JOIN
public.verses v ON c.id = v.chapter_id
INNER JOIN
public.projects p ON b.project_id = p.id
WHERE
c.started_at IS NOT NULL
AND v.text IS NOT NULL
AND p.code = project_code;
END;
$$;
ALTER FUNCTION "public"."find_books_with_chapters_and_verses"("project_code" "text") OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."fix_sorting"("table_name" "text", "column_name" "text") RETURNS "void"
LANGUAGE "plpgsql"
AS $$
DECLARE
sql text;
BEGIN
sql := format('
WITH sorted_notes AS (
SELECT id,
row_number() OVER (PARTITION BY %I, parent_id ORDER BY sorting) - 1 AS new_sorting
FROM %I
WHERE sorting IS NOT NULL
)
UPDATE %I tn
SET sorting = sn.new_sorting
FROM sorted_notes sn
WHERE tn.id = sn.id', column_name, table_name, table_name);
EXECUTE sql;
END;
$$;
ALTER FUNCTION "public"."fix_sorting"("table_name" "text", "column_name" "text") OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."get_books_not_null_level_checks"("project_code" "text") RETURNS TABLE("book_code" "public"."book_code", "level_checks" "json")
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
project_id bigint;
BEGIN
SELECT id INTO project_id FROM public.projects WHERE code = project_code;
IF project_id IS NULL THEN
RETURN;
END IF;
IF authorize(auth.uid(), project_id) NOT IN ('user', 'admin', 'coordinator', 'moderator') THEN
RETURN;
END IF;
RETURN QUERY
SELECT
b.code AS book_code,
b.level_checks
FROM
public.books b
INNER JOIN
public.projects p ON b.project_id = p.id
WHERE
p.code = project_code
AND b.level_checks IS NOT NULL;
END;
$$;
ALTER FUNCTION "public"."get_books_not_null_level_checks"("project_code" "text") OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."get_current_steps"("project_id" bigint) RETURNS TABLE("title" "text", "project" "text", "book" "public"."book_code", "chapter" smallint, "step" smallint, "started_at" timestamp without time zone)
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
BEGIN
-- должен быть на проекте
IF authorize(auth.uid(), get_current_steps.project_id) IN ('user') THEN
RETURN;
END IF;
--
RETURN query SELECT steps.title, projects.code as project, books.code as book, chapters.num as chapter, steps.sorting as step, chapters.started_at
FROM verses
LEFT JOIN chapters ON (verses.chapter_id = chapters.id)
LEFT JOIN books ON (chapters.book_id = books.id)
LEFT JOIN steps ON (verses.current_step = steps.id)
LEFT JOIN projects ON (projects.id = verses.project_id)
WHERE verses.project_id = get_current_steps.project_id
AND chapters.started_at IS NOT NULL
AND chapters.finished_at IS NULL
AND project_translator_id = (SELECT id FROM project_translators WHERE project_translators.project_id = get_current_steps.project_id AND user_id = auth.uid())
GROUP BY books.id, chapters.id, verses.current_step, steps.id, projects.id;
END;
$$;
ALTER FUNCTION "public"."get_current_steps"("project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."get_is_await_team"("project_code" "text", "chapter_num" smallint, "book_code" "public"."book_code", "step" bigint) RETURNS boolean
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
cur_chapter_id BIGINT;
cur_project_id BIGINT;
is_awaiting_team_var BOOLEAN;
BEGIN
SELECT projects.id INTO cur_project_id
FROM PUBLIC.projects
WHERE code = project_code;
IF cur_project_id IS NULL THEN
RETURN FALSE;
END IF;
IF authorize(auth.uid(), cur_project_id) = 'user' THEN
RETURN FALSE;
END IF;
SELECT chapters.id INTO cur_chapter_id
FROM PUBLIC.chapters
LEFT JOIN PUBLIC.books ON chapters.book_id = books.id
WHERE num = chapter_num AND chapters.project_id = cur_project_id AND books.code = book_code;
IF cur_chapter_id IS NULL THEN
RETURN FALSE;
END IF;
SELECT is_awaiting_team INTO is_awaiting_team_var
FROM steps
WHERE project_id = cur_project_id AND sorting = get_is_await_team.step;
IF (is_awaiting_team_var = FALSE) THEN
RETURN FALSE;
END IF;
IF EXISTS (
SELECT 1
FROM public.verses
LEFT JOIN public.project_translators ON verses.project_translator_id = project_translators.id
LEFT JOIN public.users ON project_translators.user_id = users.id
LEFT JOIN public.steps ON verses.current_step = steps.id
WHERE verses.project_id = cur_project_id AND verses.chapter_id = cur_chapter_id
AND verses.project_translator_id IS NOT NULL and steps.sorting < get_is_await_team.step
) THEN RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;
$$;
ALTER FUNCTION "public"."get_is_await_team"("project_code" "text", "chapter_num" smallint, "book_code" "public"."book_code", "step" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."get_max_sorting"("table_name" "text", "user_id" "uuid" DEFAULT NULL::"uuid", "project_id" bigint DEFAULT NULL::bigint) RETURNS integer
LANGUAGE "plpgsql" SECURITY DEFINER
AS $_$
DECLARE
max_sorting_value integer;
BEGIN
IF table_name = 'personal_notes' THEN
EXECUTE format('
SELECT COALESCE(MAX(sorting), -1)
FROM %I
WHERE parent_id IS NULL AND user_id = $1', table_name)
INTO max_sorting_value
USING user_id;
ELSIF table_name = 'team_notes' THEN
EXECUTE format('
SELECT COALESCE(MAX(sorting), -1)
FROM %I
WHERE parent_id IS NULL AND project_id = $1', table_name)
INTO max_sorting_value
USING project_id;
END IF;
RETURN max_sorting_value + 1;
END;
$_$;
ALTER FUNCTION "public"."get_max_sorting"("table_name" "text", "user_id" "uuid", "project_id" bigint) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."get_verses"("project_id" bigint, "chapter" smallint, "book" "public"."book_code") RETURNS TABLE("verse_id" bigint, "num" smallint, "verse" "text")
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
verses_list RECORD;
cur_chapter_id BIGINT;
BEGIN
-- должен быть на проекте
IF authorize(auth.uid(), get_verses.project_id) IN ('user') THEN
RETURN;
END IF;
SELECT chapters.id into cur_chapter_id
FROM PUBLIC.chapters
WHERE chapters.num = get_verses.chapter AND chapters.project_id = get_verses.project_id AND chapters.book_id = (SELECT id FROM PUBLIC.books WHERE books.code = get_verses.book AND books.project_id = get_verses.project_id);
-- узнать id главы
IF cur_chapter_id IS NULL THEN
RETURN;
END IF;
-- вернуть айди стиха, номер и текст для определенного переводчика и из определенной главы
return query SELECT verses.id as verse_id, verses.num, verses.text as verse
FROM public.verses
WHERE verses.project_translator_id = (SELECT id
FROM PUBLIC.project_translators
WHERE project_translators.user_id = auth.uid()
AND project_translators.project_id = get_verses.project_id)
AND verses.project_id = get_verses.project_id
AND verses.chapter_id = cur_chapter_id
ORDER BY verses.num;
END;
$$;
ALTER FUNCTION "public"."get_verses"("project_id" bigint, "chapter" smallint, "book" "public"."book_code") OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."get_whole_chapter"("project_code" "text", "chapter_num" smallint, "book_code" "public"."book_code") RETURNS TABLE("verse_id" bigint, "num" smallint, "verse" "text", "translator" "text")
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
DECLARE
verses_list RECORD;
cur_chapter_id BIGINT;
cur_project_id BIGINT;
BEGIN
SELECT projects.id INTO cur_project_id
FROM PUBLIC.projects
WHERE projects.code = get_whole_chapter.project_code;
-- find out the project_id
IF cur_project_id IS NULL THEN
RETURN;
END IF;
-- user must be assigned to this project
IF authorize(auth.uid(), cur_project_id) IN ('user') THEN
RETURN;
END IF;
SELECT chapters.id INTO cur_chapter_id
FROM PUBLIC.chapters
JOIN PUBLIC.books ON chapters.book_id = books.id
WHERE chapters.num = get_whole_chapter.chapter_num
AND chapters.project_id = cur_project_id
AND books.code = get_whole_chapter.book_code
AND books.project_id = cur_project_id;
-- find out the chapter id
IF cur_chapter_id IS NULL THEN
RETURN;
END IF;
-- return the verse id, number, and text from a specific chapter
RETURN query SELECT verses.id AS verse_id, verses.num, verses.text AS verse, users.login AS translator
FROM public.verses LEFT OUTER JOIN public.project_translators ON (verses.project_translator_id = project_translators.id) LEFT OUTER JOIN public.users ON (project_translators.user_id = users.id)
WHERE verses.project_id = cur_project_id
AND verses.chapter_id = cur_chapter_id
AND verses.num < 201
ORDER BY verses.num;
END;
$$;
ALTER FUNCTION "public"."get_whole_chapter"("project_code" "text", "chapter_num" smallint, "book_code" "public"."book_code") OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."get_words_page"("search_query" "text", "words_per_page" integer, "page_number" integer, "project_id_param" bigint) RETURNS TABLE("dict_id" "text", "dict_project_id" bigint, "dict_title" "text", "dict_data" "json", "dict_created_at" timestamp without time zone, "dict_changed_at" timestamp without time zone, "dict_deleted_at" timestamp without time zone, "total_records" bigint)
LANGUAGE "plpgsql" SECURITY DEFINER