/kioow.core/observers/routes/affiliate.actions.js

https://gitlab.com/xescpereta/kioow.core · JavaScript · 231 lines · 170 code · 38 blank · 23 comment · 18 complexity · f36f2f36989afc5ebb1e9980d24be86e 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 AFFILIATE SUBSCRIBER ACTIONS STUFF....');
  6. console.log(data);
  7. //1) Send EMAILS: **************** START *********************
  8. console.log('New Affiliate: ' + data.code);
  9. var mailtemplates = {
  10. affiliate: 'ytoaffiliatethanks', //the mail for the affiliate
  11. yto: 'omtnewaffiliate' //the mail for OM/YTO Admins
  12. };
  13. var ml = require('../../factory/mailing');
  14. var mailer = new ml.Mailer();
  15. var mmux = require('../../mediator/mailstack.mediator');
  16. var msend = new mmux.MailStackMediator();
  17. var alldone = {
  18. mails: 0,
  19. total: 2,
  20. mailbodys: []
  21. };
  22. // add url Call to Action
  23. // *****************
  24. // mail al afiliado
  25. // *****************
  26. data.url = subscriberworker.core.get('fronturl') +'/affiliate/account/?code='+data.code;
  27. //the mail for the affiliate
  28. mailer.SetupEmail(
  29. data.user.email,
  30. mailtemplates.affiliate,
  31. data,
  32. function (afiMail) {
  33. //send emails...
  34. msend.send(afiMail,
  35. function (ok) {
  36. alldone.mails++;
  37. alldone.mailbodys.push(afiMail);
  38. if (alldone.mails == alldone.total) {
  39. subscriberworker.setfinished({
  40. ResultOK: true,
  41. Message: 'All mails sended to affiliate and omt',
  42. Mails: alldone.mailbodys
  43. });
  44. }
  45. },
  46. function (err) {
  47. subscriberworker.seterror(err);
  48. });
  49. });
  50. // *************************
  51. //the mail for OM/YTO Admins
  52. // *************************
  53. mailer.SetupEmail(
  54. 'kioow@outlook.com',
  55. mailtemplates.yto,
  56. data,
  57. function (adminMail) {
  58. msend.send(adminMail,
  59. function (ok) {
  60. alldone.mails++;
  61. alldone.mailbodys.push(adminMail);
  62. if (alldone.mails == alldone.total) {
  63. subscriberworker.setfinished({
  64. ResultOK: true,
  65. Message: 'All mails sended to affiliate and omt',
  66. Mails: alldone.mailbodys
  67. });
  68. }
  69. }, function (err) {
  70. subscriberworker.seterror(err);
  71. });
  72. });
  73. //1) Send EMAILS: **************** END *********************
  74. });
  75. socket.on('update', function (data) {
  76. console.log('An AFFILIATE HAS BEEN UPDATED...');
  77. var ml = require('../../factory/mailing');
  78. var mailer = new ml.Mailer();
  79. var mmux = require('../../mediator/mailstack.mediator');
  80. var msend = new mmux.MailStackMediator();
  81. var affiliate_edited = data.current;
  82. var affiliate_original = data.original;
  83. //flux control
  84. var assertwatcher = common.eventtrigger.eventcarrier(common.utils.getToken());
  85. //completion
  86. //###### Complete properties to check - Add property name to force new checkings...
  87. var complete = {
  88. registervalid: false,
  89. emailchanged: false
  90. //suponemosalgo: false
  91. };
  92. var completeresults = {
  93. errors: [],
  94. messages: []
  95. };
  96. function checkandfinish() {
  97. var completed = true;
  98. for (var prop in complete) {
  99. completed = completed && complete[prop];
  100. }
  101. if (completed) {
  102. assertwatcher.emit('done');
  103. }
  104. }
  105. //####### START ############ --- check conditions and launch events... ADD CODE HERE...
  106. function checkregistervalid() {
  107. var dothisaction = false;
  108. dothisaction = (
  109. (affiliate_original.membership.registervalid == null || affiliate_original.membership.registervalid == false) &&
  110. affiliate_edited.membership.registervalid == true);
  111. return dothisaction;
  112. }
  113. assertwatcher.on('registervalid', function () {
  114. if (checkregistervalid()) {
  115. var templatename = 'ytoaffiliateconfirmed';
  116. affiliate_edited.url = subscriberworker.core.get('fronturl') +'/affiliate/inicio';
  117. mailer.SetupEmail(
  118. affiliate_edited.contact.email,
  119. templatename,
  120. affiliate_edited,
  121. function (afiMail) {
  122. msend.send(afiMail,
  123. function (ok) {
  124. completeresults.messages.push('Affiliate Register Valid: Notification mail sent');
  125. complete.registervalid = true;
  126. checkandfinish();
  127. }, function (err) {
  128. completeresults.errors.push(err);
  129. complete.registervalid = true;
  130. checkandfinish();
  131. });
  132. });
  133. //do action... send mail... and complete...
  134. } else {
  135. complete.registervalid = true;
  136. checkandfinish();
  137. }
  138. });
  139. //####### START DETEC EMAIL CHANGE
  140. function checkemailchanged() {
  141. var dothisaction = false;
  142. console.log("++ afiliado actual: ",affiliate_original);
  143. console.log("++ EMail actual: ",affiliate_original.user.email);
  144. console.log("++ EMail modificado: ",affiliate_edited.user.email);
  145. console.log("++ afiliado modificado: ",affiliate_edited);
  146. dothisaction = (affiliate_original.user.email != affiliate_edited.user.email);
  147. return dothisaction;
  148. }
  149. assertwatcher.on('emailchanged', function () {
  150. if (checkemailchanged()) {
  151. var templatename = 'ytoaffiliateconfirmed';
  152. mailer.SetupEmail(
  153. affiliate_edited.contact.email,
  154. //'antosango@gmail.com',
  155. templatename,
  156. affiliate_edited,
  157. function (afiMail) {
  158. msend.send(afiMail,
  159. function (ok) {
  160. completeresults.messages.push('Affiliate Register Updated Email: Notification mail sent');
  161. complete.emailchanged = true;
  162. checkandfinish();
  163. }, function (err) {
  164. completeresults.errors.push(err);
  165. complete.emailchanged = true;
  166. checkandfinish();
  167. });
  168. });
  169. //do action... send mail... and complete...
  170. } else {
  171. complete.emailchanged = true;
  172. checkandfinish();
  173. }
  174. });
  175. assertwatcher.on('done', function () {
  176. (completeresults.errors.length > 0) ? subscriberworker.seterror(complete.errors) : subscriberworker.setfinished({
  177. ResultOK: true,
  178. Message: 'Affiliate update actions finished'
  179. });
  180. });
  181. //####### END ############ --- check conditions and launch events... ADD CODE HERE...
  182. //exec assertions..
  183. for (var prop in complete) {
  184. assertwatcher.emit(prop);
  185. }
  186. });
  187. socket.on('delete', function (data) {
  188. subscriberworker.setfinished();
  189. });
  190. socket.on('generic.action', function (data) {
  191. subscriberworker.setfinished();
  192. });
  193. }