|
| 1 | +class IssueDatetimesController < ApplicationController |
| 2 | + before_action :find_issue, only: [:show, :update, :destroy] |
| 3 | + before_action :require_edit_permission, only: [:update, :destroy] |
| 4 | + before_action :find_project, only: [:index] |
| 5 | + accept_api_auth :index, :show, :update, :destroy |
| 6 | + |
| 7 | + def show |
| 8 | + render json: issue_payload(@issue) |
| 9 | + end |
| 10 | + |
| 11 | + # Accepts {"starts_at": <iso8601|null>, "ends_at": <iso8601|null>}. |
| 12 | + # A timestamp sets both the core date and the time of day; null clears |
| 13 | + # the time of day and keeps the date. Omitted keys are left untouched. |
| 14 | + def update |
| 15 | + zone = RedmineIssueDatetime.reference_zone |
| 16 | + @issue.init_journal(User.current) |
| 17 | + |
| 18 | + [[:starts_at, :start_date, :start_time=], [:ends_at, :due_date, :due_time=]].each do |param, date_attr, time_writer| |
| 19 | + next unless params.key?(param) |
| 20 | + |
| 21 | + value = params[param] |
| 22 | + if value.present? |
| 23 | + timestamp = parse_timestamp(value) |
| 24 | + return render_parse_error(param) if timestamp.nil? |
| 25 | + |
| 26 | + local = timestamp.in_time_zone(zone) |
| 27 | + @issue.send(:"#{date_attr}=", local.to_date) |
| 28 | + @issue.send(time_writer, local.strftime('%H:%M')) |
| 29 | + else |
| 30 | + @issue.send(time_writer, '') |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + if @issue.save |
| 35 | + render json: issue_payload(@issue.reload) |
| 36 | + else |
| 37 | + render json: {errors: @issue.errors.full_messages}, status: :unprocessable_entity |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + # Clears the times (back to all-day); the core dates stay. |
| 42 | + def destroy |
| 43 | + @issue.init_journal(User.current) |
| 44 | + @issue.start_time = '' |
| 45 | + @issue.due_time = '' |
| 46 | + if @issue.save |
| 47 | + head :no_content |
| 48 | + else |
| 49 | + render json: {errors: @issue.errors.full_messages}, status: :unprocessable_entity |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + # Bulk read for external consumers (schedulers, sync clients). |
| 54 | + def index |
| 55 | + unless User.current.allowed_to?(:view_issues, @project) |
| 56 | + return render_403 |
| 57 | + end |
| 58 | + |
| 59 | + scope = IssueDatetime |
| 60 | + .joins(issue: :project) |
| 61 | + .where(issues: {project_id: @project.id}) |
| 62 | + .where(Issue.visible_condition(User.current)) |
| 63 | + if params[:updated_since].present? |
| 64 | + since = parse_timestamp(params[:updated_since]) |
| 65 | + return render_parse_error(:updated_since) if since.nil? |
| 66 | + |
| 67 | + scope = scope.where('issue_datetimes.updated_at >= ?', since) |
| 68 | + end |
| 69 | + |
| 70 | + render json: { |
| 71 | + issue_datetimes: scope.order(:issue_id).map { |record| record_payload(record) } |
| 72 | + } |
| 73 | + end |
| 74 | + |
| 75 | + private |
| 76 | + |
| 77 | + def find_issue |
| 78 | + @issue = Issue.find(params[:issue_id]) |
| 79 | + render_403 unless @issue.visible? |
| 80 | + rescue ActiveRecord::RecordNotFound |
| 81 | + render_404 |
| 82 | + end |
| 83 | + |
| 84 | + def require_edit_permission |
| 85 | + render_403 unless @issue.editable? |
| 86 | + end |
| 87 | + |
| 88 | + def find_project |
| 89 | + @project = Project.find(params[:project_id]) |
| 90 | + rescue ActiveRecord::RecordNotFound |
| 91 | + render_404 |
| 92 | + end |
| 93 | + |
| 94 | + def parse_timestamp(value) |
| 95 | + Time.iso8601(value.to_s) |
| 96 | + rescue ArgumentError |
| 97 | + nil |
| 98 | + end |
| 99 | + |
| 100 | + def render_parse_error(param) |
| 101 | + render json: {errors: ["#{param} must be an ISO 8601 timestamp"]}, status: :unprocessable_entity |
| 102 | + end |
| 103 | + |
| 104 | + def issue_payload(issue) |
| 105 | + record = issue.issue_datetime |
| 106 | + { |
| 107 | + issue_id: issue.id, |
| 108 | + start_date: issue.start_date, |
| 109 | + due_date: issue.due_date, |
| 110 | + starts_at: record&.starts_at&.iso8601, |
| 111 | + ends_at: record&.ends_at&.iso8601, |
| 112 | + updated_at: record&.updated_at&.iso8601 |
| 113 | + } |
| 114 | + end |
| 115 | + |
| 116 | + def record_payload(record) |
| 117 | + { |
| 118 | + issue_id: record.issue_id, |
| 119 | + starts_at: record.starts_at&.iso8601, |
| 120 | + ends_at: record.ends_at&.iso8601, |
| 121 | + updated_at: record.updated_at.iso8601 |
| 122 | + } |
| 123 | + end |
| 124 | +end |
0 commit comments