-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvalidations_helper.rb
More file actions
204 lines (181 loc) · 5.24 KB
/
Copy pathvalidations_helper.rb
File metadata and controls
204 lines (181 loc) · 5.24 KB
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
module ValidationsHelper
def current_user
@current_user = AuthorizeApiRequest.call(request.headers).result
end
def user
@user = User.find(@project.user_id)
end
def project
@project = Project.find(@release.project_id)
end
def project_grade
@project = Project.find(@grade.project.id)
end
def release
@release = Release.find(@sprint.release_id)
end
def sprint(component_type)
if component_type == "story"
@sprint = Sprint.find(@story.sprint_id)
elsif component_type == "revision"
@sprint = Sprint.find(@revision.sprint_id)
elsif component_type == "retrospective"
@sprint = Sprint.find(@retrospective.sprint_id)
end
end
def verifies_id(current_id, previous_id, component_type)
if component_type == "user" && current_id != 0
id = current_id
@user = User.find(params[:id].to_i)
elsif component_type == "user" && previous_id != 0
user_id = previous_id
@user = User.find(params[:user_id].to_i)
elsif component_type == "project" && current_id != 0
id = current_id
@project = Project.find(params[:id].to_i)
elsif component_type == "project" && previous_id != 0
project_id = previous_id
@project = Project.find(params[:project_id].to_i)
elsif component_type == "release" && current_id != 0
id = current_id
@release = Release.find(params[:id].to_i)
elsif component_type == "release" && previous_id != 0
release_id = previous_id
@release = Release.find(params[:release_id].to_i)
elsif component_type == "grade" && current_id != 0
id = current_id
@grade = Grade.find(params[:id].to_i)
elsif component_type == "grade" && previous_id != 0
grade_id = previous_id
@grade = Grade.find(params[:grade_id].to_i)
elsif component_type == "sprint" && current_id != 0
id = current_id
@sprint = Sprint.find(params[:id].to_i)
elsif component_type == "sprint" && previous_id != 0
sprint_id = previous_id
@sprint = Sprint.find(params[:sprint_id].to_i)
end
end
def validate_user(id, user_id)
current_user
verifies_id(id, user_id, "user")
if @current_user.id == @user.id
return true
else
render json: { error: "Not Authorized" }, status: 401
end
end
def validate_project(id, project_id)
current_user
verifies_id(id, project_id, "project")
user
if @current_user.id == @user.id
return true
else
render json: { error: "Not Authorized" }, status: 401
end
end
def validate_grade_and_release(id, element_id, component_type)
current_user
verifies_id(id, element_id, component_type)
if component_type == "grade"
project_grade
else
project
end
user
if @current_user.id == @user.id
return true
else
render json: { error: "Not Authorized" }, status: 401
end
end
def validate_sprint(id, sprint_id)
current_user
verifies_id(id, sprint_id, "sprint")
release
project
user
if @current_user.id == @user.id
return true
else
render json: { error: "Not Authorized" }, status: 401
end
end
def validate_sprints_date(component_type, component_params)
if @release.initial_date > @sprint.initial_date ||
@release.final_date < @sprint.initial_date
false
elsif @release.final_date < @sprint.final_date ||
@release.initial_date > @sprint.final_date
false
else
return true
end
end
def validate_stories(story_points, id, sprint_id)
current_user
verifies_id(id, sprint_id, "sprint")
release
project
user
if @project.is_scoring
if story_points != nil
return true
else
return false
end
else
if story_points != nil
return false
else
return true
end
end
end
def validate_sprint_dependencies(id, component_type)
current_user
if component_type == "story"
@story = Story.find(params[:id].to_i)
sprint("story")
elsif component_type == "revision"
@revision = Revision.find(params[:id].to_i)
sprint("revision")
elsif component_type == "retrospective"
@retrospective = Retrospective.find(params[:id].to_i)
sprint("retrospective")
end
release
project
user
if @current_user.id == @user.id
return true
else
render json: { error: "Not Authorized" }, status: 401
end
end
def create_sprint_dependencies(component_type, component_params)
if component_type == "revision" && @sprint.revision == nil
@component = Revision.create(component_params)
save_component(@component)
elsif component_type == "retrospective" && @sprint.retrospective == nil
@component = Retrospective.create(component_params)
save_component(@component)
else
render json: { error: "Cannot create multiple #{component_type}" }, status: 403
end
end
def save_component(component)
@component = component
@component.sprint_id = @sprint.id
if @component.save
render json: @component, status: :created
else
render json: @component.errors, status: :unprocessable_entity
end
end
def update_amount_of_sprints
@release.amount_of_sprints = @release.sprints.count
@release.save
end
end