1class EmailReminder
 
2  # email out a list of all petitions that have reached the threshold or that have been marked for a response
 
3  def self.threshold_email_reminder
 
4    admin_users = AdminUser.by_role(AdminUser::MODERATOR_ROLE)
 
5    if admin_users.any?
 
6      petitions =  Petition.threshold.where(:notified_by_email => false).order(:signature_count)
 
7      # only email if there are one or more petitions
 
8      if petitions.any?
 
 9        logger.info('Sending threshold email')
 
10        AdminMailer.threshold_email_reminder(admin_users, petitions).deliver_now
 
 
12        # mark all petitions as having been notified by email
 
13        petitions.each do |petition|
 
14          petition.update_attribute(:notified_by_email, true)
 
15        end
 
16      end
 
17    end
 
18  rescue Exception => e
 
19    logger.error("#{e.class.name} while processing threshold_email_reminders: #{e.message}", e)
 
20  end
 
 
22  def self.special_resend_of_signature_email_validation(date = '2011-08-14')
 
23    scope = Signature.where(state: Signature::PENDING_STATE)
 
24    scope = scope.where("created_at > ?", Date.new(2011, 8, 14))
 
25    scope = scope.where("updated_at < ?", date.to_date)
 
 
27    scope.find_each do |signature|
 
28      begin
 
29        PetitionMailer.special_resend_of_email_confirmation_for_signer(signature).deliver_now
 
30      rescue Net::SMTPSyntaxError
 
31        logger.warn("cannot send email to #{signature.email}")
 
32        # ignore a syntax error
 
33      end
 
34      signature.update_attribute(:updated_at, Time.current)
 
35      logger.info("Special resend sent to #{signature.email}")
 
36    end
 
37  end
 
 
39  def self.logger
 
40    unless @logger
 
41      logfilename = "email_reminders.log"
 
42      @logger = AuditLogger.new(Rails.root.join('log', logfilename), 'email reminders')
 
43    end
 
44    @logger
 
45  end
 
46end