]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
Added Issue Type + Issue Reassigning + Last updated_by
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :require_user
6   before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore,:comment]
7   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
8
9   def index
10     # Get user role
11     if @user.administrator?
12       @user_role = "administrator"
13     else
14       @user_role = "moderator"
15     end
16
17     # If search
18     if params[:search_by_user]
19       @find_user = User.find_by_display_name(params[:search_by_user])
20       if @find_user
21         @issues = Issue.where(reported_user_id: @find_user.id, issue_type: @user_role).order(:status)
22       else 
23         @issues = Issue.where(issue_type: @user_role).order(:status)
24         notice = t('issues.index.search.user_not_found') 
25       end
26
27       if @find_user !=nil and @issues.first == nil
28         @issues = Issue.where(issue_type: @user_role).order(:status)
29         notice = t('issues.index.search.issues_not_found')
30       end
31
32       if notice
33         redirect_to issues_path, notice: notice
34       end 
35     
36     else
37       @issues = Issue.where(issue_type: @user_role).order(:status)
38     end
39   end
40
41   def show
42     @read_reports = @issue.read_reports
43     @unread_reports = @issue.unread_reports
44     @comments = @issue.comments
45     @related_issues = @issue.user.issues
46     if @issue.updated_by
47       @updated_by_admin = User.find(@issue.updated_by)
48     end
49   end
50
51   def new
52     unless create_new_issue_params.blank?
53       @issue = Issue.find_or_initialize_by(create_new_issue_params)
54       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
55       @report_strings_yaml = t( path)
56     end
57   end
58
59   def create
60     admin_issues = [ 'DiaryEntry', 'DiaryComment', 'User']
61     moderator_issues = []
62     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
63     # Check if Issue alrwady exists
64     if !@issue 
65       @issue = Issue.find_or_initialize_by(issue_params)
66       @issue.updated_by = nil
67       @admins = UserRole.where(role: "administrator")
68       @admins.each do |admin|
69         Notifier.new_issue_notification(User.find(admin.user_id)).deliver_now
70       end
71
72       # Reassign to moderators if it is a moderator issue
73       @issue.issue_type = "administrator"
74       if moderator_issues.include? @issue.reportable.class.name
75         reassign_issue
76       end
77     end
78
79     # Check if details provided are sufficient
80     if check_report_params
81       @report = @issue.reports.build(report_params)
82       details =  get_report_details
83       @report.reporter_user_id = @user.id
84       @report.details = details
85
86       # Checking if instance has been updated since last report
87       @last_report = @issue.reports.order(updated_at: :desc).last
88       if check_if_updated
89         if @issue.reopen
90           @issue.save!
91         end
92       end
93
94       if @issue.save!
95         redirect_to root_path, notice: t('issues.create.successful_report')
96       end
97     else
98       redirect_to new_issue_path(reportable_type: @issue.reportable_type,reportable_id: @issue.reportable_id, reported_user_id: @issue.reported_user_id), notice: t('issues.create.provide_details')
99     end
100   end
101
102   def update
103     @issue = Issue.find_by(issue_params)
104     # Check if details provided are sufficient
105     if check_report_params
106       @report = @issue.reports.where(reporter_user_id: @user.id).first
107       
108       if @report == nil
109         @report = @issue.reports.build(report_params)
110         @report.reporter_user_id = @user.id
111         notice = t('issues.update.new_report')
112       end
113
114       details =  get_report_details
115       @report.details = details    
116
117     # Checking if instance has been updated since last report
118       @last_report = @issue.reports.order(updated_at: :desc).last
119       if check_if_updated
120         @issue.reopen
121         @issue.save!
122       end
123
124       if notice == nil
125         notice = t('issues.update.successful_update')
126       end
127
128       if @report.save!
129         redirect_to root_path, notice: notice
130       end
131     else
132       redirect_to new_issue_path(reportable_type: @issue.reportable_type,reportable_id: @issue.reportable_id, reported_user_id: @issue.reported_user_id), notice: t('issues.update.provide_details')
133     end  
134   end
135
136   def comment
137     @issue = Issue.find(params[:id])
138     @issue_comment = @issue.comments.build(issue_comment_params)
139     @issue_comment.commenter_user_id = @user.id
140     if params[:reassign]
141       reassign_issue
142       @issue_comment.reassign = true
143     end
144     @issue_comment.save!
145     @issue.updated_by = @user.id
146     @issue.save!
147     redirect_to @issue
148   end
149
150   # Status Transistions
151   def resolve
152     if @issue.resolve
153       @issue.save!
154       redirect_to @issue, notice: t('issues.resolved')
155     else
156       render :show
157     end
158   end
159
160   def ignore
161     if @issue.ignore
162       @issue.updated_by = @user.id
163       @issue.save!
164       redirect_to @issue, notice: t('issues.ignored')
165     else
166       render :show
167     end
168   end
169
170   def reopen
171     if @issue.reopen
172       @issue.updated_by = @user.id      
173       @issue.save!
174       redirect_to @issue, notice: t('issues.reopened')
175     else
176       render :show
177     end
178   end
179
180   # Reassign Issues between Administrators and Moderators
181   def reassign_issue
182     if @issue.issue_type == "moderator"
183       @issue.issue_type = "administrator"
184     else
185       @issue.issue_type = "moderator"
186     end
187     @issue.save!
188   end
189
190   private
191
192     def check_if_updated
193       if @issue.reportable and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at
194         return true
195       else
196         return false
197       end
198     end
199  
200     def get_report_details
201       details = params[:report][:details] + "--||--"
202       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
203       @report_strings_yaml = t( path)
204       @report_strings_yaml.each do |k,v|
205         if params[k.to_sym]
206           details = details + params[k.to_sym] + "--||--"
207         end
208       end
209       return details
210     end
211
212     def check_report_params
213       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
214       @report_strings_yaml = t( path)
215       if params[:report] and params[:report][:details]
216         @report_strings_yaml.each do |k,v|
217           if params[k.to_sym]
218             return true
219           end
220         end
221       end
222       return false
223     end
224
225     def find_issue
226       @issue = Issue.find(params[:id])
227     end
228
229     def check_permission
230       unless @user.administrator?
231         flash[:error] = t('application.require_admin.not_an_admin')
232         redirect_to root_path
233       end
234     end
235
236     def create_new_issue_params
237       params.permit(:reportable_id, :reportable_type, :reported_user_id)
238     end
239
240     def issue_params
241       params[:issue].permit(:reportable_id, :reportable_type,:reported_user_id)
242     end
243
244     def report_params
245       params[:report].permit(:details)
246     end
247
248     def issue_comment_params
249       params.require(:issue_comment).permit(:body)
250     end
251
252 end