Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rake task and service for publishing a course #4698

Merged
merged 7 commits into from
Nov 19, 2024

Conversation

inulty-dfe
Copy link
Contributor

@inulty-dfe inulty-dfe commented Nov 18, 2024

Context

We sometimes need to publish a course through the console. The main scenario is for when the course is in a previous recruitment cycle.
It's good to have a stable service to run this in a repeatable and safe way

Changes proposed in this pull request

Extract a Service Courses::PublishService from the methods in the Course model.

The service accepts a user and a course
The task accepts a uuid and user email

  • I created a Rake tasks which accepts an email address so we don't commit an admins email address to the code.
  • I chose the uuid because courses' uuid are unique across all RecruitmentCycles.

I've used the service in the Publish::CoursesController#publish action

Guidance to review

@inulty-dfe inulty-dfe force-pushed the im/publish-old-course-for-register branch 2 times, most recently from fb3bbdf to c0c0aec Compare November 18, 2024 16:41
@inulty-dfe
Copy link
Contributor Author

@inulty-dfe inulty-dfe marked this pull request as ready for review November 18, 2024 16:45
@inulty-dfe inulty-dfe requested a review from a team as a code owner November 18, 2024 16:45
@inulty-dfe inulty-dfe added the deploy A Review App will be created for PRs with this label label Nov 18, 2024
@inulty-dfe inulty-dfe changed the title Rake task and service for pulishing a course Rake task and service for publishing a course Nov 18, 2024
end
course = Course.find_by(uuid:)

raise UnpublishableError unless course.publishable?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it should raise an error. I want to use this in the controller so I don't want to catch this error.

Should this

  1. return false if the course is unpulishable?
  2. return the course and expect the caller to check if the course was published?

Copy link
Contributor

@tomas-stefano tomas-stefano Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go for a mixed solution 1 and 2.

This is how I'd write this:

module Courses
  class CoursePublisher
    def initialize(course:, user:)
      @course = course
      @user = user
    end

    def call
      return false unless @course.publishable?

      publish_course
      notify_course_published

      @course
    end

    private

    def publish_course
      Course.transaction do
        @course.publish_sites
        @course.publish_enrichment(@user)
        @course.application_status_open!
      end
    end

    def notify_course_published
      NotificationService::CoursePublished.call(course: @course)
    end
  end
end

And on controller (some fake code to exemplify):

def publish
  course = Course.find_by(uuid: params[:uuid])
  
  if result = Courses::CoursePublisher.new(course:, user: current_user).call
    redirect_to course_path(result), notice: 'Course published successfully!'
  else
    redirect_to courses_path, alert: 'Course could not be published.'
  end
end

I put the notification out of the transaction. Not sure if it should though.

Also Publish has this pattern of include ServicePattern. Not sure if I like to suffix everything with Service though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I'll incorporate this into the PR

@inulty-dfe inulty-dfe force-pushed the im/publish-old-course-for-register branch 2 times, most recently from 16a9f5b to 6c523fb Compare November 19, 2024 10:48
@inulty-dfe inulty-dfe force-pushed the im/publish-old-course-for-register branch from 6c523fb to e5d6dc1 Compare November 19, 2024 10:51
Copy link
Contributor

@tomas-stefano tomas-stefano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

What do you think about adding a documentation or small content to support playbook?

@inulty-dfe inulty-dfe force-pushed the im/publish-old-course-for-register branch from 2b321b7 to ca771bd Compare November 19, 2024 11:24
@inulty-dfe inulty-dfe merged commit 6247066 into main Nov 19, 2024
@inulty-dfe inulty-dfe deleted the im/publish-old-course-for-register branch November 19, 2024 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deploy A Review App will be created for PRs with this label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants