diff --git a/app/models/affiliate.rb b/app/models/affiliate.rb index db8561c..b6af692 100644 --- a/app/models/affiliate.rb +++ b/app/models/affiliate.rb @@ -6,6 +6,7 @@ class Affiliate < ApplicationRecord EXPOSED_ATTRIBUTES = %i[name logo_link logo_url] has_one_attached :logo + has_many :owners validates :logo, blob: { content_type: ['image/png', 'image/jpg', 'image/jpeg'] } diff --git a/app/models/owner.rb b/app/models/owner.rb index e2db322..e78a839 100644 --- a/app/models/owner.rb +++ b/app/models/owner.rb @@ -9,22 +9,23 @@ class Owner < ApplicationRecord :confirmable, :recoverable, jwt_revocation_strategy: JwtDenylist validates :email, uniqueness: true, presence: true - validates :password, - presence: true, - length: { in: Devise.password_length }, - confirmation: true, - on: :create - - validates :password, - allow_nil: true, - length: { in: Devise.password_length }, - confirmation: true, + validates :password, + presence: true, + length: { in: Devise.password_length }, + confirmation: true, + on: :create + + validates :password, + allow_nil: true, + length: { in: Devise.password_length }, + confirmation: true, on: :update scope :affiliate, -> { where.not(affiliate: [nil, '']).order(affiliate: :asc) } scope :with_stripe_data, -> { where.not(stripe_subscription_id: nil) } belongs_to :frontend + belongs_to :affiliate, optional: true has_many :companies, dependent: :destroy has_many :areas, through: :companies @@ -50,8 +51,7 @@ def blocked? def affiliate_logo return unless affiliate - - Affiliate.find_by(code: affiliate)&.logo_url + affiliate.logo_url end def frontend_url @@ -74,8 +74,7 @@ def stripe_price_id main_price_id = ENV['STRIPE_SUBSCRIPTION_PRICE_ID'] raise "ENV['STRIPE_SUBSCRIPTION_PRICE_ID'] is empty" unless main_price_id.present? - - Affiliate.find_by(code: affiliate)&.stripe_price_id_monthly || main_price_id + affiliate.stripe_price_id_monthly || main_price_id end def stripe_subscription @@ -106,4 +105,9 @@ def trial_end # There is not trial if the trial is blank or has already been passed, else it has to be at least two days in the future trial_ends_at? && trial_ends_at.future? ? [trial_ends_at, 50.hours.from_now].max.to_i : nil end + + def self.non_associated_affiliates + # To identify owners that have used an affiliate thats not properly registered in the affiliates table. + Owner.where(affiliate: nil).where.not(affiliate_code: nil) + end end diff --git a/db/migrate/20210514123815_change_affiliate_column_name.rb b/db/migrate/20210514123815_change_affiliate_column_name.rb new file mode 100644 index 0000000..6611b94 --- /dev/null +++ b/db/migrate/20210514123815_change_affiliate_column_name.rb @@ -0,0 +1,5 @@ +class ChangeAffiliateColumnName < ActiveRecord::Migration[6.1] + def change + rename_column :owners, :affiliate, :affiliate_code + end +end diff --git a/db/migrate/20210514152111_add_affiliate_ref_to_owners.rb b/db/migrate/20210514152111_add_affiliate_ref_to_owners.rb new file mode 100644 index 0000000..e82c233 --- /dev/null +++ b/db/migrate/20210514152111_add_affiliate_ref_to_owners.rb @@ -0,0 +1,5 @@ +class AddAffiliateRefToOwners < ActiveRecord::Migration[6.1] + def change + add_reference :owners, :affiliate, foreign_key: true, type: :uuid + end +end diff --git a/db/schema.rb b/db/schema.rb index 3c47b60..42159ab 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_05_05_084831) do +ActiveRecord::Schema.define(version: 2021_05_14_152111) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -116,7 +116,7 @@ t.string "confirmation_token" t.datetime "confirmed_at" t.datetime "confirmation_sent_at" - t.string "affiliate" + t.string "affiliate_code" t.string "stripe_customer_id" t.string "stripe_subscription_id" t.datetime "trial_ends_at" @@ -131,6 +131,8 @@ t.string "street" t.string "zip" t.string "city" + t.uuid "affiliate_id" + t.index ["affiliate_id"], name: "index_owners_on_affiliate_id" t.index ["confirmation_token"], name: "index_owners_on_confirmation_token", unique: true t.index ["email"], name: "index_owners_on_email", unique: true t.index ["frontend_id"], name: "index_owners_on_frontend_id" @@ -165,6 +167,7 @@ add_foreign_key "areas", "companies" add_foreign_key "companies", "owners" add_foreign_key "data_requests", "companies" + add_foreign_key "owners", "affiliates" add_foreign_key "owners", "frontends" add_foreign_key "tickets", "areas" end diff --git a/db/scripts/join_affiliate_to_owner.rb b/db/scripts/join_affiliate_to_owner.rb new file mode 100644 index 0000000..c1fc91f --- /dev/null +++ b/db/scripts/join_affiliate_to_owner.rb @@ -0,0 +1,9 @@ +owners_with_affiliate = Owner.all.select { |owner| owner.affiliate_code } + +owners_with_affiliate.each do |owner| + matching_affiliate = Affiliate.find_by(code: owner.affiliate_code) + if matching_affiliate + owner.affiliate = matching_affiliate + owner.save! + end +end diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2a..9ad7e4b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,27 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) -# Character.create(name: 'Luke', movie: movies.first) +Affiliate.destroy_all +Owner.destroy_all + +7.times do + FactoryBot.create(:affiliate) +end +affiliates = Affiliate.all +affiliates.each { |affiliate| puts "#{affiliate.name} #{affiliate.code}" } + +20.times do + FactoryBot.create(:owner) +end +owners = Owner.all +owners.each { |owner| puts owner.name } + + +# give 9 owners an affilate +owners_with_affiliates = owners[0..8] +owners_with_affiliates.each do |owner| + owner.update!(affiliate_code: affiliates.sample.code) +end + +# give 3 owners affiliates which are not yet entities in the db +owners_with_other_affiliates = owners[9..11] +owners_with_other_affiliates.each do |owner| + owner.update!(affiliate_code: affiliates.sample.code.prepend('a').chop) +end