Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions app/controllers/api/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/api/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions app/controllers/api/trainee_progresses_controller.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions app/controllers/api/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/views/api/reports/_report.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
22 changes: 22 additions & 0 deletions app/views/api/reports/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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
75 changes: 75 additions & 0 deletions app/views/api/trainee_progresses/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Loading