diff --git a/app/controllers/api/reservations_controller.rb b/app/controllers/api/reservations_controller.rb index 5ef8e1848..3e1d709c5 100644 --- a/app/controllers/api/reservations_controller.rb +++ b/app/controllers/api/reservations_controller.rb @@ -23,7 +23,7 @@ class API::ReservationsController < API::ApiController def create method = current_user.admin? ? :local : :stripe - user_id = current_user.admin? ? reservation_params[:user_id] : current_user.id + user_id = current_user.admin? ? params[:reservation][:user_id] : current_user.id @reservation = Reservation.new(reservation_params) is_reserve = Reservations::Reserve.new(user_id, current_user.id) @@ -56,8 +56,7 @@ class API::ReservationsController < API::ApiController end def reservation_params - params.require(:reservation).permit(:user_id, :message, :reservable_id, :reservable_type, :card_token, :plan_id, - :nb_reserve_places, + params.require(:reservation).permit(:message, :reservable_id, :reservable_type, :card_token, :plan_id, :nb_reserve_places, tickets_attributes: %i[event_price_category_id booked], slots_attributes: %i[id start_at end_at availability_id offered]) end diff --git a/app/controllers/api/subscriptions_controller.rb b/app/controllers/api/subscriptions_controller.rb index 9bd1d98bd..582f9d701 100644 --- a/app/controllers/api/subscriptions_controller.rb +++ b/app/controllers/api/subscriptions_controller.rb @@ -16,10 +16,10 @@ class API::SubscriptionsController < API::ApiController head 403 else method = current_user.admin? ? :local : :stripe - user_id = current_user.admin? ? subscription_params[:user_id] : current_user.id + user_id = current_user.admin? ? params[:user_id] : current_user.id @subscription = Subscription.new(subscription_params) - is_subscribe = Subscriptions::Subscribe.new(user_id, current_user.id) + is_subscribe = Subscriptions::Subscribe.new(current_user.id, user_id) .pay_and_save(@subscription, method, coupon_params[:coupon_code], true) if is_subscribe @@ -35,7 +35,7 @@ class API::SubscriptionsController < API::ApiController free_days = params[:subscription][:free] || false - res = Subscriptions::Subscribe.new(@subscription.user_id, current_user.id) + res = Subscriptions::Subscribe.new(current_user.id) .extend_subscription(@subscription, subscription_update_params[:expired_at], free_days) if res.is_a?(Subscription) @subscription = res @@ -56,7 +56,7 @@ class API::SubscriptionsController < API::ApiController # Never trust parameters from the scary internet, only allow the white list through. def subscription_params - params.require(:subscription).permit(:plan_id, :user_id, :card_token) + params.require(:subscription).permit(:plan_id, :card_token) end def coupon_params diff --git a/app/exceptions/no_profile_error.rb b/app/exceptions/no_profile_error.rb new file mode 100644 index 000000000..62abc40f3 --- /dev/null +++ b/app/exceptions/no_profile_error.rb @@ -0,0 +1,3 @@ +# Raised when an expected profile (statistic, invoicing or normal) was not found on an user +class NoProfileError < StandardError +end diff --git a/app/models/invoicing_profile.rb b/app/models/invoicing_profile.rb index ce33662fa..d8c8fa2ee 100644 --- a/app/models/invoicing_profile.rb +++ b/app/models/invoicing_profile.rb @@ -16,17 +16,8 @@ class InvoicingProfile < ActiveRecord::Base has_many :history_values, dependent: :nullify - after_create :create_a_wallet - - def full_name # if first_name or last_name is nil, the empty string will be used as a temporary replacement (first_name || '').humanize.titleize + ' ' + (last_name || '').humanize.titleize end - - private - - def create_a_wallet - create_wallet - end end diff --git a/app/models/profile.rb b/app/models/profile.rb index 2718afc75..b673cd417 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -12,7 +12,7 @@ class Profile < ActiveRecord::Base validates :last_name, presence: true, length: { maximum: 30 } validates_numericality_of :phone, only_integer: true, allow_blank: false - after_save :update_invoicing_profile, if: :invoicing_data_was_modified? + after_commit :update_invoicing_profile, if: :invoicing_data_was_modified?, on: [:update] def full_name # if first_name or last_name is nil, the empty string will be used as a temporary replacement @@ -41,18 +41,12 @@ class Profile < ActiveRecord::Base end def update_invoicing_profile - if user.invoicing_profile.nil? - InvoicingProfile.create!( - user: user, - first_name: first_name, - last_name: last_name - ) - else - user.invoicing_profile.update_attributes( - first_name: first_name, - last_name: last_name - ) - end + raise NoProfileError if user.invoicing_profile.nil? + + user.invoicing_profile.update_attributes( + first_name: first_name, + last_name: last_name + ) end end diff --git a/app/models/reservation.rb b/app/models/reservation.rb index 24744f6b1..3d46a9e7d 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -1,7 +1,7 @@ class Reservation < ActiveRecord::Base include NotifyWith::NotificationAttachedObject - belongs_to :user + belongs_to :statistic_profile has_many :slots_reservations, dependent: :destroy has_many :slots, through: :slots_reservations @@ -240,8 +240,8 @@ class Reservation < ActiveRecord::Base # TODO: refactoring customer = Stripe::Customer.retrieve(user.stp_customer_id) if plan_id - self.subscription = Subscription.find_or_initialize_by(user_id: user.id) - subscription.attributes = { plan_id: plan_id, user_id: user.id, card_token: card_token, expiration_date: nil } + self.subscription = Subscription.find_or_initialize_by(statistic_profile_id: statistic_profile_id) + subscription.attributes = { plan_id: plan_id, statistic_profile_id: statistic_profile_id, card_token: card_token, expiration_date: nil } if subscription.save_with_payment(operator_id, false) self.stp_invoice_id = invoice_items.first.refresh.invoice invoice.stp_invoice_id = invoice_items.first.refresh.invoice @@ -338,8 +338,8 @@ class Reservation < ActiveRecord::Base end # check reservation amount total and strip invoice total to pay is equal - # @params stp_invoice[Stripe::Invoice] - # @params coupon_code[String] + # @param stp_invoice[Stripe::Invoice] + # @param coupon_code[String] # return Boolean def is_equal_reservation_total_and_stp_invoice_total(stp_invoice, coupon_code = nil) compute_amount_total_to_pay(coupon_code) == stp_invoice.total @@ -375,8 +375,8 @@ class Reservation < ActiveRecord::Base return false unless valid? if plan_id - self.subscription = Subscription.find_or_initialize_by(user_id: user.id) - subscription.attributes = { plan_id: plan_id, user_id: user.id, expiration_date: nil } + self.subscription = Subscription.find_or_initialize_by(statistic_profile_id: statistic_profile_id) + subscription.attributes = { plan_id: plan_id, statistic_profile_id: statistic_profile_id, expiration_date: nil } if subscription.save_with_local_payment(operator_id, false) invoice.invoice_items.push InvoiceItem.new( amount: subscription.plan.amount, @@ -405,6 +405,10 @@ class Reservation < ActiveRecord::Base total end + def user + statistic_profile.user + end + private def machine_not_already_reserved diff --git a/app/models/statistic_profile.rb b/app/models/statistic_profile.rb index db90a956c..acc6cd495 100644 --- a/app/models/statistic_profile.rb +++ b/app/models/statistic_profile.rb @@ -9,6 +9,12 @@ class StatisticProfile < ActiveRecord::Base belongs_to :group # relations to reservations, trainings, subscriptions + has_many :subscriptions, dependent: :destroy + accepts_nested_attributes_for :subscriptions, allow_destroy: false + + has_many :reservations, dependent: :destroy + accepts_nested_attributes_for :reservations, allow_destroy: false + def str_gender gender ? 'male' : 'female' end diff --git a/app/models/subscription.rb b/app/models/subscription.rb index a6ca65f50..191eee43e 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -2,7 +2,7 @@ class Subscription < ActiveRecord::Base include NotifyWith::NotificationAttachedObject belongs_to :plan - belongs_to :user + belongs_to :statistic_profile has_many :invoices, as: :invoiced, dependent: :destroy has_many :offer_days, dependent: :destroy @@ -223,6 +223,10 @@ class Subscription < ActiveRecord::Base false end + def user + statistic_profile.user + end + private def notify_member_subscribed_plan diff --git a/app/models/user.rb b/app/models/user.rb index 43ab43596..f64407028 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -31,18 +31,12 @@ class User < ActiveRecord::Base has_many :project_users, dependent: :destroy has_many :projects, through: :project_users - has_many :reservations, dependent: :destroy - accepts_nested_attributes_for :reservations, allow_destroy: true - # Trainings that were already passed has_many :user_trainings, dependent: :destroy has_many :trainings, through: :user_trainings belongs_to :group - has_many :subscriptions, dependent: :destroy - accepts_nested_attributes_for :subscriptions, allow_destroy: true - has_many :users_credits, dependent: :destroy has_many :credits, through: :users_credits @@ -65,21 +59,27 @@ class User < ActiveRecord::Base before_create :assign_default_role after_commit :create_stripe_customer, on: [:create] after_commit :notify_admin_when_user_is_created, on: :create + after_create :init_dependencies after_update :notify_group_changed, if: :group_id_changed? - after_save :update_invoicing_profile, if: :invoicing_data_was_modified? - after_save :update_statistic_profile, if: :statistic_data_was_modified? + after_commit :update_invoicing_profile, if: :invoicing_data_was_modified?, on: [:update] + after_commit :update_statistic_profile, if: :statistic_data_was_modified?, on: [:update] attr_accessor :cgu delegate :first_name, to: :profile delegate :last_name, to: :profile + delegate :subscriptions, to: :statistic_profile + delegate :reservations, to: :statistic_profile + delegate :wallet, to: :invoicing_profile + delegate :wallet_transactions, to: :invoicing_profile + delegate :invoices, to: :invoicing_profile validate :cgu_must_accept, if: :new_record? validates :username, presence: true, uniqueness: true, length: { maximum: 30 } scope :active, -> { where(is_active: true) } - scope :without_subscription, -> { includes(:subscriptions).where(subscriptions: { user_id: nil }) } - scope :with_subscription, -> { joins(:subscriptions) } + scope :without_subscription, -> { includes(statistic_profile: [:subscriptions]).where(subscriptions: { statistic_profile_id: nil }) } + scope :with_subscription, -> { joins(statistic_profile: [:subscriptions]) } def to_json(*) ApplicationController.new.view_context.render( @@ -132,18 +132,6 @@ class User < ActiveRecord::Base my_projects.to_a.concat projects end - def invoices - invoicing_profile.invoices - end - - def wallet - invoicing_profile.wallet - end - - def wallet_transactions - invoicing_profile.wallet_transactions - end - def generate_subscription_invoice(operator_id) return unless subscription @@ -363,36 +351,42 @@ class User < ActiveRecord::Base end def invoicing_data_was_modified? - email_changed? or new_record? + email_changed? end def statistic_data_was_modified? - group_id_changed? or new_record? + group_id_changed? + end + + def init_dependencies + ip = InvoicingProfile.create!( + user: self, + email: email, + first_name: first_name, + last_name: last_name + ) + Wallet.create!( + invoicing_profile: ip + ) + StatisticProfile.create!( + user: self, + group_id: group_id + ) end def update_invoicing_profile - if invoicing_profile.nil? - InvoicingProfile.create!( - user: self, - email: email - ) - else - invoicing_profile.update_attributes( - email: email - ) - end + raise NoProfileError if user.invoicing_profile.nil? + + invoicing_profile.update_attributes( + email: email + ) end def update_statistic_profile - if statistic_profile.nil? - StatisticProfile.create!( - user: self, - group_id: group_id - ) - else - statistic_profile.update_attributes( - group_id: group_id - ) - end + raise NoProfileError if user.statistic_profile.nil? + + statistic_profile.update_attributes( + group_id: group_id + ) end end diff --git a/app/services/availabilities/availabilities_service.rb b/app/services/availabilities/availabilities_service.rb index ba94b6f94..019082cae 100644 --- a/app/services/availabilities/availabilities_service.rb +++ b/app/services/availabilities/availabilities_service.rb @@ -93,7 +93,7 @@ class Availabilities::AvailabilitiesService def reservations(reservable) Reservation.where('reservable_type = ? and reservable_id = ?', reservable.class.name, reservable.id) - .includes(:slots, user: [:profile]) + .includes(:slots, statistic_profile: [user: [:profile]]) .references(:slots, :user) .where('slots.start_at > ?', Time.now) end diff --git a/app/services/members/list_service.rb b/app/services/members/list_service.rb index 9fe939826..24013efdf 100644 --- a/app/services/members/list_service.rb +++ b/app/services/members/list_service.rb @@ -6,9 +6,10 @@ class Members::ListService def list(params) @query = User.includes(:profile, :group, :subscriptions) .joins(:profile, + :statistic_profile, :group, :roles, - 'LEFT JOIN "subscriptions" ON "subscriptions"."user_id" = "users"."id" ' \ + 'LEFT JOIN "subscriptions" ON "subscriptions"."statistic_profile_id" = "statistic_profiles"."id" ' \ 'LEFT JOIN "plans" ON "plans"."id" = "subscriptions"."plan_id"') .where("users.is_active = 'true' AND roles.name = 'member'") .order(list_order(params)) @@ -31,12 +32,13 @@ class Members::ListService def search(current_user, query, subscription) members = User.includes(:profile) .joins(:profile, + :statistic_profile, :roles, - 'LEFT JOIN "subscriptions" ON "subscriptions"."user_id" = "users"."id" AND ' \ + 'LEFT JOIN "subscriptions" ON "subscriptions"."statistic_profile_id" = "statistic_profiles"."id" AND ' \ '"subscriptions"."created_at" = ( ' \ 'SELECT max("created_at") ' \ 'FROM "subscriptions" ' \ - 'WHERE "user_id" = "users"."id")') + 'WHERE "statistic_profile_id" = "statistic_profiles"."id")') .where("users.is_active = 'true' AND roles.name = 'member'") .limit(50) query.downcase.split(' ').each do |word| diff --git a/app/services/reservations/reserve.rb b/app/services/reservations/reserve.rb index 8bab493e2..128262b63 100644 --- a/app/services/reservations/reserve.rb +++ b/app/services/reservations/reserve.rb @@ -10,7 +10,7 @@ class Reservations::Reserve end def pay_and_save(reservation, payment_method, coupon) - reservation.user_id = user_id + reservation.statistic_profile_id = User.find(user_id).statistic_profile.id if payment_method == :local reservation.save_with_local_payment(operator_id, coupon) elsif payment_method == :stripe diff --git a/app/services/subscriptions/subscribe.rb b/app/services/subscriptions/subscribe.rb index 4e9fb2bc6..74ea491f7 100644 --- a/app/services/subscriptions/subscribe.rb +++ b/app/services/subscriptions/subscribe.rb @@ -4,13 +4,15 @@ class Subscriptions::Subscribe attr_accessor :user_id, :operator_id - def initialize(user_id, operator_id) + def initialize(operator_id, user_id = nil) @user_id = user_id @operator_id = operator_id end def pay_and_save(subscription, payment_method, coupon, invoice) - subscription.user_id = user_id + return false if user_id.nil? + + subscription.statistic_profile_id = User.find(user_id).statistic_profile.id if payment_method == :local subscription.save_with_local_payment(operator_id, invoice, coupon) elsif payment_method == :stripe @@ -23,7 +25,7 @@ class Subscriptions::Subscribe new_sub = Subscription.create( plan_id: subscription.plan_id, - user_id: subscription.user_id, + statistic_profile_id: subscription.statistic_profile_id, expiration_date: new_expiration_date ) if new_sub.save @@ -32,4 +34,4 @@ class Subscriptions::Subscribe end false end -end \ No newline at end of file +end diff --git a/app/validators/subscription_group_validator.rb b/app/validators/subscription_group_validator.rb index f53ea16cb..c35874719 100644 --- a/app/validators/subscription_group_validator.rb +++ b/app/validators/subscription_group_validator.rb @@ -1,7 +1,7 @@ class SubscriptionGroupValidator < ActiveModel::Validator def validate(record) - if record.user.group != record.plan.group - record.errors[:plan_id] << "This plan is not compatible with the current user's group" - end + return if record.statistic_profile.group_id == record.plan.group + + record.errors[:plan_id] << "This plan is not compatible with the current user's group" end -end \ No newline at end of file +end diff --git a/app/views/api/reservations/show.json.jbuilder b/app/views/api/reservations/show.json.jbuilder index b265a94c8..2e86ca070 100644 --- a/app/views/api/reservations/show.json.jbuilder +++ b/app/views/api/reservations/show.json.jbuilder @@ -1,16 +1,18 @@ json.id @reservation.id -json.user_id @reservation.user_id +json.user_id @reservation.statistic_profile.user_id json.user do json.id @reservation.user.id - json.subscribed_plan do - json.partial! 'api/shared/plan', plan: @reservation.user.subscribed_plan - end if @reservation.user.subscribed_plan + if @reservation.user.subscribed_plan + json.subscribed_plan do + json.partial! 'api/shared/plan', plan: @reservation.user.subscribed_plan + end + end json.training_credits @reservation.user.training_credits do |tc| json.training_id tc.creditable_id end json.machine_credits @reservation.user.machine_credits do |mc| json.machine_id mc.creditable_id - json.hours_used mc.users_credits.find_by(user_id: @reservation.user_id).hours_used + json.hours_used mc.users_credits.find_by(user_id: @reservation.statistic_profile.user_id).hours_used end end json.message @reservation.message diff --git a/db/migrate/20190521122429_create_invoicing_profiles.rb b/db/migrate/20190521122429_create_invoicing_profiles.rb index fd072e9b9..0d1c423b0 100644 --- a/db/migrate/20190521122429_create_invoicing_profiles.rb +++ b/db/migrate/20190521122429_create_invoicing_profiles.rb @@ -11,5 +11,8 @@ class CreateInvoicingProfiles < ActiveRecord::Migration add_reference :organizations, :invoicing_profile, index: true, foreign_key: true add_reference :invoices, :invoicing_profile, index: true, foreign_key: true + add_reference :wallets, :invoicing_profile, index: true, foreign_key: true + add_reference :wallet_transactions, :invoicing_profile, index: true, foreign_key: true + add_reference :history_values, :invoicing_profile, index: true, foreign_key: true end end diff --git a/db/migrate/20190603123916_migrate_wallet_to_invoicing_profile.rb b/db/migrate/20190523123916_migrate_wallet_to_invoicing_profile.rb similarity index 100% rename from db/migrate/20190603123916_migrate_wallet_to_invoicing_profile.rb rename to db/migrate/20190523123916_migrate_wallet_to_invoicing_profile.rb diff --git a/db/migrate/20190603140823_migrate_history_value_to_invoicing_profile.rb b/db/migrate/20190523140823_migrate_history_value_to_invoicing_profile.rb similarity index 100% rename from db/migrate/20190603140823_migrate_history_value_to_invoicing_profile.rb rename to db/migrate/20190523140823_migrate_history_value_to_invoicing_profile.rb diff --git a/db/migrate/20190528140012_remove_user_id_columns.rb b/db/migrate/20190528140012_remove_user_id_columns.rb new file mode 100644 index 000000000..84621123d --- /dev/null +++ b/db/migrate/20190528140012_remove_user_id_columns.rb @@ -0,0 +1,9 @@ +class RemoveUserIdColumns < ActiveRecord::Migration + def change + remove_column :invoices, :user_id, :integer + remove_reference :organizations, :profile, index: true, foreign_key: true + remove_reference :wallets, :user, index: true, foreign_key: true + remove_reference :wallet_transactions, :user, index: true, foreign_key: true + remove_reference :history_values, :user, index: true, foreign_key: true + end +end diff --git a/db/migrate/20190528140012_remove_user_id_from_invoice.rb b/db/migrate/20190528140012_remove_user_id_from_invoice.rb deleted file mode 100644 index 961ac3402..000000000 --- a/db/migrate/20190528140012_remove_user_id_from_invoice.rb +++ /dev/null @@ -1,5 +0,0 @@ -class RemoveUserIdFromInvoice < ActiveRecord::Migration - def change - remove_column :invoices, :user_id, :integer - end -end diff --git a/db/migrate/20190529120814_remove_profile_from_organization.rb b/db/migrate/20190529120814_remove_profile_from_organization.rb deleted file mode 100644 index 32c908c70..000000000 --- a/db/migrate/20190529120814_remove_profile_from_organization.rb +++ /dev/null @@ -1,5 +0,0 @@ -class RemoveProfileFromOrganization < ActiveRecord::Migration - def change - remove_reference :organizations, :profile, index: true, foreign_key: true - end -end diff --git a/db/migrate/20190603123754_add_invoicing_profile_to_wallet.rb b/db/migrate/20190603123754_add_invoicing_profile_to_wallet.rb deleted file mode 100644 index 5354ebf32..000000000 --- a/db/migrate/20190603123754_add_invoicing_profile_to_wallet.rb +++ /dev/null @@ -1,6 +0,0 @@ -class AddInvoicingProfileToWallet < ActiveRecord::Migration - def change - add_reference :wallets, :invoicing_profile, index: true, foreign_key: true - add_reference :wallet_transactions, :invoicing_profile, index: true, foreign_key: true - end -end diff --git a/db/migrate/20190603130038_remove_user_id_from_wallet.rb b/db/migrate/20190603130038_remove_user_id_from_wallet.rb deleted file mode 100644 index 0f8d60e4e..000000000 --- a/db/migrate/20190603130038_remove_user_id_from_wallet.rb +++ /dev/null @@ -1,6 +0,0 @@ -class RemoveUserIdFromWallet < ActiveRecord::Migration - def change - remove_reference :wallets, :user, index: true, foreign_key: true - remove_reference :wallet_transactions, :user, index: true, foreign_key: true - end -end diff --git a/db/migrate/20190603140740_add_invoicing_profile_to_history_value.rb b/db/migrate/20190603140740_add_invoicing_profile_to_history_value.rb deleted file mode 100644 index 6b5e004fc..000000000 --- a/db/migrate/20190603140740_add_invoicing_profile_to_history_value.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddInvoicingProfileToHistoryValue < ActiveRecord::Migration - def change - add_reference :history_values, :invoicing_profile, index: true, foreign_key: true - end -end diff --git a/db/migrate/20190603141109_remove_user_id_from_history_value.rb b/db/migrate/20190603141109_remove_user_id_from_history_value.rb deleted file mode 100644 index 685b884a0..000000000 --- a/db/migrate/20190603141109_remove_user_id_from_history_value.rb +++ /dev/null @@ -1,5 +0,0 @@ -class RemoveUserIdFromHistoryValue < ActiveRecord::Migration - def change - remove_reference :history_values, :user, index: true, foreign_key: true - end -end diff --git a/db/migrate/20190603150642_create_statistic_profile.rb b/db/migrate/20190603150642_create_statistic_profile.rb index 68106a700..5dce99692 100644 --- a/db/migrate/20190603150642_create_statistic_profile.rb +++ b/db/migrate/20190603150642_create_statistic_profile.rb @@ -6,5 +6,8 @@ class CreateStatisticProfile < ActiveRecord::Migration t.belongs_to :group, index: true, foreign_key: true t.belongs_to :user, index: true, foreign_key: true end + + add_reference :reservations, :statistic_profile, index: true, foreign_key: true + add_reference :subscriptions, :statistic_profile, index: true, foreign_key: true end end diff --git a/db/migrate/20190604064929_migrate_reservation_to_statistic_profile.rb b/db/migrate/20190604064929_migrate_reservation_to_statistic_profile.rb new file mode 100644 index 000000000..6a6439442 --- /dev/null +++ b/db/migrate/20190604064929_migrate_reservation_to_statistic_profile.rb @@ -0,0 +1,19 @@ +class MigrateReservationToStatisticProfile < ActiveRecord::Migration + def up + Reservation.all.each do |r| + user = User.find(r.user_id) + r.update_column( + 'statistic_profile_id', user.statistic_profile.id + ) + end + end + + def down + Reservation.all.each do |r| + statistic_profile = User.find(r.statistic_profile_id) + r.update_column( + 'user_id', statistic_profile.user_id + ) + end + end +end diff --git a/db/migrate/20190604065348_migrate_subscription_to_statistic_profile.rb b/db/migrate/20190604065348_migrate_subscription_to_statistic_profile.rb new file mode 100644 index 000000000..453898d56 --- /dev/null +++ b/db/migrate/20190604065348_migrate_subscription_to_statistic_profile.rb @@ -0,0 +1,19 @@ +class MigrateSubscriptionToStatisticProfile < ActiveRecord::Migration + def up + Subscription.all.each do |s| + user = User.find(s.user_id) + s.update_column( + 'statistic_profile_id', user.statistic_profile.id + ) + end + end + + def down + Subscription.all.each do |s| + statistic_profile = User.find(s.statistic_profile_id) + s.update_column( + 'user_id', statistic_profile.user_id + ) + end + end +end diff --git a/db/migrate/20190604075717_remove_gender_birthday_from_profile.rb b/db/migrate/20190604075717_remove_gender_birthday_from_profile.rb deleted file mode 100644 index 2e673b311..000000000 --- a/db/migrate/20190604075717_remove_gender_birthday_from_profile.rb +++ /dev/null @@ -1,6 +0,0 @@ -class RemoveGenderBirthdayFromProfile < ActiveRecord::Migration - def change - remove_column :profiles, :gender, :boolean - remove_column :profiles, :birthday, :date - end -end diff --git a/db/migrate/20190604075717_remove_statistic_columns.rb b/db/migrate/20190604075717_remove_statistic_columns.rb new file mode 100644 index 000000000..20c64033a --- /dev/null +++ b/db/migrate/20190604075717_remove_statistic_columns.rb @@ -0,0 +1,8 @@ +class RemoveStatisticColumns < ActiveRecord::Migration + def change + remove_column :profiles, :gender, :boolean + remove_column :profiles, :birthday, :date + remove_column :reservations, :user_id + remove_column :subscriptions, :user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index e6602899f..4e99a8f1a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,8 +15,8 @@ ActiveRecord::Schema.define(version: 20190604075717) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - enable_extension "unaccent" enable_extension "pg_trgm" + enable_extension "unaccent" create_table "abuses", force: :cascade do |t| t.integer "signaled_id" @@ -44,14 +44,14 @@ ActiveRecord::Schema.define(version: 20190604075717) do end create_table "addresses", force: :cascade do |t| - t.string "address" - t.string "street_number" - t.string "route" - t.string "locality" - t.string "country" - t.string "postal_code" + t.string "address", limit: 255 + t.string "street_number", limit: 255 + t.string "route", limit: 255 + t.string "locality", limit: 255 + t.string "country", limit: 255 + t.string "postal_code", limit: 255 t.integer "placeable_id" - t.string "placeable_type" + t.string "placeable_type", limit: 255 t.datetime "created_at" t.datetime "updated_at" end @@ -67,9 +67,9 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "assets", force: :cascade do |t| t.integer "viewable_id" - t.string "viewable_type" - t.string "attachment" - t.string "type" + t.string "viewable_type", limit: 255 + t.string "attachment", limit: 255 + t.string "type", limit: 255 t.datetime "created_at" t.datetime "updated_at" end @@ -86,12 +86,12 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "availabilities", force: :cascade do |t| t.datetime "start_at" t.datetime "end_at" - t.string "available_type" + t.string "available_type", limit: 255 t.datetime "created_at" t.datetime "updated_at" t.integer "nb_total_places" - t.boolean "destroying", default: false - t.boolean "lock", default: false + t.boolean "destroying", default: false + t.boolean "lock", default: false end create_table "availability_tags", force: :cascade do |t| @@ -105,7 +105,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "availability_tags", ["tag_id"], name: "index_availability_tags_on_tag_id", using: :btree create_table "categories", force: :cascade do |t| - t.string "name" + t.string "name", limit: 255 t.datetime "created_at" t.datetime "updated_at" t.string "slug" @@ -114,7 +114,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "categories", ["slug"], name: "index_categories_on_slug", unique: true, using: :btree create_table "components", force: :cascade do |t| - t.string "name", null: false + t.string "name", limit: 255, null: false end create_table "coupons", force: :cascade do |t| @@ -132,7 +132,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "credits", force: :cascade do |t| t.integer "creditable_id" - t.string "creditable_type" + t.string "creditable_type", limit: 255 t.integer "plan_id" t.integer "hours" t.datetime "created_at" @@ -173,7 +173,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "event_themes", ["slug"], name: "index_event_themes_on_slug", unique: true, using: :btree create_table "events", force: :cascade do |t| - t.string "title" + t.string "title", limit: 255 t.text "description" t.datetime "created_at" t.datetime "updated_at" @@ -211,10 +211,10 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "exports", ["user_id"], name: "index_exports_on_user_id", using: :btree create_table "friendly_id_slugs", force: :cascade do |t| - t.string "slug", null: false - t.integer "sluggable_id", null: false + t.string "slug", limit: 255, null: false + t.integer "sluggable_id", null: false t.string "sluggable_type", limit: 50 - t.string "scope" + t.string "scope", limit: 255 t.datetime "created_at" end @@ -224,10 +224,10 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree create_table "groups", force: :cascade do |t| - t.string "name" + t.string "name", limit: 255 t.datetime "created_at" t.datetime "updated_at" - t.string "slug" + t.string "slug", limit: 255 t.boolean "disabled" end @@ -247,7 +247,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "invoice_items", force: :cascade do |t| t.integer "invoice_id" - t.string "stp_invoice_item_id" + t.string "stp_invoice_item_id", limit: 255 t.integer "amount" t.datetime "created_at" t.datetime "updated_at" @@ -261,16 +261,16 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "invoices", force: :cascade do |t| t.integer "invoiced_id" - t.string "invoiced_type" - t.string "stp_invoice_id" + t.string "invoiced_type", limit: 255 + t.string "stp_invoice_id", limit: 255 t.integer "total" t.datetime "created_at" t.datetime "updated_at" - t.string "reference" - t.string "avoir_mode" + t.string "reference", limit: 255 + t.string "avoir_mode", limit: 255 t.datetime "avoir_date" t.integer "invoice_id" - t.string "type" + t.string "type", limit: 255 t.boolean "subscription_to_expire" t.text "description" t.integer "wallet_amount" @@ -299,17 +299,17 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "invoicing_profiles", ["user_id"], name: "index_invoicing_profiles_on_user_id", using: :btree create_table "licences", force: :cascade do |t| - t.string "name", null: false + t.string "name", limit: 255, null: false t.text "description" end create_table "machines", force: :cascade do |t| - t.string "name", null: false + t.string "name", limit: 255, null: false t.text "description" t.text "spec" t.datetime "created_at" t.datetime "updated_at" - t.string "slug" + t.string "slug", limit: 255 t.boolean "disabled" end @@ -326,14 +326,14 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "notifications", force: :cascade do |t| t.integer "receiver_id" t.integer "attached_object_id" - t.string "attached_object_type" + t.string "attached_object_type", limit: 255 t.integer "notification_type_id" - t.boolean "is_read", default: false + t.boolean "is_read", default: false t.datetime "created_at" t.datetime "updated_at" t.string "receiver_type" - t.boolean "is_send", default: false - t.jsonb "meta_data", default: {} + t.boolean "is_send", default: false + t.jsonb "meta_data", default: {} end add_index "notifications", ["notification_type_id"], name: "index_notifications_on_notification_type_id", using: :btree @@ -402,20 +402,20 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "organizations", ["invoicing_profile_id"], name: "index_organizations_on_invoicing_profile_id", using: :btree create_table "plans", force: :cascade do |t| - t.string "name" + t.string "name", limit: 255 t.integer "amount" - t.string "interval" + t.string "interval", limit: 255 t.integer "group_id" - t.string "stp_plan_id" + t.string "stp_plan_id", limit: 255 t.datetime "created_at" t.datetime "updated_at" - t.integer "training_credit_nb", default: 0 - t.boolean "is_rolling", default: true + t.integer "training_credit_nb", default: 0 + t.boolean "is_rolling", default: true t.text "description" t.string "type" t.string "base_name" - t.integer "ui_weight", default: 0 - t.integer "interval_count", default: 1 + t.integer "ui_weight", default: 0 + t.integer "interval_count", default: 1 t.string "slug" t.boolean "disabled" end @@ -445,9 +445,9 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "profiles", force: :cascade do |t| t.integer "user_id" - t.string "first_name" - t.string "last_name" - t.string "phone" + t.string "first_name", limit: 255 + t.string "last_name", limit: 255 + t.string "phone", limit: 255 t.text "interest" t.text "software_mastered" t.datetime "created_at" @@ -477,7 +477,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do t.integer "project_id" t.datetime "created_at" t.datetime "updated_at" - t.string "title" + t.string "title", limit: 255 t.integer "step_nb" end @@ -488,27 +488,27 @@ ActiveRecord::Schema.define(version: 20190604075717) do t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" - t.boolean "is_valid", default: false - t.string "valid_token" + t.boolean "is_valid", default: false + t.string "valid_token", limit: 255 end add_index "project_users", ["project_id"], name: "index_project_users_on_project_id", using: :btree add_index "project_users", ["user_id"], name: "index_project_users_on_user_id", using: :btree create_table "projects", force: :cascade do |t| - t.string "name" + t.string "name", limit: 255 t.text "description" t.datetime "created_at" t.datetime "updated_at" t.integer "author_id" t.text "tags" t.integer "licence_id" - t.string "state" - t.string "slug" + t.string "state", limit: 255 + t.string "slug", limit: 255 t.datetime "published_at" end - add_index "projects", ["slug"], name: "index_projects_on_slug", unique: true, using: :btree + add_index "projects", ["slug"], name: "index_projects_on_slug", using: :btree create_table "projects_components", force: :cascade do |t| t.integer "project_id" @@ -543,24 +543,24 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "projects_themes", ["theme_id"], name: "index_projects_themes_on_theme_id", using: :btree create_table "reservations", force: :cascade do |t| - t.integer "user_id" t.text "message" t.datetime "created_at" t.datetime "updated_at" t.integer "reservable_id" - t.string "reservable_type" - t.string "stp_invoice_id" + t.string "reservable_type", limit: 255 + t.string "stp_invoice_id", limit: 255 t.integer "nb_reserve_places" + t.integer "statistic_profile_id" end - add_index "reservations", ["reservable_type", "reservable_id"], name: "index_reservations_on_reservable_type_and_reservable_id", using: :btree + add_index "reservations", ["reservable_id", "reservable_type"], name: "index_reservations_on_reservable_id_and_reservable_type", using: :btree + add_index "reservations", ["statistic_profile_id"], name: "index_reservations_on_statistic_profile_id", using: :btree add_index "reservations", ["stp_invoice_id"], name: "index_reservations_on_stp_invoice_id", using: :btree - add_index "reservations", ["user_id"], name: "index_reservations_on_user_id", using: :btree create_table "roles", force: :cascade do |t| - t.string "name" + t.string "name", limit: 255 t.integer "resource_id" - t.string "resource_type" + t.string "resource_type", limit: 255 t.datetime "created_at" t.datetime "updated_at" end @@ -634,18 +634,18 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "statistic_fields", force: :cascade do |t| t.integer "statistic_index_id" - t.string "key" - t.string "label" + t.string "key", limit: 255 + t.string "label", limit: 255 t.datetime "created_at" t.datetime "updated_at" - t.string "data_type" + t.string "data_type", limit: 255 end add_index "statistic_fields", ["statistic_index_id"], name: "index_statistic_fields_on_statistic_index_id", using: :btree create_table "statistic_graphs", force: :cascade do |t| t.integer "statistic_index_id" - t.string "chart_type" + t.string "chart_type", limit: 255 t.integer "limit" t.datetime "created_at" t.datetime "updated_at" @@ -654,12 +654,12 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "statistic_graphs", ["statistic_index_id"], name: "index_statistic_graphs_on_statistic_index_id", using: :btree create_table "statistic_indices", force: :cascade do |t| - t.string "es_type_key" - t.string "label" + t.string "es_type_key", limit: 255 + t.string "label", limit: 255 t.datetime "created_at" t.datetime "updated_at" - t.boolean "table", default: true - t.boolean "ca", default: true + t.boolean "table", default: true + t.boolean "ca", default: true end create_table "statistic_profiles", force: :cascade do |t| @@ -673,8 +673,8 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "statistic_profiles", ["user_id"], name: "index_statistic_profiles_on_user_id", using: :btree create_table "statistic_sub_types", force: :cascade do |t| - t.string "key" - t.string "label" + t.string "key", limit: 255 + t.string "label", limit: 255 t.datetime "created_at" t.datetime "updated_at" end @@ -691,8 +691,8 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "statistic_types", force: :cascade do |t| t.integer "statistic_index_id" - t.string "key" - t.string "label" + t.string "key", limit: 255 + t.string "label", limit: 255 t.boolean "graph" t.datetime "created_at" t.datetime "updated_at" @@ -709,16 +709,16 @@ ActiveRecord::Schema.define(version: 20190604075717) do create_table "subscriptions", force: :cascade do |t| t.integer "plan_id" - t.integer "user_id" - t.string "stp_subscription_id" + t.string "stp_subscription_id", limit: 255 t.datetime "created_at" t.datetime "updated_at" t.datetime "expiration_date" t.datetime "canceled_at" + t.integer "statistic_profile_id" end add_index "subscriptions", ["plan_id"], name: "index_subscriptions_on_plan_id", using: :btree - add_index "subscriptions", ["user_id"], name: "index_subscriptions_on_user_id", using: :btree + add_index "subscriptions", ["statistic_profile_id"], name: "index_subscriptions_on_statistic_profile_id", using: :btree create_table "tags", force: :cascade do |t| t.string "name" @@ -729,7 +729,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree create_table "themes", force: :cascade do |t| - t.string "name", null: false + t.string "name", limit: 255, null: false end create_table "tickets", force: :cascade do |t| @@ -744,13 +744,13 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_index "tickets", ["reservation_id"], name: "index_tickets_on_reservation_id", using: :btree create_table "trainings", force: :cascade do |t| - t.string "name" + t.string "name", limit: 255 t.datetime "created_at" t.datetime "updated_at" t.integer "nb_total_places" - t.string "slug" + t.string "slug", limit: 255 t.text "description" - t.boolean "public_page", default: true + t.boolean "public_page", default: true t.boolean "disabled" end @@ -806,31 +806,31 @@ ActiveRecord::Schema.define(version: 20190604075717) do 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", default: "", null: false - t.string "encrypted_password", default: "", null: false - t.string "reset_password_token" + t.string "email", limit: 255, default: "", null: false + t.string "encrypted_password", limit: 255, default: "", null: false + t.string "reset_password_token", limit: 255 t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false + t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" - t.string "current_sign_in_ip" - t.string "last_sign_in_ip" - t.string "confirmation_token" + t.string "current_sign_in_ip", limit: 255 + t.string "last_sign_in_ip", limit: 255 + t.string "confirmation_token", limit: 255 t.datetime "confirmed_at" t.datetime "confirmation_sent_at" - t.string "unconfirmed_email" - t.integer "failed_attempts", default: 0, null: false - t.string "unlock_token" + t.string "unconfirmed_email", limit: 255 + t.integer "failed_attempts", default: 0, null: false + t.string "unlock_token", limit: 255 t.datetime "locked_at" t.datetime "created_at" t.datetime "updated_at" - t.boolean "is_allow_contact", default: true + t.boolean "is_allow_contact", default: true t.integer "group_id" - t.string "stp_customer_id" - t.string "username" - t.string "slug" - t.boolean "is_active", default: true + t.string "stp_customer_id", limit: 255 + t.string "username", limit: 255 + t.string "slug", limit: 255 + t.boolean "is_active", default: true t.string "provider" t.string "uid" t.string "auth_token" @@ -914,6 +914,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_foreign_key "prices", "plans" add_foreign_key "projects_spaces", "projects" add_foreign_key "projects_spaces", "spaces" + add_foreign_key "reservations", "statistic_profiles" add_foreign_key "slots_reservations", "reservations" add_foreign_key "slots_reservations", "slots" add_foreign_key "spaces_availabilities", "availabilities" @@ -921,6 +922,7 @@ ActiveRecord::Schema.define(version: 20190604075717) do add_foreign_key "statistic_custom_aggregations", "statistic_types" add_foreign_key "statistic_profiles", "groups" add_foreign_key "statistic_profiles", "users" + add_foreign_key "subscriptions", "statistic_profiles" add_foreign_key "tickets", "event_price_categories" add_foreign_key "tickets", "reservations" add_foreign_key "user_tags", "tags" diff --git a/test/fixtures/reservations.yml b/test/fixtures/reservations.yml index 662aff4a7..7d456a425 100644 --- a/test/fixtures/reservations.yml +++ b/test/fixtures/reservations.yml @@ -1,7 +1,7 @@ reservation_1: id: 1 - user_id: 7 + statistic_profile_id: 7 message: created_at: 2012-03-12 11:03:31.651441000 Z updated_at: 2012-03-12 11:03:31.651441000 Z @@ -12,7 +12,7 @@ reservation_1: reservation_2: id: 2 - user_id: 3 + statistic_profile_id: 3 message: created_at: 2015-06-10 11:20:01.341130000 Z updated_at: 2015-06-10 11:20:01.341130000 Z diff --git a/test/fixtures/subscriptions.yml b/test/fixtures/subscriptions.yml index 3c3b04e08..d5057c2f4 100644 --- a/test/fixtures/subscriptions.yml +++ b/test/fixtures/subscriptions.yml @@ -2,7 +2,7 @@ subscription_1: id: 1 plan_id: 2 - user_id: 3 + statistic_profile_id: 3 stp_subscription_id: sub_8DGB4ErIc2asOv created_at: <%= 10.days.ago.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> updated_at: <%= 10.days.ago.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> @@ -12,7 +12,7 @@ subscription_1: subscription_2: id: 2 plan_id: 3 - user_id: 4 + statistic_profile_id: 4 stp_subscription_id: created_at: <%= 10.days.ago.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> updated_at: <%= 10.days.ago.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> @@ -23,7 +23,7 @@ subscription_2: subscription_3: id: 3 plan_id: 1 - user_id: 7 + statistic_profile_id: 7 stp_subscription_id: created_at: 2012-03-12 11:03:31.651441000 Z updated_at: 2012-03-12 11:03:31.651441000 Z diff --git a/test/integration/subscriptions/create_as_admin_test.rb b/test/integration/subscriptions/create_as_admin_test.rb index 13e0f8057..15be278b4 100644 --- a/test/integration/subscriptions/create_as_admin_test.rb +++ b/test/integration/subscriptions/create_as_admin_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Subscriptions class CreateAsAdminTest < ActionDispatch::IntegrationTest @@ -7,11 +9,11 @@ module Subscriptions login_as(@admin, scope: :user) end - test "admin successfully takes a subscription for a user" do + test 'admin successfully takes a subscription for a user' do user = User.find_by(username: 'jdupond') plan = Plan.find_by(group_id: user.group.id, type: 'Plan', base_name: 'Mensuel') - VCR.use_cassette("subscriptions_admin_create_success") do + VCR.use_cassette('subscriptions_admin_create_success') do post '/api/subscriptions', { subscription: { diff --git a/test/services/subscription_extension_after_reservation_test.rb b/test/services/subscription_extension_after_reservation_test.rb index 345c132f0..25ec82036 100644 --- a/test/services/subscription_extension_after_reservation_test.rb +++ b/test/services/subscription_extension_after_reservation_test.rb @@ -8,14 +8,14 @@ class SubscriptionExtensionAfterReservationTest < ActiveSupport::TestCase @plan = Plan.find(3) @plan.update!(is_rolling: true) - @user = User.joins(:subscriptions).find_by(subscriptions: { plan: @plan }) + @user = User.joins(statistic_profile: [:subscriptions]).find_by(subscriptions: { plan_id: @plan.id }) @user.reservations.destroy_all # ensure no reservations @availability = @machine.availabilities.first slot = Slot.new(start_at: @availability.start_at, end_at: @availability.end_at, availability_id: @availability.id) - @reservation_machine = Reservation.new(user: @user, reservable: @machine, slots: [slot]) - @reservation_training = Reservation.new(user: @user, reservable: @training, slots: [slot]) + @reservation_machine = Reservation.new(statistic_profile: @user.statistic_profile, reservable: @machine, slots: [slot]) + @reservation_training = Reservation.new(statistic_profile: @user.statistic_profile, reservable: @training, slots: [slot]) @reservation_training.save! end diff --git a/test/services/users_credits_manager_test.rb b/test/services/users_credits_manager_test.rb index f8a3c72c0..f26ddca9d 100644 --- a/test/services/users_credits_manager_test.rb +++ b/test/services/users_credits_manager_test.rb @@ -5,11 +5,11 @@ class UsersCreditsManagerTest < ActiveSupport::TestCase @machine = Machine.find(6) @training = Training.find(2) @plan = Plan.find(3) - @user = User.joins(:subscriptions).find_by(subscriptions: { plan: @plan }) + @user = User.joins(statistic_profile: [:subscriptions]).find_by(subscriptions: { plan_id: @plan.id }) @user.users_credits.destroy_all @availability = @machine.availabilities.first - @reservation_machine = Reservation.new(user: @user, reservable: @machine) - @reservation_training = Reservation.new(user: @user, reservable: @training) + @reservation_machine = Reservation.new(statistic_profile: @user.statistic_profile, reservable: @machine) + @reservation_training = Reservation.new(statistic_profile: @user.statistic_profile, reservable: @training) end ## context machine reservation