From 9026ae793213a91e4437914a741f042034bb98b9 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Wed, 12 May 2021 17:39:15 +0200 Subject: [PATCH 01/12] remove comment, test on docker db --- db/seeds.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2a..8b13789 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1 @@ -# 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) + From e156c5b91997b23c8dc09f7d0522550c711eac64 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Thu, 13 May 2021 20:12:47 +0200 Subject: [PATCH 02/12] seed db with 20 owners and 7 affiliates --- db/seeds.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 8b13789..14c94c8 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1 +1,27 @@ +Affiliate.destroy_all +Owner.destroy_all +7.times do + FactoryBot.create(:affiliate) +end +affiliates = Affiliate.all +affiliates.each { |a| puts "#{a.name} #{a.code}" } + +20.times do + FactoryBot.create(:owner) +end +owners = Owner.all +owners.each { |o| puts o.name } + + +# give 9 owners an affilate +owners_with_affiliates = owners[0..8] +owners_with_affiliates.each do |owner| + owner.update!(affiliate: 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: affiliates.sample.code.prepend('a').chop) +end From 5dc53eb79fc425e97a30c52254c3f521121ee784 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Fri, 14 May 2021 14:40:41 +0200 Subject: [PATCH 03/12] generate migration to change column name from affiliate to affiliate_code in owners table --- db/migrate/20210514123815_change_affiliate_column_name.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20210514123815_change_affiliate_column_name.rb 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 From c43834395ab7bcdf10007b83fc354137190ddd77 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Fri, 14 May 2021 15:07:15 +0200 Subject: [PATCH 04/12] run migration successful - affiliate is now affiliate_code --- db/schema.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 3c47b60..2dc4103 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_123815) 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" From 68ba3c9876509c00cf495f9fd733353996ccc393 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Fri, 14 May 2021 15:41:07 +0200 Subject: [PATCH 05/12] generate migration to add affiliate:reference on owners table --- db/migrate/20210514131228_add_affiliate_ref_to_owners.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20210514131228_add_affiliate_ref_to_owners.rb diff --git a/db/migrate/20210514131228_add_affiliate_ref_to_owners.rb b/db/migrate/20210514131228_add_affiliate_ref_to_owners.rb new file mode 100644 index 0000000..4a4f562 --- /dev/null +++ b/db/migrate/20210514131228_add_affiliate_ref_to_owners.rb @@ -0,0 +1,5 @@ +class AddAffiliateRefToOwners < ActiveRecord::Migration[6.1] + def change + add_reference :owners, :affiliates, foreign_key: true + end +end From fde8f2cef3e16076bbd88648db5e166aa1af60f1 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Fri, 14 May 2021 17:31:18 +0200 Subject: [PATCH 06/12] run migration to add affiliate foreign key to owners table --- ...s.rb => 20210514152111_add_affiliate_ref_to_owners.rb} | 2 +- db/schema.rb | 5 ++++- db/seeds.rb | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) rename db/migrate/{20210514131228_add_affiliate_ref_to_owners.rb => 20210514152111_add_affiliate_ref_to_owners.rb} (54%) diff --git a/db/migrate/20210514131228_add_affiliate_ref_to_owners.rb b/db/migrate/20210514152111_add_affiliate_ref_to_owners.rb similarity index 54% rename from db/migrate/20210514131228_add_affiliate_ref_to_owners.rb rename to db/migrate/20210514152111_add_affiliate_ref_to_owners.rb index 4a4f562..e82c233 100644 --- a/db/migrate/20210514131228_add_affiliate_ref_to_owners.rb +++ b/db/migrate/20210514152111_add_affiliate_ref_to_owners.rb @@ -1,5 +1,5 @@ class AddAffiliateRefToOwners < ActiveRecord::Migration[6.1] def change - add_reference :owners, :affiliates, foreign_key: true + add_reference :owners, :affiliate, foreign_key: true, type: :uuid end end diff --git a/db/schema.rb b/db/schema.rb index 2dc4103..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_14_123815) 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" @@ -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/seeds.rb b/db/seeds.rb index 14c94c8..9ad7e4b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,23 +5,23 @@ FactoryBot.create(:affiliate) end affiliates = Affiliate.all -affiliates.each { |a| puts "#{a.name} #{a.code}" } +affiliates.each { |affiliate| puts "#{affiliate.name} #{affiliate.code}" } 20.times do FactoryBot.create(:owner) end owners = Owner.all -owners.each { |o| puts o.name } +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: affiliates.sample.code) + 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: affiliates.sample.code.prepend('a').chop) + owner.update!(affiliate_code: affiliates.sample.code.prepend('a').chop) end From 05257c46ea82481a3e1b3297bf702e0d27f66fdc Mon Sep 17 00:00:00 2001 From: peter huyser Date: Fri, 14 May 2021 17:32:59 +0200 Subject: [PATCH 07/12] update associations in affiliate and owner models --- app/models/affiliate.rb | 1 + app/models/owner.rb | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) 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..955cac2 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 :affilate has_many :companies, dependent: :destroy has_many :areas, through: :companies @@ -50,7 +51,7 @@ def blocked? def affiliate_logo return unless affiliate - + Affiliate.find_by(code: affiliate)&.logo_url end From 6e2f677401c940499137d7944d95498f175fed9e Mon Sep 17 00:00:00 2001 From: peter huyser Date: Fri, 14 May 2021 17:58:13 +0200 Subject: [PATCH 08/12] fix typo in owner model - owner can be assigned an affiliate instance! --- app/models/owner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/owner.rb b/app/models/owner.rb index 955cac2..512a4a8 100644 --- a/app/models/owner.rb +++ b/app/models/owner.rb @@ -25,7 +25,7 @@ class Owner < ApplicationRecord scope :with_stripe_data, -> { where.not(stripe_subscription_id: nil) } belongs_to :frontend - belongs_to :affilate + belongs_to :affiliate has_many :companies, dependent: :destroy has_many :areas, through: :companies From 8e5a51cf54a26a2627d901d65bcc83e091fddbf1 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Sat, 15 May 2021 10:42:14 +0200 Subject: [PATCH 09/12] run script to join owners table with correct affiliate based on matching affiliate code --- db/scripts/join_affiliate_to_owner.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 db/scripts/join_affiliate_to_owner.rb diff --git a/db/scripts/join_affiliate_to_owner.rb b/db/scripts/join_affiliate_to_owner.rb new file mode 100644 index 0000000..083fb85 --- /dev/null +++ b/db/scripts/join_affiliate_to_owner.rb @@ -0,0 +1,7 @@ +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) + owner.affiliate = matching_affiliate if matching_affiliate + owner.save! +end From cb7509cd8bbe7bd6d498c84f840499c5bb0523c3 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Sat, 15 May 2021 12:03:47 +0200 Subject: [PATCH 10/12] update owners association optional: true --- app/models/owner.rb | 2 +- db/scripts/join_affiliate_to_owner.rb | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/owner.rb b/app/models/owner.rb index 512a4a8..0d60540 100644 --- a/app/models/owner.rb +++ b/app/models/owner.rb @@ -25,7 +25,7 @@ class Owner < ApplicationRecord scope :with_stripe_data, -> { where.not(stripe_subscription_id: nil) } belongs_to :frontend - belongs_to :affiliate + belongs_to :affiliate, optional: true has_many :companies, dependent: :destroy has_many :areas, through: :companies diff --git a/db/scripts/join_affiliate_to_owner.rb b/db/scripts/join_affiliate_to_owner.rb index 083fb85..c1fc91f 100644 --- a/db/scripts/join_affiliate_to_owner.rb +++ b/db/scripts/join_affiliate_to_owner.rb @@ -2,6 +2,8 @@ owners_with_affiliate.each do |owner| matching_affiliate = Affiliate.find_by(code: owner.affiliate_code) - owner.affiliate = matching_affiliate if matching_affiliate - owner.save! + if matching_affiliate + owner.affiliate = matching_affiliate + owner.save! + end end From fdcfc2a3c688408f4f0874652824ab8fcecf64c8 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Sun, 16 May 2021 09:57:53 +0200 Subject: [PATCH 11/12] update owner model to accomodate new affiliate association --- app/models/owner.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/models/owner.rb b/app/models/owner.rb index 0d60540..d4ece6f 100644 --- a/app/models/owner.rb +++ b/app/models/owner.rb @@ -51,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 @@ -75,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 From 62ce88333f906f81a246ef59563d5c746aafae01 Mon Sep 17 00:00:00 2001 From: peter huyser Date: Sun, 16 May 2021 11:17:26 +0200 Subject: [PATCH 12/12] add class method to owner.rb identifing owners with affiliate_codes with no corresponding affiliate --- app/models/owner.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/owner.rb b/app/models/owner.rb index d4ece6f..e78a839 100644 --- a/app/models/owner.rb +++ b/app/models/owner.rb @@ -105,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