diff --git a/terraso_backend/apps/collaboration/models/memberships.py b/terraso_backend/apps/collaboration/models/memberships.py index 0a91ded41..69de78eb4 100644 --- a/terraso_backend/apps/collaboration/models/memberships.py +++ b/terraso_backend/apps/collaboration/models/memberships.py @@ -54,7 +54,10 @@ class MembershipList(BaseModel): default=DEFAULT_MEMERBSHIP_TYPE, ) - def save_membership(self, user_email, user_role, membership_status, validation_func): + def save_membership( + self, user_email, user_role, membership_status, validation_func, membership_class=None + ): + membership_class = membership_class or Membership user = User.objects.filter(email=user_email).first() user_exists = user is not None @@ -73,7 +76,7 @@ def save_membership(self, user_email, user_role, membership_status, validation_f raise ValidationError("User cannot request membership") if is_new: - membership = Membership( + membership = membership_class( membership_list=self, user=user if user_exists else None, pending_email=user_email if not user_exists else None, diff --git a/terraso_backend/apps/graphql/schema/schema.graphql b/terraso_backend/apps/graphql/schema/schema.graphql index aa9ebef7e..e134fe9f8 100644 --- a/terraso_backend/apps/graphql/schema/schema.graphql +++ b/terraso_backend/apps/graphql/schema/schema.graphql @@ -1861,7 +1861,7 @@ input ProjectDeleteMutationInput { type ProjectAddUserMutationPayload { errors: GenericScalar project: ProjectNode! - membership: CollaborationMembershipNode! + membership: ProjectMembershipNode! clientMutationId: String } @@ -1875,7 +1875,7 @@ input ProjectAddUserMutationInput { type ProjectDeleteUserMutationPayload { errors: GenericScalar project: ProjectNode! - membership: CollaborationMembershipNode! + membership: ProjectMembershipNode! clientMutationId: String } @@ -1888,7 +1888,7 @@ input ProjectDeleteUserMutationInput { type ProjectUpdateUserRoleMutationPayload { errors: GenericScalar project: ProjectNode! - membership: CollaborationMembershipNode! + membership: ProjectMembershipNode! clientMutationId: String } diff --git a/terraso_backend/apps/project_management/graphql/projects.py b/terraso_backend/apps/project_management/graphql/projects.py index d190af631..79767ae88 100644 --- a/terraso_backend/apps/project_management/graphql/projects.py +++ b/terraso_backend/apps/project_management/graphql/projects.py @@ -25,9 +25,6 @@ from graphene_django.filter import DjangoFilterConnectionField, TypedFilter from apps.audit_logs import api as log_api -from apps.collaboration.graphql.memberships import ( - CollaborationMembershipNode as MembershipNode, -) from apps.collaboration.graphql.memberships import ( MembershipListNodeMixin, MembershipNodeMixin, @@ -73,7 +70,7 @@ def resolve_user_role(self, info): match self.user_role: case "viewer": return UserRole.viewer - case "constributor": + case "contributor": return UserRole.contributor case "manager": return UserRole.manager @@ -287,9 +284,9 @@ def mutate_and_get_payload(cls, root, info, **kwargs): class ProjectAddUserMutation(BaseWriteMutation): project = graphene.Field(ProjectNode, required=True) - membership = graphene.Field(MembershipNode, required=True) + membership = graphene.Field(ProjectMembershipNode, required=True) - model_class = Membership + model_class = ProjectMembership class Input: project_id = graphene.ID(required=True) @@ -322,9 +319,10 @@ def validate(context): user_role=role.value, membership_status=Membership.APPROVED, validation_func=validate, + membership_class=ProjectMembership, ) except ValidationError as e: - cls.not_allowed_create(model=Membership, msg=e.message) + cls.not_allowed_create(model=cls.model_class, msg=e.message) membership_added_signal.send(sender=cls, membership=membership, user=request_user) @@ -333,7 +331,7 @@ def validate(context): class ProjectDeleteUserMutation(BaseWriteMutation): project = graphene.Field(ProjectNode, required=True) - membership = graphene.Field(MembershipNode, required=True) + membership = graphene.Field(ProjectMembershipNode, required=True) model_class = Membership @@ -380,10 +378,10 @@ def mutate_and_get_payload(cls, root, info, project_id, user_id): class ProjectUpdateUserRoleMutation(BaseWriteMutation): - model_class = Membership + model_class = ProjectMembership project = graphene.Field(ProjectNode, required=True) - membership = graphene.Field(MembershipNode, required=True) + membership = graphene.Field(ProjectMembershipNode, required=True) class Input: project_id = graphene.ID(required=True) diff --git a/terraso_backend/apps/project_management/models/projects.py b/terraso_backend/apps/project_management/models/projects.py index ff36f8902..2b7d37075 100644 --- a/terraso_backend/apps/project_management/models/projects.py +++ b/terraso_backend/apps/project_management/models/projects.py @@ -146,7 +146,9 @@ def add_user_with_role(self, user: User, role: str): ) def get_membership(self, user: User): - return self.membership_list.memberships.filter(user=user).first() + return ProjectMembership.objects.filter( + membership_list=self.membership_list, user=user + ).first() def mark_seen_by(self, user: User): self.seen_by.add(user)