From ece726eea08d590562ea23f0f1a51f246d70e504 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Tue, 4 Oct 2022 13:25:36 +0200 Subject: [PATCH] feat(chalice): session's notes single tag feat(chalice): session's notes search public only --- api/chalicelib/core/sessions_notes.py | 18 +++++++++------ api/schemas.py | 4 ++-- ee/api/chalicelib/core/sessions_notes.py | 22 +++++++++++-------- .../db/init_dbs/postgresql/1.8.2/1.8.2.sql | 2 +- .../db/init_dbs/postgresql/init_schema.sql | 2 +- .../db/init_dbs/postgresql/1.8.2/1.8.2.sql | 2 +- .../db/init_dbs/postgresql/init_schema.sql | 2 +- 7 files changed, 30 insertions(+), 22 deletions(-) diff --git a/api/chalicelib/core/sessions_notes.py b/api/chalicelib/core/sessions_notes.py index f0e7bfb48..a586dd75c 100644 --- a/api/chalicelib/core/sessions_notes.py +++ b/api/chalicelib/core/sessions_notes.py @@ -27,13 +27,17 @@ def get_session_notes(tenant_id, project_id, session_id, user_id): def get_all_notes_by_project_id(tenant_id, project_id, user_id, data: schemas.SearchNoteSchema): with pg_client.PostgresClient() as cur: - conditions = ["sessions_notes.project_id = %(project_id)s", "sessions_notes.deleted_at IS NULL", - "(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public)"] + conditions = ["sessions_notes.project_id = %(project_id)s", "sessions_notes.deleted_at IS NULL"] extra_params = {} if data.tags and len(data.tags) > 0: k = "tag_value" - conditions.append(sessions._multiple_conditions(f"%({k})s = ANY (sessions_notes.tags)", data.tags, value_key=k)) + conditions.append( + sessions._multiple_conditions(f"%({k})s = sessions_notes.tag", data.tags, value_key=k)) extra_params = sessions._multiple_values(data.tags, value_key=k) + if data.shared_only: + conditions.append("sessions_notes.is_public") + else: + conditions.append("(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public)") query = cur.mogrify(f"""SELECT sessions_notes.* FROM sessions_notes WHERE {" AND ".join(conditions)} @@ -51,8 +55,8 @@ def get_all_notes_by_project_id(tenant_id, project_id, user_id, data: schemas.Se def create(tenant_id, user_id, project_id, session_id, data: schemas.SessionNoteSchema): with pg_client.PostgresClient() as cur: - query = cur.mogrify(f"""INSERT INTO public.sessions_notes (message, user_id, tags, session_id, project_id, timestamp, is_public) - VALUES (%(message)s, %(user_id)s, %(tags)s, %(session_id)s, %(project_id)s, %(timestamp)s, %(is_public)s) + query = cur.mogrify(f"""INSERT INTO public.sessions_notes (message, user_id, tag, session_id, project_id, timestamp, is_public) + VALUES (%(message)s, %(user_id)s, %(tag)s, %(session_id)s, %(project_id)s, %(timestamp)s, %(is_public)s) RETURNING *;""", {"user_id": user_id, "project_id": project_id, "session_id": session_id, **data.dict()}) cur.execute(query) @@ -66,8 +70,8 @@ def edit(tenant_id, user_id, project_id, note_id, data: schemas.SessionUpdateNot sub_query = [] if data.message is not None: sub_query.append("message = %(message)s") - if data.tags is not None: - sub_query.append("tags = %(tags)s") + if data.tag is not None and len(data.tag) > 0: + sub_query.append("tag = %(tag)s") if data.is_public is not None: sub_query.append("is_public = %(is_public)s") if data.timestamp is not None: diff --git a/api/schemas.py b/api/schemas.py index db5c0cf82..0b54906a1 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -1090,6 +1090,7 @@ class SearchNoteSchema(_PaginatedSchema): sort: str = Field(default="createdAt") order: SortOrderType = Field(default=SortOrderType.desc) tags: Optional[List[str]] = Field(default=[]) + shared_only: bool = Field(default=False) class Config: alias_generator = attribute_to_camel_case @@ -1097,7 +1098,7 @@ class SearchNoteSchema(_PaginatedSchema): class SessionNoteSchema(BaseModel): message: str = Field(..., min_length=2) - tags: List[str] = Field(default=[]) + tag: Optional[str] = Field(default=None) timestamp: int = Field(default=-1) is_public: bool = Field(default=False) @@ -1107,7 +1108,6 @@ class SessionNoteSchema(BaseModel): class SessionUpdateNoteSchema(SessionNoteSchema): message: Optional[str] = Field(default=None, min_length=2) - tags: Optional[List[str]] = Field(default=None) timestamp: Optional[int] = Field(default=None, ge=-1) is_public: Optional[bool] = Field(default=None) diff --git a/ee/api/chalicelib/core/sessions_notes.py b/ee/api/chalicelib/core/sessions_notes.py index df0e8bfa6..dd47fd227 100644 --- a/ee/api/chalicelib/core/sessions_notes.py +++ b/ee/api/chalicelib/core/sessions_notes.py @@ -28,13 +28,18 @@ def get_session_notes(tenant_id, project_id, session_id, user_id): def get_all_notes_by_project_id(tenant_id, project_id, user_id, data: schemas.SearchNoteSchema): with pg_client.PostgresClient() as cur: - conditions = ["sessions_notes.project_id = %(project_id)s", "sessions_notes.deleted_at IS NULL", - "(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public AND users.tenant_id = %(tenant_id)s)"] + conditions = ["sessions_notes.project_id = %(project_id)s", "sessions_notes.deleted_at IS NULL"] extra_params = {} if data.tags and len(data.tags) > 0: k = "tag_value" - conditions.append(sessions._multiple_conditions(f"%({k})s = ANY (sessions_notes.tags)", data.tags, value_key=k)) + conditions.append( + sessions._multiple_conditions(f"%({k})s = sessions_notes.tag", data.tags, value_key=k)) extra_params = sessions._multiple_values(data.tags, value_key=k) + if data.shared_only: + conditions.append("sessions_notes.is_public AND users.tenant_id = %(tenant_id)s") + else: + conditions.append( + "(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public AND users.tenant_id = %(tenant_id)s)") query = cur.mogrify(f"""SELECT sessions_notes.* FROM sessions_notes INNER JOIN users USING (user_id) @@ -53,8 +58,8 @@ def get_all_notes_by_project_id(tenant_id, project_id, user_id, data: schemas.Se def create(tenant_id, user_id, project_id, session_id, data: schemas.SessionNoteSchema): with pg_client.PostgresClient() as cur: - query = cur.mogrify(f"""INSERT INTO public.sessions_notes (message, user_id, tags, session_id, project_id, timestamp, is_public) - VALUES (%(message)s, %(user_id)s, %(tags)s, %(session_id)s, %(project_id)s, %(timestamp)s, %(is_public)s) + query = cur.mogrify(f"""INSERT INTO public.sessions_notes (message, user_id, tag, session_id, project_id, timestamp, is_public) + VALUES (%(message)s, %(user_id)s, %(tag)s, %(session_id)s, %(project_id)s, %(timestamp)s, %(is_public)s) RETURNING *;""", {"user_id": user_id, "project_id": project_id, "session_id": session_id, **data.dict()}) cur.execute(query) @@ -68,8 +73,8 @@ def edit(tenant_id, user_id, project_id, note_id, data: schemas.SessionUpdateNot sub_query = [] if data.message is not None: sub_query.append("message = %(message)s") - if data.tags is not None: - sub_query.append("tags = %(tags)s") + if data.tag is not None and len(data.tag) > 0: + sub_query.append("tag = %(tag)s") if data.is_public is not None: sub_query.append("is_public = %(is_public)s") if data.timestamp is not None: @@ -99,8 +104,7 @@ def delete(tenant_id, user_id, project_id, note_id): cur.execute( cur.mogrify("""\ UPDATE public.sessions_notes - SET - deleted_at = timezone('utc'::text, now()) + SET deleted_at = timezone('utc'::text, now()) WHERE note_id = %(note_id)s AND project_id = %(project_id)s\ diff --git a/ee/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql b/ee/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql index 68376168d..1fd27342f 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql @@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS sessions_notes created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'), user_id integer NULL REFERENCES users (user_id) ON DELETE SET NULL, deleted_at timestamp without time zone NULL DEFAULT NULL, - tags text[] NOT NULL DEFAULT '{}', + tag text NULL, session_id bigint NOT NULL REFERENCES sessions (session_id) ON DELETE CASCADE, project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, timestamp integer NOT NULL DEFAULT -1, diff --git a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql index 02c6e135a..13f2db5cf 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -875,7 +875,7 @@ $$ created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'), user_id integer NULL REFERENCES users (user_id) ON DELETE SET NULL, deleted_at timestamp without time zone NULL DEFAULT NULL, - tags text[] NOT NULL DEFAULT '{}', + tag text NULL, session_id bigint NOT NULL REFERENCES sessions (session_id) ON DELETE CASCADE, project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, timestamp integer NOT NULL DEFAULT -1, diff --git a/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql b/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql index c27fa3e7a..5abda1259 100644 --- a/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql +++ b/scripts/helm/db/init_dbs/postgresql/1.8.2/1.8.2.sql @@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS sessions_notes created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'), user_id integer NULL REFERENCES users (user_id) ON DELETE SET NULL, deleted_at timestamp without time zone NULL DEFAULT NULL, - tags text[] NOT NULL DEFAULT '{}', + tag text NULL, session_id bigint NOT NULL REFERENCES sessions (session_id) ON DELETE CASCADE, project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, timestamp integer NOT NULL DEFAULT -1, diff --git a/scripts/helm/db/init_dbs/postgresql/init_schema.sql b/scripts/helm/db/init_dbs/postgresql/init_schema.sql index 5cf4ed75e..8c3c07d24 100644 --- a/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -1016,7 +1016,7 @@ $$ created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'), user_id integer NULL REFERENCES users (user_id) ON DELETE SET NULL, deleted_at timestamp without time zone NULL DEFAULT NULL, - tags text[] NOT NULL DEFAULT '{}', + tag text NULL, session_id bigint NOT NULL REFERENCES sessions (session_id) ON DELETE CASCADE, project_id integer NOT NULL REFERENCES projects (project_id) ON DELETE CASCADE, timestamp integer NOT NULL DEFAULT -1,