diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb index 9e653b616f5..954cb7a78ab 100644 --- a/app/controllers/api/base_controller.rb +++ b/app/controllers/api/base_controller.rb @@ -2,7 +2,9 @@ class API::BaseController < ApplicationController skip_before_action :require_active_user_login, raise: false - before_action :require_login_for_api + skip_before_action :verify_authenticity_token, if: -> { doorkeeper_token.present? } + before_action :doorkeeper_authorize!, if: -> { doorkeeper_token.present? } + before_action :require_login_for_api, unless: -> { doorkeeper_token.present? } private diff --git a/app/controllers/api/comments_controller.rb b/app/controllers/api/comments_controller.rb index c80a724cb3d..5b8a24e473d 100644 --- a/app/controllers/api/comments_controller.rb +++ b/app/controllers/api/comments_controller.rb @@ -3,8 +3,6 @@ class API::CommentsController < API::BaseController before_action :set_my_comment, only: %i[update destroy] before_action :set_available_emojis, only: %i[index create] - skip_before_action :verify_authenticity_token, if: -> { doorkeeper_token.present? } - before_action :doorkeeper_authorize!, if: -> { doorkeeper_token.present? } before_action -> { doorkeeper_authorize! :write }, only: %i[create update destroy], if: -> { doorkeeper_token.present? } def index diff --git a/app/controllers/api/reports_controller.rb b/app/controllers/api/reports_controller.rb index e8908afdb38..6c9794d0ed2 100644 --- a/app/controllers/api/reports_controller.rb +++ b/app/controllers/api/reports_controller.rb @@ -13,4 +13,8 @@ def index @reports = @reports.includes(:checks).unchecked.not_wip end + + def show + @report = Report.includes(:user, :practices, :checks, comments: :user).find(params[:id]) + end end diff --git a/app/controllers/api/trainee_progresses_controller.rb b/app/controllers/api/trainee_progresses_controller.rb new file mode 100644 index 00000000000..39114c56be0 --- /dev/null +++ b/app/controllers/api/trainee_progresses_controller.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class API::TraineeProgressesController < API::BaseController + def index + @trainees = User.trainees + .preload(:company, :course, :avatar_attachment, :active_practices, + :reports, learnings: :practice) + .order(:company_id, :created_at) + @trainees = @trainees.where(company_id: params[:company_id]) if params[:company_id].present? + + @week_start = parse_week_start + @week_end = @week_start + 4.days # 月〜金 + end + + private + + def parse_week_start + if params[:week_start].present? + Date.parse(params[:week_start]) + else + Date.current.beginning_of_week + end + rescue ArgumentError + Date.current.beginning_of_week + end +end diff --git a/app/controllers/api/users_controller.rb b/app/controllers/api/users_controller.rb index 8c57f60ad8c..452f409cbcf 100644 --- a/app/controllers/api/users_controller.rb +++ b/app/controllers/api/users_controller.rb @@ -2,9 +2,6 @@ class API::UsersController < API::BaseController before_action :set_user, only: %i[show update] - before_action :require_login_for_api - before_action :require_login_for_api, except: :show - before_action :doorkeeper_authorize!, if: -> { doorkeeper_token.present? }, only: :show PAGER_NUMBER = 24 def index diff --git a/app/views/api/reports/_report.json.jbuilder b/app/views/api/reports/_report.json.jbuilder index 8aac763affc..95a879d692a 100644 --- a/app/views/api/reports/_report.json.jbuilder +++ b/app/views/api/reports/_report.json.jbuilder @@ -1,5 +1,6 @@ json.id report.id json.title report.title +json.description report.description json.reportedOn l(report.reported_on) json.url report_url(report) json.editPath edit_report_path(report) diff --git a/app/views/api/reports/show.json.jbuilder b/app/views/api/reports/show.json.jbuilder new file mode 100644 index 00000000000..9a017757ca8 --- /dev/null +++ b/app/views/api/reports/show.json.jbuilder @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +json.partial! 'api/reports/report', report: @report +json.partial! 'api/reports/checks', checks: @report.checks + +json.user do + json.partial! 'api/users/user', user: @report.user +end + +json.practices @report.practices do |practice| + json.id practice.id + json.title practice.title +end + +json.comments @report.comments.order(:created_at) do |comment| + json.id comment.id + json.description comment.description + json.createdAt comment.created_at + json.user do + json.partial! 'api/users/user', user: comment.user + end +end diff --git a/app/views/api/trainee_progresses/index.json.jbuilder b/app/views/api/trainee_progresses/index.json.jbuilder new file mode 100644 index 00000000000..b9c84e70f8a --- /dev/null +++ b/app/views/api/trainee_progresses/index.json.jbuilder @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +json.weekStart @week_start +json.weekEnd @week_end + +json.trainees @trainees do |trainee| # rubocop:disable Metrics/BlockLength + user_course_practice = UserCoursePractice.new(trainee) + + # learningsを一度取得してフィルタリング + all_learnings = trainee.learnings.reject { |l| l.status == 'unstarted' } + + week_learnings = all_learnings.select do |l| + l.updated_at >= @week_start.beginning_of_day && + l.updated_at <= @week_end.end_of_day + end + + current_learning = all_learnings + .select { |l| l.status == 'started' } + .max_by(&:updated_at) + + # 対象週の平日に対応する日報 + week_reports = trainee.reports + .select { |r| r.reported_on >= @week_start && r.reported_on <= @week_end && !r.wip? } + .sort_by(&:reported_on) + + json.id trainee.id + json.loginName trainee.login_name + json.longName trainee.long_name + json.avatarUrl trainee.avatar_url + + json.company do + if trainee.company.present? + json.id trainee.company.id + json.name trainee.company.name + end + end + + json.course do + if trainee.course.present? + json.id trainee.course.id + json.title trainee.course.title + end + end + + # 週次アクティビティ + json.weeklyActivity do + json.reportCount week_reports.size + json.weekdays 5 + json.reportDates(week_reports.map { |r| r.reported_on.to_s }) + json.practiceStatusChanges week_learnings.size + json.practiceChanges week_learnings do |learning| + json.practiceId learning.practice_id + json.practiceTitle learning.practice.title + json.status learning.status + json.updatedAt learning.updated_at + end + end + + # 現在のプラクティス + json.currentPractice do + if current_learning + days_on_practice = (Date.current - current_learning.updated_at.to_date).to_i + json.id current_learning.practice_id + json.title current_learning.practice.title + json.daysOnPractice days_on_practice + end + end + + # 全体進捗 + json.overallProgress do + json.completedPracticesCount user_course_practice.completed_required_practices.size + json.requiredPracticesCount user_course_practice.required_practices.size + json.completedPercentage user_course_practice.completed_percentage.round(1) + end +end diff --git a/config/routes/api.rb b/config/routes/api.rb index ab92d0700c2..6299fc7e170 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -43,13 +43,13 @@ resource :position, only: %i(update), controller: "coding_tests/position" end resources :coding_test_submissions, only: %i(create) - resources :reports, only: %i(index) namespace "reports" do resources :unchecked, only: %i(index) do get 'counts', on: :collection end resources :recents, only: %i(index) end + resources :reports, only: %i(index show) resources :watches, only: %i(index create destroy) namespace 'watches' do resources :toggle, only: %i(index) @@ -84,5 +84,6 @@ resources :movies, only: %i(index update) resources :metadata, only: %i(index) resources :micro_reports, only: %i(update) + resources :trainee_progresses, only: %i(index) end end