notification-processor 4.17.0 → 4.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+
3
+ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
+
5
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
+
7
+ // Generated by CoffeeScript 2.7.0
8
+ (function () {
9
+ var MERCADOLIBRE_API_CORE_URL, MERCADOLIBRE_API_MASTER_TOKEN, NodeCache, Promise, UserIdTranslator, _, request, retry, translatedCache;
10
+
11
+ _ = require("lodash");
12
+
13
+ Promise = require("bluebird");
14
+
15
+ request = require("request-promise");
16
+
17
+ retry = require("bluebird-retry");
18
+
19
+ NodeCache = require("node-cache");
20
+
21
+ translatedCache = new NodeCache({
22
+ stdTTL: 0,
23
+ checkperiod: 0
24
+ });
25
+
26
+ MERCADOLIBRE_API_CORE_URL = process.env.MERCADOLIBRE_API_CORE_URL || "https://apps.producteca.com/mercadolibre-api";
27
+
28
+ MERCADOLIBRE_API_MASTER_TOKEN = process.env.MERCADOLIBRE_API_MASTER_TOKEN;
29
+
30
+ module.exports = UserIdTranslator = function () {
31
+ function UserIdTranslator() {
32
+ _classCallCheck(this, UserIdTranslator);
33
+
34
+ this.translate = this.translate.bind(this);
35
+ this.getCompanyId = this.getCompanyId.bind(this);
36
+ this._setInCache = this._setInCache.bind(this);
37
+ this._translateUserId = this._translateUserId.bind(this);
38
+ this.translatedCache = translatedCache;
39
+ this.translate = this.translate.bind(this);
40
+ }
41
+
42
+ _createClass(UserIdTranslator, [{
43
+ key: "translate",
44
+ value: function translate(userId) {
45
+ if (!MERCADOLIBRE_API_MASTER_TOKEN) {
46
+ return Promise.resolve(null);
47
+ }
48
+ return this.getCompanyId(userId);
49
+ }
50
+ }, {
51
+ key: "getCompanyId",
52
+ value: function getCompanyId(userId) {
53
+ var companyId;
54
+ companyId = this.translatedCache.get(userId);
55
+ if (companyId) {
56
+ return Promise.resolve(companyId);
57
+ } else {
58
+ return this._translateUserId(userId);
59
+ }
60
+ }
61
+ }, {
62
+ key: "_setInCache",
63
+ value: function _setInCache(userId, userInfo) {
64
+ var success;
65
+ success = this.translatedCache.set(userId, userInfo);
66
+ if (success) {
67
+ console.log("UserId %s ===> %s was stored in cache successfully", userId, JSON.stringify(userInfo));
68
+ }
69
+ return Promise.resolve(success);
70
+ }
71
+ }, {
72
+ key: "_translateUserId",
73
+ value: function _translateUserId(userId) {
74
+ var _this = this;
75
+
76
+ console.log("Making request to translate", userId);
77
+ return retry(function () {
78
+ return request.get({
79
+ url: MERCADOLIBRE_API_CORE_URL + "/users/me",
80
+ json: true,
81
+ qs: {
82
+ authenticationType: "mercadolibre"
83
+ },
84
+ auth: {
85
+ user: "" + userId,
86
+ password: MERCADOLIBRE_API_MASTER_TOKEN
87
+ }
88
+ }).promise();
89
+ }).then(function (userInformation) {
90
+ return {
91
+ app: userInformation.app,
92
+ companyId: userInformation.tenantId || userInformation.companyId
93
+ };
94
+ }).tap(function (_ref) {
95
+ var companyId = _ref.companyId;
96
+
97
+ return console.log("UserId translated %s ==> %s", userId, companyId);
98
+ }).catch(function (reason) {
99
+ if (_.includes([401, 500], reason.statusCode)) {
100
+ return "Unknown";
101
+ }
102
+ throw reason;
103
+ }).tap(function (companyId) {
104
+ return _this._setInCache(userId, companyId);
105
+ });
106
+ }
107
+ }]);
108
+
109
+ return UserIdTranslator;
110
+ }();
111
+ }).call(undefined);
@@ -28,11 +28,18 @@
28
28
  var _ref$context$bindingD = _ref.context.bindingData,
29
29
  insertionTime = _ref$context$bindingD.insertionTime,
30
30
  dequeueCount = _ref$context$bindingD.dequeueCount,
31
+ messageId = _ref$context$bindingD.messageId,
32
+ Message = _ref$context$bindingD.Message,
33
+ MessageId = _ref$context$bindingD.MessageId,
31
34
  message = _ref.message;
32
35
 
33
36
  return {
34
37
  message: message,
35
- meta: { insertionTime: insertionTime, dequeueCount: dequeueCount },
38
+ meta: {
39
+ insertionTime: insertionTime,
40
+ dequeueCount: dequeueCount,
41
+ messageId: messageId || MessageId || Message
42
+ },
36
43
  type: "as"
37
44
  };
38
45
  },