diff --git a/api/chalicelib/core/integrations_global.py b/api/chalicelib/core/integrations_global.py index f69fa99c0..66cd38a74 100644 --- a/api/chalicelib/core/integrations_global.py +++ b/api/chalicelib/core/integrations_global.py @@ -51,10 +51,10 @@ def get_global_integrations_status(tenant_id, user_id, project_id): AND provider='elasticsearch')) AS {schemas.IntegrationType.elasticsearch.value}, EXISTS((SELECT 1 FROM public.webhooks - WHERE type='slack')) AS {schemas.IntegrationType.slack.value}, + WHERE type='slack' AND deleted_at ISNULL)) AS {schemas.IntegrationType.slack.value}, EXISTS((SELECT 1 FROM public.webhooks - WHERE type='msteams')) AS {schemas.IntegrationType.ms_teams.value};""", + WHERE type='msteams' AND deleted_at ISNULL)) AS {schemas.IntegrationType.ms_teams.value};""", {"user_id": user_id, "tenant_id": tenant_id, "project_id": project_id}) ) current_integrations = cur.fetchone() diff --git a/api/chalicelib/core/tenants.py b/api/chalicelib/core/tenants.py index cabcdae7b..5479178d8 100644 --- a/api/chalicelib/core/tenants.py +++ b/api/chalicelib/core/tenants.py @@ -6,59 +6,50 @@ from chalicelib.core import users, license def get_by_tenant_id(tenant_id): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify( - f"""SELECT - tenants.tenant_id, - tenants.name, - tenants.api_key, - tenants.created_at, - '{license.EDITION}' AS edition, - openreplay_version() AS version_number, - tenants.opt_out - FROM public.tenants - LIMIT 1;""", - {"tenantId": tenant_id})) + query = cur.mogrify(f"""SELECT tenants.tenant_id, + tenants.name, + tenants.api_key, + tenants.created_at, + '{license.EDITION}' AS edition, + openreplay_version() AS version_number, + tenants.opt_out + FROM public.tenants + LIMIT 1;""", + {"tenantId": tenant_id}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) def get_by_api_key(api_key): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify( - f"""SELECT - 1 AS tenant_id, - tenants.name, - tenants.created_at - FROM public.tenants - WHERE tenants.api_key = %(api_key)s - LIMIT 1;""", - {"api_key": api_key}) - ) + query = cur.mogrify(f"""SELECT 1 AS tenant_id, + tenants.name, + tenants.created_at + FROM public.tenants + WHERE tenants.api_key = %(api_key)s + LIMIT 1;""", + {"api_key": api_key}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) def generate_new_api_key(tenant_id): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify( - f"""UPDATE public.tenants - SET api_key=generate_api_key(20) - RETURNING api_key;""", - {"tenant_id": tenant_id}) - ) + query = cur.mogrify(f"""UPDATE public.tenants + SET api_key=generate_api_key(20) + RETURNING api_key;""", + {"tenant_id": tenant_id}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) def edit_client(tenant_id, changes): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify(f"""\ - UPDATE public.tenants - SET {", ".join([f"{helper.key_to_snake_case(k)} = %({k})s" for k in changes.keys()])} - RETURNING name, opt_out;""", - {"tenantId": tenant_id, **changes}) - ) + query = cur.mogrify(f"""UPDATE public.tenants + SET {", ".join([f"{helper.key_to_snake_case(k)} = %({k})s" for k in changes.keys()])} + RETURNING name, opt_out;""", + {"tenant_id": tenant_id, **changes}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) diff --git a/ee/api/chalicelib/core/custom_metrics.py b/ee/api/chalicelib/core/custom_metrics.py index e4dd9beb3..b7502edd9 100644 --- a/ee/api/chalicelib/core/custom_metrics.py +++ b/ee/api/chalicelib/core/custom_metrics.py @@ -489,8 +489,8 @@ def delete(project_id, metric_id, user_id): RETURNING data;""", {"metric_id": metric_id, "project_id": project_id, "user_id": user_id}) ) - # for EE only - row = cur.fetchone() + # for EE only + row = cur.fetchone() if row: if row["data"] and not sessions_favorite.favorite_session_exists(session_id=row["data"]["sessionId"]): keys = sessions_mobs. \ diff --git a/ee/api/chalicelib/core/integrations_global.py b/ee/api/chalicelib/core/integrations_global.py index 8de504f09..e601a94cc 100644 --- a/ee/api/chalicelib/core/integrations_global.py +++ b/ee/api/chalicelib/core/integrations_global.py @@ -51,10 +51,10 @@ def get_global_integrations_status(tenant_id, user_id, project_id): AND provider='elasticsearch')) AS {schemas.IntegrationType.elasticsearch.value}, EXISTS((SELECT 1 FROM public.webhooks - WHERE type='slack' AND tenant_id=%(tenant_id)s)) AS {schemas.IntegrationType.slack.value}, + WHERE type='slack' AND tenant_id=%(tenant_id)s AND deleted_at ISNULL)) AS {schemas.IntegrationType.slack.value}, EXISTS((SELECT 1 FROM public.webhooks - WHERE type='msteams' AND tenant_id=%(tenant_id)s)) AS {schemas.IntegrationType.ms_teams.value};""", + WHERE type='msteams' AND tenant_id=%(tenant_id)s AND deleted_at ISNULL)) AS {schemas.IntegrationType.ms_teams.value};""", {"user_id": user_id, "tenant_id": tenant_id, "project_id": project_id}) ) current_integrations = cur.fetchone() diff --git a/ee/api/chalicelib/core/tenants.py b/ee/api/chalicelib/core/tenants.py index 70d0bb5b7..30a87bd29 100644 --- a/ee/api/chalicelib/core/tenants.py +++ b/ee/api/chalicelib/core/tenants.py @@ -6,88 +6,76 @@ from chalicelib.utils import pg_client def get_by_tenant_key(tenant_key): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify( - f"""SELECT - tenants.tenant_id, - tenants.name, - tenants.api_key, - tenants.created_at, - '{license.EDITION}' AS edition, - openreplay_version() AS version_number, - tenants.opt_out, - tenants.tenant_key - FROM public.tenants - WHERE tenants.tenant_key = %(tenant_key)s - AND tenants.deleted_at ISNULL - LIMIT 1;""", - {"tenant_key": tenant_key}) - ) + query = cur.mogrify(f"""SELECT tenants.tenant_id, + tenants.name, + tenants.api_key, + tenants.created_at, + '{license.EDITION}' AS edition, + openreplay_version() AS version_number, + tenants.opt_out, + tenants.tenant_key + FROM public.tenants + WHERE tenants.tenant_key = %(tenant_key)s + AND tenants.deleted_at ISNULL + LIMIT 1;""", + {"tenant_key": tenant_key}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) def get_by_tenant_id(tenant_id): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify( - f"""SELECT - tenants.tenant_id, - tenants.name, - tenants.api_key, - tenants.created_at, - '{license.EDITION}' AS edition, - openreplay_version() AS version_number, - tenants.opt_out, - tenants.tenant_key - FROM public.tenants - WHERE tenants.tenant_id = %(tenantId)s - AND tenants.deleted_at ISNULL - LIMIT 1;""", - {"tenantId": tenant_id}) - ) + query = cur.mogrify(f"""SELECT tenants.tenant_id, + tenants.name, + tenants.api_key, + tenants.created_at, + '{license.EDITION}' AS edition, + openreplay_version() AS version_number, + tenants.opt_out, + tenants.tenant_key + FROM public.tenants + WHERE tenants.tenant_id = %(tenantId)s + AND tenants.deleted_at ISNULL + LIMIT 1;""", + {"tenantId": tenant_id}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) def get_by_api_key(api_key): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify( - f"""SELECT - tenants.tenant_id, - tenants.name, - tenants.created_at - FROM public.tenants - WHERE tenants.api_key = %(api_key)s - AND tenants.deleted_at ISNULL - LIMIT 1;""", - {"api_key": api_key}) - ) + query = cur.mogrify(f"""SELECT tenants.tenant_id, + tenants.name, + tenants.created_at + FROM public.tenants + WHERE tenants.api_key = %(api_key)s + AND tenants.deleted_at ISNULL + LIMIT 1;""", + {"api_key": api_key}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) def generate_new_api_key(tenant_id): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify( - f"""UPDATE public.tenants - SET api_key=generate_api_key(20) - WHERE tenant_id= %(tenant_id)s AND deleted_at ISNULL - RETURNING api_key;""", - {"tenant_id": tenant_id}) - ) + query = cur.mogrify(f"""UPDATE public.tenants + SET api_key=generate_api_key(20) + WHERE tenant_id= %(tenant_id)s + AND deleted_at ISNULL + RETURNING api_key;""", + {"tenant_id": tenant_id}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) def edit_client(tenant_id, changes): with pg_client.PostgresClient() as cur: - cur.execute( - cur.mogrify(f"""\ - UPDATE public.tenants - SET {", ".join([f"{helper.key_to_snake_case(k)} = %({k})s" for k in changes.keys()])} - WHERE tenant_id= %(tenant_id)s AND deleted_at ISNULL - RETURNING name, opt_out;""", - {"tenantId": tenant_id, **changes}) - ) + query = cur.mogrify(f"""UPDATE public.tenants + SET {", ".join([f"{helper.key_to_snake_case(k)} = %({k})s" for k in changes.keys()])} + WHERE tenant_id= %(tenant_id)s AND deleted_at ISNULL + RETURNING name, opt_out;""", + {"tenant_id": tenant_id, **changes}) + cur.execute(query=query) return helper.dict_to_camel_case(cur.fetchone()) diff --git a/ee/api/schemas_ee.py b/ee/api/schemas_ee.py index e09677775..f5a3b8de0 100644 --- a/ee/api/schemas_ee.py +++ b/ee/api/schemas_ee.py @@ -22,11 +22,11 @@ class CurrentContext(schemas.CurrentContext): class RolePayloadSchema(BaseModel): - name: str = Field(...) - description: Optional[str] = Field(None) + name: str = Field(..., min_length=1, max_length=40) + description: Optional[str] = Field(default=None) permissions: List[Permissions] = Field(...) - all_projects: bool = Field(True) - projects: List[int] = Field([]) + all_projects: bool = Field(default=True) + projects: List[int] = Field(default=[]) _transform_name = validator('name', pre=True, allow_reuse=True)(schemas.remove_whitespace) class Config: