summaryrefslogtreecommitdiff
path: root/lib/api/user_teams.rb
blob: 05aa72f0e92b43d5f42185e22e27e858f57c7f60 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
module API
  # user_teams API
  class UserTeams < Grape::API
    before { authenticate! }

    resource :user_teams do
      helpers do
        def handle_team_member_errors(errors)
          if errors[:permission].any?
            render_api_error!(errors[:permission], 422)
          end
          not_found!
        end

        def validate_access_level?(level)
          [UsersProject::GUEST, UsersProject::REPORTER, UsersProject::DEVELOPER, UsersProject::MASTER].include? level.to_i
        end
      end


      # Get a user_teams list
      #
      # Example Request:
      #  GET /user_teams
      get do
        if current_user.admin
          @user_teams = paginate UserTeam
        else
          @user_teams = paginate current_user.user_teams
        end
        present @user_teams, with: Entities::UserTeam
      end


      # Create user_team. Available only for admin
      #
      # Parameters:
      #   name (required) - The name of the user_team
      #   path (required) - The path of the user_team
      # Example Request:
      #   POST /user_teams
      post do
        authenticated_as_admin!
        required_attributes! [:name, :path]

        attrs = attributes_for_keys [:name, :path]
        @user_team = UserTeam.new(attrs)
        @user_team.owner = current_user

        if @user_team.save
          present @user_team, with: Entities::UserTeam
        else
          not_found!
        end
      end


      # Get a single user_team
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      # Example Request:
      #   GET /user_teams/:id
      get ":id" do
        @user_team = UserTeam.find(params[:id])
        if current_user.admin or current_user.user_teams.include? @user_team
          present @user_team, with: Entities::UserTeam
        else
          not_found!
        end
      end


      # Get user_team members
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      # Example Request:
      #   GET /user_teams/:id/members
      get ":id/members" do
        @user_team = UserTeam.find(params[:id])
        if current_user.admin or current_user.user_teams.include? @user_team
          @members = paginate @user_team.members
          present @members, with: Entities::TeamMember, user_team: @user_team
        else
          not_found!
        end
      end


      # Add a new user_team member
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      #   user_id (required) - The ID of a user
      #   access_level (required) - Project access level
      # Example Request:
      #   POST /user_teams/:id/members
      post ":id/members" do
        authenticated_as_admin!
        required_attributes! [:user_id, :access_level]

        if not validate_access_level?(params[:access_level])
          render_api_error!("Wrong access level", 422)
        end

        @user_team = UserTeam.find(params[:id])
        if @user_team
          team_member = @user_team.user_team_user_relationships.find_by_user_id(params[:user_id])
          # Not existing member
          if team_member.nil?
            @user_team.add_member(params[:user_id], params[:access_level], false)
            team_member = @user_team.user_team_user_relationships.find_by_user_id(params[:user_id])

            if team_member.nil?
              render_api_error!("Error creating membership", 500)
            else
              @member = team_member.user
              present @member, with: Entities::TeamMember, user_team: @user_team
            end
          else
            render_api_error!("Already exists", 409)
          end
        else
          not_found!
        end
      end


      # Get a single team member from user_team
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      #   user_id (required) - The ID of a team member
      # Example Request:
      #   GET /user_teams/:id/members/:user_id
      get ":id/members/:user_id" do
        @user_team = UserTeam.find(params[:id])
        if current_user.admin or current_user.user_teams.include? @user_team
          team_member = @user_team.user_team_user_relationships.find_by_user_id(params[:user_id])
          unless team_member.nil?
            present team_member.user, with: Entities::TeamMember, user_team: @user_team
          else
            not_found!
          end
        else
          not_found!
        end
      end

      # Remove a team member from user_team
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      #   user_id (required) - The ID of a team member
      # Example Request:
      #   DELETE /user_teams/:id/members/:user_id
      delete ":id/members/:user_id" do
        authenticated_as_admin!

        @user_team = UserTeam.find(params[:id])
        if @user_team
          team_member = @user_team.user_team_user_relationships.find_by_user_id(params[:user_id])
          unless team_member.nil?
            team_member.destroy
          else
            not_found!
          end
        else
          not_found!
        end
      end


      # Get to user_team assigned projects
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      # Example Request:
      #   GET /user_teams/:id/projects
      get ":id/projects" do
        @user_team = UserTeam.find(params[:id])
        if current_user.admin or current_user.user_teams.include? @user_team
          @projects = paginate @user_team.projects
          present @projects, with: Entities::TeamProject, user_team: @user_team
        else
          not_found!
        end
      end


      # Add a new user_team project
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      #   project_id (required) - The ID of a project
      #   greatest_access_level (required) - Project access level
      # Example Request:
      #   POST /user_teams/:id/projects
      post ":id/projects" do
        authenticated_as_admin!
        required_attributes! [:project_id, :greatest_access_level]

        if not validate_access_level?(params[:greatest_access_level])
          render_api_error!("Wrong greatest_access_level", 422)
        end

        @user_team = UserTeam.find(params[:id])
        if @user_team
          team_project = @user_team.user_team_project_relationships.find_by_project_id(params[:project_id])

          # No existing project
          if team_project.nil?
            @user_team.assign_to_projects([params[:project_id]], params[:greatest_access_level])
            team_project = @user_team.user_team_project_relationships.find_by_project_id(params[:project_id])
            if team_project.nil?
              render_api_error!("Error creating project assignment", 500)
            else
              @project = team_project.project
              present @project, with: Entities::TeamProject, user_team: @user_team
            end
          else
            render_api_error!("Already exists", 409)
          end
        else
          not_found!
        end
      end

      # Show a single team project from user_team
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      #   project_id (required) - The ID of a project assigned to the team
      # Example Request:
      #   GET /user_teams/:id/projects/:project_id
      get ":id/projects/:project_id" do
        @user_team = UserTeam.find(params[:id])
        if current_user.admin or current_user.user_teams.include? @user_team
          team_project = @user_team.user_team_project_relationships.find_by_project_id(params[:project_id])
          unless team_project.nil?
            present team_project.project, with: Entities::TeamProject, user_team: @user_team
          else
            not_found!
          end
        else
          not_found!
        end
      end

      # Remove a team project from user_team
      #
      # Parameters:
      #   id (required) - The ID of a user_team
      #   project_id (required) - The ID of a project assigned to the team
      # Example Request:
      #   DELETE /user_teams/:id/projects/:project_id
      delete ":id/projects/:project_id" do
        authenticated_as_admin!

        @user_team = UserTeam.find(params[:id])
        if @user_team
          team_project = @user_team.user_team_project_relationships.find_by_project_id(params[:project_id])
          unless team_project.nil?
            team_project.destroy
          else
            not_found!
          end
        else
          not_found!
        end
      end

    end
  end
end