@platform-modules/civil-aviation-authority 2.3.330 → 2.3.331

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ -- Annual Training Plan parent/item migration.
2
+ -- Run once per environment before deploying the bulk upload API.
3
+
4
+ DO $$ BEGIN
5
+ CREATE TYPE annual_training_plan_requests_import_status_enum
6
+ AS ENUM ('Validated', 'Imported', 'Failed');
7
+ EXCEPTION
8
+ WHEN duplicate_object THEN NULL;
9
+ END $$;
10
+
11
+ ALTER TABLE annual_training_plan_requests
12
+ ADD COLUMN IF NOT EXISTS plan_name VARCHAR(255),
13
+ ADD COLUMN IF NOT EXISTS plan_number VARCHAR(30),
14
+ ADD COLUMN IF NOT EXISTS plan_year INTEGER,
15
+ ADD COLUMN IF NOT EXISTS source_file_url VARCHAR(1000),
16
+ ADD COLUMN IF NOT EXISTS source_file_name VARCHAR(255),
17
+ ADD COLUMN IF NOT EXISTS total_departments INTEGER NOT NULL DEFAULT 0,
18
+ ADD COLUMN IF NOT EXISTS total_courses INTEGER NOT NULL DEFAULT 0,
19
+ ADD COLUMN IF NOT EXISTS total_participants INTEGER NOT NULL DEFAULT 0,
20
+ ADD COLUMN IF NOT EXISTS import_status annual_training_plan_requests_import_status_enum;
21
+
22
+ CREATE UNIQUE INDEX IF NOT EXISTS uq_annual_training_plan_requests_plan_number
23
+ ON annual_training_plan_requests(plan_number)
24
+ WHERE plan_number IS NOT NULL;
25
+
26
+ CREATE TABLE IF NOT EXISTS annual_training_plan_items (
27
+ id SERIAL PRIMARY KEY,
28
+ request_id INTEGER NOT NULL REFERENCES annual_training_plan_requests(id) ON DELETE CASCADE,
29
+ row_number INTEGER NOT NULL,
30
+ department_id INTEGER NOT NULL,
31
+ section_id INTEGER,
32
+ course_name VARCHAR(255) NOT NULL,
33
+ no_of_participants INTEGER NOT NULL,
34
+ course_cost NUMERIC(10,2) NOT NULL,
35
+ total_cost NUMERIC(14,2) NOT NULL,
36
+ proposed_implementation_date VARCHAR(500) NOT NULL,
37
+ description TEXT NOT NULL,
38
+ reason TEXT NOT NULL,
39
+ job_competency VARCHAR(255),
40
+ place annual_training_plan_requests_place_enum,
41
+ location VARCHAR(255),
42
+ name_of_participants JSONB,
43
+ employee_list JSONB,
44
+ created_by INTEGER,
45
+ updated_by INTEGER,
46
+ created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
47
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
48
+ is_deleted BOOLEAN DEFAULT FALSE,
49
+ CONSTRAINT uq_annual_training_plan_items_request_row UNIQUE(request_id, row_number)
50
+ );
51
+
52
+ CREATE INDEX IF NOT EXISTS ix_annual_training_plan_items_request
53
+ ON annual_training_plan_items(request_id);
54
+ CREATE INDEX IF NOT EXISTS ix_annual_training_plan_items_department
55
+ ON annual_training_plan_items(department_id);
56
+
57
+ -- Preserve old one-course requests as one child item, then remove the duplicated parent columns.
58
+ DO $$ BEGIN
59
+ IF EXISTS (
60
+ SELECT 1 FROM information_schema.columns
61
+ WHERE table_schema = 'public'
62
+ AND table_name = 'annual_training_plan_requests'
63
+ AND column_name = 'course_name'
64
+ ) THEN
65
+ ALTER TABLE annual_training_plan_requests
66
+ ALTER COLUMN course_name DROP NOT NULL,
67
+ ALTER COLUMN no_of_participants DROP NOT NULL,
68
+ ALTER COLUMN course_cost DROP NOT NULL,
69
+ ALTER COLUMN proposed_implementation_date DROP NOT NULL,
70
+ ALTER COLUMN description DROP NOT NULL,
71
+ ALTER COLUMN reason DROP NOT NULL;
72
+
73
+ INSERT INTO annual_training_plan_items (
74
+ request_id, row_number, department_id, section_id, course_name,
75
+ no_of_participants, course_cost, total_cost, proposed_implementation_date,
76
+ description, reason, job_competency, place, location,
77
+ name_of_participants, employee_list, created_by, updated_by,
78
+ created_at, updated_at, is_deleted
79
+ )
80
+ SELECT
81
+ r.id, 1, COALESCE(r.req_user_department_id, 0), r.req_user_section_id,
82
+ r.course_name, r.no_of_participants, r.course_cost, r.total_cost,
83
+ r.proposed_implementation_date, r.description, r.reason,
84
+ r.job_competency, r.place, r.location, r.name_of_participants,
85
+ r.employee_list, r.created_by, r.updated_by, r.created_at, r.updated_at, r.is_deleted
86
+ FROM annual_training_plan_requests r
87
+ WHERE r.course_name IS NOT NULL
88
+ AND r.req_user_department_id IS NOT NULL
89
+ AND NOT EXISTS (
90
+ SELECT 1 FROM annual_training_plan_items i WHERE i.request_id = r.id
91
+ );
92
+
93
+ UPDATE annual_training_plan_requests r
94
+ SET total_departments = x.total_departments,
95
+ total_courses = x.total_courses,
96
+ total_participants = x.total_participants
97
+ FROM (
98
+ SELECT request_id,
99
+ COUNT(DISTINCT department_id)::INTEGER AS total_departments,
100
+ COUNT(*)::INTEGER AS total_courses,
101
+ COALESCE(SUM(no_of_participants), 0)::INTEGER AS total_participants
102
+ FROM annual_training_plan_items
103
+ WHERE is_deleted = FALSE
104
+ GROUP BY request_id
105
+ ) x
106
+ WHERE r.id = x.request_id;
107
+
108
+ UPDATE annual_training_plan_requests r
109
+ SET total_cost = x.total_cost
110
+ FROM (
111
+ SELECT request_id, COALESCE(SUM(total_cost), 0) AS total_cost
112
+ FROM annual_training_plan_items
113
+ WHERE is_deleted = FALSE
114
+ GROUP BY request_id
115
+ ) x
116
+ WHERE r.id = x.request_id;
117
+
118
+ ALTER TABLE annual_training_plan_requests
119
+ DROP COLUMN IF EXISTS course_name,
120
+ DROP COLUMN IF EXISTS no_of_participants,
121
+ DROP COLUMN IF EXISTS course_cost,
122
+ DROP COLUMN IF EXISTS proposed_implementation_date,
123
+ DROP COLUMN IF EXISTS description,
124
+ DROP COLUMN IF EXISTS place,
125
+ DROP COLUMN IF EXISTS location,
126
+ DROP COLUMN IF EXISTS reason,
127
+ DROP COLUMN IF EXISTS job_competency,
128
+ DROP COLUMN IF EXISTS name_of_participants,
129
+ DROP COLUMN IF EXISTS employee_list;
130
+ END IF;
131
+ END $$;
132
+
133
+ UPDATE annual_training_plan_requests
134
+ SET total_cost = 0
135
+ WHERE total_cost IS NULL;
136
+
137
+ ALTER TABLE annual_training_plan_requests
138
+ ALTER COLUMN total_cost TYPE NUMERIC(14,2),
139
+ ALTER COLUMN total_cost SET DEFAULT 0,
140
+ ALTER COLUMN total_cost SET NOT NULL;
@@ -0,0 +1,80 @@
1
+ -- One plan per course inside an annual training plan request.
2
+ -- Run once per environment before using per-plan approval.
3
+
4
+ DO $$ BEGIN
5
+ CREATE TYPE annual_training_plan_department_plans_status_enum
6
+ AS ENUM ('Pending', 'Approved', 'Rejected');
7
+ EXCEPTION
8
+ WHEN duplicate_object THEN NULL;
9
+ END $$;
10
+
11
+ DO $$ BEGIN
12
+ CREATE TYPE annual_training_plan_requests_submission_source_enum
13
+ AS ENUM ('Excel', 'Direct');
14
+ EXCEPTION
15
+ WHEN duplicate_object THEN NULL;
16
+ END $$;
17
+
18
+ CREATE TABLE IF NOT EXISTS annual_training_plan_department_plans (
19
+ id SERIAL PRIMARY KEY,
20
+ request_id INTEGER NOT NULL REFERENCES annual_training_plan_requests(id) ON DELETE CASCADE,
21
+ department_id INTEGER NOT NULL,
22
+ status annual_training_plan_department_plans_status_enum NOT NULL DEFAULT 'Pending',
23
+ created_by INTEGER,
24
+ updated_by INTEGER,
25
+ created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
26
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
27
+ is_deleted BOOLEAN DEFAULT FALSE
28
+ );
29
+
30
+ CREATE INDEX IF NOT EXISTS ix_annual_training_plan_department_plans_request
31
+ ON annual_training_plan_department_plans(request_id);
32
+
33
+ CREATE TABLE IF NOT EXISTS annual_training_plan_department_plan_decisions (
34
+ id SERIAL PRIMARY KEY,
35
+ department_plan_id INTEGER NOT NULL REFERENCES annual_training_plan_department_plans(id) ON DELETE CASCADE,
36
+ approval_level INTEGER NOT NULL,
37
+ status annual_training_plan_department_plans_status_enum NOT NULL,
38
+ comment TEXT,
39
+ decided_by INTEGER,
40
+ created_by INTEGER,
41
+ updated_by INTEGER,
42
+ created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
43
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
44
+ is_deleted BOOLEAN DEFAULT FALSE
45
+ );
46
+
47
+ CREATE INDEX IF NOT EXISTS ix_atp_department_plan_decisions_plan_level
48
+ ON annual_training_plan_department_plan_decisions(department_plan_id, approval_level);
49
+
50
+ ALTER TABLE annual_training_plan_items
51
+ ADD COLUMN IF NOT EXISTS department_plan_id INTEGER
52
+ REFERENCES annual_training_plan_department_plans(id) ON DELETE SET NULL;
53
+
54
+ ALTER TABLE annual_training_plan_requests
55
+ ADD COLUMN IF NOT EXISTS submission_source annual_training_plan_requests_submission_source_enum;
56
+
57
+ DO $$
58
+ DECLARE
59
+ rec RECORD;
60
+ new_id INTEGER;
61
+ BEGIN
62
+ FOR rec IN
63
+ SELECT id, request_id, department_id, created_by, updated_by, created_at, updated_at
64
+ FROM annual_training_plan_items
65
+ WHERE is_deleted = false
66
+ AND department_plan_id IS NULL
67
+ LOOP
68
+ INSERT INTO annual_training_plan_department_plans (
69
+ request_id, department_id, status, created_by, updated_by, created_at, updated_at, is_deleted
70
+ ) VALUES (
71
+ rec.request_id, rec.department_id, 'Pending', rec.created_by, rec.updated_by,
72
+ rec.created_at, rec.updated_at, false
73
+ )
74
+ RETURNING id INTO new_id;
75
+
76
+ UPDATE annual_training_plan_items
77
+ SET department_plan_id = new_id
78
+ WHERE id = rec.id;
79
+ END LOOP;
80
+ END $$;