change(api): ee related changes for notes
This commit is contained in:
parent
b8a70367ed
commit
c0a5415eb9
1 changed files with 58 additions and 22 deletions
|
|
@ -31,6 +31,7 @@ def get_note(tenant_id, project_id, user_id, note_id, share=None):
|
||||||
row = helper.dict_to_camel_case(row)
|
row = helper.dict_to_camel_case(row)
|
||||||
if row:
|
if row:
|
||||||
row["createdAt"] = TimeUTC.datetime_to_timestamp(row["createdAt"])
|
row["createdAt"] = TimeUTC.datetime_to_timestamp(row["createdAt"])
|
||||||
|
row["updatedAt"] = TimeUTC.datetime_to_timestamp(row["updatedAt"])
|
||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -57,42 +58,74 @@ 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):
|
def get_all_notes_by_project_id(tenant_id, project_id, user_id, data: schemas.SearchNoteSchema):
|
||||||
with pg_client.PostgresClient() as cur:
|
with pg_client.PostgresClient() as cur:
|
||||||
conditions = ["sessions_notes.project_id = %(project_id)s", "sessions_notes.deleted_at IS NULL"]
|
# base conditions
|
||||||
extra_params = {}
|
conditions = [
|
||||||
if data.tags and len(data.tags) > 0:
|
"sessions_notes.project_id = %(project_id)s",
|
||||||
k = "tag_value"
|
"sessions_notes.deleted_at IS NULL"
|
||||||
|
]
|
||||||
|
params = {"project_id": project_id, "user_id": user_id, "tenant_id": tenant_id}
|
||||||
|
|
||||||
|
# tag conditions
|
||||||
|
if data.tags:
|
||||||
|
tag_key = "tag_value"
|
||||||
conditions.append(
|
conditions.append(
|
||||||
sh.multi_conditions(f"%({k})s = sessions_notes.tag", data.tags, value_key=k))
|
sh.multi_conditions(f"%({tag_key})s = sessions_notes.tag", data.tags, value_key=tag_key)
|
||||||
extra_params = sh.multi_values(data.tags, value_key=k)
|
)
|
||||||
|
params.update(sh.multi_values(data.tags, value_key=tag_key))
|
||||||
|
|
||||||
|
# filter by ownership or shared status
|
||||||
if data.shared_only:
|
if data.shared_only:
|
||||||
conditions.append("sessions_notes.is_public AND users.tenant_id = %(tenant_id)s")
|
conditions.append("sessions_notes.is_public")
|
||||||
elif data.mine_only:
|
elif data.mine_only:
|
||||||
conditions.append("sessions_notes.user_id = %(user_id)s")
|
conditions.append("sessions_notes.user_id = %(user_id)s")
|
||||||
else:
|
else:
|
||||||
conditions.append(
|
conditions.append("(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public)")
|
||||||
"(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public AND users.tenant_id = %(tenant_id)s)")
|
|
||||||
query = cur.mogrify(f"""SELECT COUNT(1) OVER () AS full_count, sessions_notes.*, users.name AS user_name
|
# search condition
|
||||||
FROM sessions_notes INNER JOIN users USING (user_id)
|
if data.search:
|
||||||
WHERE {" AND ".join(conditions)}
|
conditions.append("sessions_notes.message ILIKE %(search)s")
|
||||||
ORDER BY created_at {data.order}
|
params["search"] = f"%{data.search}%"
|
||||||
LIMIT {data.limit} OFFSET {data.limit * (data.page - 1)};""",
|
|
||||||
{"project_id": project_id, "user_id": user_id, "tenant_id": tenant_id, **extra_params})
|
query = f"""
|
||||||
|
SELECT
|
||||||
|
COUNT(1) OVER () AS full_count,
|
||||||
|
sessions_notes.*,
|
||||||
|
users.name AS user_name
|
||||||
|
FROM
|
||||||
|
sessions_notes
|
||||||
|
INNER JOIN
|
||||||
|
users USING (user_id)
|
||||||
|
WHERE
|
||||||
|
{" AND ".join(conditions)}
|
||||||
|
ORDER BY
|
||||||
|
created_at {data.order}
|
||||||
|
LIMIT
|
||||||
|
%(limit)s OFFSET %(offset)s;
|
||||||
|
"""
|
||||||
|
params.update({
|
||||||
|
"limit": data.limit,
|
||||||
|
"offset": data.limit * (data.page - 1)
|
||||||
|
})
|
||||||
|
|
||||||
|
query = cur.mogrify(query, params)
|
||||||
logger.debug(query)
|
logger.debug(query)
|
||||||
cur.execute(query=query)
|
cur.execute(query)
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
|
|
||||||
result = {"count": 0, "notes": helper.list_to_camel_case(rows)}
|
result = {"count": 0, "notes": helper.list_to_camel_case(rows)}
|
||||||
if len(rows) > 0:
|
if rows:
|
||||||
result["count"] = rows[0]["fullCount"]
|
result["count"] = rows[0]["fullCount"]
|
||||||
for row in rows:
|
for row in rows:
|
||||||
row["createdAt"] = TimeUTC.datetime_to_timestamp(row["createdAt"])
|
row["createdAt"] = TimeUTC.datetime_to_timestamp(row["createdAt"])
|
||||||
row.pop("fullCount")
|
row.pop("fullCount")
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def create(tenant_id, user_id, project_id, session_id, data: schemas.SessionNoteSchema):
|
def create(tenant_id, user_id, project_id, session_id, data: schemas.SessionNoteSchema):
|
||||||
with pg_client.PostgresClient() as cur:
|
with pg_client.PostgresClient() as cur:
|
||||||
query = cur.mogrify(f"""INSERT INTO public.sessions_notes (message, user_id, tag, session_id, project_id, timestamp, is_public)
|
query = cur.mogrify(f"""INSERT INTO public.sessions_notes (message, user_id, tag, session_id, project_id, timestamp, is_public, thumbnail, start_at, end_at)
|
||||||
VALUES (%(message)s, %(user_id)s, %(tag)s, %(session_id)s, %(project_id)s, %(timestamp)s, %(is_public)s)
|
VALUES (%(message)s, %(user_id)s, %(tag)s, %(session_id)s, %(project_id)s, %(timestamp)s, %(is_public)s, %(thumbnail)s, %(start_at)s, %(end_at)s)
|
||||||
RETURNING *,(SELECT name FROM users WHERE users.user_id=%(user_id)s AND users.tenant_id=%(tenant_id)s) AS user_name;""",
|
RETURNING *,(SELECT name FROM users WHERE users.user_id=%(user_id)s AND users.tenant_id=%(tenant_id)s) AS user_name;""",
|
||||||
{"user_id": user_id, "project_id": project_id, "session_id": session_id,
|
{"user_id": user_id, "project_id": project_id, "session_id": session_id,
|
||||||
**data.model_dump(),
|
**data.model_dump(),
|
||||||
|
|
@ -114,6 +147,9 @@ def edit(tenant_id, user_id, project_id, note_id, data: schemas.SessionUpdateNot
|
||||||
sub_query.append("is_public = %(is_public)s")
|
sub_query.append("is_public = %(is_public)s")
|
||||||
if data.timestamp is not None:
|
if data.timestamp is not None:
|
||||||
sub_query.append("timestamp = %(timestamp)s")
|
sub_query.append("timestamp = %(timestamp)s")
|
||||||
|
|
||||||
|
sub_query.append("updated_at = timezone('utc'::text, now())")
|
||||||
|
|
||||||
with pg_client.PostgresClient() as cur:
|
with pg_client.PostgresClient() as cur:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
cur.mogrify(f"""UPDATE public.sessions_notes
|
cur.mogrify(f"""UPDATE public.sessions_notes
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue