<commit_before>var mongoose = require('mongoose');
var PhoneGroup = require('./phone-group').PhoneGroup;
var textMessage = require('../text_message.js');

var outgoingMessageSchema = new mongoose.Schema({
  body: { type: String, required: true },
  uuid: { type: String },
  messageStatus: { type: String },
  createdAt: { type: Date },
  updatedAt: { type: Date }
});

outgoingMessageSchema.pre('save', function(next){
  now = new Date();
  this.updatedAt = now;
  if ( !this.createdAt ) {
    this.createdAt = now;
  }
  next();
});

var incomingMessageSchema = new mongoose.Schema({
  body: { type: String, required: true },
  raw: { type: String },
  createdAt: { type: Date },
  updatedAt: { type: Date }
});

incomingMessageSchema.pre('save', function(next){
  now = new Date();
  this.updatedAt = now;
  if ( !this.createdAt ) {
    this.createdAt = now;
  }
  next();
});

var phoneSchema = new mongoose.Schema({
  number: { type: Number, unique: true },
  active: { type: Boolean, required: true, default: true },
  incomingMessages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'IncomingMessage' }],
  // incomingMessages: [{
  //   raw: { type: String },
  //   body: { type: String }
  // }],
  outgoingMessages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'OutgoingMessage' }],
  createdAt: { type: Date },
  updatedAt: { type: Date }
});

phoneSchema.pre('save', function(next){
  now = new Date();
  this.updatedAt = now;
  if ( !this.createdAt ) {
    this.createdAt = now;
  }
  next();
});

phoneSchema.methods.addToGroup = function(group) {
  group.phones.addToSet(this._id)
  return group.saveAsync();
}

phoneSchema.methods.sendMessage = function(message) {
  var self = this;
  if (!self.active) {
    // do nothing now...
    //throw new Error('can not send to inactive phone');
  }
  else {
    var om = new OutgoingMessage({body: message})
    return om.save().then(function() {
      self.outgoingMessages.push(om);
      return self.saveAsync();
    }).then(function() {
      return textMessage.send(self.number, message);
    }).then(function(res) {
      // TODO: move to callback?...
      if(/queued/.test(res[0].body.message)) {
        om.messageStatus = 'queued';
        om.uuid = res[0].body.message_uuid;
        return om.saveAsync();
      }
      else {
        console.log(res[0].body);
        // do nothing now...
        //throw new Error('something went wrong with text message creation');
      }
    });
  }
};

phoneSchema.methods.processStopKeywords = function(kw) {
  var deactivate = false;
  var stopKeywords = ['stop', 'end', 'unsubscribe', 'remove', 'quit', 'block']
  stopKeywords.forEach(function(skw) {
    if (kw.toLowerCase() == skw.toLowerCase()) {
      this.sendMessage(process.env.UNSUB_MESSAGE);
      deactivate = true;
    }
  }.bind(this));
  this.active = deactivate ? false : true;
};

phoneSchema.methods.processHelpKeyword = function() {
  //var activeState = this.active;
  this.sendMessage(process.env.HELP_MESSAGE);
}


var Phone = mongoose.model('Phone', phoneSchema);
var OutgoingMessage = mongoose.model('OutgoingMessage', outgoingMessageSchema);
var IncomingMessage = mongoose.model('IncomingMessage', incomingMessageSchema);

Phone.handleIncomingMessage = function(values) {
  //if ( !values['From'] ) { throw 'from value is undef.' }
  console.log (values);
  var im = new IncomingMessage({ raw: JSON.stringify(values), body: values.Text });
  console.log (im);
  var firstWord = im.body.trim().split(' ')[0];
  var phoneId;
  return Promise.resolve(Phone.findOne({number: values.From}).execAsync()
    .then(function(p) {
      p = p ? p : new Phone({number: values.From})
      p.incomingMessages.push(im);<commit_msg>debugging<commit_after>var mongoose = require('mongoose');
var PhoneGroup = require('./phone-group').PhoneGroup;
var textMessage = require('../text_message.js');

var outgoingMessageSchema = new mongoose.Schema({
  body: { type: String, required: true },
  uuid: { type: String },
  messageStatus: { type: String },
  createdAt: { type: Date },
  updatedAt: { type: Date }
});

outgoingMessageSchema.pre('save', function(next){
  now = new Date();
  this.updatedAt = now;
  if ( !this.createdAt ) {
    this.createdAt = now;
  }
  next();
});

var incomingMessageSchema = new mongoose.Schema({
  body: { type: String, required: true },
  raw: { type: String },
  createdAt: { type: Date },
  updatedAt: { type: Date }
});

incomingMessageSchema.pre('save', function(next){
  now = new Date();
  this.updatedAt = now;
  if ( !this.createdAt ) {
    this.createdAt = now;
  }
  next();
});

var phoneSchema = new mongoose.Schema({
  number: { type: Number, unique: true },
  active: { type: Boolean, required: true, default: true },
  incomingMessages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'IncomingMessage' }],
  // incomingMessages: [{
  //   raw: { type: String },
  //   body: { type: String }
  // }],
  outgoingMessages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'OutgoingMessage' }],
  createdAt: { type: Date },
  updatedAt: { type: Date }
});

phoneSchema.pre('save', function(next){
  now = new Date();
  this.updatedAt = now;
  if ( !this.createdAt ) {
    this.createdAt = now;
  }
  next();
});

phoneSchema.methods.addToGroup = function(group) {
  group.phones.addToSet(this._id)
  return group.saveAsync();
}

phoneSchema.methods.sendMessage = function(message) {
  var self = this;
  if (!self.active) {
    // do nothing now...
    //throw new Error('can not send to inactive phone');
  }
  else {
    var om = new OutgoingMessage({body: message})
    return om.save().then(function() {
      self.outgoingMessages.push(om);
      return self.saveAsync();
    }).then(function() {
      return textMessage.send(self.number, message);
    }).then(function(res) {
      // TODO: move to callback?...
      if(/queued/.test(res[0].body.message)) {
        om.messageStatus = 'queued';
        om.uuid = res[0].body.message_uuid;
        return om.saveAsync();
      }
      else {
        console.log(res[0].body);
        // do nothing now...
        //throw new Error('something went wrong with text message creation');
      }
    });
  }
};

phoneSchema.methods.processStopKeywords = function(kw) {
  var deactivate = false;
  var stopKeywords = ['stop', 'end', 'unsubscribe', 'remove', 'quit', 'block']
  stopKeywords.forEach(function(skw) {
    if (kw.toLowerCase() == skw.toLowerCase()) {
      this.sendMessage(process.env.UNSUB_MESSAGE);
      deactivate = true;
    }
  }.bind(this));
  this.active = deactivate ? false : true;
};

phoneSchema.methods.processHelpKeyword = function() {
  //var activeState = this.active;
  this.sendMessage(process.env.HELP_MESSAGE);
}


var Phone = mongoose.model('Phone', phoneSchema);
var OutgoingMessage = mongoose.model('OutgoingMessage', outgoingMessageSchema);
var IncomingMessage = mongoose.model('IncomingMessage', incomingMessageSchema);

Phone.handleIncomingMessage = function(values) {
  //if ( !values['From'] ) { throw 'from value is undef.' }
  console.log (values);
  var im = new IncomingMessage({ raw: JSON.stringify(values), body: values.Text });
  console.log (im);
  var firstWord = im.body.trim().split(' ')[0];
  var phoneId;
  return Promise.resolve(Phone.findOne({number: values.From}).execAsync()
    .then(function(p) {
      console.log("p:", p);
      p = p ? p : new Phone({number: values.From})
      console.log("now p:", p);
      p.incomingMessages.push(im);
{'fraction_non_alphanumeric': 0.1642550911039657, 'fraction_numerical': 0.0010718113612004287, 'mean_word_length': 3.4807923169267707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5801487', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2402, 'n_words': 540}