Skip to content

Commit 4c6c69a

Browse files
authored
Local user and group info updated with TwoPercent (#519)
[SHD-1320](https://runway.powerhrg.com/backlog_items/SHD-1320) - Adds `Audiences::Group` to store group data - Watches for group related update/create/patch from TwoPercent and keeps the related `Audiences::Group` updated - Watches for user related update/create/patch from TwoPercent and keeps the related `Audiences::ExternalUser` updated -
1 parent 2cb73c7 commit 4c6c69a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1005
-52
lines changed

audiences/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ gem "webmock", "~> 3.18"
2525
gem "mysql2", "~> 0.5.6"
2626
gem "pg", "~> 1.0"
2727
gem "puma", "~> 6.3"
28+
gem "two_percent", "~> 0.4.0"

audiences/Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PATH
22
remote: .
33
specs:
44
audiences (1.6.1)
5+
aether_observatory (~> 1.0)
56
rails (>= 6.0)
67

78
GEM
@@ -68,6 +69,9 @@ GEM
6869
zeitwerk (~> 2.3)
6970
addressable (2.8.7)
7071
public_suffix (>= 2.0.2, < 7.0)
72+
aether_observatory (1.0.0)
73+
activemodel (>= 6.0.6.1)
74+
activesupport (>= 6.0.6.1)
7175
appraisal (2.5.0)
7276
bundler
7377
rake
@@ -265,6 +269,9 @@ GEM
265269
thor (1.3.2)
266270
timeout (0.4.3)
267271
tomlrb (2.0.3)
272+
two_percent (0.4.0)
273+
aether_observatory (>= 1.0)
274+
rails (>= 6.1)
268275
tzinfo (2.0.6)
269276
concurrent-ruby (~> 1.0)
270277
unicode-display_width (2.6.0)
@@ -314,6 +321,7 @@ DEPENDENCIES
314321
rubocop (>= 1.32)
315322
rubocop-powerhome
316323
shoulda-matchers (~> 5.0)
324+
two_percent (~> 0.4.0)
317325
vite_rails (~> 3.0)
318326
webmock (~> 3.18)
319327

audiences/app/models/audiences/external_user.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
module Audiences
44
class ExternalUser < ApplicationRecord
5+
has_many :group_memberships, dependent: :destroy
6+
has_many :groups, through: :group_memberships
7+
58
if Audiences.config.identity_class
69
belongs_to :identity, class_name: Audiences.config.identity_class, # rubocop:disable Rails/ReflectionClassName
710
primary_key: Audiences.config.identity_key,
@@ -10,6 +13,12 @@ class ExternalUser < ApplicationRecord
1013
inverse_of: false
1114
end
1215

16+
def picture_urls = [picture_url]
17+
18+
def picture_urls=(urls)
19+
self.picture_url = urls&.first
20+
end
21+
1322
def self.fetch(external_ids, count: 100)
1423
return [] unless external_ids.any?
1524

@@ -23,11 +32,12 @@ def self.wrap(resources)
2332
return [] unless resources&.any?
2433

2534
attrs = resources.map do |data|
26-
{ user_id: data["externalId"], data: data, created_at: Time.current, updated_at: Time.current }
35+
{ scim_id: data["id"], user_id: data["externalId"], data: data, created_at: Time.current,
36+
updated_at: Time.current }
2737
end
28-
unique_by = :user_id if connection.supports_insert_conflict_target?
38+
unique_by = :scim_id if connection.supports_insert_conflict_target?
2939
upsert_all(attrs, unique_by: unique_by) # rubocop:disable Rails/SkipsModelValidations
30-
where(user_id: attrs.pluck(:user_id))
40+
where(scim_id: attrs.pluck(:scim_id))
3141
end
3242

3343
def as_json(...)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module Audiences
4+
class Group < ApplicationRecord
5+
has_many :group_memberships, dependent: :destroy
6+
has_many :external_users, through: :group_memberships
7+
8+
validates :display_name, presence: true
9+
validates :external_id, presence: true
10+
validates :scim_id, presence: true
11+
end
12+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module Audiences
4+
class GroupMembership < ApplicationRecord
5+
belongs_to :external_user
6+
belongs_to :group
7+
end
8+
end

audiences/audiences.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
2121
end
2222

2323
spec.required_ruby_version = ">= 2.7"
24+
spec.add_dependency "aether_observatory", "~> 1.0"
2425
spec.add_dependency "rails", ">= 6.0"
2526
spec.metadata["rubygems_mfa_required"] = "true"
2627
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
class CreateAudiencesGroups < ActiveRecord::Migration[6.1]
4+
def change
5+
create_table :audiences_groups do |t|
6+
t.string :external_id
7+
t.string :scim_id
8+
t.string :display_name
9+
t.string :resource_type
10+
11+
t.timestamps
12+
13+
t.index %i[resource_type external_id], unique: true
14+
t.index %i[resource_type scim_id], unique: true
15+
end
16+
end
17+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
class AddScimIdToAudiencesExternalUsers < ActiveRecord::Migration[6.1]
4+
def change
5+
add_column :audiences_external_users, :scim_id, :string
6+
add_index :audiences_external_users, :scim_id, unique: true
7+
end
8+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class CreateAudiencesGroupMemberships < ActiveRecord::Migration[6.1]
4+
def change
5+
create_table :audiences_group_memberships do |t|
6+
t.belongs_to :external_user, null: false, foreign_key: false, table: :audiences_external_users
7+
t.belongs_to :group, null: false, foreign_key: false, table: :audiences_external_users
8+
9+
t.timestamps
10+
end
11+
end
12+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class PopulateExternalUsersScimId < ActiveRecord::Migration[6.1]
4+
def down; end
5+
6+
def up
7+
Audiences::ExternalUser.find_each do |user|
8+
user.update!(
9+
scim_id: user.data&.fetch("id", nil),
10+
display_name: user.data&.fetch("displayName", nil),
11+
picture_urls: user.data&.fetch("photos", [])&.pluck("value")
12+
)
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)