{"size": 946, "ext": "js", "max_stars_repo_path": "server/configureChangeFeeds/userEmailUpdated.js", "max_stars_repo_name": "LearnersGuild/idm-api-spec", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/configureChangeFeeds/userEmailUpdated.js", "max_issues_repo_name": "LearnersGuild/idm-api-spec", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/configureChangeFeeds/userEmailUpdated.js", "max_forks_repo_name": "LearnersGuild/idm-api-spec", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.2564102564, "max_line_length": 79, "alphanum_fraction": 0.6871035941}
/* eslint-disable no-console, camelcase */
import processChangeFeedWithAutoReconnect from 'rethinkdb-changefeed-reconnect'

import {connect} from 'src/db'
import handleConnectionError from './util/handleConnectionError'

const r = connect()

export default function userEmailUpdated(queueService) {
  processChangeFeedWithAutoReconnect(
    _getFeed,
    _getFeedProcessor(queueService),
    handleConnectionError,
    {changefeedName: 'user email updated'}
  )
}

function _getFeed() {
  return r.table('users')
    .changes()
    .filter(
      r.row('old_val').default({})('email')
        .ne(
          r.row('new_val')('email')
        )
    )
}

function _getFeedProcessor(queueService) {
  const userEmailUpdatedQueue = queueService.getQueue('userEmailUpdated')
  return ({new_val: user}) => {
    const jobOpts = {
      attempts: 3,
      backoff: {type: 'fixed', delay: 60000},
    }
    userEmailUpdatedQueue.add(user, jobOpts)
  }
}
