From e43899ed8a42bcc59cfa91db4ce5fce6178af11e Mon Sep 17 00:00:00 2001 From: David Code Howard Date: Wed, 20 Sep 2023 14:03:02 -0400 Subject: [PATCH] feat: Use enum for project update mutations --- .../apps/graphql/schema/schema.graphql | 10 +++---- .../project_management/graphql/projects.py | 26 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/terraso_backend/apps/graphql/schema/schema.graphql b/terraso_backend/apps/graphql/schema/schema.graphql index 6c6e21730..542dc10d3 100644 --- a/terraso_backend/apps/graphql/schema/schema.graphql +++ b/terraso_backend/apps/graphql/schema/schema.graphql @@ -586,9 +586,9 @@ type ProjectMembershipNode implements Node { } enum UserRole { - VIEWER - CONTRIBUTOR - MANAGER + viewer + contributor + manager } """An enumeration.""" @@ -1801,7 +1801,7 @@ type ProjectAddUserMutationPayload { input ProjectAddUserMutationInput { projectId: ID! userId: ID! - role: String! + role: UserRole! clientMutationId: String } @@ -1828,7 +1828,7 @@ type ProjectUpdateUserRoleMutationPayload { input ProjectUpdateUserRoleMutationInput { projectId: ID! userId: ID! - newRole: String! + newRole: UserRole! clientMutationId: String } diff --git a/terraso_backend/apps/project_management/graphql/projects.py b/terraso_backend/apps/project_management/graphql/projects.py index 30926ebf6..ba77479ef 100644 --- a/terraso_backend/apps/project_management/graphql/projects.py +++ b/terraso_backend/apps/project_management/graphql/projects.py @@ -52,9 +52,9 @@ class UserRole(graphene.Enum): - VIEWER = collaboration_roles.ROLE_VIEWER - CONTRIBUTOR = collaboration_roles.ROLE_CONTRIBUTOR - MANAGER = collaboration_roles.ROLE_MANAGER + viewer = collaboration_roles.ROLE_VIEWER + contributor = collaboration_roles.ROLE_CONTRIBUTOR + manager = collaboration_roles.ROLE_MANAGER class ProjectMembershipNode(DjangoObjectType, MembershipNodeMixin): @@ -65,12 +65,12 @@ class Meta(MembershipNodeMixin.Meta): def resolve_user_role(self, info): match self.user_role: - case "VIEWER": - return UserRole.VIEWER - case "CONTRIBUTOR": - return UserRole.CONTRIBUTOR - case "MANAGER": - return UserRole.MANAGER + case "viewer": + return UserRole.viewer + case "constributor": + return UserRole.contributor + case "manager": + return UserRole.manager case _: raise Exception(f"Unexpected user role: {self.user_role}") @@ -258,7 +258,7 @@ class ProjectAddUserMutation(BaseWriteMutation): class Input: project_id = graphene.ID(required=True) user_id = graphene.ID(required=True) - role = graphene.String(required=True) + role = graphene.Field(UserRole, required=True) @classmethod def mutate_and_get_payload(cls, root, info, project_id, user_id, role): @@ -283,7 +283,7 @@ def validate(context): try: _, membership = project.membership_list.save_membership( user_email=user.email, - user_role=role, + user_role=role.value, membership_status=Membership.APPROVED, validation_func=validate, ) @@ -352,7 +352,7 @@ class ProjectUpdateUserRoleMutation(BaseWriteMutation): class Input: project_id = graphene.ID(required=True) user_id = graphene.ID(required=True) - new_role = graphene.String(required=True) + new_role = graphene.Field(UserRole, required=True) @classmethod def mutate_and_get_payload(cls, root, info, project_id, user_id, new_role): @@ -380,7 +380,7 @@ def mutate_and_get_payload(cls, root, info, project_id, user_id, new_role): MutationTypes.UPDATE, msg="User is not allowed to change other user role" ) - target_membership.user_role = new_role + target_membership.user_role = new_role.value target_membership.save() membership_updated_signal.send(sender=cls, membership=target_membership, user=requester)