1 CREATE TABLE students ( 2 id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, 3 email text NOT NULL UNIQUE, 4 name text NOT NULL, 5 utc_offset interval NOT NULL DEFAULT '02:00' 6 ); 7 8 CREATE TABLE live_classes ( 9 id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, 10 title text NOT NULL, 11 starts_at timestamptz NOT NULL 12 ); 13 CREATE INDEX live_classes_starts_at_index ON live_classes (starts_at); 14 15 CREATE TABLE payments ( 16 id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, 17 student_id bigint NOT NULL REFERENCES students (id), 18 amount_piasters integer NOT NULL CHECK (amount_piasters > 0), 19 paid_at timestamptz NOT NULL DEFAULT now() 20 ); 21 CREATE INDEX payments_student_id_index ON payments (student_id); 22 CREATE INDEX payments_paid_at_index ON payments (paid_at); 23 24 -- Admin form: the instructor types the class time as Cairo time, e.g. 2026-12-03 20:00 25 INSERT INTO live_classes (title, starts_at) 26 VALUES ($1, $2::timestamp AT TIME ZONE 'UTC'); 27 28 -- Reminder email: classes in the next 24 hours, shown in each student's local time 29 SELECT s.email, c.title, 30 (c.starts_at AT TIME ZONE 'UTC') + s.utc_offset AS local_start 31 FROM live_classes c 32 CROSS JOIN students s 33 WHERE c.starts_at >= now() 34 AND c.starts_at < now() + interval '24 hours'; 35 36 -- Daily income for the accountant in Cairo 37 SELECT date_trunc('day', paid_at) AS day, 38 count(*) AS payments, 39 sum(amount_piasters) / 100.0 AS income_egp 40 FROM payments 41 WHERE paid_at >= now() - interval '30 days' 42 GROUP BY 1 43 ORDER BY 1;