Module: MnoEnterprise::Concerns::Controllers::Jpi::V1::OrganizationsController

Extended by:
ActiveSupport::Concern
Included in:
Jpi::V1::OrganizationsController
Defined in:
lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /mnoe/jpi/v1/organizations



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 51

def create
  # Create new organization
  @organization = MnoEnterprise::Organization.create(organization_update_params)

  # Add the current user as Super Admin
  @organization.add_user(current_user,'Super Admin')

  # Bust cache
  current_user.refresh_user_cache

  render 'show'
end

#destroyObject

DELETE /mnoe/jpi/v1/organizations/1



41
42
43
44
45
46
47
48
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 41

def destroy
  if organization
    authorize! :destroy, organization
    organization.destroy
  end

  head :no_content
end

#indexObject

Instance methods

GET /mnoe/jpi/v1/organizations



17
18
19
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 17

def index
  @organizations ||= current_user.organizations
end

#invite_membersObject

TODO: specs PUT /mnoe/jpi/v1/organizations/:id/invite_members



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 103

def invite_members
  # Filter
  whitelist = ['email','role','team_id']
  attributes = []
  params[:invites].each do |invite|
    attributes << invite.select { |k,v| whitelist.include?(k.to_s) }
  end

  # Authorize and create
  authorize! :invite_member, organization
  attributes.each do |invite|
    @org_invite = organization.org_invites.create(
      user_email: invite['email'],
      user_role: invite['role'],
      team_id: invite['team_id'],
      referrer_id: current_user.id
    )

    MnoEnterprise::SystemNotificationMailer.organization_invite(@org_invite).deliver_now
  end

  # Reload users
  organization.users.reload

  render 'members'
end

#remove_memberObject

TODO: specs PUT /mnoe/jpi/v1/organizations/:id/remove_member



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 151

def remove_member
  attributes = params[:member]
  @member = organization.users.where(email: attributes[:email]).first
  @member ||= organization.org_invites.active.where(user_email: attributes[:email]).first

  # Authorize and update
  authorize! :invite_member, organization
  if @member.is_a?(MnoEnterprise::User)
    organization.remove_user(@member)
  elsif @member.is_a?(MnoEnterprise::OrgInvite)
    @member.cancel!
  end

  render 'members'
end

#showObject

GET /mnoe/jpi/v1/organizations/1



22
23
24
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 22

def show
  organization # load organization
end

#updateObject

PUT /mnoe/jpi/v1/organizations/:id



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 27

def update
  # Update and Authorize
  organization.assign_attributes(organization_update_params)
  authorize! :update, organization

  # Save
  if organization.save
    render 'show_reduced'
  else
    render json: organization.errors, status: :bad_request
  end
end

#update_billingObject

PUT /mnoe/jpi/v1/organizations/:id/update_billing



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 83

def update_billing
  whitelist = ['title','first_name','last_name','number','month','year','country','verification_value','billing_address','billing_city','billing_postcode', 'billing_country']
  attributes = params[:credit_card].select { |k,v| whitelist.include?(k.to_s) }
  authorize! :manage_billing, organization

  # Upsert
  if @credit_card = organization.credit_card
    @credit_card.assign_attributes(attributes.merge(organization_id: @credit_card.organization_id))
    @credit_card.save
  end

  if @credit_card.errors.empty?
    render 'credit_card'
  else
    render json: @credit_card.errors, status: :bad_request
  end
end

#update_memberObject

TODO: specs PUT /mnoe/jpi/v1/organizations/:id/update_member



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/mno_enterprise/concerns/controllers/jpi/v1/organizations_controller.rb', line 132

def update_member
  attributes = params[:member]
  @member = organization.users.where(email: attributes[:email]).first
  @member ||= organization.org_invites.active.where(user_email: attributes[:email]).first

  # Authorize and update
  authorize! :invite_member, organization
  if @member.is_a?(MnoEnterprise::User)
    organization.users.update(id: @member.id, role: attributes[:role])
  elsif @member.is_a?(MnoEnterprise::OrgInvite)
    @member.user_role = attributes[:role]
    @member.save
  end

  render 'members'
end