Advertisement
Guest User

Untitled

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