Class: Course::UserInvitationService

Inherits:
Object
  • Object
show all
Includes:
EmailInvitationConcern, ParseInvitationConcern, ProcessInvitationConcern
Defined in:
app/services/course/user_invitation_service.rb,
app/services/concerns/course/user_invitation_service/email_invitation_concern.rb,
app/services/concerns/course/user_invitation_service/parse_invitation_concern.rb,
app/services/concerns/course/user_invitation_service/process_invitation_concern.rb

Overview

This concern deals with the creation of user invitations.

Defined Under Namespace

Modules: EmailInvitationConcern, ParseInvitationConcern, ProcessInvitationConcern

Constant Summary

Constants included from ParseInvitationConcern

ParseInvitationConcern::TRUE_VALUES

Instance Method Summary collapse

Constructor Details

#initialize(current_user, current_course) ⇒ UserInvitationService

Constructor for the user invitation service object.

Parameters:

  • current_user (User)

    The user performing this action.

  • current_course (Course)

    The course to invite users to.


13
14
15
16
17
# File 'app/services/course/user_invitation_service.rb', line 13

def initialize(current_user, current_course)
  @current_user = current_user
  @current_course = current_course
  @current_instance = @current_course.instance
end

Instance Method Details

#invite(users) ⇒ Array<Integer>|nil

Invites users to the given course.

The result of the transaction is both saving the course as well as validating validity because Rails does not handle duplicate nested attribute uniqueness constraints.

Parameters:

  • users (Array<Hash>|File|TempFile)

    Invites the given users.

Returns:

  • (Array<Integer>|nil)

    An array containing the the size of new_invitations, existing_invitations, new_course_users and existing_course_users respectively if success. nil when fail.

Raises:

  • (CSV::MalformedCSVError)

    When the file provided is invalid.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/course/user_invitation_service.rb', line 28

def invite(users)
  new_invitations = nil
  existing_invitations = nil
  new_course_users = nil
  existing_course_users = nil

  success = Course.transaction do
    new_invitations, existing_invitations, new_course_users, existing_course_users = invite_users(users)
    raise ActiveRecord::Rollback unless new_invitations.all?(&:save)
    raise ActiveRecord::Rollback unless new_course_users.all?(&:save)
    true
  end

  send_registered_emails(new_course_users) if success
  send_invitation_emails(new_invitations) if success
  success ? [new_invitations, existing_invitations, new_course_users, existing_course_users].map(&:size) : nil
end

#resend_invitation(invitations) ⇒ Boolean

Resends invitation emails to CourseUsers to the given course. This method disregards CourseUsers that do not have an 'invited' status.

Parameters:

Returns:

  • (Boolean)

    True if there were no errors in sending invitations. If all provided CourseUsers have already registered, method also returns true.


52
53
54
# File 'app/services/course/user_invitation_service.rb', line 52

def resend_invitation(invitations)
  invitations.blank? ? true : send_invitation_emails(invitations)
end