<commit_before><commit_msg>added script to subscribe all hosts to the group.expense.created of their groups<commit_after>#!/usr/bin/env node
/**
 * This script subscribes all hosts
 * to the group.expense.created event
 */
import Promise from 'bluebird';
import models from '../server/models';

const debug = require('debug')('subscribe');

const UserGroup = models.UserGroup;
const Notification = models.Notification;

const processRows = (rows) => {
    return Promise.map(rows, processRow);
};

const init = () => {
  UserGroup.findAll({ where: { role: 'HOST' }})
  .then(processRows)
  .then(() => process.exit(0));
}

const processRow = (row) => {
  const type = `group.expense.created`;
  debug(`Subscribing UserId ${row.UserId} to ${type} of GroupId ${row.GroupId}`);
  return Notification.create({
    UserId: row.UserId,
    GroupId: row.GroupId,
    type
  }).catch(() => console.error(`UserId ${row.UserId} already subscribed to ${type} of GroupId ${row.GroupId}`));
};

init();
{'fraction_non_alphanumeric': 0.16279069767441862, 'fraction_numerical': 0.0010111223458038423, 'mean_word_length': 4.469613259668508, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '7467592', 'n_tokens_mistral': 310, 'n_tokens_neox': 310, 'n_words': 86}