fix: project join sort order

This commit is contained in:
pablohashescobar 2023-08-01 17:27:04 +05:30
parent a8816ef473
commit 62340b0158

View File

@ -123,7 +123,7 @@ class ProjectViewSet(BaseViewSet):
sort_order_query = ProjectMember.objects.filter( sort_order_query = ProjectMember.objects.filter(
member=request.user, member=request.user,
project_id=OuterRef("pk"), project_id=OuterRef("pk"),
workspace__slug=self.kwargs.get("slug"), workspace__slug=self.kwargs.get("slug"),
).values("sort_order") ).values("sort_order")
projects = ( projects = (
self.get_queryset() self.get_queryset()
@ -602,19 +602,27 @@ class AddMemberToProjectEndpoint(BaseAPIView):
) )
bulk_project_members = [] bulk_project_members = []
project_members = ProjectMember.objects.filter( project_members = (
workspace=self.workspace, member_id__in=[member.get("member_id") for member in members] ProjectMember.objects.filter(
).values("member_id").annotate(sort_order_min=Min("sort_order")) workspace=self.workspace,
member_id__in=[member.get("member_id") for member in members],
)
.values("member_id")
.annotate(sort_order_min=Min("sort_order"))
)
for member in members: for member in members:
sort_order = [project_member.get("sort_order") for project_member in project_members] sort_order = [
project_member.get("sort_order")
for project_member in project_members
]
bulk_project_members.append( bulk_project_members.append(
ProjectMember( ProjectMember(
member_id=member.get("member_id"), member_id=member.get("member_id"),
role=member.get("role", 10), role=member.get("role", 10),
project_id=project_id, project_id=project_id,
workspace_id=project.workspace_id, workspace_id=project.workspace_id,
sort_order=sort_order[0] - 10000 if len(sort_order) else 65535 sort_order=sort_order[0] - 10000 if len(sort_order) else 65535,
) )
) )
@ -809,11 +817,21 @@ class ProjectJoinEndpoint(BaseAPIView):
member=request.user, workspace__slug=slug member=request.user, workspace__slug=slug
) )
smallest_sort_order = (
ProjectMember.objects.filter(
workspace_id=self.project.workspace_id, member=self.member
)
.aggregate(smallest=Min("sort_order"))
.get("smallest", 65535)
- 10000
)
workspace_role = workspace_member.role workspace_role = workspace_member.role
workspace = workspace_member.workspace workspace = workspace_member.workspace
ProjectMember.objects.bulk_create( bulk_project_members = []
[ for project_id in project_ids:
bulk_project_members.append(
ProjectMember( ProjectMember(
project_id=project_id, project_id=project_id,
member=request.user, member=request.user,
@ -822,9 +840,14 @@ class ProjectJoinEndpoint(BaseAPIView):
else (15 if workspace_role == 10 else workspace_role), else (15 if workspace_role == 10 else workspace_role),
workspace=workspace, workspace=workspace,
created_by=request.user, created_by=request.user,
sort_order=smallest_sort_order,
) )
for project_id in project_ids )
],
smallest_sort_order = smallest_sort_order - 10000
ProjectMember.objects.bulk_create(
bulk_project_members,
ignore_conflicts=True, ignore_conflicts=True,
) )