mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
migrate trainings to statistic_profile and adjust code
This commit is contained in:
parent
0a5e373730
commit
fe2a6f7120
@ -23,8 +23,8 @@ class API::AvailabilitiesController < API::ApiController
|
||||
def public
|
||||
start_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:start])
|
||||
end_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:end]).end_of_day
|
||||
@reservations = Reservation.includes(:slots, user: [:profile])
|
||||
.references(:slots, :user)
|
||||
@reservations = Reservation.includes(:slots, :statistic_profile)
|
||||
.references(:slots)
|
||||
.where('slots.start_at >= ? AND slots.end_at <= ?', start_date, end_date)
|
||||
|
||||
machine_ids = params[:m] || []
|
||||
@ -93,7 +93,7 @@ class API::AvailabilitiesController < API::ApiController
|
||||
|
||||
def reservations
|
||||
authorize Availability
|
||||
@reservation_slots = @availability.slots.includes(reservations: [user: [:profile]]).order('slots.start_at ASC')
|
||||
@reservation_slots = @availability.slots.includes(reservations: [statistic_profile: [user: [:profile]]]).order('slots.start_at ASC')
|
||||
end
|
||||
|
||||
def export_availabilities
|
||||
|
@ -41,8 +41,8 @@ class API::PricesController < API::ApiController
|
||||
# user
|
||||
user = User.find(price_parameters[:user_id])
|
||||
# reservable
|
||||
if price_parameters[:reservable_id].nil?
|
||||
@amount = {elements: nil, total: 0, before_coupon: 0}
|
||||
if [nil, ''].include? price_parameters[:reservable_id]
|
||||
@amount = { elements: nil, total: 0, before_coupon: 0 }
|
||||
else
|
||||
reservable = price_parameters[:reservable_type].constantize.find(price_parameters[:reservable_id])
|
||||
@amount = Price.compute(current_user.admin?,
|
||||
|
@ -58,7 +58,7 @@ class API::TrainingsController < API::ApiController
|
||||
authorize Training
|
||||
@training = Training.find(params[:id])
|
||||
@availabilities = @training.availabilities
|
||||
.includes(slots: { reservations: { user: %i[profile trainings] } })
|
||||
.includes(slots: { reservations: { statistic_profile: [:trainings, user: [:profile]] } })
|
||||
.order('start_at DESC')
|
||||
end
|
||||
|
||||
|
@ -65,6 +65,7 @@ class User < ActiveRecord::Base
|
||||
delegate :last_name, to: :profile
|
||||
delegate :subscriptions, to: :statistic_profile
|
||||
delegate :reservations, to: :statistic_profile
|
||||
delegate :trainings, to: :statistic_profile
|
||||
delegate :wallet, to: :invoicing_profile
|
||||
delegate :wallet_transactions, to: :invoicing_profile
|
||||
delegate :invoices, to: :invoicing_profile
|
||||
|
@ -1,15 +0,0 @@
|
||||
class UserTraining < ActiveRecord::Base
|
||||
include NotifyWith::NotificationAttachedObject
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :training
|
||||
|
||||
after_commit :notify_user_training_valid, on: :create
|
||||
|
||||
private
|
||||
def notify_user_training_valid
|
||||
NotificationCenter.call type: 'notify_user_training_valid',
|
||||
receiver: user,
|
||||
attached_object: self
|
||||
end
|
||||
end
|
@ -4,11 +4,12 @@
|
||||
class Availabilities::StatusService
|
||||
def initialize(current_user_role)
|
||||
@current_user_role = current_user_role
|
||||
@show_name = (@current_user_role == 'admin' || Setting.find_by(name: 'display_name_enable').value == 'true')
|
||||
end
|
||||
|
||||
# check that the provided machine slot is reserved or not and modify it accordingly
|
||||
def machine_reserved_status(slot, reservations, user)
|
||||
show_name = (@current_user_role == 'admin' || Setting.find_by(name: 'display_name_enable').value == 'true')
|
||||
statistic_profile_id = user.statistic_profile.id
|
||||
reservations.each do |r|
|
||||
r.slots.each do |s|
|
||||
next unless slot.machine.id == r.reservable_id
|
||||
@ -17,11 +18,11 @@ class Availabilities::StatusService
|
||||
|
||||
slot.id = s.id
|
||||
slot.is_reserved = true
|
||||
slot.title = "#{slot.machine.name} - #{show_name ? r.user.profile.full_name : I18n.t('availabilities.not_available')}"
|
||||
slot.title = "#{slot.machine.name} - #{@show_name ? r.user.profile.full_name : I18n.t('availabilities.not_available')}"
|
||||
slot.can_modify = true if @current_user_role == 'admin'
|
||||
slot.reservations.push r
|
||||
|
||||
next unless r.user == user
|
||||
next unless r.statistic_profile_id == statistic_profile_id
|
||||
|
||||
slot.title = "#{slot.machine.name} - #{I18n.t('availabilities.i_ve_reserved')}"
|
||||
slot.can_modify = true
|
||||
@ -33,6 +34,7 @@ class Availabilities::StatusService
|
||||
|
||||
# check that the provided space slot is reserved or not and modify it accordingly
|
||||
def space_reserved_status(slot, reservations, user)
|
||||
statistic_profile_id = user.statistic_profile.id
|
||||
reservations.each do |r|
|
||||
r.slots.each do |s|
|
||||
next unless slot.space.id == r.reservable_id
|
||||
@ -42,7 +44,7 @@ class Availabilities::StatusService
|
||||
slot.can_modify = true if @current_user_role == 'admin'
|
||||
slot.reservations.push r
|
||||
|
||||
next unless r.user == user
|
||||
next unless r.statistic_profile_id == statistic_profile_id
|
||||
|
||||
slot.id = s.id
|
||||
slot.title = I18n.t('availabilities.i_ve_reserved')
|
||||
@ -55,6 +57,7 @@ class Availabilities::StatusService
|
||||
|
||||
# check that the provided availability (training or event) is reserved or not and modify it accordingly
|
||||
def training_event_reserved_status(availability, reservations, user)
|
||||
statistic_profile_id = user.statistic_profile.id
|
||||
reservations.each do |r|
|
||||
r.slots.each do |s|
|
||||
next unless (
|
||||
@ -63,7 +66,7 @@ class Availabilities::StatusService
|
||||
) && s.start_at == availability.start_at && s.canceled_at.nil?
|
||||
|
||||
availability.slot_id = s.id
|
||||
if r.user == user
|
||||
if r.statistic_profile_id == statistic_profile_id
|
||||
availability.is_reserved = true
|
||||
availability.can_modify = true
|
||||
end
|
||||
|
@ -7,8 +7,8 @@ json.array!(@reservation_slots) do |slot|
|
||||
json.reservable_id slot.reservation.reservable_id
|
||||
json.reservable_type slot.reservation.reservable_type
|
||||
json.user do
|
||||
json.id slot.reservation.user.id
|
||||
json.name slot.reservation.user.profile.full_name
|
||||
json.id slot.reservation.statistic_profile&.user_id
|
||||
json.name slot.reservation.statistic_profile&.user&.profile&.full_name
|
||||
end
|
||||
json.canceled_at slot.canceled_at
|
||||
end
|
||||
|
@ -3,7 +3,7 @@ requested_current = (current_user and current_user.id == @member.id)
|
||||
json.partial! 'api/members/member', member: @member
|
||||
json.extract! @member, :uid, :slug, :is_allow_contact, :is_allow_newsletter
|
||||
|
||||
json.training_ids @member.training_ids
|
||||
json.training_ids @member.statistic_profile.training_ids
|
||||
json.trainings @member.trainings do |t|
|
||||
json.id t.id
|
||||
json.name t.name
|
||||
@ -13,7 +13,7 @@ json.training_reservations @member.reservations.where(reservable_type: 'Training
|
||||
json.start_at r.slots.first.start_at
|
||||
json.end_at r.slots.first.end_at
|
||||
json.reservable r.reservable
|
||||
json.is_valid @member.training_ids.include?(r.reservable_id)
|
||||
json.is_valid @member.statistic_profile.training_ids.include?(r.reservable_id)
|
||||
json.canceled_at r.slots.first.canceled_at
|
||||
end
|
||||
|
||||
|
@ -4,8 +4,8 @@ json.availabilities @availabilities do |a|
|
||||
json.start_at a.start_at.iso8601
|
||||
json.end_at a.end_at.iso8601
|
||||
json.reservation_users a.slots.map do |slot|
|
||||
json.id slot.reservations.first.user_id
|
||||
json.full_name slot.reservations.first.user.profile.full_name
|
||||
json.is_valid slot.reservations.first.user.trainings.include?(@training)
|
||||
json.id slot.reservations.first.statistic_profile.user_id
|
||||
json.full_name slot.reservations.first.statistic_profile&.user&.profile&.full_name
|
||||
json.is_valid slot.reservations.first.statistic_profile.trainings.include?(@training)
|
||||
end
|
||||
end
|
||||
|
@ -1,6 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# https://github.com/ruckus/active-record-query-trace
|
||||
# traces the origin of active record queries, useful for optimisation purposes
|
||||
if Rails.env.development?
|
||||
ActiveRecordQueryTrace.enabled = false # set to true to enable
|
||||
ActiveRecordQueryTrace.enabled = false # set to true to enable
|
||||
ActiveRecordQueryTrace.level = :app
|
||||
end
|
||||
|
@ -0,0 +1,22 @@
|
||||
class MigrateUserTrainingsToStatisticProfileTrainings < ActiveRecord::Migration
|
||||
def up
|
||||
user_trainings = execute('SELECT * FROM user_trainings')
|
||||
|
||||
user_trainings.each do |ut|
|
||||
user = User.find(ut['user_id'])
|
||||
StatisticProfileTraining.create!(
|
||||
statistic_profile_id: user.statistic_profile.id,
|
||||
training_id: ut['training_id'],
|
||||
created_at: ut['created_at']
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
StatisticProfileTraining.all.each do |spt|
|
||||
statistic_profile = StatisticProfile.find(spt.statistic_profile_id)
|
||||
execute("INSERT INTO user_trainings (user_id, training_id, created_at, updated_at)
|
||||
VALUES (#{statistic_profile.user_id}, #{spt.training_id}, '#{spt.created_at.utc}', '#{DateTime.now.utc}')")
|
||||
end
|
||||
end
|
||||
end
|
5
db/migrate/20190606074801_delete_user_trainings.rb
Normal file
5
db/migrate/20190606074801_delete_user_trainings.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class DeleteUserTrainings < ActiveRecord::Migration
|
||||
def change
|
||||
drop_table :user_trainings
|
||||
end
|
||||
end
|
24
db/schema.rb
24
db/schema.rb
@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20190604075717) do
|
||||
ActiveRecord::Schema.define(version: 20190606074801) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@ -662,6 +662,16 @@ ActiveRecord::Schema.define(version: 20190604075717) do
|
||||
t.boolean "ca", default: true
|
||||
end
|
||||
|
||||
create_table "statistic_profile_trainings", force: :cascade do |t|
|
||||
t.integer "statistic_profile_id"
|
||||
t.integer "training_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
add_index "statistic_profile_trainings", ["statistic_profile_id"], name: "index_statistic_profile_trainings_on_statistic_profile_id", using: :btree
|
||||
add_index "statistic_profile_trainings", ["training_id"], name: "index_statistic_profile_trainings_on_training_id", using: :btree
|
||||
|
||||
create_table "statistic_profiles", force: :cascade do |t|
|
||||
t.boolean "gender"
|
||||
t.date "birthday"
|
||||
@ -795,16 +805,6 @@ ActiveRecord::Schema.define(version: 20190604075717) do
|
||||
add_index "user_tags", ["tag_id"], name: "index_user_tags_on_tag_id", using: :btree
|
||||
add_index "user_tags", ["user_id"], name: "index_user_tags_on_user_id", using: :btree
|
||||
|
||||
create_table "user_trainings", force: :cascade do |t|
|
||||
t.integer "user_id"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.integer "training_id"
|
||||
end
|
||||
|
||||
add_index "user_trainings", ["training_id"], name: "index_user_trainings_on_training_id", using: :btree
|
||||
add_index "user_trainings", ["user_id"], name: "index_user_trainings_on_user_id", using: :btree
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.string "email", limit: 255, default: "", null: false
|
||||
t.string "encrypted_password", limit: 255, default: "", null: false
|
||||
@ -920,6 +920,8 @@ ActiveRecord::Schema.define(version: 20190604075717) do
|
||||
add_foreign_key "spaces_availabilities", "availabilities"
|
||||
add_foreign_key "spaces_availabilities", "spaces"
|
||||
add_foreign_key "statistic_custom_aggregations", "statistic_types"
|
||||
add_foreign_key "statistic_profile_trainings", "statistic_profiles"
|
||||
add_foreign_key "statistic_profile_trainings", "trainings"
|
||||
add_foreign_key "statistic_profiles", "groups"
|
||||
add_foreign_key "statistic_profiles", "users"
|
||||
add_foreign_key "subscriptions", "statistic_profiles"
|
||||
|
@ -1,9 +0,0 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
statistic_profile_id:
|
||||
training_id:
|
||||
|
||||
two:
|
||||
statistic_profile_id:
|
||||
training_id:
|
0
test/fixtures/user_trainings.yml
vendored
0
test/fixtures/user_trainings.yml
vendored
Loading…
x
Reference in New Issue
Block a user