Changes between Version 2 and Version 3 of Triggers


Ignore:
Timestamp:
08/24/25 17:24:34 (9 days ago)
Author:
221007
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Triggers

    v2 v3  
    212212EXECUTE FUNCTION setting_punishment_depending_on_type();
    213213}}}
     214
     215=== 8. Проверка за дали Person објектот на кого се однесува !EmploymentReport е возрасна личност
     216
     217{{{
     218CREATE OR REPLACE FUNCTION check_if_person_is_adult()
     219RETURNS TRIGGER AS $$
     220DECLARE
     221    person_date_of_birth DATE;
     222    person_date_of_death DATE;
     223    person_is_alive_status BOOLEAN;
     224    created_at_report_date DATE;
     225BEGIN
     226    --I need the data so I can make the checks
     227    SELECT p.date_of_birth, p.is_alive, p.date_of_death, r.created_at
     228    INTO person_date_of_birth, person_is_alive_status, person_date_of_death, created_at_report_date
     229    FROM report r
     230    JOIN person p ON r.person_id = p.person_id
     231    WHERE r.report_id = NEW.report_id;
     232    IF person_is_alive_status IS TRUE THEN
     233        IF EXTRACT(YEAR FROM age(created_at_report_date, person_date_of_birth)) < 18 THEN
     234            RAISE EXCEPTION 'Person must be adult to have an employment report!';
     235        END IF;
     236    ELSIF person_is_alive_status IS FALSE THEN
     237        IF EXTRACT(YEAR FROM age(created_at_report_date, person_date_of_birth)) < 18 THEN
     238            RAISE EXCEPTION 'Person was not adult at the time of report creation!';
     239        ELSIF created_at_report_date > person_date_of_death THEN
     240            RAISE EXCEPTION 'The report cannot be created after the person''s death!';
     241        END IF;
     242    END IF;
     243    RETURN NEW;
     244END;
     245$$ LANGUAGE plpgsql;
     246
     247CREATE TRIGGER trg_check_if_person_adult
     248BEFORE INSERT OR UPDATE ON EmploymentReport
     249FOR EACH ROW
     250EXECUTE FUNCTION check_if_person_is_adult();
     251}}}
     252
     253=== 9. Тригер кој овозможува креирање на профил за секој регистриран корисник на апликацијата, веднаш по регистрација
     254
     255{{{
     256CREATE FUNCTION create_profile_automatically_when_user_comes() RETURNS TRIGGER AS $$
     257BEGIN
     258    INSERT INTO UserProfile (user_id, role_id, username)
     259    VALUES (NEW.user_id, 2, NEW.name || ' ' || NEW.surname);
     260    RETURN NEW;
     261END;
     262$$ LANGUAGE plpgsql;
     263
     264CREATE TRIGGER trg_create_profile_for_user
     265AFTER INSERT ON ReportiumUser
     266FOR EACH ROW
     267EXECUTE FUNCTION create_profile_automatically_when_user_comes();
     268}}}
     269
     270=== 10. Валидатор на embg форматот
     271
     272{{{
     273--function for validating embg format
     274CREATE OR REPLACE FUNCTION validate_embg_format()
     275RETURNS TRIGGER AS $$
     276DECLARE
     277    date_part TEXT;
     278    gender_code TEXT;
     279BEGIN
     280    date_part := TO_CHAR(NEW.date_of_birth, 'DD')
     281        || TO_CHAR(NEW.date_of_birth, 'MM')
     282        || RIGHT(TO_CHAR(EXTRACT(YEAR FROM NEW.date_of_birth)::INT,'9999'),3);
     283        --this is going to fetch the last 3 digits from the year
     284        --example: if it is 1990 -> it fetches 990
     285    IF SUBSTRING(NEW.embg FROM 1 FOR 7) <> date_part THEN
     286        RAISE EXCEPTION 'EMBG date part does not match the date of birth!';
     287    END IF;
     288        --gender check
     289    gender_code := SUBSTRING(NEW.embg FROM 8 FOR 3);
     290    IF (NEW.gender = 'Male' AND gender_code <> '450') OR
     291       (NEW.gender = 'Female' AND gender_code <> '455') THEN
     292        RAISE EXCEPTION 'EMBG gender code does not match gender!';
     293    END IF;
     294    RETURN NEW;
     295END;
     296$$ LANGUAGE plpgsql;
     297
     298CREATE TRIGGER check_embg_format
     299BEFORE INSERT OR UPDATE ON Person
     300FOR EACH ROW
     301EXECUTE FUNCTION validate_embg_format();
     302}}}
     303
     304=== 11. Проверка за is_alive статус кај Person објект
     305
     306{{{
     307CREATE OR REPLACE FUNCTION alive_status_check()
     308RETURNS TRIGGER AS $$
     309BEGIN
     310    IF NEW.date_of_death IS NOT NULL THEN
     311        NEW.is_alive := FALSE;
     312    END IF;
     313    RETURN NEW;
     314END;
     315$$ LANGUAGE plpgsql;
     316
     317CREATE TRIGGER alive_status_check
     318BEFORE INSERT OR UPDATE ON Person
     319FOR EACH ROW
     320EXECUTE FUNCTION alive_status_check();
     321}}}
     322