From 5ce8808b083193f69e08f2864f0d632f67c4b37c Mon Sep 17 00:00:00 2001 From: Landung 'Don' Setiawan Date: Fri, 8 Nov 2024 16:52:14 -0800 Subject: [PATCH] feat: Add checklists models and populate --- .../support_sphere/models/enums/__init__.py | 3 +- .../support_sphere/models/enums/priority.py | 11 ++++ .../support_sphere/models/public/__init__.py | 22 +++---- .../support_sphere/models/public/checklist.py | 51 +++++++++++++++++ ...st_steps_template.py => checklist_step.py} | 17 ++++-- .../models/public/checklist_steps_order.py | 17 ++++-- .../models/public/checklist_steps_state.py | 32 +++++++++++ .../models/public/checklist_type.py | 49 ---------------- .../support_sphere/models/public/frequency.py | 29 ++++++++++ .../models/public/recurring_type.py | 29 ---------- .../models/public/user_checklist.py | 27 ++++----- .../models/public/user_checklist_state.py | 33 ----------- .../models/public/user_profile.py | 4 +- .../scripts/execute_sql_statement.py | 50 ++++++++++++++++ .../scripts/resources/data/checklists.json | 57 +++++++++++++++++++ 15 files changed, 279 insertions(+), 152 deletions(-) create mode 100644 src/support_sphere_py/src/support_sphere/models/enums/priority.py create mode 100644 src/support_sphere_py/src/support_sphere/models/public/checklist.py rename src/support_sphere_py/src/support_sphere/models/public/{checklist_steps_template.py => checklist_step.py} (63%) create mode 100644 src/support_sphere_py/src/support_sphere/models/public/checklist_steps_state.py delete mode 100644 src/support_sphere_py/src/support_sphere/models/public/checklist_type.py create mode 100644 src/support_sphere_py/src/support_sphere/models/public/frequency.py delete mode 100644 src/support_sphere_py/src/support_sphere/models/public/recurring_type.py delete mode 100644 src/support_sphere_py/src/support_sphere/models/public/user_checklist_state.py create mode 100644 src/support_sphere_py/src/support_sphere/scripts/resources/data/checklists.json diff --git a/src/support_sphere_py/src/support_sphere/models/enums/__init__.py b/src/support_sphere_py/src/support_sphere/models/enums/__init__.py index 3cefa23..d788a5d 100644 --- a/src/support_sphere_py/src/support_sphere/models/enums/__init__.py +++ b/src/support_sphere_py/src/support_sphere/models/enums/__init__.py @@ -1,6 +1,7 @@ from support_sphere.models.enums.app_permissions import AppPermissions from support_sphere.models.enums.app_roles import AppRoles from support_sphere.models.enums.operational_status import OperationalStatus +from support_sphere.models.enums.priority import Priority -__all__ = ['AppPermissions', 'AppRoles', 'OperationalStatus'] +__all__ = ['AppPermissions', 'AppRoles', 'OperationalStatus', Priority] diff --git a/src/support_sphere_py/src/support_sphere/models/enums/priority.py b/src/support_sphere_py/src/support_sphere/models/enums/priority.py new file mode 100644 index 0000000..2668875 --- /dev/null +++ b/src/support_sphere_py/src/support_sphere/models/enums/priority.py @@ -0,0 +1,11 @@ +from enum import Enum + + +class Priority(Enum): + LOW = ("low", "Low Priority") + MEDIUM = ("medium", "Medium Priority") + HIGH = ("high", "High Priority") + + def __init__(self, priority, description): + self.priority = priority + self.description = description diff --git a/src/support_sphere_py/src/support_sphere/models/public/__init__.py b/src/support_sphere_py/src/support_sphere/models/public/__init__.py index 62f68b9..8f71ae7 100644 --- a/src/support_sphere_py/src/support_sphere/models/public/__init__.py +++ b/src/support_sphere_py/src/support_sphere/models/public/__init__.py @@ -1,13 +1,13 @@ from support_sphere.models.public.checklist_steps_order import ChecklistStepsOrder -from support_sphere.models.public.checklist_steps_template import ChecklistStepsTemplate -from support_sphere.models.public.checklist_type import ChecklistType +from support_sphere.models.public.checklist_step import ChecklistStep +from support_sphere.models.public.checklist_steps_state import ChecklistStepsState +from support_sphere.models.public.checklist import Checklist from support_sphere.models.public.cluster import Cluster from support_sphere.models.public.household import Household from support_sphere.models.public.operational_event import OperationalEvent from support_sphere.models.public.people import People from support_sphere.models.public.people_group import PeopleGroup from support_sphere.models.public.point_of_interest import PointOfInterest -from support_sphere.models.public.recurring_type import RecurringType from support_sphere.models.public.resource import Resource from support_sphere.models.public.resource_subtype_tag import ResourceSubtypeTag from support_sphere.models.public.resource_tag import ResourceTag @@ -15,28 +15,28 @@ from support_sphere.models.public.resource_cv import ResourceCV from support_sphere.models.public.role_permission import RolePermission from support_sphere.models.public.user_captain_cluster import UserCaptainCluster -from support_sphere.models.public.user_checklist import UserChecklist -from support_sphere.models.public.user_checklist_state import UserChecklistState from support_sphere.models.public.user_profile import UserProfile from support_sphere.models.public.user_resource import UserResource from support_sphere.models.public.user_role import UserRole from support_sphere.models.public.signup_code import SignupCode +from support_sphere.models.public.frequency import Frequency +from support_sphere.models.public.user_checklist import UserChecklists # New models created should be exposed by adding to __all__. This is used by SQLModel.metadata # https://sqlmodel.tiangolo.com/tutorial/create-db-and-table/#sqlmodel-metadata-order-matters __all__ = [ + "Checklist", "ChecklistStepsOrder", - "ChecklistStepsTemplate", - "ChecklistType", + "ChecklistStep", + "ChecklistStepsState", "Cluster", "Household", "OperationalEvent", "People", "PeopleGroup", "PointOfInterest", - "RecurringType", "Resource", "ResourceSubtypeTag", "ResourceTag", @@ -45,9 +45,9 @@ "RolePermission", "SignupCode", "UserCaptainCluster", - "UserChecklist", - "UserChecklistState", - "UserProfile", "UserResource", + "UserProfile", + "UserChecklists", "UserRole", + "Frequency", ] diff --git a/src/support_sphere_py/src/support_sphere/models/public/checklist.py b/src/support_sphere_py/src/support_sphere/models/public/checklist.py new file mode 100644 index 0000000..950bff8 --- /dev/null +++ b/src/support_sphere_py/src/support_sphere/models/public/checklist.py @@ -0,0 +1,51 @@ +import uuid +import datetime +from typing import Optional +from sqlalchemy import Enum + +from support_sphere.models.base import BasePublicSchemaModel +from support_sphere.models.enums import Priority +from sqlmodel import Field, Relationship + + +class Checklist(BasePublicSchemaModel, table=True): + """ + Represents a checklist entity in the 'public' schema under the 'checklists' table. + This table defines different types of checklists, including the associated frequency. + + Attributes + ---------- + id : uuid + The unique identifier for the checklist. + recurring_type_id : uuid + The foreign key referring to the frequency. + title : str + The title of the checklist. + description : str, optional + A detailed description of the checklist. + current_version : int + The current version of this checklist. + updated_at : datetime + The timestamp for the last update of this checklist. + frequency : Frequency, optional + A relationship to the `Frequency` model. + user_checklists : list[UserChecklists] + A list of `UserChecklist` entities associated with this checklist. + """ + + __tablename__ = "checklists" + + id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) + title: str | None = Field(nullable=False) + description: str | None = Field(nullable=True) + notes: str | None = Field(nullable=True) + updated_at: datetime.datetime = Field( + default_factory=lambda: datetime.datetime.now(datetime.UTC), + nullable=False + ) + priority: Priority = Field(default=Priority.LOW, sa_type=Enum(Priority, name="priority"), nullable=False) + frequency_id: uuid.UUID | None = Field(foreign_key="public.frequency.id", nullable=True) + + frequency: Optional["Frequency"] = Relationship(back_populates="checklists", cascade_delete=False) + user_checklists: list["UserChecklists"] = Relationship(back_populates="checklists", cascade_delete=False) + checklist_steps_orders: list["ChecklistStepsOrder"] = Relationship(back_populates="checklists", cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_template.py b/src/support_sphere_py/src/support_sphere/models/public/checklist_step.py similarity index 63% rename from src/support_sphere_py/src/support_sphere/models/public/checklist_steps_template.py rename to src/support_sphere_py/src/support_sphere/models/public/checklist_step.py index cb9c89d..5287227 100644 --- a/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_template.py +++ b/src/support_sphere_py/src/support_sphere/models/public/checklist_step.py @@ -1,11 +1,12 @@ import uuid from typing import Optional +import datetime from support_sphere.models.base import BasePublicSchemaModel from sqlmodel import Field, Relationship -class ChecklistStepsTemplate(BasePublicSchemaModel, table=True): +class ChecklistStep(BasePublicSchemaModel, table=True): """ Represents a template for checklist steps in the 'public' schema under the 'checklist_steps_templates' table. @@ -17,14 +18,18 @@ class ChecklistStepsTemplate(BasePublicSchemaModel, table=True): The title of the checklist step template. description : str, optional A detailed description of the checklist step template. - checklist_steps_order : ChecklistStepsOrder, optional + checklist_steps_order : list[ChecklistStepsOrder] A relationship to the `ChecklistStepsOrder` model, which defines the order of these steps within a checklist. """ - __tablename__ = "checklist_steps_templates" + __tablename__ = "checklist_steps" id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) - title: str | None = Field(nullable=False) + label: str | None = Field(nullable=False) description: str | None = Field(nullable=True) + updated_at: datetime.datetime = Field( + default_factory=lambda: datetime.datetime.now(datetime.UTC), + nullable=False + ) - checklist_steps_order: Optional["ChecklistStepsOrder"] = Relationship(back_populates="checklist_steps_template", - cascade_delete=False) + checklist_steps_orders: list["ChecklistStepsOrder"] = Relationship(back_populates="checklist_steps", + cascade_delete=True) diff --git a/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_order.py b/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_order.py index 15c1187..d7e21e6 100644 --- a/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_order.py +++ b/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_order.py @@ -1,5 +1,6 @@ import uuid from typing import Optional +import datetime from support_sphere.models.base import BasePublicSchemaModel from sqlmodel import Field, Relationship @@ -28,10 +29,14 @@ class ChecklistStepsOrder(BasePublicSchemaModel, table=True): __tablename__ = "checklist_steps_orders" - checklist_types_id: uuid.UUID | None = Field(primary_key=True, foreign_key="public.checklist_types.id") - checklist_steps_templates_id: uuid.UUID | None = Field(foreign_key="public.checklist_steps_templates.id") - priority: int | None = Field(nullable=False) - version: int | None = Field(nullable=False) + id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) + checklist_id: uuid.UUID = Field(foreign_key="public.checklists.id", nullable=False) + checklist_step_id: uuid.UUID = Field(foreign_key="public.checklist_steps.id", nullable=False) + priority: int = Field(nullable=False) + updated_at: datetime.datetime = Field( + default_factory=lambda: datetime.datetime.now(datetime.UTC), + nullable=False + ) - checklist_steps_template: Optional["ChecklistStepsTemplate"] = Relationship(back_populates="checklist_steps_order", cascade_delete=False) - checklist_type: Optional["ChecklistType"] = Relationship(back_populates="checklist_steps_order", cascade_delete=False) + checklists: list["Checklist"] = Relationship(back_populates="checklist_steps_orders", cascade_delete=False) + checklist_steps: list["ChecklistStep"] = Relationship(back_populates="checklist_steps_orders", cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_state.py b/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_state.py new file mode 100644 index 0000000..667b1f7 --- /dev/null +++ b/src/support_sphere_py/src/support_sphere/models/public/checklist_steps_state.py @@ -0,0 +1,32 @@ +import uuid +from typing import Optional +import datetime + +from support_sphere.models.base import BasePublicSchemaModel +from sqlmodel import Field, Relationship + + +class ChecklistStepsState(BasePublicSchemaModel, table=True): + """ + Represents a template for checklist steps in the 'public' schema under the 'checklist_steps_templates' table. + + Attributes + ---------- + id : uuid + The unique identifier for the checklist step template. + title : str + The title of the checklist step template. + description : str, optional + A detailed description of the checklist step template. + checklist_steps_order : ChecklistStepsOrder, optional + A relationship to the `ChecklistStepsOrder` model, which defines the order of these steps within a checklist. + """ + __tablename__ = "checklist_steps_states" + + id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) + checklist_steps_order_id: uuid.UUID = Field(foreign_key="public.checklist_steps_orders.id", nullable=False) + user_profile_id: uuid.UUID = Field(foreign_key="public.user_profiles.id", nullable=False) + is_completed: bool = Field(default=False, nullable=False) + + checklist_steps_order: "ChecklistStepsOrder" = Relationship(cascade_delete=False) + user_profile: Optional["UserProfile"] = Relationship(back_populates="checklist_steps_state", cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/models/public/checklist_type.py b/src/support_sphere_py/src/support_sphere/models/public/checklist_type.py deleted file mode 100644 index 47068b1..0000000 --- a/src/support_sphere_py/src/support_sphere/models/public/checklist_type.py +++ /dev/null @@ -1,49 +0,0 @@ -import uuid -from datetime import datetime -from typing import Optional - -from support_sphere.models.base import BasePublicSchemaModel -from sqlmodel import Field, Relationship - - -class ChecklistType(BasePublicSchemaModel, table=True): - - """ - Represents a checklist type entity in the 'public' schema under the 'checklist_types' table. - This table defines different types of checklists, including the associated recurring type. - - Attributes - ---------- - id : uuid - The unique identifier for the checklist type. - recurring_type_id : uuid - The foreign key referring to the recurring type. - title : str - The title of the checklist type. - description : str, optional - A detailed description of the checklist type. - current_version : int - The current version of this checklist type. - updated_at : datetime - The timestamp for the last update of this checklist type. - recurring_type : RecurringType, optional - A relationship to the `RecurringType` model. - user_checklists : list[UserChecklist] - A list of `UserChecklist` entities associated with this checklist type. - checklist_steps_order : ChecklistStepsOrder, optional - A relationship to define the order of checklist steps for this checklist type. - """ - - __tablename__ = "checklist_types" - - id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) - recurring_type_id: uuid.UUID | None = Field(foreign_key="public.recurring_types.id", nullable=True) - title: str | None = Field(nullable=False) - description: str | None = Field(nullable=True) - current_version: int | None = Field(nullable=False) - updated_at: datetime = Field(nullable=False) - - recurring_type: Optional["RecurringType"] = Relationship(back_populates="checklist_type", cascade_delete=False) - user_checklists: list["UserChecklist"] = Relationship(back_populates="checklist_type", cascade_delete=False) - checklist_steps_order: Optional["ChecklistStepsOrder"] = Relationship(back_populates="checklist_type", - cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/models/public/frequency.py b/src/support_sphere_py/src/support_sphere/models/public/frequency.py new file mode 100644 index 0000000..33b6d2b --- /dev/null +++ b/src/support_sphere_py/src/support_sphere/models/public/frequency.py @@ -0,0 +1,29 @@ +import uuid +from typing import Optional + +from support_sphere.models.base import BasePublicSchemaModel +from sqlmodel import Field, Relationship + + +class Frequency(BasePublicSchemaModel, table=True): + """ + Represents a frequency in the 'public' schema under the 'checklist_frequency' table. + + Attributes + ---------- + id : uuid + The unique identifier for the frequency. + name : str + num_days : int + The number of days between recurrences. + checklists: list[Checklists] + A relationship to the `Checklist` model, representing the checklists that can have this frequency. + """ + + __tablename__ = "frequency" + + id: uuid.UUID | None = Field(default_factory=uuid.uuid4, primary_key=True) + name: str | None = Field(nullable=False) + num_days: int | None = Field(nullable=False) + + checklists: list["Checklist"] = Relationship(back_populates="frequency", cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/models/public/recurring_type.py b/src/support_sphere_py/src/support_sphere/models/public/recurring_type.py deleted file mode 100644 index 56132dc..0000000 --- a/src/support_sphere_py/src/support_sphere/models/public/recurring_type.py +++ /dev/null @@ -1,29 +0,0 @@ -import uuid -from typing import Optional - -from support_sphere.models.base import BasePublicSchemaModel -from sqlmodel import Field, Relationship - - -class RecurringType(BasePublicSchemaModel, table=True): - """ - Represents a recurring type in the 'public' schema under the 'recurring_types' table. - - Attributes - ---------- - id : uuid - The unique identifier for the recurring type. - name : str - num_days : int - The number of days between recurrences. - checklist_type : ChecklistType, optional - A relationship to the `ChecklistType` model, representing the checklist types that can have this recurrence type. - """ - - __tablename__ = "recurring_types" - - id: uuid.UUID | None = Field(default_factory=uuid.uuid4, primary_key=True) - name: str | None = Field(nullable=False) - num_days: int | None = Field(nullable=False) - - checklist_type: Optional["ChecklistType"] = Relationship(back_populates="recurring_type", cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/models/public/user_checklist.py b/src/support_sphere_py/src/support_sphere/models/public/user_checklist.py index b9e96c9..92805d5 100644 --- a/src/support_sphere_py/src/support_sphere/models/public/user_checklist.py +++ b/src/support_sphere_py/src/support_sphere/models/public/user_checklist.py @@ -6,7 +6,7 @@ from sqlmodel import Field, Relationship -class UserChecklist(BasePublicSchemaModel, table=True): +class UserChecklists(BasePublicSchemaModel, table=True): """ Represents a checklist associated with a user in the 'public' schema under the 'user_checklists' table. @@ -25,23 +25,18 @@ class UserChecklist(BasePublicSchemaModel, table=True): The due date for the checklist completion. last_completed_version : int, optional The version number of the checklist that was last completed by the user. - checklist_type : ChecklistType, optional - A relationship to the `ChecklistType` model, representing the type of the checklist. - user_profile : UserProfile, optional - A relationship to the `UserProfile` model, representing the user who owns the checklist. - user_checklist_state : UserChecklistState, optional - A relationship to the `UserChecklistState` model, representing the current state of the checklist. + checklists : list[Checklist] + A relationship to the `Checklist` model, representing the checklist. + user_profile : UserProfile + A relationship to the `UserProfile` model, representing the user who owns the checklists. """ __tablename__ = "user_checklists" id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) - user_id: uuid.UUID | None = Field(foreign_key="public.user_profiles.id", nullable=False) - checklist_type_id: uuid.UUID = Field(foreign_key="public.checklist_types.id", nullable=False) - due_date: datetime = Field(nullable=True) - last_completed_version: int | None = Field(nullable=False) - - checklist_type: Optional["ChecklistType"] = Relationship(back_populates="user_checklists", cascade_delete=False) - user_profile: Optional["UserProfile"] = Relationship(back_populates="user_checklists", cascade_delete=False) - user_checklist_state: Optional["UserChecklistState"] = Relationship(back_populates="user_checklist", - cascade_delete=False) + checklist_id: uuid.UUID = Field(foreign_key="public.checklists.id", nullable=False) + user_profile_id: uuid.UUID = Field(foreign_key="public.user_profiles.id", nullable=False) + completed_at: datetime | None = Field(nullable=True) + + checklists: list["Checklist"] = Relationship(back_populates="user_checklists", cascade_delete=False) + user_profile: "UserProfile" = Relationship(back_populates="user_checklists", cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/models/public/user_checklist_state.py b/src/support_sphere_py/src/support_sphere/models/public/user_checklist_state.py deleted file mode 100644 index 7bf7f87..0000000 --- a/src/support_sphere_py/src/support_sphere/models/public/user_checklist_state.py +++ /dev/null @@ -1,33 +0,0 @@ -import uuid -from datetime import datetime -from typing import Optional - -from support_sphere.models.base import BasePublicSchemaModel -from sqlmodel import Field, Relationship - - -class UserChecklistState(BasePublicSchemaModel, table=True): - - """ - Represents the state of a user checklist in the 'public' schema under the 'user_checklist_states' table. - This table tracks whether a checklist has been completed and when it was completed. - - Attributes - ---------- - id : uuid - The unique identifier for the checklist state, which also serves as a foreign key from the `user_checklists` table. - completed : bool - Indicates whether the checklist has been completed (True or False). - completed_at : datetime - The date and time when the checklist was completed. - user_checklist : UserChecklist, optional - A relationship to the `UserChecklist` model, representing the checklist whose state is being tracked. - """ - - __tablename__ = "user_checklist_states" - - id: uuid.UUID | None = Field(primary_key=True, foreign_key="public.user_checklists.id") - completed: bool | None = Field(nullable=False) - completed_at: datetime = Field(nullable=False) - - user_checklist: Optional["UserChecklist"] = Relationship(back_populates="user_checklist_state", cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/models/public/user_profile.py b/src/support_sphere_py/src/support_sphere/models/public/user_profile.py index 432c323..e76e9f1 100644 --- a/src/support_sphere_py/src/support_sphere/models/public/user_profile.py +++ b/src/support_sphere_py/src/support_sphere/models/public/user_profile.py @@ -48,4 +48,6 @@ class UserProfile(BasePublicSchemaModel, table=True): user_role: Optional["UserRole"] = Relationship(back_populates="user_profile", cascade_delete=False) operational_events: list["OperationalEvent"] = Relationship(back_populates="user_profile", cascade_delete=False) user_resources: list["UserResource"] = Relationship(back_populates="user_profile", cascade_delete=False) - user_checklists: list["UserChecklist"] = Relationship(back_populates="user_profile", cascade_delete=False) + user_checklists: list["UserChecklists"] = Relationship(back_populates="user_profile", cascade_delete=False) + checklist_steps_state: list["ChecklistStepsState"] = Relationship(back_populates="user_profile", + cascade_delete=False) diff --git a/src/support_sphere_py/src/support_sphere/scripts/execute_sql_statement.py b/src/support_sphere_py/src/support_sphere/scripts/execute_sql_statement.py index adfab5c..28a06f2 100644 --- a/src/support_sphere_py/src/support_sphere/scripts/execute_sql_statement.py +++ b/src/support_sphere_py/src/support_sphere/scripts/execute_sql_statement.py @@ -107,6 +107,53 @@ COMMIT; """ +# SQL For Checklist triggers setup +checklist_triggers_sql = """ +BEGIN; + CREATE OR REPLACE FUNCTION insert_user_checklists_for_all_users() + RETURNS TRIGGER AS $$ + DECLARE + user_record RECORD; + BEGIN + -- Loop through each user in the user_profiles table + FOR user_record IN SELECT id FROM public.user_profiles LOOP + -- Insert a new row into user_checklists for each user + INSERT INTO public.user_checklists (id, checklist_id, user_profile_id) + VALUES (gen_random_uuid(), NEW.id, user_record.id); + END LOOP; + + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + CREATE OR REPLACE FUNCTION insert_checklist_steps_state_for_all_users() + RETURNS TRIGGER AS $$ + DECLARE + user_record RECORD; + BEGIN + -- Loop through each user in the user_profiles table + FOR user_record IN SELECT id FROM public.user_profiles LOOP + -- Insert a new row into checklist_steps_states for each user + INSERT INTO public.checklist_steps_states (id, checklist_steps_order_id, user_profile_id, is_completed) + VALUES (gen_random_uuid(), NEW.id, user_record.id, FALSE); + END LOOP; + + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + CREATE OR REPLACE TRIGGER trigger_insert_user_checklists_for_all_users + AFTER INSERT ON public.checklists + FOR EACH ROW + EXECUTE FUNCTION insert_user_checklists_for_all_users(); + + CREATE OR REPLACE TRIGGER trigger_insert_checklist_steps_state_for_all_users + AFTER INSERT ON public.checklist_steps_orders + FOR EACH ROW + EXECUTE FUNCTION insert_checklist_steps_state_for_all_users(); +COMMIT; +""" + # Execute the SQL commands def run_custom_sql_statement( @@ -137,6 +184,9 @@ def run_all(): logger.info("Activating realtime tables...") run_custom_sql_statement(activate_realtime_tables_sql) + logger.info("Setting up checklist triggers...") + run_custom_sql_statement(checklist_triggers_sql) + if __name__ == '__main__': execute_sql_app() diff --git a/src/support_sphere_py/src/support_sphere/scripts/resources/data/checklists.json b/src/support_sphere_py/src/support_sphere/scripts/resources/data/checklists.json new file mode 100644 index 0000000..c35a34e --- /dev/null +++ b/src/support_sphere_py/src/support_sphere/scripts/resources/data/checklists.json @@ -0,0 +1,57 @@ +{ + "checklists": [ + { + "title": "Personal Emergency Preparedness", + "purpose": "This checklist helps individuals prepare for a variety of emergency situations, from natural disasters to unexpected crises, ensuring they have the necessary resources and plans in place to stay safe.", + "steps": [ + { + "step": "Assemble an Emergency Kit", + "description": "Collect essential supplies like food, water, medications, flashlights, and a first aid kit. Aim for a 72-hour supply to sustain you during the initial phase of an emergency." + }, + { + "step": "Create a Family Communication Plan", + "description": "Establish a way to communicate with family members during an emergency, including identifying a central contact person outside the area for everyone to check in with." + }, + { + "step": "Know Your Local Risks", + "description": "Identify specific risks in your area, such as hurricanes, earthquakes, or flooding, to better tailor your emergency preparations." + }, + { + "step": "Learn Basic First Aid and CPR", + "description": "Familiarize yourself with first aid and CPR techniques, which can be crucial during emergencies if immediate medical help isn’t available." + }, + { + "step": "Secure Important Documents", + "description": "Gather and protect important documents, like identification, insurance policies, and medical records, storing them in a waterproof, fireproof container." + } + ] + }, + { + "title": "Workplace Emergency Preparedness", + "purpose": "This checklist is designed for businesses and organizations to help staff prepare for emergencies, minimizing risk and ensuring a quick, organized response to keep everyone safe.", + "steps": [ + { + "step": "Develop an Emergency Response Plan", + "description": "Create a comprehensive response plan detailing evacuation routes, shelter-in-place procedures, and communication protocols." + }, + { + "step": "Conduct Regular Drills", + "description": "Schedule drills for various scenarios like fire, earthquake, or active shooter situations to ensure employees know what to do in an emergency." + }, + { + "step": "Establish a Communication Chain", + "description": "Identify key personnel responsible for communicating updates during an emergency and establish clear methods for reaching all employees." + }, + { + "step": "Stock Workplace Emergency Supplies", + "description": "Ensure the workplace has accessible emergency supplies, including first aid kits, fire extinguishers, and personal protective equipment." + }, + { + "step": "Train Employees on Emergency Procedures", + "description": "Provide regular training on the emergency response plan, including how to safely evacuate, assist colleagues, and locate emergency supplies." + } + ] + } + ] + } + \ No newline at end of file