diff --git a/app/components/cpd_course_item_component.rb b/app/components/cpd_course_item_component.rb
index 1f99abfdca..cdc6b6adc1 100644
--- a/app/components/cpd_course_item_component.rb
+++ b/app/components/cpd_course_item_component.rb
@@ -10,6 +10,14 @@ def initialize(activity:, current_user:)
@current_user = current_user
@course = Achiever::Course::Template.maybe_find_by_activity_code(activity.stem_activity_code) if activity.stem_activity_code.present?
- @achievement = current_user.achievements.to_a.find { _1.activity_id == activity.id }
+ @achievements = current_user.achievements.where(activity: @activity.id)
+
+ @achievement_state = if @achievements.in_state(:complete).any?
+ :complete
+ elsif @achievements.in_state(:enrolled).any?
+ :in_progress
+ else
+ :not_started
+ end
end
end
diff --git a/app/components/cpd_course_item_component/cpd_course_item_component.html.erb b/app/components/cpd_course_item_component/cpd_course_item_component.html.erb
index e6f40f0c89..0ebe9847b7 100644
--- a/app/components/cpd_course_item_component/cpd_course_item_component.html.erb
+++ b/app/components/cpd_course_item_component/cpd_course_item_component.html.erb
@@ -25,10 +25,11 @@
- <% if @achievement %>
-
- <%= @achievement.complete? ? 'Completed' : 'In progress' %>
-
+
+ <% if @achievement_state == :complete %>
+
Completed
+ <% elsif @achievement_state == :in_progress %>
+
In progress
<% else %>
<%= link_to "Book now", course_path(id: @activity.stem_activity_code, name: @activity.slug),
class: "govuk-button button" %>
diff --git a/spec/components/cpd_course_item_component_spec.rb b/spec/components/cpd_course_item_component_spec.rb
index 08da8902eb..e7c8f3f23b 100644
--- a/spec/components/cpd_course_item_component_spec.rb
+++ b/spec/components/cpd_course_item_component_spec.rb
@@ -37,6 +37,26 @@
end
end
+ context "with dropped achievement" do
+ let!(:achievement) { create(:dropped_achievement, user: current_user, activity:) }
+
+ before do
+ render_inline(described_class.new(activity:, current_user:))
+ end
+
+ it "should render the element" do
+ expect(page).to have_css(".cpd-course-item")
+ end
+
+ it "should render title" do
+ expect(page).to have_css(".govuk-body", text: activity.title)
+ end
+
+ it "should render the book now button" do
+ expect(page).to have_link("Book now", href: "/courses/#{activity.stem_activity_code}/#{activity.slug}")
+ end
+ end
+
context "when complete" do
let!(:achievement) { create(:completed_achievement, user: current_user, activity:) }
@@ -60,7 +80,7 @@
render_inline(described_class.new(activity:, current_user:))
end
- it "should show completed flag" do
+ it "should show in progress flag" do
expect(page).to have_css(".status-tag", text: "In progress")
end
@@ -68,4 +88,54 @@
expect(page).not_to have_link("Book now", href: "/courses/#{activity.stem_activity_code}/#{activity.slug}")
end
end
+
+ context "with dropped achievement and completed" do
+ let!(:achievement) { create(:dropped_achievement, user: current_user, activity:) }
+ let!(:achievement) { create(:completed_achievement, user: current_user, activity:) }
+
+ before do
+ render_inline(described_class.new(activity:, current_user:))
+ end
+
+ it "should render the element" do
+ expect(page).to have_css(".cpd-course-item")
+ end
+
+ it "should render title" do
+ expect(page).to have_css(".govuk-body", text: activity.title)
+ end
+
+ it "should not show book button" do
+ expect(page).not_to have_link("Book now", href: "/courses/#{activity.stem_activity_code}/#{activity.slug}")
+ end
+
+ it "should show completed flag" do
+ expect(page).to have_css(".status-tag", text: "Completed")
+ end
+ end
+
+ context "with dropped achievement and in_progress" do
+ let!(:achievement) { create(:dropped_achievement, user: current_user, activity:) }
+ let!(:achievement) { create(:achievement, user: current_user, activity:) }
+
+ before do
+ render_inline(described_class.new(activity:, current_user:))
+ end
+
+ it "should render the element" do
+ expect(page).to have_css(".cpd-course-item")
+ end
+
+ it "should render title" do
+ expect(page).to have_css(".govuk-body", text: activity.title)
+ end
+
+ it "should not show book button" do
+ expect(page).not_to have_link("Book now", href: "/courses/#{activity.stem_activity_code}/#{activity.slug}")
+ end
+
+ it "should show in progress flag" do
+ expect(page).to have_css(".status-tag", text: "In progress")
+ end
+ end
end