<commit_before># Logic for DB roundup queries & email sending
class Notifier

  # Construct a hash of the form:
  # {
  #   <EMAIL>:
  #    {
  #      properties: [{property: <Property>, feedback_inputs: [<FeedbackInput>, <FeedbackInput>, ...], unsubscribe_token: '<PASSWORD>'}, {...}]
  #    },
  #   <EMAIL>: {...}
  # }
  def self.send_weekly_notifications
    puts 'Start of notifier method'

    subs = self.subscription_with_activity_since_last_email_sent

    emails_hash = self.build_hash_for_mailer(subs)

    self.deliver_emails(emails_hash)
  end

  # Get the NotificationSubscriptions which have had activity since last notification email was sent
  def self.subscription_with_activity_since_last_email_sent
    NotificationSubscription.includes(property: :feedback_inputs)
                            .where(confirmed: true)
                            .where('feedback_inputs.created_at >= notification_subscriptions.last_email_sent_at')
  end

  # Group all the <NotificationSubscription>'s by email
  def self.build_hash_for_mailer(subs)
    result = Hash.new { |hash, k| hash[k] = {} }
    result.tap do |result|
      subs.each do |sub|
        email    = sub.email
        property = sub.property

        result[email][:properties] ||= []
        result[email][:properties] << {
          property: property,
          unsubscribe_token: sub.auth_token,
          feedback_inputs: property.feedback_inputs.where('created_at >= ?', sub.last_email_sent_at)
        }
        sub.update_attributes(last_email_sent_at: DateTime.now)
      end
    end
  end

  # Deliver for each email address in hash
  def self.deliver_emails(emails_hash)<commit_msg>Modify Notifier to send emails if confirmed OR if bulk-added<commit_after># Logic for DB roundup queries & email sending
class Notifier

  # Construct a hash of the form:
  # {
  #   <EMAIL>:
  #    {
  #      properties: [{property: <Property>, feedback_inputs: [<FeedbackInput>, <FeedbackInput>, ...], unsubscribe_token: '<PASSWORD>'}, {...}]
  #    },
  #   <EMAIL>: {...}
  # }
  def self.send_weekly_notifications
    puts 'Start of notifier method'

    subs = self.subscription_with_activity_since_last_email_sent

    emails_hash = self.build_hash_for_mailer(subs)

    self.deliver_emails(emails_hash)
  end

  # Get the NotificationSubscriptions which have had activity since last notification email was sent
  def self.subscription_with_activity_since_last_email_sent
    NotificationSubscription.includes(property: :feedback_inputs)
                            .where("confirmed = ? or bulk_added = ?", true, true)
                            .where('feedback_inputs.created_at >= notification_subscriptions.last_email_sent_at')
  end

  # Group all the <NotificationSubscription>'s by email
  def self.build_hash_for_mailer(subs)
    result = Hash.new { |hash, k| hash[k] = {} }
    result.tap do |result|
      subs.each do |sub|
        email    = sub.email
        property = sub.property

        result[email][:properties] ||= []
        result[email][:properties] << {
          property: property,
          unsubscribe_token: sub.auth_token,
          feedback_inputs: property.feedback_inputs.where('created_at >= ?', sub.last_email_sent_at)
        }
        sub.update_attributes(last_email_sent_at: DateTime.now)
      end
    end
  end

  # Deliver for each email address in hash
  def self.deliver_emails(emails_hash)
{'fraction_non_alphanumeric': 0.10143652887716212, 'fraction_numerical': 0.0, 'mean_word_length': 3.033096926713948, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6776959', 'n_tokens_mistral': 1044, 'n_tokens_neox': 1024, 'n_words': 225}