Skip to content

Commit

Permalink
feat: Use enum for project update mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
David Code Howard committed Sep 20, 2023
1 parent dfc9c12 commit e43899e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,9 @@ type ProjectMembershipNode implements Node {
}

enum UserRole {
VIEWER
CONTRIBUTOR
MANAGER
viewer
contributor
manager
}

"""An enumeration."""
Expand Down Expand Up @@ -1801,7 +1801,7 @@ type ProjectAddUserMutationPayload {
input ProjectAddUserMutationInput {
projectId: ID!
userId: ID!
role: String!
role: UserRole!
clientMutationId: String
}

Expand All @@ -1828,7 +1828,7 @@ type ProjectUpdateUserRoleMutationPayload {
input ProjectUpdateUserRoleMutationInput {
projectId: ID!
userId: ID!
newRole: String!
newRole: UserRole!
clientMutationId: String
}

Expand Down
26 changes: 13 additions & 13 deletions terraso_backend/apps/project_management/graphql/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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}")

Expand Down Expand Up @@ -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):
Expand All @@ -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,
)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e43899e

Please sign in to comment.