import twilio from 'twilio';

var phonesToContact = function() {
  var startYear = moment().startOf('year').toDate();
  var previousYearsPhones = _.uniq(Orders.find({
    courseName: {$regex: /^((?!-12).)*$/m},
    createdAt: {$lt: startYear}
  }, {
    fields: {phone: 1}
  }).map(function(e){return e.phone}));

  var thisYearPhones = _.uniq(Orders.find({
    courseName: {$regex: /^((?!-12).)*$/m},
    createdAt: {$gt: startYear}
  }, {
    fields: {phone: 1}
  }).map(function(e){return e.phone}));

  return _.compact(_.difference(previousYearsPhones, thisYearPhones));
};

var smsJobs = JobCollection('smsJobQueue');

Meteor.startup(function () {
  smsJobs.startJobServer();
  smsJobs.processJobs(
    'sms',
    {
      concurrency: 4,
      pollInterval: 1000
    },
    function (job, callback) {
      // Create a client:
      var client = new twilio.RestClient(Settings.findOne({key: 'SMS-ACCOUNT-SID'}).value, Settings.findOne({key: 'SMS-AUTH-TOKEN'}).value );
      console.log('myTwilio.RestClient initialized');
      var to = '+351' + job.data.phone;

      client.sendMessage({
        to: to, // Any number Twilio can deliver to
        from: Settings.findOne({key: 'sms-from'}).value, // +15005550006 : A number you bought from Twilio and can use for outbound communication
        body: job.data.text // body of the SMS message
      }, Meteor.bindEnvironment((err,res) => {
        if (!err) {
          console.log('myTwilio.RestClient resp from : ' + res.from); // outputs "+14506667788"
          console.log('myTwilio.RestClient resp body: ' + res.body); // outputs "word to your mother."
          job.done('sms sent!');
          job.remove();
        } else {
          pretty_error = JSON.stringify(err, null, 2)
          console.log('err: ' + pretty_error);
          job.fail(pretty_error);
        }
        callback()
      }));
    }
  );
});

Meteor.methods({
  sendSMSAll: function (text) {
    check([text], [String]);
    _.each(phonesToContact(), function(phone) {
      var job = new Job(smsJobs, 'sms', {
        phone: phone,
        text: text
      });

      job.priority('normal')
      .retry({
        retries: 5,
        wait: 60
      }).save();
    });
  },
  priceSMSs: function() {
    return phonesToContact().length * 0.05;
  }
});

@@ -796,24 +796,76 @@
 callback) %7B%0A
+      if (job.data.phone.substring(0,1) != '2') %7B%0A  
       // Cre
@@ -878,16 +878,18 @@
 client:%0A
+  
       va
@@ -1024,24 +1024,26 @@
 ue );%0A      
+ 
+ 
 console.log(
@@ -1080,24 +1080,26 @@
 ed');%0A      
+  
 var to = '+3
@@ -1127,16 +1127,18 @@
 %0A%0A      
+ 
+ 
 client.s
@@ -1150,16 +1150,18 @@
 ssage(%7B%0A
+  
         
@@ -1212,16 +1212,18 @@
         
+  
 from: Se
@@ -1360,16 +1360,18 @@
         
+  
 body: jo
@@ -1415,16 +1415,18 @@
 e%0A      
+  
 %7D, Meteo
@@ -1458,16 +1458,18 @@
 s) =%3E %7B%0A
+  
         
@@ -1482,32 +1482,34 @@
 rr) %7B%0A          
+  
 console.log('myT
@@ -1578,16 +1578,18 @@
 667788%22%0A
+  
         
@@ -1689,24 +1689,26 @@
 %22%0A          
+  
 job.done('sm
@@ -1714,24 +1714,26 @@
 ms sent!');%0A
+  
           jo
@@ -1752,16 +1752,18 @@
         
+  
 %7D else %7B
@@ -1763,16 +1763,18 @@
  else %7B%0A
+  
         
@@ -1821,32 +1821,34 @@
 l, 2)%0A          
+  
 console.log('err
@@ -1860,32 +1860,34 @@
  pretty_error);%0A
+  
           job.fa
@@ -1912,18 +1912,22 @@
         
-%7D%0A
+  %7D%0A  
         
@@ -1943,21 +1943,103 @@
 )%0A      
+  
 %7D));%0A
+      %7D else %7B%0A        job.done('not a mobile!');%0A        job.remove();%0A      %7D%0A
     %7D%0A  
@@ -2168,24 +2168,76 @@
 on(phone) %7B%0A
+      if (job.data.phone.substring(0,1) != '2') %7B%0A  
       var jo
@@ -2274,16 +2274,18 @@
         
+ 
+ 
 phone: p
@@ -2298,16 +2298,18 @@
         
+  
 text: te
@@ -2317,21 +2317,25 @@
 t%0A      
+ 
+ 
 %7D);%0A%0A
+  
       jo
@@ -2361,16 +2361,18 @@
 )%0A      
+ 
+ 
 .retry(%7B
@@ -2372,16 +2372,18 @@
 retry(%7B%0A
+  
         
@@ -2402,16 +2402,18 @@
         
+ 
+ 
 wait: 60
@@ -2413,16 +2413,18 @@
 ait: 60%0A
+  
       %7D)
@@ -2428,24 +2428,32 @@
  %7D).save();%0A
+      %7D%0A
     %7D);%0A  %7D,

Send only to mobile!