From 0e259d6664cc95cf6cdb28d18a43ca6791ee7272 Mon Sep 17 00:00:00 2001 From: Juliano Quatrin Nunes Date: Fri, 17 Jan 2025 12:47:43 -0300 Subject: [PATCH] feat: add creation of special badges --- .../gamification/lib/gamification/commands.rb | 10 ++++++ .../gamification/lib/gamification/events.rb | 8 +++++ .../lib/gamification/special_badge.rb | 32 +++++++++++++++++++ .../gamification/on_special_badge_created.rb | 13 ++++++++ 4 files changed, 63 insertions(+) create mode 100644 apps/govquests-api/govquests/gamification/lib/gamification/special_badge.rb create mode 100644 apps/govquests-api/rails_app/app/read_models/gamification/on_special_badge_created.rb diff --git a/apps/govquests-api/govquests/gamification/lib/gamification/commands.rb b/apps/govquests-api/govquests/gamification/lib/gamification/commands.rb index 2d8217dc..d3709785 100644 --- a/apps/govquests-api/govquests/gamification/lib/gamification/commands.rb +++ b/apps/govquests-api/govquests/gamification/lib/gamification/commands.rb @@ -78,4 +78,14 @@ class EarnBadge < Infra::Command alias_method :aggregate_id, :user_id end + + class CreateSpecialBadge < Infra::Command + attribute :badge_id, Infra::Types::String + attribute :display_data, Infra::Types::Hash + attribute :badge_type, Infra::Types::String + attribute :badge_data, Infra::Types::Hash + attribute :points, Infra::Types::Integer + + alias_method :aggregate_id, :badge_id + end end diff --git a/apps/govquests-api/govquests/gamification/lib/gamification/events.rb b/apps/govquests-api/govquests/gamification/lib/gamification/events.rb index c4ac2788..a53e1286 100644 --- a/apps/govquests-api/govquests/gamification/lib/gamification/events.rb +++ b/apps/govquests-api/govquests/gamification/lib/gamification/events.rb @@ -66,4 +66,12 @@ class BadgeEarned < Infra::Event attribute :badgeable_type, Infra::Types::String attribute :earned_at, Infra::Types::DateTime end + + class SpecialBadgeCreated < Infra::Event + attribute :badge_id, Infra::Types::String + attribute :display_data, Infra::Types::Hash + attribute :badge_type, Infra::Types::String + attribute :badge_data, Infra::Types::Hash + attribute :points, Infra::Types::Integer + end end diff --git a/apps/govquests-api/govquests/gamification/lib/gamification/special_badge.rb b/apps/govquests-api/govquests/gamification/lib/gamification/special_badge.rb new file mode 100644 index 00000000..d2d1edb9 --- /dev/null +++ b/apps/govquests-api/govquests/gamification/lib/gamification/special_badge.rb @@ -0,0 +1,32 @@ +module Gamification + class SpecialBadge + include AggregateRoot + + def initialize(id) + @id = id + @display_data = nil + @badge_type = nil + @badge_data = nil + @points = nil + end + + def create(display_data, badge_type, badge_data, points) + apply SpecialBadgeCreated.new(data: { + badge_id: @id, + display_data:, + badge_type:, + badge_data:, + points:, + }) + end + + private + + on SpecialBadgeCreated do |event| + @display_data = event.data[:display_data] + @badge_type = event.data[:badge_type] + @badge_data = event.data[:badge_data] + @points = event.data[:points] + end + end +end diff --git a/apps/govquests-api/rails_app/app/read_models/gamification/on_special_badge_created.rb b/apps/govquests-api/rails_app/app/read_models/gamification/on_special_badge_created.rb new file mode 100644 index 00000000..c95be3a2 --- /dev/null +++ b/apps/govquests-api/rails_app/app/read_models/gamification/on_special_badge_created.rb @@ -0,0 +1,13 @@ +module Gamification + class OnSpecialBadgeCreated + def call(event) + SpecialBadgeReadModel.create!( + badge_id: event.data[:badge_id], + display_data: event.data[:display_data], + badge_type: event.data[:badge_type], + badge_data: event.data[:badge_data], + points: event.data[:points] + ) + end + end +end