Skip to content

Commit

Permalink
Add Course::PublishService
Browse files Browse the repository at this point in the history
  Extract the process of publishing a course to a service
  • Loading branch information
inulty-dfe committed Nov 18, 2024
1 parent 2b06d91 commit 181844b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/services/courses/publish_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Courses
class PublishService
UnpublishableError = Class.new(StandardError)

class << self
def publish(uuid:, user:)
Rails.logger.tagged('Course::PublishService') do |l|
l.info "publishing course uuid: #{uuid}"
end
course = Course.joins(:provider).find_by(uuid:)

raise UnpublishableError unless course.publishable?

Course.transaction do
course.publish_sites
course.publish_enrichment(user)
course.application_status_open!
NotificationService::CoursePublished.call(course:)
end
end
end
end
end
24 changes: 24 additions & 0 deletions spec/services/courses/publish_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Courses::PublishService do
let(:uuid) { 'b39fe8fe-7cc5-42b8-a06f-d4461b7eb84e' }
let(:course) { create(:course, :publishable, uuid:) }
let(:user) { create(:user, :admin) }

describe 'publishable' do
it 'gets published' do
described_class.publish(uuid: course.uuid, user:)
expect(course.reload).to be_published
end
end

describe 'publish fails' do
it 'raises UnpublishableError' do
expect do
described_class.publish(uuid: course.uuid, user:)
end.to raise_error(described_class::UnpublishableError)
end
end
end

0 comments on commit 181844b

Please sign in to comment.