Skip to content

Instantly share code, notes, and snippets.

@josegl
Created October 28, 2015 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josegl/66d5e8740c6f91b0f102 to your computer and use it in GitHub Desktop.
Save josegl/66d5e8740c6f91b0f102 to your computer and use it in GitHub Desktop.
passing to es6 class
diff --git a/server/modules/ventaLead/lib/VentaLead.js b/server/modules/ventaLead/lib/VentaLead.js
index 563c2bc..dede967 100644
--- a/server/modules/ventaLead/lib/VentaLead.js
+++ b/server/modules/ventaLead/lib/VentaLead.js
@@ -5,87 +5,76 @@ var Car = require('../../car');
var util = require('../../../../lib/util');
var Canalcar = require('../../../../lib/canalcarApiClient');
-var VentaLead = function (rawVentaLead){
- var self = this;
- if (rawVentaLead){
- this.car = new Car(rawVentaLead.car);
- this.client = new Client(rawVentaLead.client);
- this.inquiry = rawVentaLead.inquiry;
- this.source = rawVentaLead.source;
- this.gmail_msg_id = rawVentaLead.gmail_msg_id;
- } else {
- this.car = new Car(null);
- this.client = new Client(null);
- this.inquiry = null;
- this.source = null;
- this.gmail_msg_id = null;
+class VentaLead {
+ constructor(rawVentaLead) {
+ if (rawVentaLead){
+ this.car = new Car(rawVentaLead.car);
+ this.client = new Client(rawVentaLead.client);
+ this.inquiry = rawVentaLead.inquiry;
+ this.source = rawVentaLead.source;
+ this.gmail_msg_id = rawVentaLead.gmail_msg_id;
+ } else {
+ this.car = new Car(null);
+ this.client = new Client(null);
+ this.inquiry = null;
+ this.source = null;
+ this.gmail_msg_id = null;
+ }
}
- this.save = function (){
- return new Promise(function(resolve, reject){
- self.client.createOrUpdate().then(function (createdOrUpdatedClient){
- self.client = createdOrUpdatedClient;
- return self.car.createOrUpdate();
- }).then(function(createdOrUpdatedCar){
- self.car = createdOrUpdatedCar;
- var ventaLead = self._composeVentaLead();
- return self._saveVentaLead(ventaLead);
- }).then(function(){
- resolve();
- }).catch (function(err){
+ save(){
+ var result = {ok: true};
+ return new Promise((resolve, reject) =>{
+ if (!this.validate()){
+ result.ok = false;
+ resolve (result);
+ }
+ this.client.createOrUpdate().then(createdOrUpdatedClient => {
+ this.client = createdOrUpdatedClient;
+ return this.car.createOrUpdate();
+ }).then(createdOrUpdatedCar => {
+ this.car = createdOrUpdatedCar;
+ var ventaLead = this._composeVentaLead();
+ return this._saveVentaLead(ventaLead);
+ }).then(() => {
+ resolve(result);
+ }).catch (err => {
reject(err);
});
});
}
- this.validate = function (){
- if ((/llamada perdida/i.test(self.inquiry) ||
- /llamada atendida/i.test(self.inquiry)))
- return (self.client.validate());
+
+ validate(){
+ if ((/llamada perdida/i.test(this.inquiry) ||
+ /llamada atendida/i.test(this.inquiry)))
+ return (this.client.validate());
else
- return (self.car.validate() && self.client.validate());
+ return (this.car.validate() && this.client.validate());
}
-}
-
-/* OJO. Esta funcion no la estamos usando */
-VentaLead.prototype._ventaLeadAlreadyInMongo = function (ventaLead){
- return new Promise(function(resolve, reject){
- var query = {gmail_msg_id: ventaLead.gmail_mgs_id};
- LeadVentaModel.find(query, function (err, foundLead){
- if (err)
- reject(err);
- resolve(foundLead);
- });
- });
-}
-VentaLead.prototype._saveVentaLead = function (ventaLead){
- return new Promise (function (resolve, reject){
- var leadToSave = new LeadVentaModel (ventaLead);
- leadToSave.save (function (err){
- if (err)
- reject(err);
- resolve();
+ _saveVentaLead(){
+ return new Promise ((resolve, reject) => {
+ var leadToSave = new LeadVentaModel (ventaLead);
+ leadToSave.save (err => {
+ if (err)
+ reject(err);
+ resolve();
+ });
});
- });
-}
+ }
-VentaLead.prototype._composeVentaLead = function (){
- var car = this.car;
- var client = this.client;
- var inquiry = this.inquiry;
- var source = this.source;
- var gmail_msg_id = this.gmail_msg_id;
- var carId = null;
- if (car !== null && car !== undefined)
- carId = car.id;
- var lead = {
- car_id : carId,
- client_id : client.id,
- inquiry : inquiry,
- source : source,
- gmail_msg_id: gmail_msg_id
- };
- return lead;
+ _composeVentaLead(){
+ var carId = null;
+ if (this.car !== null && this.car !== undefined)
+ carId = this.car.id;
+ return {
+ car_id : carId,
+ client_id : this.client.id,
+ inquiry : this.inquiry,
+ source : this.source,
+ gmail_msg_id: this.gmail_msg_id
+ };
+ }
}
module.exports = VentaLead;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment