-
Notifications
You must be signed in to change notification settings - Fork 6
/
create_organization_role.rb
27 lines (21 loc) · 1.04 KB
/
create_organization_role.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true
class Mutations::CreateOrganizationRole < Mutations::BaseMutation
field :organization_role, Types::OrganizationRoleType, null: false
argument :organization_id, ID, required: false, camelize: true
argument :organization_role, Types::OrganizationRoleInputType, required: true, camelize: false
argument :user_ids, [ID], required: false, camelize: true
argument :permissions, [Types::PermissionInputType], required: true
attr_reader :organization
define_authorization_check do |args|
@organization = Organization.find(args[:organization_id])
policy(OrganizationRole.new(organization: organization)).create?
end
def resolve(**args)
new_role = organization.organization_roles.create!(args[:organization_role].to_h)
new_role.update!(user_ids: args[:user_ids])
args[:permissions].each { |permission| new_role.permissions.create!(permission) }
# not sure why, but if I don't do this it seems like permissions get returned twice
new_role.reload
{ organization_role: new_role }
end
end