-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create user track aggregate, handle start and progress tracks
- Loading branch information
1 parent
41d4f1c
commit e35f9c3
Showing
14 changed files
with
199 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
apps/govquests-api/govquests/questing/lib/questing/user_track.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
module Questing | ||
class UserTrack | ||
include AggregateRoot | ||
|
||
TrackNotStartedError = Class.new(StandardError) | ||
TrackAlreadyStartedError = Class.new(StandardError) | ||
TrackAlreadyCompletedError = Class.new(StandardError) | ||
QuestsNotCompletedError = Class.new(StandardError) | ||
InvalidQuestError = Class.new(StandardError) | ||
|
||
def initialize(id) | ||
@id = id | ||
@state = :not_started | ||
@track_id = nil | ||
@user_id = nil | ||
@quests = [] | ||
@progress = {} | ||
end | ||
|
||
def start(track_id, user_id, quests) | ||
raise TrackAlreadyStartedError if @state == :in_progress | ||
raise TrackAlreadyCompletedError if @state == :completed | ||
|
||
apply TrackStarted.new(data: { | ||
user_track_id: @id, | ||
track_id: track_id, | ||
user_id: user_id, | ||
quests: quests | ||
}) | ||
end | ||
|
||
def add_progress(quest_id) | ||
raise TrackNotStartedError unless @state == :in_progress | ||
|
||
unless @quests.include?(quest_id) | ||
raise InvalidQuestError, "Quest #{quest_id} is not part of this track." | ||
end | ||
|
||
apply TrackProgressUpdated.new(data: { | ||
user_track_id: @id, | ||
quest_id: quest_id, | ||
}) | ||
|
||
complete if all_quests_completed? | ||
end | ||
|
||
def complete | ||
raise TrackNotStartedError unless @state == :in_progress | ||
raise TrackAlreadyCompletedError if @state == :completed | ||
raise QuestsNotCompletedError unless all_quests_completed? | ||
|
||
apply TrackCompleted.new(data: { | ||
user_track_id: @id, | ||
track_id: @track_id, | ||
user_id: @user_id | ||
}) | ||
end | ||
|
||
private | ||
|
||
def all_quests_completed? | ||
(@quests - @progress.keys).empty? | ||
end | ||
|
||
on TrackStarted do |event| | ||
@state = :in_progress | ||
@track_id = event.data[:track_id] | ||
@user_id = event.data[:user_id] | ||
@quests = event.data[:quests] | ||
@progress = {} | ||
end | ||
|
||
on TrackProgressUpdated do |event| | ||
@progress[event.data[:quest_id]] = true | ||
end | ||
|
||
on TrackCompleted do |event| | ||
@state = :completed | ||
end | ||
end | ||
end |
5 changes: 2 additions & 3 deletions
5
apps/govquests-api/rails_app/app/read_models/questing/on_track_completed.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
apps/govquests-api/rails_app/app/read_models/questing/on_track_started.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Questing | ||
class OnTrackStarted | ||
def call(event) | ||
user_track_id = event.data[:user_track_id] | ||
track_id = event.data[:track_id] | ||
user_id = event.data[:user_id] | ||
|
||
UserTrackReadModel.find_or_create_by(user_track_id:, track_id:, user_id:).update( | ||
status: "in_progress" | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
apps/govquests-api/rails_app/db/migrate/20250120205824_add_user_track_id_to_user_tracks.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddUserTrackIdToUserTracks < ActiveRecord::Migration[8.1] | ||
def change | ||
add_column :user_tracks, :user_track_id, :string | ||
|
||
add_index :user_tracks, :user_track_id, unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.