feat(chalice): changed notes

This commit is contained in:
Taha Yassine Kraiem 2023-01-30 12:31:13 +01:00
parent d15961ace8
commit 90a424b7a6
2 changed files with 23 additions and 23 deletions

View file

@ -12,7 +12,7 @@ from chalicelib.utils.TimeUTC import TimeUTC
def get_note(tenant_id, project_id, user_id, note_id, share=None): def get_note(tenant_id, project_id, user_id, note_id, share=None):
with pg_client.PostgresClient() as cur: with pg_client.PostgresClient() as cur:
query = cur.mogrify(f"""SELECT sessions_notes.*, users.name AS creator_name query = cur.mogrify(f"""SELECT sessions_notes.*, users.name AS user_name
{",(SELECT name FROM users WHERE user_id=%(share)s AND deleted_at ISNULL) AS share_name" if share else ""} {",(SELECT name FROM users WHERE user_id=%(share)s AND deleted_at ISNULL) AS share_name" if share else ""}
FROM sessions_notes INNER JOIN users USING (user_id) FROM sessions_notes INNER JOIN users USING (user_id)
WHERE sessions_notes.project_id = %(project_id)s WHERE sessions_notes.project_id = %(project_id)s
@ -32,8 +32,8 @@ def get_note(tenant_id, project_id, user_id, note_id, share=None):
def get_session_notes(tenant_id, project_id, session_id, user_id): def get_session_notes(tenant_id, project_id, session_id, user_id):
with pg_client.PostgresClient() as cur: with pg_client.PostgresClient() as cur:
query = cur.mogrify(f"""SELECT sessions_notes.* query = cur.mogrify(f"""SELECT sessions_notes.*, users.name AS user_name
FROM sessions_notes FROM sessions_notes INNER JOIN users USING (user_id)
WHERE sessions_notes.project_id = %(project_id)s WHERE sessions_notes.project_id = %(project_id)s
AND sessions_notes.deleted_at IS NULL AND sessions_notes.deleted_at IS NULL
AND sessions_notes.session_id = %(session_id)s AND sessions_notes.session_id = %(session_id)s
@ -66,8 +66,8 @@ def get_all_notes_by_project_id(tenant_id, project_id, user_id, data: schemas.Se
conditions.append("sessions_notes.user_id = %(user_id)s") conditions.append("sessions_notes.user_id = %(user_id)s")
else: else:
conditions.append("(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public)") conditions.append("(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public)")
query = cur.mogrify(f"""SELECT sessions_notes.* query = cur.mogrify(f"""SELECT sessions_notes.*, users.name AS user_name
FROM sessions_notes FROM sessions_notes INNER JOIN users USING (user_id)
WHERE {" AND ".join(conditions)} WHERE {" AND ".join(conditions)}
ORDER BY created_at {data.order.value} ORDER BY created_at {data.order.value}
LIMIT {data.limit} OFFSET {data.limit * (data.page - 1)};""", LIMIT {data.limit} OFFSET {data.limit * (data.page - 1)};""",
@ -85,7 +85,7 @@ def create(tenant_id, user_id, project_id, session_id, data: schemas.SessionNote
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)
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)
RETURNING *;""", RETURNING *,(SELECT name FROM users WHERE users.user_id=%(user_id)s) AS user_name;""",
{"user_id": user_id, "project_id": project_id, "session_id": session_id, **data.dict()}) {"user_id": user_id, "project_id": project_id, "session_id": session_id, **data.dict()})
cur.execute(query) cur.execute(query)
result = helper.dict_to_camel_case(cur.fetchone()) result = helper.dict_to_camel_case(cur.fetchone())
@ -114,7 +114,7 @@ def edit(tenant_id, user_id, project_id, note_id, data: schemas.SessionUpdateNot
AND user_id = %(user_id)s AND user_id = %(user_id)s
AND note_id = %(note_id)s AND note_id = %(note_id)s
AND deleted_at ISNULL AND deleted_at ISNULL
RETURNING *;""", RETURNING *,(SELECT name FROM users WHERE users.user_id=%(user_id)s) AS user_name;""",
{"project_id": project_id, "user_id": user_id, "note_id": note_id, **data.dict()}) {"project_id": project_id, "user_id": user_id, "note_id": note_id, **data.dict()})
) )
row = helper.dict_to_camel_case(cur.fetchone()) row = helper.dict_to_camel_case(cur.fetchone())
@ -156,7 +156,7 @@ def share_to_slack(tenant_id, user_id, project_id, note_id, webhook_id):
blocks.append({"type": "context", blocks.append({"type": "context",
"elements": [{"type": "plain_text", "elements": [{"type": "plain_text",
"text": f"Tag: *{note['tag']}*"}]}) "text": f"Tag: *{note['tag']}*"}]})
bottom = f"Created by {note['creatorName'].capitalize()}" bottom = f"Created by {note['userName'].capitalize()}"
if user_id != note["userId"]: if user_id != note["userId"]:
bottom += f"\nSent by {note['shareName']}: " bottom += f"\nSent by {note['shareName']}: "
blocks.append({"type": "context", blocks.append({"type": "context",
@ -195,7 +195,7 @@ def share_to_msteams(tenant_id, user_id, project_id, note_id, webhook_id):
"spacing": "Small", "spacing": "Small",
"text": f"Tag: *{note['tag']}*", "text": f"Tag: *{note['tag']}*",
"size": "Small"}) "size": "Small"})
bottom = f"Created by {note['creatorName'].capitalize()}" bottom = f"Created by {note['userName'].capitalize()}"
if user_id != note["userId"]: if user_id != note["userId"]:
bottom += f"\nSent by {note['shareName']}: " bottom += f"\nSent by {note['shareName']}: "
blocks.append({"type": "TextBlock", blocks.append({"type": "TextBlock",

View file

@ -12,7 +12,7 @@ from chalicelib.utils.TimeUTC import TimeUTC
def get_note(tenant_id, project_id, user_id, note_id, share=None): def get_note(tenant_id, project_id, user_id, note_id, share=None):
with pg_client.PostgresClient() as cur: with pg_client.PostgresClient() as cur:
query = cur.mogrify(f"""SELECT sessions_notes.*, users.name AS creator_name query = cur.mogrify(f"""SELECT sessions_notes.*, users.name AS user_name
{",(SELECT name FROM users WHERE tenant_id=%(tenant_id)s AND user_id=%(share)s AND deleted_at ISNULL) AS share_name" if share else ""} {",(SELECT name FROM users WHERE tenant_id=%(tenant_id)s AND user_id=%(share)s AND deleted_at ISNULL) AS share_name" if share else ""}
FROM sessions_notes INNER JOIN users USING (user_id) FROM sessions_notes INNER JOIN users USING (user_id)
WHERE sessions_notes.project_id = %(project_id)s WHERE sessions_notes.project_id = %(project_id)s
@ -33,9 +33,8 @@ def get_note(tenant_id, project_id, user_id, note_id, share=None):
def get_session_notes(tenant_id, project_id, session_id, user_id): def get_session_notes(tenant_id, project_id, session_id, user_id):
with pg_client.PostgresClient() as cur: with pg_client.PostgresClient() as cur:
query = cur.mogrify(f"""SELECT sessions_notes.* query = cur.mogrify(f"""SELECT sessions_notes.*, users.name AS user_name
FROM sessions_notes FROM sessions_notes INNER JOIN users USING (user_id)
INNER JOIN users USING (user_id)
WHERE sessions_notes.project_id = %(project_id)s WHERE sessions_notes.project_id = %(project_id)s
AND sessions_notes.deleted_at IS NULL AND sessions_notes.deleted_at IS NULL
AND sessions_notes.session_id = %(session_id)s AND sessions_notes.session_id = %(session_id)s
@ -69,11 +68,10 @@ def get_all_notes_by_project_id(tenant_id, project_id, user_id, data: schemas.Se
else: else:
conditions.append( conditions.append(
"(sessions_notes.user_id = %(user_id)s OR sessions_notes.is_public AND users.tenant_id = %(tenant_id)s)") "(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.* query = cur.mogrify(f"""SELECT sessions_notes.*, users.name AS user_name
FROM sessions_notes FROM sessions_notes INNER JOIN users USING (user_id)
INNER JOIN users USING (user_id)
WHERE {" AND ".join(conditions)} WHERE {" AND ".join(conditions)}
ORDER BY created_at {data.order} ORDER BY created_at {data.order.value}
LIMIT {data.limit} OFFSET {data.limit * (data.page - 1)};""", LIMIT {data.limit} OFFSET {data.limit * (data.page - 1)};""",
{"project_id": project_id, "user_id": user_id, "tenant_id": tenant_id, **extra_params}) {"project_id": project_id, "user_id": user_id, "tenant_id": tenant_id, **extra_params})
@ -89,8 +87,9 @@ def create(tenant_id, user_id, project_id, session_id, data: schemas.SessionNote
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)
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)
RETURNING *;""", 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, **data.dict()}) {"user_id": user_id, "project_id": project_id, "session_id": session_id, **data.dict(),
"tenant_id": tenant_id})
cur.execute(query) cur.execute(query)
result = helper.dict_to_camel_case(cur.fetchone()) result = helper.dict_to_camel_case(cur.fetchone())
if result: if result:
@ -118,8 +117,9 @@ def edit(tenant_id, user_id, project_id, note_id, data: schemas.SessionUpdateNot
AND user_id = %(user_id)s AND user_id = %(user_id)s
AND note_id = %(note_id)s AND note_id = %(note_id)s
AND deleted_at ISNULL AND deleted_at ISNULL
RETURNING *;""", RETURNING *,(SELECT name FROM users WHERE users.user_id=%(user_id)s AND users.tenant_id=%(tenant_id)s) AS user_name;""",
{"project_id": project_id, "user_id": user_id, "note_id": note_id, **data.dict()}) {"project_id": project_id, "user_id": user_id, "note_id": note_id, **data.dict(),
"tenant_id": tenant_id})
) )
row = helper.dict_to_camel_case(cur.fetchone()) row = helper.dict_to_camel_case(cur.fetchone())
if row: if row:
@ -160,7 +160,7 @@ def share_to_slack(tenant_id, user_id, project_id, note_id, webhook_id):
blocks.append({"type": "context", blocks.append({"type": "context",
"elements": [{"type": "plain_text", "elements": [{"type": "plain_text",
"text": f"Tag: *{note['tag']}*"}]}) "text": f"Tag: *{note['tag']}*"}]})
bottom = f"Created by {note['creatorName'].capitalize()}" bottom = f"Created by {note['userName'].capitalize()}"
if user_id != note["userId"]: if user_id != note["userId"]:
bottom += f"\nSent by {note['shareName']}: " bottom += f"\nSent by {note['shareName']}: "
blocks.append({"type": "context", blocks.append({"type": "context",
@ -199,7 +199,7 @@ def share_to_msteams(tenant_id, user_id, project_id, note_id, webhook_id):
"spacing": "Small", "spacing": "Small",
"text": f"Tag: *{note['tag']}*", "text": f"Tag: *{note['tag']}*",
"size": "Small"}) "size": "Small"})
bottom = f"Created by {note['creatorName'].capitalize()}" bottom = f"Created by {note['userName'].capitalize()}"
if user_id != note["userId"]: if user_id != note["userId"]:
bottom += f"\nSent by {note['shareName']}: " bottom += f"\nSent by {note['shareName']}: "
blocks.append({"type": "TextBlock", blocks.append({"type": "TextBlock",