/kioow.core/observers/routes/tailormade.queries.actions.js

https://gitlab.com/xescpereta/kioow.core · JavaScript · 294 lines · 212 code · 49 blank · 33 comment · 20 complexity · a99755140b09f70d140e1142ef464f35 MD5 · raw file

  1. module.exports = function (subscriberworker, socket) {
  2. var common = require('kioow.common');
  3. socket.on('new', function (data) {
  4. //TASK ON NEW AFFILIATE:
  5. console.log('NEW QUERY SUBSCRIBER ACTIONS STUFF....');
  6. console.log(data);
  7. //1) Send EMAILS: **************** START *********************
  8. console.log('New Query: ' + data.code);
  9. var mailtemplates = {
  10. affiliate: 'ytoaffiliatenewrequest', //the mail for the affiliate
  11. yto: 'omtnewaffiliaterequest' //the mail for OM/YTO Admins
  12. };
  13. var helper = require('./affiliate.actions.helpers');
  14. var ml = require('../../factory/mailing');
  15. var mailer = new ml.Mailer();
  16. var mmux = require('../../mediator/mailstack.mediator');
  17. var msend = new mmux.MailStackMediator();
  18. var alldone = {
  19. mails: 0,
  20. total: 2,
  21. mailbodys: []
  22. };
  23. // variable to send
  24. var dataSend = {};
  25. dataSend.request = data;
  26. dataSend.querycode = data.code;
  27. dataSend.companyname= data.affiliate.company.name;
  28. //************
  29. // mail to OMT
  30. //************
  31. // add url Call to Action ADMIN
  32. dataSend.url = subscriberworker.core.get('frontadminurl') +'/omt-response?code='+data.code;
  33. //the mail for OMT/YTO Admins
  34. mailer.SetupEmail(
  35. 'kioow@outlook.com',
  36. mailtemplates.yto,
  37. dataSend,
  38. function (adminMail) {
  39. msend.send(adminMail,
  40. function (ok) {
  41. alldone.mails++;
  42. alldone.mailbodys.push(adminMail);
  43. if (alldone.mails == alldone.total) {
  44. subscriberworker.setfinished({
  45. ResultOK: true,
  46. Message: 'All mails sended to affiliate and omt',
  47. Mails: alldone.mailbodys
  48. });
  49. }
  50. }, function (err) {
  51. subscriberworker.seterror(err);
  52. });
  53. });
  54. //*****************
  55. // mail to afiliado
  56. //*****************
  57. // variables a enviar
  58. var to;
  59. var dataToSend = {};
  60. dataToSend.request = data;
  61. dataToSend.querycode = data.code;
  62. if (data.affiliate.contact.bookingContact !== null && data.affiliate.contact.bookingContact !== undefined){
  63. to = data.affiliate.contact.bookingContact.email;
  64. } else {
  65. to = data.affiliate.contact.email;
  66. }
  67. console.log("++ mail afiliado: ",to);
  68. // add url Call to Action Affiliate
  69. dataToSend.url = subscriberworker.core.get('fronturl') +'/affiliate/request/'+data.code;
  70. //the mail for the affiliate
  71. mailer.SetupEmail(
  72. to,
  73. mailtemplates.affiliate,
  74. dataToSend,
  75. function (afiMail) {
  76. //send emails...
  77. msend.send(afiMail,
  78. function (ok) {
  79. alldone.mailbodys.push(afiMail);
  80. // actualizar el historic de la query
  81. var historicYTO = {
  82. date: new Date(),
  83. state: data.state,
  84. user: data.affiliate.user.email,
  85. mailsend: [{ name: mailtemplates.affiliate, date: new Date() },{ name: mailtemplates.yto, date: new Date() }]
  86. };
  87. helper.addhistoric({
  88. core: subscriberworker.core,
  89. userquery: data,
  90. historic: historicYTO
  91. }, function (result) {
  92. alldone.mails++;
  93. console.log("++ enviado todos los correos correctamente.");
  94. if (alldone.mails == alldone.total) {
  95. subscriberworker.setfinished({
  96. ResultOK: true,
  97. Message: 'All mails sended to affiliate and omt',
  98. Mails: alldone.mailbodys
  99. });
  100. }
  101. }, function (err) {
  102. alldone.mails++;
  103. if (alldone.mails == alldone.total) {
  104. subscriberworker.seterror({
  105. ResultOK: false,
  106. Message: err,
  107. Mails: alldone.mailbodys
  108. });
  109. }
  110. });
  111. },
  112. function (err) {
  113. subscriberworker.seterror(err);
  114. });
  115. });
  116. });
  117. /**
  118. * si se produce una actualizacion de la query se lanza esto subscriber
  119. */
  120. socket.on('update', function (data) {
  121. console.log('A QUERY HAS BEEN UPDATED...');
  122. var ml = require('../../factory/mailing');
  123. var mailer = new ml.Mailer();
  124. var mmux = require('../../mediator/mailstack.mediator');
  125. var msend = new mmux.MailStackMediator();
  126. var helper = require('./affiliate.actions.helpers');
  127. var query_edited = data.current;
  128. var query_original = data.original;
  129. //flux control
  130. var assertwatcher = common.eventtrigger.eventcarrier(common.utils.getToken());
  131. //completion
  132. //###### Complete properties to check - Add property name to force new checkings...
  133. var complete = {
  134. notifyCancellationAdmin: false
  135. };
  136. //
  137. // INIT CANCELATION notifications
  138. //
  139. var mailtemplates = {
  140. cancellyto: 'ytoadmincancellquery' //the mail for OMT/YTO Admins
  141. };
  142. var completeresults = {
  143. errors: [],
  144. messages: [],
  145. };
  146. function checkandfinish() {
  147. var completed = true;
  148. for (var prop in complete) {
  149. completed = completed && complete[prop];
  150. }
  151. if (completed) {
  152. assertwatcher.emit('done');
  153. }
  154. }
  155. assertwatcher.on('notifyCancellationAdmin', function () {
  156. if (query_edited.cancelled != null &&
  157. query_edited.cancelled.cancelDate !== null &&
  158. (query_original.cancelled == null || query_original.cancelled.cancelDate !== query_edited.cancelled.cancelDate)) {
  159. console.log ('query '+query_original.code+' has been cancelled by affiliate.');
  160. // send notification to booking ADMIN
  161. //
  162. var sendData = {};
  163. sendData.ca ={
  164. url : subscriberworker.core.get('frontadminurl') +'/omt-response?code='+query_original.code,
  165. txt : 'IR A LA SOLICITUD'
  166. };
  167. //********actializar las quotes de la query a canceladas
  168. var upd = {
  169. $set : {
  170. status: 'cancelled',
  171. 'cancelled.cancelDate': query_edited.cancelled.cancelDate,
  172. 'cancelled.user' : query_edited.cancelled.user,
  173. 'cancelled.byTraveler': query_edited.cancelled.byTraveler,
  174. 'cancelled.reason': query_edited.cancelled.reason
  175. }
  176. };
  177. helper.update({
  178. core: subscriberworker.core,
  179. query: { userqueryCode: query_edited.code },
  180. update: upd,
  181. collectionname: 'Quotes'
  182. });
  183. //***********************
  184. var historicQuery = {
  185. date: new Date(),
  186. state: query_edited.state,
  187. user: query_edited.affiliate.user.email,
  188. mailsend: [
  189. { name: mailtemplates.cancellyto, date: new Date() }]
  190. };
  191. sendData.query = query_edited;
  192. sendData.url = subscriberworker.core.get('frontadminurl') +'/omt-response?code='+query_original.code;
  193. sendData.request = query_edited;
  194. sendData.querycode = query_edited.code;
  195. //the mail for OMT/YTO Admins
  196. mailer.SetupEmail(
  197. 'kioow@outlook.com',
  198. mailtemplates.cancellyto,
  199. sendData,
  200. function (adminMail) {
  201. msend.send(adminMail,
  202. function (ok) {
  203. helper.addhistoric({
  204. core: subscriberworker.core,
  205. userquery: query_edited,
  206. historic: historicQuery
  207. }, function (res) {
  208. complete.notifyCancellationAdmin = true;
  209. checkandfinish();
  210. }, function (err) {
  211. complete.errors.push(err);
  212. complete.notifyCancellationAdmin = true;
  213. checkandfinish();
  214. });
  215. }, function (err) {
  216. complete.errors.push(err);
  217. complete.notifyCancellationAdmin = true;
  218. checkandfinish();
  219. });
  220. });
  221. } else {
  222. complete.notifyCancellationAdmin = true;
  223. checkandfinish();
  224. }
  225. });
  226. //####### START ############ --- check conditions and launch events... ADD CODE HERE...
  227. assertwatcher.on('done', function () {
  228. (completeresults.errors.length > 0) ? subscriberworker.seterror(complete.errors) : subscriberworker.setfinished({
  229. ResultOK: true,
  230. Message: 'Affiliate update actions finished'
  231. });
  232. });
  233. //####### END ############ --- check conditions and launch events... ADD CODE HERE...
  234. //exec assertions..
  235. for (var prop in complete) {
  236. assertwatcher.emit(prop);
  237. }
  238. });
  239. socket.on('delete', function (data) {
  240. subscriberworker.setfinished();
  241. });
  242. socket.on('generic.action', function (data) {
  243. subscriberworker.setfinished();
  244. });
  245. }