Skip to content

Commit

Permalink
refactor: redesign badges using polymorphic association
Browse files Browse the repository at this point in the history
  • Loading branch information
juliano-quatrin-nunes committed Jan 10, 2025
1 parent 819d7fb commit b8dc871
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 64 deletions.
3 changes: 1 addition & 2 deletions apps/govquests-api/govquests/questing/lib/questing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def call(event_store, command_bus)

class CommandHandler < Infra::CommandHandlerRegistry
handle "Questing::CreateQuest", aggregate: Quest do |quest, cmd|
quest.create(cmd.display_data, cmd.audience, cmd.badge_id, cmd.badge_display_data)
quest.create(cmd.display_data, cmd.audience, cmd.badge_display_data)
end

handle "Questing::AssociateActionWithQuest", aggregate: Quest do |quest, cmd|
Expand Down Expand Up @@ -56,7 +56,6 @@ class CommandHandler < Infra::CommandHandlerRegistry
track.create(
display_data: cmd.display_data,
quest_ids: cmd.quest_ids,
badge_id: cmd.badge_id,
badge_display_data: cmd.badge_display_data
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class CreateQuest < Infra::Command
attribute :quest_id, Infra::Types::UUID
attribute :display_data, Infra::Types::Hash
attribute :audience, Infra::Types::String
attribute :badge_id, Infra::Types::UUID
attribute :badge_display_data, Infra::Types::Hash

alias_method :aggregate_id, :quest_id
Expand Down Expand Up @@ -54,7 +53,6 @@ class CreateTrack < Infra::Command
attribute :track_id, Infra::Types::UUID
attribute :display_data, Infra::Types::Hash
attribute :quest_ids, Infra::Types::Array
attribute :badge_id, Infra::Types::UUID
attribute :badge_display_data, Infra::Types::Hash

alias_method :aggregate_id, :track_id
Expand Down
2 changes: 0 additions & 2 deletions apps/govquests-api/govquests/questing/lib/questing/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class QuestCreated < Infra::Event
attribute :quest_id, Infra::Types::UUID
attribute :display_data, Infra::Types::Hash
attribute :audience, Infra::Types::String
attribute :badge_id, Infra::Types::UUID
attribute :badge_display_data, Infra::Types::Hash
end

Expand Down Expand Up @@ -42,7 +41,6 @@ class TrackCreated < Infra::Event
attribute :track_id, Infra::Types::UUID
attribute :display_data, Infra::Types::Hash
attribute :quest_ids, Infra::Types::Array
attribute :badge_id, Infra::Types::UUID
attribute :badge_display_data, Infra::Types::Hash
end
end
3 changes: 1 addition & 2 deletions apps/govquests-api/govquests/questing/lib/questing/quest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ def initialize(id)
@reward_pools = {}
end

def create(display_data, audience, badge_id, badge_display_data)
def create(display_data, audience, badge_display_data)
display_data ||= {}

apply QuestCreated.new(data: {
quest_id: @id,
display_data: display_data,
audience: audience,
badge_id: badge_id,
badge_display_data: badge_display_data
})
end
Expand Down
7 changes: 2 additions & 5 deletions apps/govquests-api/govquests/questing/lib/questing/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ def initialize(id)
@id = id
@quests = []
@display_data = nil
@badge_id = nil
end

def create(display_data:, quest_ids:, badge_id:, badge_display_data:)
def create(display_data:, quest_ids:, badge_display_data:)
raise AlreadyExists if @display_data

apply TrackCreated.new(
data: {
track_id: @id,
display_data: display_data,
quest_ids: quest_ids,
badge_id: badge_id,
badge_display_data: badge_display_data
}
)
Expand All @@ -29,8 +27,7 @@ def create(display_data:, quest_ids:, badge_id:, badge_display_data:)
on TrackCreated do |event|
@display_data = event.data[:display_data]
@quest_ids = event.data[:quest_ids]
@badge_id = event.data[:badge_id]
@display_data = event.data[:badge_display_data]
@badge_display_data = event.data[:badge_display_data]
end
end
end
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
module Questing
class OnQuestCreated
def call(event)
Rewarding::BadgeReadModel.create!(
badge_id: event.data[:badge_id],
display_data: event.data[:badge_display_data]
)

slug = event.data[:display_data][:title].downcase.tr(" ", "-")

quest = QuestReadModel.create!(
quest_id: event.data[:quest_id],
badge_id: event.data[:badge_id],
slug: slug,
audience: event.data[:audience],
status: "created",
display_data: event.data[:display_data]
)

badge_id = SecureRandom.uuid
Rewarding::BadgeReadModel.create!(
badge_id: badge_id,
display_data: event.data[:badge_display_data],
badgeable: quest
)
Rails.logger.info "Quest created in read model: #{quest.quest_id}"
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module Questing
class OnTrackCreated
def call(event)
Rewarding::BadgeReadModel.create!(
badge_id: event.data[:badge_id],
display_data: event.data[:badge_display_data]
)
TrackReadModel.create!(
track = TrackReadModel.create!(
track_id: event.data[:track_id],
display_data: event.data[:display_data],
quest_ids: event.data[:quest_ids],
badge_id: event.data[:badge_id]
quest_ids: event.data[:quest_ids]
)

badge_id = SecureRandom.uuid
Rewarding::BadgeReadModel.create!(
badge_id: badge_id,
display_data: event.data[:badge_display_data],
badgeable: track
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ class QuestReadModel < ApplicationRecord
validates :status, presence: true
validates :display_data, presence: true

belongs_to :badge,
has_one :badge,
class_name: "Rewarding::BadgeReadModel",
foreign_key: "badge_id",
primary_key: "badge_id",
optional: true
as: :badgeable
end
end

Expand All @@ -32,11 +30,9 @@ class QuestReadModel < ApplicationRecord
# status :string not null
# created_at :datetime not null
# updated_at :datetime not null
# badge_id :string
# quest_id :string not null
#
# Indexes
#
# index_quests_on_badge_id (badge_id)
# index_quests_on_quest_id (quest_id) UNIQUE
#
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ class TrackReadModel < ApplicationRecord
self.table_name = "tracks"

attribute :quest_ids, :jsonb, array: true
attribute :badge_id, :string

validates :track_id, presence: true, uniqueness: true
validates :display_data, presence: true
validates :badge_id, presence: true, allow_nil: true

def quests
QuestReadModel.where(quest_id: quest_ids)
end

belongs_to :badge,
has_one :badge,
class_name: "Rewarding::BadgeReadModel",
foreign_key: "badge_id",
primary_key: "badge_id",
optional: true
as: :badgeable
end
end

Expand All @@ -30,11 +26,9 @@ def quests
# quest_ids :jsonb is an Array
# created_at :datetime not null
# updated_at :datetime not null
# badge_id :string
# track_id :string not null
#
# Indexes
#
# index_tracks_on_badge_id (badge_id)
# index_tracks_on_track_id (track_id) UNIQUE
#
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ module Rewarding
class BadgeReadModel < ApplicationRecord
self.table_name = "badges"

has_many :tracks,
class_name: "Tracking::TrackReadModel",
foreign_key: "badge_id",
primary_key: "badge_id"

has_many :quests,
class_name: "Questing::QuestReadModel",
foreign_key: "badge_id",
primary_key: "badge_id"
belongs_to :badgeable, polymorphic: true

validates :badge_id, presence: true, uniqueness: true
validates :display_data, presence: true
Expand All @@ -21,13 +13,16 @@ class BadgeReadModel < ApplicationRecord
#
# Table name: badges
#
# id :bigint not null, primary key
# display_data :jsonb not null
# created_at :datetime not null
# updated_at :datetime not null
# badge_id :string not null
# id :bigint not null, primary key
# badgeable_type :string not null
# display_data :jsonb not null
# created_at :datetime not null
# updated_at :datetime not null
# badge_id :string not null
# badgeable_id :string not null
#
# Indexes
#
# index_badges_on_badge_id (badge_id) UNIQUE
# index_badges_on_badge_id (badge_id) UNIQUE
# index_badges_on_badgeable_type_and_badgeable_id (badgeable_type,badgeable_id) UNIQUE
#
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UpdateBadgesTableWithPolymorphic < ActiveRecord::Migration[8.1]
def change
remove_column :badges, :track_id if column_exists?(:badges, :track_id)
remove_column :badges, :quest_id if column_exists?(:badges, :quest_id)

add_column :badges, :badgeable_type, :string, null: false
add_column :badges, :badgeable_id, :string, null: false

add_index :badges, [:badgeable_type, :badgeable_id], unique: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RemoveBadgeIdFromTracksAndQuests < ActiveRecord::Migration[8.1]
def change
remove_index :tracks, :badge_id
remove_index :quests, :badge_id

remove_column :tracks, :badge_id
remove_column :quests, :badge_id
end
end
9 changes: 4 additions & 5 deletions apps/govquests-api/rails_app/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions apps/govquests-api/rails_app/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ def self.create_action(action_data)
module QuestCreation
def self.create_quest_with_rewards(quest_data)
quest_id = SecureRandom.uuid
badge_id = SecureRandom.uuid

Rails.configuration.command_bus.call(
Questing::CreateQuest.new(
quest_id: quest_id,
display_data: quest_data[:display_data],
audience: quest_data[:audience],
badge_id: badge_id,
badge_display_data: quest_data[:badge_display_data]
)
)
Expand Down Expand Up @@ -69,14 +67,12 @@ def self.associate_action_with_quest(quest_id, action_id, position)
module TrackCreation
def self.create_track_with_quests(track_data, quest_id_map)
track_id = SecureRandom.uuid
badge_id = SecureRandom.uuid

Rails.configuration.command_bus.call(
Questing::CreateTrack.new(
track_id: track_id,
display_data: track_data[:display_data],
quest_ids: track_data[:quests].map { |quest_title| quest_id_map[quest_title] },
badge_id: badge_id,
badge_display_data: track_data[:badge_display_data]
)
)
Expand Down

0 comments on commit b8dc871

Please sign in to comment.