<commit_before>
// TaskGeneratorStore generates tasks

// TaskGenerator
// - task that will be generated
// - when to next generate the task
// - lastTimeGenerated


// Contact
// - info about what type of task to generate
// - nextTimeToGenerateTask






// _________

// In general

// We should probably have Contacts, and Messages, and Users
// Contacts generate new Messages (in draft format)
// Users send those Messages out 



const AppDispatcher = require('../dispatchers/AppDispatcher');
const Constants = require('../constants/AppConstants');
const BaseStore = require('./BaseStore');
const assign = require('object-assign');
const ActionCreator = require('../actions/ActionCreators');

const PouchDB = require('pouchdb');

let db = new PouchDB('contacts');

function addItem(name, cadence) {
	var now = new Date();
  db.put({
    _id: new Date().toJSON(),
    name: name,
    cadence: cadence, // number of messages to send monthly
    nextTimeToSendMessage: now
  });
};

function updateItem(item) {
  var updateItem = function(doc){
    db.put(item);
  };
  return db.get(item._id).then(updateItem);
};

function getAll() {
	return db.allDocs({include_docs: true}).then(function(resp){
    return new Promise(function(resolve, reject){
      if (resp) {
        resolve(resp.rows.map(row => row.doc));
      }
      else {
        reject({error: 'ERROR: no response'});
      }
    });
  });
};

function createMessages() {
	getAll().then(function(contacts){
		var now = new Date();
		contacts.filter(function(contact){
			return (now >= contact.nextTimeToSendMessage);
		}).forEach(function(contact){
			// addTask
			ActionCreator.Messages.add({
				to: contact.name
			});
			// updateContact
			now.setTime(now.getTime() + 5*1000);
			contact.nextTimeToSendMessage = now;
			ActionCreator.Contacts.update(contact);
		});
	});
};

// Facebook style store creation.
let ContactStore = assign({}, BaseStore, {

  // public methods used by Controller-View to operate on data
  getAll() {
    return getAll();
  },

  // register store with dispatcher, allowing actions to flow through
  dispatcherIndex: AppDispatcher.register(function(payload) {
    let action = payload.action;

    switch(action.type) {
      // create new messages
      case Constants.ActionTypes.CREATE_MESSAGES:
      	createMessages();
        break;
      // update contact
      case Constants.ActionTypes.UPDATE_CONTACT:
      	updateItem(action.item).then(function(resp){
          ContactStore.emitChange();
        });
        break;
    }
  })

});



module.exports = ContactStore;<commit_msg>cleaned up ContactStore<commit_after>
// TaskGeneratorStore generates tasks

// TaskGenerator
// - task that will be generated
// - when to next generate the task
// - lastTimeGenerated


// Contact
// - info about what type of task to generate
// - nextTimeToGenerateTask






// _________

// In general

// We should probably have Contacts, and Messages, and Users
// Contacts generate new Messages (in draft format)
// Users send those Messages out 



const AppDispatcher = require('../dispatchers/AppDispatcher');
const Constants = require('../constants/AppConstants');
const BaseStore = require('./BaseStore');
const assign = require('object-assign');
const ActionCreator = require('../actions/ActionCreators');

const PouchDB = require('pouchdb');

let db = new PouchDB('contacts');

function addItem(name, cadence) {
	var now = new Date();
  return db.put({
    _id: new Date().toJSON(),
    name: name,
    cadence: cadence, // number of messages to send monthly
    nextTimeToSendMessage: now
  });
};

function updateItem(item) {
  var updateItem = function(doc){
    db.put(item);
  };
  return db.get(item._id).then(updateItem);
};

function getAll() {
	return db.allDocs({include_docs: true}).then(function(resp){
    return new Promise(function(resolve, reject){
      if (resp) {
        resolve(resp.rows.map(row => row.doc));
      }
      else {
        reject({error: 'ERROR: no response'});
      }
    });
  });
};

function createMessages() {
	getAll().then(function(contacts){
		var now = new Date();
		contacts.filter(function(contact){
			return (now >= contact.nextTimeToSendMessage);
		}).forEach(function(contact){
			// addTask
			ActionCreator.Messages.add({
				to: contact.name
			});
			// updateContact
			now.setTime(now.getTime() + 5*1000);
			contact.nextTimeToSendMessage = now;
			ActionCreator.Contacts.update(contact);
		});
	});
};

// Facebook style store creation.
let ContactStore = assign({}, BaseStore, {

  // public methods used by Controller-View to operate on data
  getAll() {
    return getAll();
  },

  // register store with dispatcher, allowing actions to flow through
  dispatcherIndex: AppDispatcher.register(function(payload) {
    let action = payload.action;

    switch(action.type) {
    	// add new contact
      case Constants.ActionTypes.ADD_CONTACT:
      	addItem(action.item.name, action.item.cadence).then(function(resp){
      		ContactStore.emitChange();
      	});
        break;
    	// update contact
      case Constants.ActionTypes.UPDATE_CONTACT:
      	updateItem(action.item).then(function(resp){
          ContactStore.emitChange();
        });
        break;
      // create new messages
      case Constants.ActionTypes.CREATE_MESSAGES:
      	createMessages();
        break;
    }
  })

});



module.exports = ContactStore;
{'fraction_non_alphanumeric': 0.13267949666913398, 'fraction_numerical': 0.001850481125092524, 'mean_word_length': 3.1931730023273857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2949506', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1582, 'n_words': 399}