Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.64 KB | None | 0 0
  1. module Mailing
  2.   class OnboardingTeacherService
  3.     include Service
  4.     handle_asynchronously :call, queue: 'mailing'
  5.  
  6.     def initialize(operation, options = {})
  7.       @operation = operation
  8.       @options = options
  9.     end
  10.  
  11.     def call
  12.       send(@operation, @options) if respond_to?(@operation, true)
  13.     end
  14.  
  15.     private
  16.  
  17.     def send_teacher_how_to_add_students(options)
  18.       return unless options[:teacher]
  19.       return unless Feature.teacher_how_to_add_students?
  20.  
  21.       teacher = options[:teacher]
  22.       return if teacher.email.blank?
  23.       return unless teacher.label_for(:email_teacher_onboarding_triggers).in?(%w[b c])
  24.       return unless teacher.groups.any? && teacher.students.uniq.count < 2
  25.  
  26.       AzbookaMailer.send_mail(:teacher_how_to_add_students, teacher_how_to_add_students_params(teacher))
  27.     end
  28.  
  29.     def teacher_how_to_add_students_params(teacher)
  30.       class_book = teacher.class_books.first
  31.       {
  32.         email: teacher.email,
  33.         tname: teacher.io,
  34.         addedclass: class_book.name
  35.       }
  36.     end
  37.  
  38.     def send_help_for_teacher_with_add_students(options)
  39.       return unless options[:teacher]
  40.       return unless Feature.help_for_teacher_with_add_students?
  41.  
  42.       teacher = options[:teacher]
  43.       return if teacher.email.blank?
  44.       return unless teacher.label_for(:email_teacher_onboarding_triggers) == 'c'
  45.       return unless teacher.groups.any? && teacher.students.uniq.count < 3
  46.  
  47.       AzbookaMailer.send_mail(:help_for_teacher_with_add_students, help_for_teacher_with_add_students_params(teacher))
  48.     end
  49.  
  50.     def help_for_teacher_with_add_students_params(teacher)
  51.       class_book = teacher.class_books.order(created_at: :desc).first
  52.       {
  53.         email: teacher.email,
  54.         tname: teacher.io,
  55.         addedclass: class_book.name,
  56.         classid: class_book.id
  57.       }
  58.     end
  59.  
  60.     def send_teacher_how_to_add_parents(options)
  61.       return unless options[:teacher]
  62.       return unless Feature.teacher_how_to_add_parents?
  63.  
  64.       teacher = options[:teacher]
  65.       return if teacher.email.blank?
  66.       return unless teacher.label_for(:email_teacher_onboarding_triggers).in?(%w[b c])
  67.       return unless teacher.groups.present? && teacher.students.present? &&
  68.                     teacher.students.all? { |s| s.parent2_id.nil? }
  69.  
  70.       AzbookaMailer.send_mail(:teacher_how_to_add_parents, teacher_how_to_add_parent_params(teacher))
  71.     end
  72.  
  73.     def teacher_how_to_add_parent_params(teacher)
  74.       {
  75.         email: teacher.email,
  76.         tname: teacher.io,
  77.         classid: teacher.group_with_most_students_without_parents.id
  78.       }
  79.     end
  80.  
  81.     def send_help_for_teacher_to_add_parents(options)
  82.       return unless options[:teacher]
  83.       return unless Feature.help_for_teacher_to_add_parents?
  84.  
  85.       teacher = options[:teacher]
  86.       return if teacher.email.blank?
  87.       return unless teacher.label_for(:email_teacher_onboarding_triggers) == 'c'
  88.       return unless teacher.groups.any? && teacher.students.uniq.count >= 3 && teacher.students.none?(&:parent2_id)
  89.  
  90.       AzbookaMailer.send_mail(:help_for_teacher_to_add_parents, help_for_teacher_to_add_parents_params(teacher))
  91.     end
  92.  
  93.     def help_for_teacher_to_add_parents_params(teacher)
  94.       class_book = teacher.group_with_most_students_without_parents.class_book
  95.       {
  96.         email: teacher.email,
  97.         tname: teacher.io,
  98.         addedclass: class_book.name,
  99.         students: class_book.students.map(&:name_with_short_surname).join(', '),
  100.         classid: class_book
  101.       }
  102.     end
  103.  
  104.     def send_activation_certificate_for_teacher_activity(options)
  105.       return unless options[:teacher]
  106.       return unless Feature.activation_certificate_for_teacher_activity?
  107.  
  108.       teacher = options[:teacher]
  109.       return if teacher.email.blank?
  110.       return unless teacher.label_for(:email_teacher_onboarding_triggers).in?(%w[b c])
  111.       return unless teacher&.class_books&.any? && (teacher.students.count == 5) &&
  112.                     teacher.students.all? { |s| s.parent2.present? }
  113.  
  114.       AzbookaMailer.send_mail(:activation_certificate_for_teacher_activity,
  115.                               activation_certificate_for_teacher_activity_params(teacher))
  116.     end
  117.  
  118.     def activation_certificate_for_teacher_activity_params(teacher)
  119.       {
  120.         email: teacher.email,
  121.         tname: teacher.io
  122.       }
  123.     end
  124.  
  125.     def send_teacher_activation_certificate_for_add_parents(options)
  126.       return unless options[:class_book]
  127.       return unless Feature.teacher_activation_certificate_for_add_parents?
  128.  
  129.       class_book = options[:class_book]
  130.       return if class_book.teacher.email.blank?
  131.       return unless class_book.teacher.label_for(:email_teacher_onboarding_triggers).in?(%w[b c])
  132.       return if class_book.students.count < 5 && class_book.students.any? { |student| student.parent2.present? }
  133.  
  134.       AzbookaMailer.send_mail(:teacher_activation_certificate_for_add_parents,
  135.                               teacher_activation_certificate_for_add_parents_params(class_book))
  136.     end
  137.  
  138.     def teacher_activation_certificate_for_add_parents_params(class_book)
  139.       {
  140.         email: class_book.teacher.email,
  141.         tname: class_book.teacher.io,
  142.         addedclass: class_book.name,
  143.         addedclassID: class_book.id,
  144.         APnumber: class_book.students.count,
  145.         APChild1: class_book.students[0].name_with_short_surname,
  146.         APChild2: class_book.students[1].name_with_short_surname,
  147.         APChild3: class_book.students[2].name_with_short_surname,
  148.         APChild4: class_book.students[3].name_with_short_surname,
  149.         APChild5: class_book.students[4].name_with_short_surname
  150.       }
  151.     end
  152.   end
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement