|
| 1 | +-- Update moped_project table with should_sync_ecapris_statuses column and comment |
| 2 | +ALTER TABLE moped_project |
| 3 | +ADD COLUMN should_sync_ecapris_statuses BOOLEAN NOT NULL DEFAULT TRUE; |
| 4 | + |
| 5 | +-- Add comment for the new column |
| 6 | +COMMENT ON COLUMN moped_project.should_sync_ecapris_statuses IS 'Indicates if project statuses should be synced with eCapris'; |
| 7 | + |
| 8 | + |
| 9 | +-- Create ecapris_status_updates table with column comments |
| 10 | +CREATE TABLE public.ecapris_subproject_statuses ( |
| 11 | + id SERIAL PRIMARY KEY, |
| 12 | + ecapris_subproject_id TEXT NOT NULL, |
| 13 | + subproject_name TEXT NOT NULL, |
| 14 | + subproject_status_id INTEGER NOT NULL UNIQUE, |
| 15 | + current_status_fl BOOLEAN NOT NULL, |
| 16 | + sub_project_status_desc TEXT, |
| 17 | + review_timestamp TIMESTAMP WITH TIME ZONE NOT NULL, |
| 18 | + subproject_status_impacts TEXT, |
| 19 | + summary_description TEXT, |
| 20 | + reviewed_by_name TEXT NOT NULL, |
| 21 | + reviewed_by_email TEXT, |
| 22 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL, |
| 23 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL, |
| 24 | + created_by_user_id INTEGER REFERENCES moped_users (user_id) ON DELETE RESTRICT ON UPDATE CASCADE, |
| 25 | + updated_by_user_id INTEGER REFERENCES moped_users (user_id) ON DELETE RESTRICT ON UPDATE CASCADE |
| 26 | +); |
| 27 | + |
| 28 | +COMMENT ON TABLE public.ecapris_subproject_statuses IS 'Stores eCAPRIS subproject status records synced from the FSD Data Warehouse to supplement the moped_proj_notes table records.'; |
| 29 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.id IS 'Primary key for the table'; |
| 30 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.ecapris_subproject_id IS 'eCapris subproject ID number'; |
| 31 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.subproject_name IS 'Name of eCapris subproject'; |
| 32 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.subproject_status_id IS 'Unique ID of subproject status from eCapris'; |
| 33 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.current_status_fl IS 'Is this the current and most recent status?'; |
| 34 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.sub_project_status_desc IS 'Content of the subproject status'; |
| 35 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.review_timestamp IS 'Timestamp of the status update - MM/DD/YYYY HH:MM:SS format'; |
| 36 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.subproject_status_impacts IS 'Updates on project blockers'; |
| 37 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.summary_description IS 'More of a public-ready status could be used down the road; nullable in eCapris'; |
| 38 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.reviewed_by_name IS 'First and last name of author; nullable in eCapris'; |
| 39 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.reviewed_by_email IS 'Email of author'; |
| 40 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.created_at IS 'Timestamp when the record was created'; |
| 41 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.updated_at IS 'Timestamp when the record was last updated'; |
| 42 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.created_by_user_id IS 'ID of the user who created the record'; |
| 43 | +COMMENT ON COLUMN public.ecapris_subproject_statuses.updated_by_user_id IS 'ID of the user who updated the record'; |
| 44 | + |
| 45 | +-- Create trigger to set updated_at audit column before update |
| 46 | +CREATE TRIGGER set_ecapris_subproject_statuses_updated_at BEFORE UPDATE ON public.ecapris_subproject_statuses FOR EACH ROW EXECUTE FUNCTION public.set_updated_at(); |
| 47 | + |
| 48 | +COMMENT ON TRIGGER set_ecapris_subproject_statuses_updated_at ON public.ecapris_subproject_statuses IS 'Trigger to set updated_at on row update'; |
| 49 | + |
| 50 | +-- Create function to search for eCapris subproject status author match by email and set created_by_user_id if found |
| 51 | +CREATE OR REPLACE FUNCTION public.find_ecapris_user_match_by_email() |
| 52 | +RETURNS TRIGGER AS $$ |
| 53 | +BEGIN |
| 54 | + -- Try to find matching user by email (case-insensitive) email |
| 55 | + NEW.created_by_user_id := ( |
| 56 | + SELECT user_id |
| 57 | + FROM moped_users |
| 58 | + WHERE LOWER(email) = LOWER(NEW.reviewed_by_email) |
| 59 | + LIMIT 1 |
| 60 | + ); |
| 61 | + |
| 62 | + RETURN NEW; |
| 63 | +END; |
| 64 | +$$ LANGUAGE plpgsql; |
| 65 | + |
| 66 | +COMMENT ON FUNCTION public.find_ecapris_user_match_by_email() IS 'Function to attempt matching eCapris status author email to Moped users by email'; |
| 67 | + |
| 68 | +-- Create trigger to call function to try match author (if there is one) before insert |
| 69 | +CREATE TRIGGER find_ecapris_user_match_by_email BEFORE INSERT ON public.ecapris_subproject_statuses |
| 70 | +FOR EACH ROW EXECUTE FUNCTION public.find_ecapris_user_match_by_email(); |
| 71 | + |
| 72 | +COMMENT ON TRIGGER find_ecapris_user_match_by_email ON public.ecapris_subproject_statuses IS 'Trigger to attempt match of eCapris author to Moped user by email on insert'; |
0 commit comments