<commit_before>import { compact, merge } from 'lodash'
import sampleData from './sampleData.json'
import formatData from './formatData'
import personalizeData from './personalizeData'
import {
  defaultTimeRange,
  getPosts,
  getRecipients,
  shouldSendData
} from './util'

const templateId = 'tem_eCnLj6q75A7Ruu9zsLgppN'

const timePeriod = type => {
  switch (type) {
    case 'daily': return 'yesterday'
    case 'weekly': return 'last week'
  }
}

export const prepareDigestData = (id, type, opts = {}) => {
  let startTime = opts.startTime
  let endTime = opts.endTime
  if (!opts.startTime) {
    const range = defaultTimeRange(type)
    startTime = range[0]
    endTime = range[1]
  }
  return Community.find(id).then(c =>
    getPosts(c, startTime, endTime)
    .then(formatData(c))
    .then(data => merge({
      community_id: c.id,
      community_name: c.get('name'),
      community_avatar_url: c.get('avatar_url'),
      community_url: Frontend.Route.community(c),
      time_period: timePeriod(type)
    }, data)))
}

const sendToUser = (user, data, opts = {}) => {
  const versionName = opts.versionName || (user.id % 2 === 0 ? 'multi-reply-form' : 'default')

  return personalizeData(user, data, merge(opts, {versionName}))
  .then(data =>
    opts.dryRun ||
    Email.sendSimpleEmail(user.get('email'), templateId, data, {
      sender: {name: data.community_name || 'Hylo'},
      version_name: versionName
    }))
}

export const sendDigestToUser = (id, userId, type, opts = {}) =>
  prepareDigestData(id, type, opts).then(data =>
    (shouldSendData(data, id) || Promise.resolve(opts.forceSend))
    .then(ok => ok && User.find(userId).then(user =>
      sendToUser(user, data, opts))))

export const sendDigest = (id, type, opts = {}) => {
  if (id === 998) {
    // special case for IDIN
    opts.versionName = 'default'
  }
  return prepareDigestData(id, type, opts).then(data =>
    shouldSendData(data, id).then(ok => ok && getRecipients(id, type)
    .then(users => Promise.each(users, user => sendToUser(user, data, opts)))
    .then(users => users.length)))
}

export const sendAllDigests = (type, opts) =>
  Community.where('daily_digest', true).query().pluck('id')<commit_msg>fix IDIN digest version forcing<commit_after>import { compact, merge } from 'lodash'
import sampleData from './sampleData.json'
import formatData from './formatData'
import personalizeData from './personalizeData'
import {
  defaultTimeRange,
  getPosts,
  getRecipients,
  shouldSendData
} from './util'

const templateId = 'tem_eCnLj6q75A7Ruu9zsLgppN'

const timePeriod = type => {
  switch (type) {
    case 'daily': return 'yesterday'
    case 'weekly': return 'last week'
  }
}

export const prepareDigestData = (id, type, opts = {}) => {
  let startTime = opts.startTime
  let endTime = opts.endTime
  if (!opts.startTime) {
    const range = defaultTimeRange(type)
    startTime = range[0]
    endTime = range[1]
  }
  return Community.find(id).then(c =>
    getPosts(c, startTime, endTime)
    .then(formatData(c))
    .then(data => merge({
      community_id: c.id,
      community_name: c.get('name'),
      community_avatar_url: c.get('avatar_url'),
      community_url: Frontend.Route.community(c),
      time_period: timePeriod(type)
    }, data)))
}

const sendToUser = (user, data, opts = {}) => {
  const versionName = opts.versionName || (user.id % 2 === 0 ? 'multi-reply-form' : 'default')

  return personalizeData(user, data, merge(opts, {versionName}))
  .then(data =>
    opts.dryRun ||
    Email.sendSimpleEmail(user.get('email'), templateId, data, {
      sender: {name: data.community_name || 'Hylo'},
      version_name: versionName
    }))
}

export const sendDigestToUser = (id, userId, type, opts = {}) =>
  prepareDigestData(id, type, opts).then(data =>
    (shouldSendData(data, id) || Promise.resolve(opts.forceSend))
    .then(ok => ok && User.find(userId).then(user =>
      sendToUser(user, data, opts))))

export const sendDigest = (id, type, opts = {}) => {
  if (Number(id) === 998) {
    // special case for IDIN
    opts.versionName = 'default'
  }
  return prepareDigestData(id, type, opts).then(data =>
    shouldSendData(data, id).then(ok => ok && getRecipients(id, type)
    .then(users => Promise.each(users, user => sendToUser(user, data, opts)))
    .then(users => users.length)))
}

export const sendAllDigests = (type, opts) =>
  Community.where('daily_digest', true).query().pluck('id')
{'fraction_non_alphanumeric': 0.1560953666216824, 'fraction_numerical': 0.005398110661268556, 'mean_word_length': 3.838955386289445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '6862885', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1496, 'n_words': 339}