From 684139a37827c7a782b265c7e076c4f00125679a Mon Sep 17 00:00:00 2001 From: Yuriy Volodin Date: Sat, 18 Jan 2025 15:30:45 +0300 Subject: [PATCH] add history stats to info mobile api endpoint --- app/controllers/api/mobile/athletes_controller.rb | 14 +++++++++++++- app/views/api/mobile/athletes/info.json.jbuilder | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/mobile/athletes_controller.rb b/app/controllers/api/mobile/athletes_controller.rb index d1524877..7622d86a 100644 --- a/app/controllers/api/mobile/athletes_controller.rb +++ b/app/controllers/api/mobile/athletes_controller.rb @@ -5,7 +5,19 @@ module Mobile class AthletesController < ApplicationController before_action :find_athlete - def info; end + def info + @history_stats = + @athlete + .volunteering + .where(activity: { date: 5.months.ago.beginning_of_month.. }) + .unscope(:order) + .group(:role, Arel.sql("date_part('month', date)")) + .count + .each_with_object({}) do |(k, v), h| + h[k.first] ||= Array.new(6, 0) + h[k.first][5 - ((Date.current.mon - k.last) % 12)] = v + end + end private diff --git a/app/views/api/mobile/athletes/info.json.jbuilder b/app/views/api/mobile/athletes/info.json.jbuilder index 45dd6a70..1017500b 100644 --- a/app/views/api/mobile/athletes/info.json.jbuilder +++ b/app/views/api/mobile/athletes/info.json.jbuilder @@ -1,7 +1,10 @@ json.call(@athlete, :name, :male) json.home_event @athlete.event&.name json.volunteering do - json.stats @athlete.stats['volunteers'] + json.stats do + json.general @athlete.stats['volunteers'] + json.history @history_stats + end json.scheduled( @athlete .volunteering