-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the process of publishing a course to a service
- Loading branch information
1 parent
2b06d91
commit 181844b
Showing
2 changed files
with
49 additions
and
0 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
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 |
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,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 |