diff --git a/bullet_train/app/models/concerns/memberships/base.rb b/bullet_train/app/models/concerns/memberships/base.rb index 8b30ac741..33751aab8 100644 --- a/bullet_train/app/models/concerns/memberships/base.rb +++ b/bullet_train/app/models/concerns/memberships/base.rb @@ -32,6 +32,8 @@ module Memberships::Base after_commit :publish_changed_quantity + after_create :set_team_time_zone, if: :is_first_membership? + after_validation :remove_user_profile_photo, if: :user_profile_photo_removal? scope :excluding_platform_agents, -> { where(platform_agent_of: nil) } @@ -157,5 +159,13 @@ def publish_changed_quantity ActiveSupport::Notifications.instrument("memberships.quantity-changed", {team:}) end + def set_team_time_zone + team.set_time_zone_from_user(user) + end + + def is_first_membership? + team.memberships.count == 1 && team.memberships.first.id == id + end + ActiveSupport.run_load_hooks :bullet_train_memberships_base, self end diff --git a/bullet_train/app/models/concerns/teams/base.rb b/bullet_train/app/models/concerns/teams/base.rb index dcd077a71..fa4410a6a 100644 --- a/bullet_train/app/models/concerns/teams/base.rb +++ b/bullet_train/app/models/concerns/teams/base.rb @@ -30,6 +30,11 @@ def initialize(attributes = nil) self.time_zone = "UTC" if time_zone.blank? end + def set_time_zone_from_user(user) + self.time_zone = user.time_zone if user.time_zone.present? + save + end + def platform_agent_access_tokens Platform::AccessToken.joins(:application).where(resource_owner_id: users.where.not(platform_agent_of_id: nil), application: {team: nil}) end diff --git a/bullet_train/test/models/team_test.rb b/bullet_train/test/models/team_test.rb index 3058c3721..d8ace514c 100644 --- a/bullet_train/test/models/team_test.rb +++ b/bullet_train/test/models/team_test.rb @@ -6,6 +6,14 @@ class TeamTest < ActiveSupport::TestCase assert_equal "UTC", team.time_zone end + test "explicitly set time_zone is not clobbered by first user" do + team = Team.create!(name: "new test team", time_zone: "Eastern Time (US & Canada)") + user = User.create!(email: "test@test.com", password: "password", password_confirmation: "password", time_zone: "Central Time (US & Canada)") + Membership.create!(team: team, user: user) + team.reload + assert_equal "Eastern Time (US & Canada)", team.time_zone + end + test "a new team gets the time_zone of the first user when they join" do team = Team.create!(name: "new test team") user = User.create!(email: "test@test.com", password: "password", password_confirmation: "password", time_zone: "Central Time (US & Canada)") @@ -13,4 +21,26 @@ class TeamTest < ActiveSupport::TestCase team.reload assert_equal "Central Time (US & Canada)", team.time_zone end + + test "default UTC time_zone is not clobbered if first user doesn't have a time zone set" do + team = Team.create!(name: "new test team") + user = User.create!(email: "test@test.com", password: "password", password_confirmation: "password", time_zone: nil) + Membership.create!(team: team, user: user) + team.reload + assert_equal "UTC", team.time_zone + end + + test "default UTC time_zone is overwritten once the first user sets a time zone" do + team = Team.create!(name: "new test team") + user = User.create!(email: "test@test.com", password: "password", password_confirmation: "password", time_zone: nil) + Membership.create!(team: team, user: user) + team.reload + assert_equal "UTC", team.time_zone + + user.time_zone = "Central Time (US & Canada)" + user.save + + team.reload + assert_equal "Central Time (US & Canada)", team.time_zone + end end