Qurrah commited on
Commit
8ac9bba
·
1 Parent(s): 99d9332

REFINE: replace Supabase operations with Workspace model methods in workspace management

Browse files
Files changed (1) hide show
  1. repositories/workspace.py +42 -35
repositories/workspace.py CHANGED
@@ -1,57 +1,64 @@
1
  from datetime import datetime, timezone
2
 
3
- from clients.supabase import supabase
 
4
 
5
 
6
- def get_workspace_by_slug(slug: str) -> dict:
7
- return (
8
- supabase.table("workspace")
9
- .select("*")
10
- .eq("slug", slug)
11
- .single()
12
- .execute()
13
- .data
14
- )
 
 
 
 
 
 
 
 
 
15
 
16
 
17
  def get_workspace_id_by_slug(slug: str) -> str:
18
- result = (
19
- supabase.table("workspace")
20
- .select("id")
21
- .eq("slug", slug)
22
- .single()
23
- .execute()
24
- .data
25
- )
26
- if not result:
27
- raise ValueError(f"No workspace found for slug: {slug}")
28
- return result["id"]
29
 
30
 
31
  def upsert_workspace(name: str, slug: str) -> dict:
32
- return (
33
- supabase.table("workspace")
34
- .upsert({"name": name, "slug": slug}, on_conflict="slug")
 
 
 
 
 
 
 
35
  .execute()
36
- .data[0]
37
  )
 
38
 
39
 
40
  def mark_workspace_indexed(workspace_id: str) -> None:
41
- supabase.table("workspace").update({
42
- "indexed_at": datetime.now(timezone.utc).isoformat(),
43
- }).eq("id", workspace_id).execute()
44
 
45
 
46
  def list_unindexed_workspaces() -> list[dict]:
47
- workspaces = (
48
- supabase.table("workspace")
49
- .select("slug, name, indexed_at")
50
- .execute()
51
- .data
52
  )
53
- return [workspace for workspace in workspaces if workspace["indexed_at"] is None]
54
 
55
 
56
  def list_workspace_slugs() -> list[dict]:
57
- return supabase.table("workspace").select("slug").execute().data
 
1
  from datetime import datetime, timezone
2
 
3
+ from core.exceptions import WorkspaceNotFoundError
4
+ from repositories.foreign_models import Workspace
5
 
6
 
7
+ def _to_dict(row: Workspace) -> dict:
8
+ return {
9
+ "id": row.id,
10
+ "name": row.name,
11
+ "slug": row.slug,
12
+ "logo": row.logo,
13
+ "metadata": row.metadata,
14
+ "created_at": row.created_at,
15
+ "indexed_at": row.indexed_at,
16
+ "reply_mode": row.reply_mode,
17
+ "usage_warn_80_month": row.usage_warn_80_month,
18
+ "usage_warn_100_month": row.usage_warn_100_month,
19
+ }
20
+
21
+
22
+ def get_workspace_by_slug(slug: str) -> dict | None:
23
+ row = Workspace.select().where(Workspace.slug == slug).limit(1).first()
24
+ return _to_dict(row) if row else None
25
 
26
 
27
  def get_workspace_id_by_slug(slug: str) -> str:
28
+ row = Workspace.select(Workspace.id).where(Workspace.slug == slug).limit(1).first()
29
+ if row is None:
30
+ raise WorkspaceNotFoundError(f"No workspace found for slug: {slug}")
31
+ return row.id
 
 
 
 
 
 
 
32
 
33
 
34
  def upsert_workspace(name: str, slug: str) -> dict:
35
+ # NOTE: mirrors prior Supabase behavior exactly, including a pre-existing
36
+ # gap - workspace.id has no DB-side default, so this insert path only
37
+ # succeeds when `slug` already exists (pure update via on_conflict). A
38
+ # genuinely new slug would hit a NOT NULL violation on `id`, same as
39
+ # before. Not fixed here - flagged separately, workspace row creation
40
+ # is expected to happen via the teammate's Better-Auth flow.
41
+ row = (
42
+ Workspace.insert(name=name, slug=slug)
43
+ .on_conflict(conflict_target=[Workspace.slug], preserve=[Workspace.name])
44
+ .returning(Workspace)
45
  .execute()
 
46
  )
47
+ return _to_dict(list(row)[0])
48
 
49
 
50
  def mark_workspace_indexed(workspace_id: str) -> None:
51
+ Workspace.update(indexed_at=datetime.now(timezone.utc)).where(
52
+ Workspace.id == workspace_id
53
+ ).execute()
54
 
55
 
56
  def list_unindexed_workspaces() -> list[dict]:
57
+ rows = Workspace.select(Workspace.slug, Workspace.name, Workspace.indexed_at).where(
58
+ Workspace.indexed_at.is_null()
 
 
 
59
  )
60
+ return [{"slug": row.slug, "name": row.name, "indexed_at": row.indexed_at} for row in rows]
61
 
62
 
63
  def list_workspace_slugs() -> list[dict]:
64
+ return [{"slug": row.slug} for row in Workspace.select(Workspace.slug)]