feat(chalice): session's notes single tag

feat(chalice): session's notes search public only
This commit is contained in:
Taha Yassine Kraiem 2022-10-04 13:25:36 +02:00
parent 3a41ca102b
commit ece726eea0
7 changed files with 30 additions and 22 deletions

View file

@ -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:

View file

@ -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)

View file

@ -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\

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,