<commit_before>import _ from 'lodash';
const {knex} = require('../util/database').connect();

function createOrUpdateVote(vote) {
  // TODO: Check that feed_item_id is image and not system

  const updateSql = `
    WITH upsert AS
      (UPDATE votes SET value = ? WHERE user_id = ? AND feed_item_id = ? RETURNING *)
      INSERT INTO votes (value, user_id, feed_item_id)
      SELECT ?, ?, ? WHERE NOT EXISTS (SELECT * FROM upsert)
  `;
  const updateParams = [vote.value, vote.user_id, vote.feed_item_id,
    vote.value, vote.user_id, vote.feed_item_id];

  return knex.transaction(function(trx) {
    return knex.raw(updateSql, updateParams)
      .transacting(trx)
      .then(result => {
        return undefined;
      });
  });
}

export {
  createOrUpdateVote
};<commit_msg>Disabled voting on feed type 'SYSTEM'<commit_after>import _ from 'lodash';
const {knex} = require('../util/database').connect();

function createOrUpdateVote(vote) {

  const updateSql = `
    WITH upsert AS
      (UPDATE votes SET value = ? WHERE user_id = ? AND feed_item_id = ? RETURNING *)
      INSERT INTO votes (value, user_id, feed_item_id)
      SELECT ?, ?, ? WHERE NOT EXISTS (SELECT * FROM upsert)
  `;

  const updateParams = [vote.value, vote.user_id, vote.feed_item_id,
    vote.value, vote.user_id, vote.feed_item_id];

  return knex.transaction(function(trx) {
    return knex.select()
      .from('feed_items')
      .where({ id: vote.feed_item_id })
      .first()
      .then(row => {
        if (!!row && row.type === 'SYSTEM') Promise.reject();
        return knex.raw(updateSql, updateParams)
          .transacting(trx);
      })
      .catch(err => {
        return undefined;
      })
      .then(result => {
        return undefined;
      });
  });
}

export {
  createOrUpdateVote
};
{'fraction_non_alphanumeric': 0.13322185061315497, 'fraction_numerical': 0.0, 'mean_word_length': 2.927789934354486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7019810', 'n_tokens_mistral': 606, 'n_tokens_neox': 586, 'n_words': 138}