Skip to content

Commit b7cfd53

Browse files
committed
refactor: remove old watchlist models and update user role structure for enhanced functionality
1 parent b157cfc commit b7cfd53

File tree

8 files changed

+330
-699
lines changed

8 files changed

+330
-699
lines changed

todo/migrations/0001_initial_postgres_models.py

Lines changed: 0 additions & 481 deletions
This file was deleted.

todo/migrations/0001_initial_setup.py

Lines changed: 289 additions & 0 deletions
Large diffs are not rendered by default.

todo/migrations/0002_remove_old_watchlist_tables.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

todo/migrations/0003_create_new_watchlist_table.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

todo/migrations/0004_update_audit_log_structure.py

Lines changed: 0 additions & 115 deletions
This file was deleted.

todo/models/postgres/user_role.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,16 @@
33

44

55
class PostgresUserRole(models.Model):
6-
"""
7-
Postgres model for user roles.
8-
"""
9-
10-
# MongoDB ObjectId as string for reference
116
mongo_id = models.CharField(max_length=24, unique=True, null=True, blank=True)
127

13-
# User role fields
14-
user_mongo_id = models.CharField(max_length=24) # MongoDB ObjectId as string
15-
role_mongo_id = models.CharField(max_length=24) # MongoDB ObjectId as string
16-
team_mongo_id = models.CharField(max_length=24, null=True, blank=True) # MongoDB ObjectId as string
17-
18-
# Timestamps
8+
user_id = models.CharField(max_length=24)
9+
role_name = models.CharField(max_length=50)
10+
scope = models.CharField(max_length=20)
11+
team_id = models.CharField(max_length=24, null=True, blank=True)
12+
is_active = models.BooleanField(default=True)
1913
created_at = models.DateTimeField(default=timezone.now)
20-
updated_at = models.DateTimeField(null=True, blank=True)
14+
created_by = models.CharField(max_length=24, default="system")
2115

22-
# References
23-
created_by = models.CharField(max_length=24) # MongoDB ObjectId as string
24-
updated_by = models.CharField(max_length=24, null=True, blank=True) # MongoDB ObjectId as string
25-
26-
# Sync metadata
2716
last_sync_at = models.DateTimeField(auto_now=True)
2817
sync_status = models.CharField(
2918
max_length=20,
@@ -38,20 +27,16 @@ class PostgresUserRole(models.Model):
3827

3928
class Meta:
4029
db_table = "postgres_user_roles"
41-
unique_together = ["user_mongo_id", "role_mongo_id", "team_mongo_id"]
30+
unique_together = ["user_id", "role_name", "scope", "team_id"]
4231
indexes = [
4332
models.Index(fields=["mongo_id"]),
44-
models.Index(fields=["user_mongo_id"]),
45-
models.Index(fields=["role_mongo_id"]),
46-
models.Index(fields=["team_mongo_id"]),
33+
models.Index(fields=["user_id"]),
34+
models.Index(fields=["role_name"]),
35+
models.Index(fields=["scope"]),
36+
models.Index(fields=["team_id"]),
37+
models.Index(fields=["is_active"]),
4738
models.Index(fields=["sync_status"]),
4839
]
4940

5041
def __str__(self):
51-
return f"User {self.user_mongo_id} has Role {self.role_mongo_id}"
52-
53-
def save(self, *args, **kwargs):
54-
if not self.pk: # New instance
55-
self.created_at = timezone.now()
56-
self.updated_at = timezone.now()
57-
super().save(*args, **kwargs)
42+
return f"User {self.user_id} has Role {self.role_name} ({self.scope})"

todo/repositories/user_role_repository.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from todo.models.user_role import UserRoleModel
77
from todo.repositories.common.mongo_repository import MongoRepository
88
from todo.constants.role import RoleScope, RoleName
9+
from todo.services.enhanced_dual_write_service import EnhancedDualWriteService
910

1011
logger = logging.getLogger(__name__)
1112

@@ -38,6 +39,27 @@ def create(cls, user_role: UserRoleModel) -> UserRoleModel:
3839
user_role_dict = user_role.model_dump(mode="json", by_alias=True, exclude_none=True)
3940
result = collection.insert_one(user_role_dict)
4041
user_role.id = result.inserted_id
42+
43+
dual_write_service = EnhancedDualWriteService()
44+
user_role_data = {
45+
"user_id": user_role.user_id,
46+
"role_name": user_role.role_name.value if hasattr(user_role.role_name, "value") else user_role.role_name,
47+
"scope": user_role.scope.value if hasattr(user_role.scope, "value") else user_role.scope,
48+
"team_id": user_role.team_id,
49+
"is_active": user_role.is_active,
50+
"created_at": user_role.created_at,
51+
"created_by": user_role.created_by,
52+
}
53+
54+
dual_write_success = dual_write_service.create_document(
55+
collection_name="user_roles", data=user_role_data, mongo_id=str(user_role.id)
56+
)
57+
58+
if not dual_write_success:
59+
import logging
60+
logger = logging.getLogger(__name__)
61+
logger.warning(f"Failed to sync user role {user_role.id} to Postgres")
62+
4163
return user_role
4264

4365
@classmethod

todo/services/dual_write_service.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,14 @@ def _transform_user_team_details_data(self, data: Dict[str, Any]) -> Dict[str, A
407407
}
408408

409409
def _transform_user_role_data(self, data: Dict[str, Any]) -> Dict[str, Any]:
410-
"""Transform user role data for Postgres."""
411410
return {
412-
"user_mongo_id": str(data.get("user_id", "")),
413-
"role_mongo_id": str(data.get("role_id", "")),
414-
"team_mongo_id": str(data.get("team_id", "")) if data.get("team_id") else None,
415-
"created_by": str(data.get("created_by", "")),
416-
"updated_by": str(data.get("updated_by", "")) if data.get("updated_by") else None,
411+
"user_id": str(data.get("user_id", "")),
412+
"role_name": data.get("role_name"),
413+
"scope": data.get("scope"),
414+
"team_id": str(data.get("team_id", "")) if data.get("team_id") else None,
415+
"is_active": data.get("is_active", True),
417416
"created_at": data.get("created_at"),
418-
"updated_at": data.get("updated_at"),
417+
"created_by": str(data.get("created_by", "")),
419418
}
420419

421420
def _transform_audit_log_data(self, data: Dict[str, Any]) -> Dict[str, Any]:

0 commit comments

Comments
 (0)