Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.84 KB | None | 0 0
  1. var _ = require('lodash'),
  2. fs = require('fs'),
  3. path = require('path'),
  4. lights = require(path.resolve('./modules/lights/server/controllers/lights.server.controller')),
  5. catcher = require(path.resolve('./modules/admins/server/controllers/catcher.server.controller')),
  6. mongoose = require('mongoose'),
  7. moment = require('moment'),
  8. config = require(path.resolve('./config/config')),
  9. isapi = require('infusionsoft-api'),
  10. is = new isapi.DataContext('XXXXX', 'XXXXX'), //infusionsoft
  11. Trans = mongoose.model('Trans'),
  12. Coupon = mongoose.model('Coupon'),
  13. Light = mongoose.model('Light'),
  14. Space = mongoose.model('Space'),
  15. History = mongoose.model('History'),
  16. stripe = require('stripe')('xxxxx'),
  17. pdf = require('pdfkit'),
  18. async = require("async"),
  19. randomstring = require("randomstring"),
  20. easypost = require('node-easypost')('xxxxx'),
  21. AWS = require('aws-sdk'),
  22. emailjs = require('emailjs');
  23.  
  24.  
  25. AWS.config.region = 'us-west-2';
  26. AWS.config.update({
  27. accessKeyId: 'XXXX',
  28. secretAccessKey: 'xxxxx'
  29. });
  30. var s3 = new AWS.S3();
  31.  
  32. var server = emailjs.server.connect({
  33. user: "support@diylightingsavings.com",
  34. password: "A1b2c3d4e5",
  35. host: "smtpout.secureserver.net",
  36. ssl: false,
  37. port: 80
  38. });
  39.  
  40. var fromAddress = {
  41. name: "Spot Lighting Supplies",
  42. street1: "1200 Oregon Ave",
  43. //street2: "4th Floor",
  44. city: "Long Beach",
  45. state: "CA",
  46. zip: "90813",
  47. phone: "415-123-4567"
  48. };
  49.  
  50. exports.final = function (req, res) {
  51. //req.user = req.body.saving.user; // the user we will buy for
  52. var rand = randomstring.generate(7);
  53. req.rand = rand;
  54. var parcel = calculteSize(req.saving.lights);
  55. var StripeObj = { // this is the Stripe Object
  56. currency: "usd",
  57. source: req.body.token, // obtained with Stripe.js
  58. description: "Infusionsoft ID: " + req.user.isId,
  59. shipping: {
  60. address : {
  61. line1: req.body.shipping.line1,
  62. line2: req.body.shipping.line2,
  63. state: req.body.shipping.state,
  64. city: req.body.shipping.city,
  65. postal_code: req.body.shipping.postal_code,
  66. country: "US",
  67. },
  68. name : req.body.shipping.firstName + " " + req.body.shipping.lastName
  69. }
  70. };
  71. updateUserInIs({
  72. Address2Street1: req.body.shipping.line1,
  73. Address2Street2: req.body.shipping.line2,
  74. State2: req.body.shipping.state,
  75. City2: req.body.shipping.city,
  76. PostalCode2: req.body.shipping.postal_code,
  77. EmailAddress2: req.body.shipping.email,
  78. Country2: "United States",
  79. Phone1: req.body.shipping.phone
  80. }, req.user.isId);
  81. StripeObj.receipt_email = req.body.shipping.email;
  82. shipping(req, function(shippingErr, shipping){ // calculate the shipping costs
  83. if(shippingErr){
  84. console.log("Shipment error");
  85. return status(200).send(shippingErr);
  86. } else {
  87. console.log("shipment succeed");
  88. }
  89.  
  90. var totalShipping = 0; // add the shipping price
  91. for(var k in shipping){
  92. totalShipping += Number(lowerShiping(shipping[k]).rate);
  93. }
  94. catcher.catch({
  95. ip: req.userIp,
  96. user: req.user._id,
  97. label: "success",
  98. title: "Shipping rate for purchase",
  99. rand: rand,
  100. json: shipping
  101. });
  102. //console.log("total shipping costs: " + totalShipping);
  103. //console.log("coupon code: " + req.body.coupon);
  104. coupon(req.body.coupon, function(err, discount){
  105. var total;
  106. console.log(err, discount);
  107. var disc = 0;
  108. if(err){
  109. total = req.saving.price + totalShipping;
  110. } else {
  111. if(discount.value){
  112. //console.log("discount: ", discount);
  113. if(discount.type === 'Percentage' && (req.saving.price - ( req.saving.price * discount.value / 100 ) + totalShipping) > 200){
  114. disc = req.saving.price * discount.value / 100;
  115. total = req.saving.price - ( req.saving.price * discount.value / 100 ) + totalShipping;
  116. } else if(discount.type === 'Money' && (req.saving.price - discount.value + totalShipping) > 200){
  117. disc = discount.value;
  118. total = req.saving.price - discount.value + totalShipping;
  119. } else {
  120. total = req.saving.price + totalShipping;
  121. }
  122. } else {
  123. total = req.saving.price + totalShipping;
  124.  
  125. }
  126. }
  127.  
  128. console.log("Origin price: " + req.saving.price);
  129. console.log("discount: " + disc);
  130. console.log("shipping: " + totalShipping);
  131. console.log("toital: " + total);
  132.  
  133. total = total.toFixed(2); // total price to charge the client
  134. total = Math.round( (total ) * 100 ); // this is the point where the amount is being transfered to cents
  135. StripeObj.amount = total;
  136. var labels = [];
  137. var tracking = [];
  138. var shippingIds = [];
  139. stripe.charges.create(StripeObj, function(errStripe, charge) { // then we charge the card!
  140. // change the Infusionsfot tag
  141. is.ContactService.addToGroup(req.user.isId,138).done(function(results){
  142. //console.log("Add buy tag: " + results);
  143. });
  144. if(errStripe){
  145. console.log("Stripe Error : " , errStripe);
  146. return res.status(200).send(errStripe);
  147. catcher.catch({
  148. ip: req.userIp,
  149. user: req.user._id,
  150. label: "fail",
  151. title: "Stripe fail",
  152. json: errStripe,
  153. rand: rand,
  154. alert: true
  155. });
  156. } else {
  157. console.log("Stripe succeed");
  158. res.status(200).json({statusCode: 200});
  159. catcher.catch({
  160. ip: req.userIp,
  161. user: req.user._id,
  162. label: "success",
  163. title: "Stripe success",
  164. json: charge,
  165. rand: rand
  166. });
  167. // and we continue:
  168. async.each(shipping, function(item, buyShipmentCb){
  169. buyShipping(item, function(errBuyShipping, shipment){ // then we buy the shipping
  170. if(errBuyShipping){
  171. console.log("error in buy shipping");
  172. console.log("erro in buy shipment: " ,errBuyShipping);
  173. catcher.catch({
  174. ip: req.userIp,
  175. user: req.user._id,
  176. label: "fail",
  177. title: "Fail to buy shipping",
  178. json: errBuyShipping,
  179. rand: rand,
  180. alert: true
  181. });
  182. return buyShipmentCb(errBuyShipping);
  183. } else {
  184. console.log("Shipment buy success");
  185. if(shipment.postage_label){
  186. labels.push(shipment.postage_label.label_url);
  187. } else{
  188. labels.push("No Label");
  189. }
  190. item.label = shipment.postage_label.label_url || "No label";
  191. catcher.catch({
  192. ip: req.userIp,
  193. user: req.user._id,
  194. label: "success",
  195. title: "Success buy shipping",
  196. json: item,
  197. rand: rand
  198. });
  199. buyShipmentCb(null);
  200. }
  201. shippingIds.push(shipment.id);
  202. //console.log("Buy shipping success! " , shipment.id);
  203. });
  204. }, function(errEach){
  205. if(errEach){
  206. console.log(errEach);
  207. } else {
  208. //console.log(shipping);
  209. var trans = new Trans({
  210. stripe: charge,
  211. saving: req.body.saving,
  212. tracking: tracking,
  213. label: labels,
  214. shippingId: shippingIds,
  215. rand: req.rand,
  216. user: req.user._id
  217. });
  218. trans.save(trans, function(errTrans, trans){ //save the info
  219. if(errTrans){
  220. console.log("error in save trans");
  221. catcher.catch({
  222. ip: req.userIp,
  223. user: req.user._id,
  224. label: "error",
  225. title: "Error in saving transaction",
  226. content: trans,
  227. json: errTrans,
  228. rand: rand
  229. });
  230. } else{
  231. console.log("transaction saved");
  232. recycle(rand, trans._id, req.user._id, req.saving.lights, total/100, function(){
  233. });
  234. }
  235. });
  236. catcher.catch({
  237. ip: req.userIp,
  238. user: req.user._id,
  239. label: "success",
  240. title: "Success in buy",
  241. json: trans,
  242. rand: rand
  243. });
  244. emailProcess(req, rand, labels, parcel, function(errEmail){ // and sending the email to the vendor
  245. if(errEmail){
  246. console.log("error in sending email");
  247. catcher.catch({
  248. ip: req.userIp,
  249. user: req.user._id,
  250. label: "error",
  251. title: "Error in sending email to vendor",
  252. content: labels,
  253. json: errEmail,
  254. rand: rand
  255. });
  256. } else {
  257. console.log("email sent");
  258. }
  259. });
  260. }
  261. });
  262. }
  263. });
  264. });
  265. });
  266. };
  267.  
  268. exports.shipping = function (req, res) {
  269. console.log("Calculate shipping");
  270. shipping(req, function(err, shipping){
  271. if(err){
  272. console.log(err);
  273. return res.send(err);
  274. } else {
  275. //console.log("Shipping is done", shipping);
  276. }
  277. var total = 0;
  278. for(var k in shipping){
  279. var single = lowerShiping(shipping[k]);
  280. if(single !== false)
  281. total += Number(single.rate);
  282. //console.log(Number(lowerShiping(shipping[k]).rate));
  283. }
  284.  
  285. if(total === 0){
  286. return res.send({message:"Error"});
  287. } else {
  288. return res.send({rate: total});
  289. }
  290. });
  291. };
  292.  
  293. exports.email = function (req, res) {
  294. emailProcess(req, function(err){
  295. res.send();
  296. });
  297. };
  298.  
  299. exports.parcel = function (req, res) {
  300. var re = calculteSize(req.saving.lights);
  301. res.send(re);
  302. };
  303.  
  304. exports.findCoupon = function (req, res) {
  305. coupon(req.params.coupon ,function(err, coupon){
  306. if(err)
  307. res.send(err);
  308. else
  309. res.send(coupon);
  310. });
  311. };
  312.  
  313. function coupon(name, callback){
  314. if(!name)
  315. return callback(true, null);
  316. Coupon.find({name : name, exp: {"$gte": new Date()}}, { exp :0, quantity:0, usage:0, limit:0, created:0, _id:0 }, function(err, doc){
  317. if(err || !doc.length){
  318. callback(err, null);
  319. } else {
  320. callback(null, doc[0]);
  321. }
  322. });
  323. }
  324.  
  325. function Varify(toAddress, callback){
  326.  
  327. easypost.Address.create(toAddress, function(err, toAddress) {
  328. toAddress.verify(function(err, response) {
  329. //console.log("varification:");
  330. //console.log(err, response);
  331. if (err) {
  332. console.log('Address is invalid.' , err);
  333. return callback(err);
  334. }
  335. else
  336. return callback(null);
  337. });
  338. });
  339.  
  340. };
  341.  
  342. function shippingFunction(toAddress, fromAddress, parcel, callback){
  343.  
  344. easypost.Shipment.create({
  345. to_address: toAddress,
  346. from_address: fromAddress,
  347. parcel: parcel
  348. }, function(err, shipment) {
  349. //console.log(err, shipment);
  350. if(err)
  351. return callback(err,null);
  352. return callback(null, shipment);
  353.  
  354. });
  355.  
  356. }
  357.  
  358. function buyShipping(shipment, callback){
  359. //console.log(shipment);
  360. shipment.buy({rate: shipment.lowestRate(['USPS'])}, function(err, shipment) {
  361. if(err){
  362. return callback(err, null);
  363. }
  364. return callback(null, shipment);
  365. });
  366. }
  367.  
  368. function shipping(req, finalCb){
  369. var results = [];
  370. var toAddress = {
  371. name: req.body.shipping.firstName + " " + req.body.shipping.lastName,
  372. street1: req.body.shipping.line1,
  373. street2: req.body.shipping.line2,
  374. city: req.body.shipping.city,
  375. state: req.body.shipping.state,
  376. zip: req.body.shipping.postal_code,
  377. country: "US",
  378. phone: req.body.shipping.phone
  379. };
  380. //console.log("to address" , toAddress);
  381. Varify(toAddress, function(err){
  382. if(err){
  383. console.log("bad address");
  384. finalCb(err, null);
  385. return catcher.catch({
  386. ip: req.userIp,
  387. user: req.user._id,
  388. label: "fail",
  389. title: "Fail to varify address",
  390. json: err
  391. });
  392. } else {
  393. console.log("Adrress legit!");
  394. catcher.catch({
  395. ip: req.userIp,
  396. user: req.user._id,
  397. label: "success",
  398. title: "Succcess to varify address",
  399. json: err
  400. });
  401. }
  402. var parcel = calculteSize(req.saving.lights);
  403. //console.log(parcel);
  404. async.each(parcel, function(item, eachCb) {
  405. //console.log("each");
  406. shippingFunction(toAddress, fromAddress, item.dimensions, function(err, shipment){
  407. if(err){
  408. eachCb(err);
  409. } else {
  410. shipment.items = item;
  411. results.push(shipment);
  412. eachCb();
  413. }
  414. });
  415. }, function(err){
  416. //console.log("done generating the shipments");
  417. if(err){
  418. catcher.catch({
  419. ip: req.userIp,
  420. user: req.user._id,
  421. label: "fail",
  422. title: "Fail to generate shipping",
  423. json: err
  424. });
  425. console.log(err);
  426. return finalCb(err, null);
  427. } else {
  428. catcher.catch({
  429. ip: req.userIp,
  430. user: req.user._id,
  431. label: "success",
  432. title: "Success to create shipping",
  433. json: results
  434. });
  435. return finalCb(null, results);
  436. }
  437.  
  438. });
  439. });
  440. };
  441.  
  442. function lowerShiping(shipping){
  443. var low = 100000.00;
  444. var obj;
  445. if(shipping.rates.length === 0)
  446. return false;
  447. for(var k in shipping.rates){
  448. if(Number(shipping.rates[k].rate) < Number(low)){
  449. obj = shipping.rates[k];
  450. low = shipping.rates[k].rate;
  451. }
  452. }
  453. return obj;
  454. }
  455.  
  456. function calculteSize(items){
  457. var bigBox = {
  458. length: 17,
  459. width: 17,
  460. height: 18
  461. };
  462. var smallBox = {
  463. length: 10,
  464. width: 10,
  465. height: 10
  466. };
  467. var maxSize = 18*17*17*0.8;
  468. var totalSize = 0;
  469. var boxes = [];
  470. var boxSize = 0;
  471. var boxWeight = 0;
  472. var boxObj = {arr:[]};
  473. for(var k in items){
  474. var tempQ = items[k].quatity;
  475. for(var p = 0 ; p < tempQ; p++){
  476. var tempS = items[k].replace.size;
  477. var tempW = items[k].replace.weight;
  478. if( (boxSize + tempS) < maxSize ){
  479. boxObj.arr.push(items[k]);
  480. boxSize += tempS;
  481. boxWeight += tempW;
  482. } else {
  483. //seal the old box
  484. boxObj.dimensions = bigBox;
  485. boxObj.dimensions.weight = boxWeight;
  486. boxObj.weight = boxWeight;
  487. boxObj.size = boxSize;
  488. boxes.push(boxObj);
  489. //open a new box
  490. var boxSize = 0;
  491. var boxWeight = 0;
  492. var boxObj = {arr:[]};
  493. //push the item to the new box
  494. boxObj.arr.push(items[k]);
  495. boxWeight += tempW;
  496. boxSize += tempS;
  497. }
  498. }
  499.  
  500. if(Number(k) === items.length -1){
  501. boxObj.dimensions = bigBox;
  502. boxObj.dimensions.weight = boxWeight;
  503. boxObj.weight = boxWeight;
  504. boxObj.size = boxSize;
  505. boxes.push(boxObj);
  506.  
  507. //resize the last box
  508. if(boxes[boxes.length-1].size < (10*10*10*0.8)){
  509. boxes[boxes.length-1].dimensions = smallBox;
  510. boxes[boxes.length-1].dimensions.weight = boxes[boxes.length-1].weight;
  511. }
  512. }
  513.  
  514. }
  515. return boxes;
  516. // 2 obejects^
  517. }
  518.  
  519. function LightsByRoom(list){
  520. var rooms = [];
  521. for(var k in list){
  522. var find = true;
  523. for(var i in rooms){
  524. if(rooms[i].id === list[k].space._id){
  525. rooms[i].lights.push(list[k]);
  526. find = false;
  527. continue;
  528. }
  529. }
  530. if(find){
  531. rooms.push({
  532. id: list[k].space._id,
  533. name: list[k].space.name,
  534. inout: list[k].space.inout,
  535. lights: [list[k]]
  536. });
  537. }
  538. }
  539. return rooms;
  540. };
  541.  
  542. function emailProcess(req, rand, label, parcel, callback){
  543. //console.log("email process");
  544. var fullName = req.body.user.firstName + " " + req.body.user.lastName;
  545. async.parallel([
  546. function(callback) {
  547. //console.log("start PDF 1");
  548. userPdf(req.saving, rand, req.user.isId, function() {
  549. callback();
  550. });
  551. },
  552. function(callback) {
  553. //console.log("start PDF 2");
  554. vendorPdf(parcel, rand, fullName, callback);
  555. }
  556. ], function(err) {
  557. if(err)
  558. return callback(err);
  559. //console.log("pdf success!");
  560. email(rand,{toEmail: req.body.user.email, label: label, name: fullName}, function(err2){
  561. //console.log('Email Sent');
  562. return callback(err);
  563. });
  564. });
  565. }
  566.  
  567. function email(rand, details, callback){
  568. var text = "DYI Lighting Savings \nShipping Labels: ";
  569. for(var k in details.label){
  570. text += details.label[k] + "\n";
  571.  
  572. }
  573. text += '\nTransaction# : ' + rand + "\nName: " + details.name;
  574. server.send({
  575. text: text,
  576. from: "DIY Lighting Savings <support@diylightingsavings.com>",
  577. to: "bn.barak@gmail.com, maxpalacio@gmail.com, brennen@spot-lighting.com",
  578. subject: "DYI Lighting Savings #"+rand,
  579. attachment:
  580. [
  581. {path:"/tmp/User-" + rand + ".pdf", type:"application/pdf", name:"Instructions-" + rand + ".pdf"},
  582. {path:"/tmp/Vendor-" + rand + ".pdf", type:"application/pdf", name:"Vendor-" + rand +".pdf"}
  583. ]
  584. }, function(err, message) {
  585. //console.log(err || message);
  586. if(err)
  587. return callback(err);
  588. else
  589. return callback(null);
  590. });
  591. }
  592.  
  593. function userPdf(saving, rand, isId, callback){
  594. var rooms = LightsByRoom(saving.lights);
  595. var doc = new pdf({"margins" : { // by default, all are 72
  596. "top": 72,
  597. "bottom": 0,
  598. "left": 72,
  599. "right": 72
  600. }});
  601. var v = 0;
  602. var pages = 0;
  603. var name = "/tmp/User-" + rand + ".pdf";
  604. doc.pipe(fs.createWriteStream(name));
  605. doc.rect(0,0,612,46).lineWidth(0).fill("#4CAF50");
  606. doc.fill("#fff");
  607. doc.text("DYI Lighting Saving", 20 ,20);
  608. doc.text("#" + rand, 300 ,20);
  609. doc.text(moment().format("MMM-DD-YYYY"), 470 ,20); V(80);
  610. doc.fill("#000").fontSize("20").text("Thank You for Choosing Lighting Savings", 130, v); V(40);
  611. doc.fill("#212121").fontSize("18").text("Follow the user instructions to start saving", 150, v); V(50);
  612. doc.fill("#000").fontSize("20").text("User Instructions", 20, v); V(30);
  613. for(var k in rooms){
  614. doc.fontSize(18).text("Room: ", 30, v); doc.fontSize(18).fill("#2E7D32").text(rooms[k].name, 90, v).fill("#000"); V(30);
  615. var lights = rooms[k].lights;
  616. for(var i in lights){
  617. doc.fontSize(14).text("Repalce - "+lights[i].name + " - with " + lights[i].replace.name , 40, v); V(20);
  618. }
  619. doc.strokeColor("#4CAF50").moveTo(40, v).lineTo(550,v).stroke() ; V(30);
  620. }
  621.  
  622. footer();
  623. doc.end();
  624.  
  625. doc.on('data', function (data) {
  626. //res.write(data);
  627. });
  628.  
  629. doc.on('end', function(){
  630. var params = { Bucket: 'user-pdf', Body: fs.readFileSync(name), ACL: 'public-read', Key: "User-" + rand + ".pdf" };
  631. console.log("isId: ", isId);
  632. s3.upload(params, function(err, S3data) {
  633. console.log(err , S3data);
  634. is.ContactService.update(isId, {
  635. EmailAddress2: S3data.Location
  636. });
  637. callback();
  638. });
  639.  
  640. });
  641.  
  642. function V(i){
  643. if(i+v > 700 ){
  644. doc.rect(0,755,612,40).lineWidth(0).fill("#E0E0E0");
  645. doc.addPage();
  646. v = i;
  647. pages++;
  648. }
  649. v += i;
  650. }
  651.  
  652. function footer(){
  653. doc.rect(0,755,612,40).lineWidth(0).fill("#E0E0E0");
  654. doc.fill("#000").text("All rights reserved © something 2016", 20 , 765);
  655. }
  656.  
  657. };
  658.  
  659. function vendorPdf(saving, rand, fullName, callback){
  660. var doc = new pdf({"margins" : { // by default, all are 72
  661. "top": 72,
  662. "bottom": 0,
  663. "left": 72,
  664. "right": 72
  665. }});
  666. var v = 0;
  667. var pages = 0;
  668. var name = "/tmp/Vendor-" + rand + ".pdf";
  669. doc.pipe(fs.createWriteStream(name));
  670.  
  671. doc.rect(0,0,612,46).lineWidth(0).fill("#4CAF50");
  672. doc.fill("#fff");
  673. doc.text("DYI Lighting Saving", 20 ,20);
  674. doc.text(moment().format("MMM-DD-YYYY"), 470 ,20); V(80);
  675.  
  676. //doc.fontSize(16).fill("#000").text("Client id: " + saving.user.isId || "" ,30 ,v); V(20);
  677. doc.fontSize(16).fill("#000").text("Transaction id: " + rand ,30 ,v); V(20);
  678. doc.fontSize(16).fill("#000").text("Full Name: " + fullName ,30 ,v); V(20);
  679. doc.fontSize(16).fill("#000").text("Products List", 30 ,v); V(30);
  680. for(var k in saving){
  681. var num = Number(k) +1;
  682. doc.fontSize(15).fill("#000").text("Box " +num, 30 ,v); V(20);
  683. doc.fontSize(14).fill("#000").text("# - Product Name - SK - Quantity", 30 ,v); V(20);
  684. for(var i in saving[k].arr){
  685. doc.fontSize(12).fill("#000").text(Number(i)+1 +" - " + saving[k].arr[i].replace.name + " - " + saving[k].arr[i].replace.sk + " - " + saving[k].arr[i].quatity, 30 ,v); V(20);
  686. }
  687. }
  688. doc.end();
  689.  
  690. doc.on('data', function (data) {
  691. //res.write(data);
  692. });
  693.  
  694. doc.on('end', function(){
  695. callback();
  696. //res.sendfile("out.pdf");
  697. });
  698.  
  699. function V(i){
  700. if(i+v > 700 ){
  701. doc.rect(0,755,612,40).lineWidth(0).fill("#E0E0E0");
  702. doc.addPage();
  703. v = i;
  704. pages++;
  705. }
  706. v += i;
  707. }
  708.  
  709. function footer(){
  710. doc.rect(0,755,612,40).lineWidth(0).fill("#E0E0E0");
  711. doc.fill("#000").text("All rights reserved © something 2016", 20 , 765);
  712.  
  713. }
  714. }
  715.  
  716. function recycle(rand, trans, user, lights, price, callback){
  717. var obj = {
  718. lights: [],
  719. rand: rand,
  720. transaction: trans,
  721. price: price,
  722. user: user
  723. };
  724.  
  725. var arr = [];
  726. for(var k in lights){
  727. obj.lights.push({
  728. id: lights[k].option, // this is the reference to the option!!!!!
  729. price: lights[k].price / lights[k].quatity,
  730. quantity: lights[k].quatity,
  731. name: lights[k].name,
  732. spaceId: lights[k].space._id
  733. });
  734. Space.update({_id: lights[k].space._id}, {lock: true}, function(errLock, lock){
  735. console.log("Lock: " + lights[k].space._id);
  736. });
  737. }
  738.  
  739. History(obj).save(function(err, history){
  740. console.log(err, history);
  741. });
  742.  
  743. Light.remove({user: user}, function(err, docs){
  744. console.log("Remove lights from user after moving to history");
  745. console.log(err, docs);
  746. callback();
  747. });
  748. }
  749.  
  750. function updateUserInIs(obj, isId){
  751. console.log(obj, isId);
  752. is.ContactService.update(isId, obj).done(function(results){
  753. console.log("Update Is: " + results);
  754. });
  755. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement