notification-processor 5.0.2 → 5.0.3

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.
Files changed (35) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/exceptions/ignored.error.js +27 -0
  3. package/lib/exceptions/non.retryable.js +27 -0
  4. package/lib/index.js +25 -0
  5. package/lib/observers/delay.levels.js +32 -0
  6. package/lib/observers/incidentsApi.observer.js +109 -0
  7. package/lib/observers/logger.observer.js +59 -0
  8. package/lib/observers/monitoringCenter.observer.js +234 -0
  9. package/lib/observers/redis.observer.js +90 -0
  10. package/lib/processor.builder.js +134 -0
  11. package/lib/processor.js +132 -0
  12. package/lib/processors/deadletter.processor.js +45 -0
  13. package/lib/processors/job/index.js +38 -0
  14. package/lib/processors/job/job.processor.js +150 -0
  15. package/lib/processors/job/notification.api.js +237 -0
  16. package/lib/processors/maxRetries.processor.js +88 -0
  17. package/lib/processors/request.async.processor.js +56 -0
  18. package/lib/processors/request.processor.js +114 -0
  19. package/lib/processors/requestWithRetries.async.processor.js +22 -0
  20. package/lib/senders/async.producteca.sender.js +145 -0
  21. package/lib/senders/index.js +10 -0
  22. package/lib/senders/meli.sender.js +46 -0
  23. package/lib/senders/producteca.sender.js +34 -0
  24. package/lib/services/oAuthApi.js +77 -0
  25. package/lib/services/redis.js +16 -0
  26. package/lib/services/userIdTranslator.js +111 -0
  27. package/lib/sources/aws.sqs.source.js +37 -0
  28. package/lib/sources/index.js +12 -0
  29. package/lib/sources/queue.source.js +26 -0
  30. package/lib/sources/service.bus.source.js +26 -0
  31. package/lib/sources/table.source.js +20 -0
  32. package/lib/sources/unknown.source.js +15 -0
  33. package/lib/specHelpers/fixture.js +29 -0
  34. package/lib/specHelpers/redis.observer.mock.js +65 -0
  35. package/package.json +1 -1
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ var Promise, UserIdTranslator, uuid;
6
+
7
+ uuid = require("uuid/v4");
8
+
9
+ Promise = require("bluebird");
10
+
11
+ UserIdTranslator = require("../services/userIdTranslator");
12
+
13
+ module.exports = {
14
+ user: function user(_ref) {
15
+ var user_id = _ref.message.user_id;
16
+
17
+ return user_id;
18
+ },
19
+ resource: function resource(_ref2) {
20
+ var _resource = _ref2.message.resource;
21
+
22
+ return _resource;
23
+ },
24
+ monitoringCenterFields: function monitoringCenterFields(notification) {
25
+ var _this = this;
26
+
27
+ return new UserIdTranslator().translate(this.user(notification)).then(function (_ref3) {
28
+ var app = _ref3.app,
29
+ companyId = _ref3.companyId;
30
+
31
+ var ref, ref1, ref2;
32
+ return Promise.props({
33
+ eventType: 'http',
34
+ resource: null,
35
+ app: app,
36
+ companyId: companyId,
37
+ userId: null,
38
+ externalReference: _this.resource(notification),
39
+ eventId: (notification != null ? (ref = notification.message) != null ? ref._id : void 0 : void 0) || uuid(),
40
+ eventTimestamp: (notification != null ? (ref1 = notification.meta) != null ? ref1.insertionTime : void 0 : void 0) ? new Date(notification != null ? (ref2 = notification.meta) != null ? ref2.insertionTime : void 0 : void 0).getTime() : void 0,
41
+ parentEventId: null
42
+ });
43
+ });
44
+ }
45
+ };
46
+ }).call(undefined);
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ var Promise;
6
+
7
+ Promise = require("bluebird");
8
+
9
+ module.exports = {
10
+ user: function user(_ref) {
11
+ var CompanyId = _ref.message.CompanyId;
12
+
13
+ return CompanyId;
14
+ },
15
+ resource: function resource(_ref2) {
16
+ var ResourceId = _ref2.message.ResourceId;
17
+
18
+ return ResourceId;
19
+ },
20
+ monitoringCenterFields: function monitoringCenterFields(notification) {
21
+ var ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7;
22
+ return Promise.props({
23
+ eventType: 'service-bus',
24
+ resource: this.resource(notification),
25
+ companyId: this.user(notification),
26
+ userId: (notification != null ? (ref = notification.message) != null ? ref.UserId : void 0 : void 0) || (notification != null ? (ref1 = notification.message) != null ? ref1.User : void 0 : void 0),
27
+ externalReference: null,
28
+ eventId: notification != null ? (ref2 = notification.message) != null ? ref2.EventId : void 0 : void 0,
29
+ eventTimestamp: (notification != null ? (ref3 = notification.meta) != null ? ref3.insertionTime : void 0 : void 0) || (notification != null ? (ref4 = notification.message) != null ? ref4.Sent : void 0 : void 0) ? new Date((notification != null ? (ref5 = notification.meta) != null ? ref5.insertionTime : void 0 : void 0) || (notification != null ? (ref6 = notification.message) != null ? ref6.Sent : void 0 : void 0)).getTime() : void 0,
30
+ parentEventId: notification != null ? (ref7 = notification.message) != null ? ref7.ParentEventId : void 0 : void 0
31
+ });
32
+ }
33
+ };
34
+ }).call(undefined);
@@ -0,0 +1,77 @@
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 NodeCache, OAUTH_API_URL, OAuthApi, Promise, request, translatedCache;
10
+
11
+ request = require("request-promise");
12
+
13
+ Promise = require("bluebird");
14
+
15
+ NodeCache = require("node-cache");
16
+
17
+ translatedCache = new NodeCache({
18
+ stdTTL: 0,
19
+ checkperiod: 0
20
+ });
21
+
22
+ OAUTH_API_URL = process.env.OAUTH_API_URL || "https://apps.producteca.com/oauth"; //TODO: Revisar que esta variable este seteada en todos lados con la interna, seguramente no
23
+
24
+ module.exports = OAuthApi = function () {
25
+ function OAuthApi(accessToken) {
26
+ _classCallCheck(this, OAuthApi);
27
+
28
+ this.scopes = this.scopes.bind(this);
29
+ this._scopes = this._scopes.bind(this);
30
+ this._doRequest = this._doRequest.bind(this);
31
+ this.accessToken = accessToken;
32
+ }
33
+
34
+ _createClass(OAuthApi, [{
35
+ key: "scopes",
36
+ value: function scopes() {
37
+ var _this = this;
38
+
39
+ var cachedValue;
40
+ cachedValue = translatedCache.get(this.accessToken);
41
+ if (cachedValue) {
42
+ return Promise.resolve(cachedValue);
43
+ }
44
+ return this._scopes().tap(function (_ref) {
45
+ var id = _ref.id,
46
+ companyId = _ref.companyId,
47
+ appId = _ref.appId;
48
+
49
+ return translatedCache.set(_this.accessToken, { id: id, companyId: companyId, appId: appId });
50
+ });
51
+ }
52
+ }, {
53
+ key: "_scopes",
54
+ value: function _scopes() {
55
+ return this._doRequest("get", "/scopes", {
56
+ access_token: this.accessToken,
57
+ fromNotificationProcessor: true
58
+ });
59
+ }
60
+ }, {
61
+ key: "_doRequest",
62
+ value: function _doRequest(verb, path) {
63
+ var qs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
64
+
65
+ var options;
66
+ options = {
67
+ url: OAUTH_API_URL + path,
68
+ qs: qs,
69
+ json: true
70
+ };
71
+ return request[verb](options).promise();
72
+ }
73
+ }]);
74
+
75
+ return OAuthApi;
76
+ }();
77
+ }).call(undefined);
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ var Promise, redis;
6
+
7
+ Promise = require("bluebird");
8
+
9
+ redis = require("redis");
10
+
11
+ Promise.promisifyAll(redis.RedisClient.prototype);
12
+
13
+ Promise.promisifyAll(redis.Multi.prototype);
14
+
15
+ module.exports = redis;
16
+ }).call(undefined);
@@ -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);
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ var _;
6
+
7
+ _ = require("lodash");
8
+
9
+ module.exports = {
10
+ newNotification: function newNotification(_ref) {
11
+ var context = _ref.context,
12
+ message = _ref.message;
13
+
14
+ return {
15
+ message: !_.isObject(message.Message) ? JSON.parse(message.Message) : message.Message,
16
+ meta: {
17
+ insertionTime: _.get(message, "Timestamp"),
18
+ messageId: _.get(message, "MessageId"),
19
+ properties: _.mapValues(_.get(message, "MessageAttributes"), function (_ref2) {
20
+ var Type = _ref2.Type,
21
+ Value = _ref2.Value;
22
+
23
+ if (_.includes(Type, 'Array')) {
24
+ return JSON.parse(Value);
25
+ } else if (Type === 'Number') {
26
+ return Number(Value);
27
+ } else {
28
+ return Value;
29
+ }
30
+ }),
31
+ dequeueCount: _.get(context, "bindingData.approximateReceiveCount")
32
+ },
33
+ type: "sqs"
34
+ };
35
+ }
36
+ };
37
+ }).call(undefined);
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ module.exports = {
6
+ AwsSQSSource: require("./aws.sqs.source"),
7
+ ServiceBusSource: require("./service.bus.source"),
8
+ QueueSource: require("./queue.source"),
9
+ TableSource: require("./table.source"),
10
+ UnknownSource: require("./unknown.source")
11
+ };
12
+ }).call(undefined);
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ module.exports = {
6
+ newNotification: function newNotification(_ref) {
7
+ var _ref$context$bindingD = _ref.context.bindingData,
8
+ insertionTime = _ref$context$bindingD.insertionTime,
9
+ dequeueCount = _ref$context$bindingD.dequeueCount,
10
+ messageId = _ref$context$bindingD.messageId,
11
+ Message = _ref$context$bindingD.Message,
12
+ MessageId = _ref$context$bindingD.MessageId,
13
+ message = _ref.message;
14
+
15
+ return {
16
+ message: message,
17
+ meta: {
18
+ insertionTime: insertionTime,
19
+ dequeueCount: dequeueCount,
20
+ messageId: messageId || MessageId || Message
21
+ },
22
+ type: "as"
23
+ };
24
+ }
25
+ };
26
+ }).call(undefined);
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ var _;
6
+
7
+ _ = require("lodash");
8
+
9
+ module.exports = {
10
+ newNotification: function newNotification(_ref) {
11
+ var bindingData = _ref.context.bindingData,
12
+ message = _ref.message;
13
+
14
+ return {
15
+ message: _.omit(message, "Sent"),
16
+ meta: {
17
+ messageId: bindingData.messageId,
18
+ insertionTime: bindingData.enqueuedTimeUtc,
19
+ dequeueCount: bindingData.deliveryCount,
20
+ properties: bindingData.userProperties || bindingData.properties
21
+ },
22
+ type: "sb"
23
+ };
24
+ }
25
+ };
26
+ }).call(undefined);
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ var _;
6
+
7
+ _ = require("lodash");
8
+
9
+ module.exports = function (OtherSource) {
10
+ return {
11
+ newNotification: function newNotification(_ref) {
12
+ var notification = _ref.message.notification;
13
+
14
+ return _.merge(notification, {
15
+ type: "table"
16
+ });
17
+ }
18
+ };
19
+ };
20
+ }).call(undefined);
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ module.exports = {
6
+ newNotification: function newNotification(_ref) {
7
+ var message = _ref.message;
8
+
9
+ return {
10
+ message: message,
11
+ type: "uk"
12
+ };
13
+ }
14
+ };
15
+ }).call(undefined);
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ // Generated by CoffeeScript 2.7.0
4
+ (function () {
5
+ var _, notification, redis;
6
+
7
+ _ = require("lodash");
8
+
9
+ redis = {
10
+ host: "127.0.0.1",
11
+ port: "1234",
12
+ db: "3",
13
+ auth: "unaCadenaDeAuth"
14
+ };
15
+
16
+ notification = {
17
+ type: "sb",
18
+ dequeueCount: 1,
19
+ message: {
20
+ CompanyId: 123,
21
+ ResourceId: 456
22
+ },
23
+ meta: {
24
+ insertionTime: "Sat, 05 Nov 2016 16:44:43 GMT"
25
+ }
26
+ };
27
+
28
+ module.exports = { redis: redis, notification: notification };
29
+ }).call(undefined);
@@ -0,0 +1,65 @@
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 MockRedis, MockRedisClient, Promise, _, proxyquire, sinon, stub;
10
+
11
+ proxyquire = require("proxyquire");
12
+
13
+ Promise = require("bluebird");
14
+
15
+ sinon = require("sinon");
16
+
17
+ _ = require("lodash");
18
+
19
+ MockRedisClient = function () {
20
+ function MockRedisClient() {
21
+ _classCallCheck(this, MockRedisClient);
22
+
23
+ this.refreshSpies = this.refreshSpies.bind(this);
24
+ this.refreshSpies();
25
+ }
26
+
27
+ _createClass(MockRedisClient, [{
28
+ key: "auth",
29
+ value: function auth() {}
30
+ }, {
31
+ key: "refreshSpies",
32
+ value: function refreshSpies() {
33
+ return this.spies = {
34
+ publishAsync: sinon.spy()
35
+ };
36
+ }
37
+ }, {
38
+ key: "publishAsync",
39
+ value: function publishAsync(key, value) {
40
+ return Promise.resolve(this.spies.publishAsync(key, value));
41
+ }
42
+ }]);
43
+
44
+ return MockRedisClient;
45
+ }();
46
+
47
+ stub = {
48
+ "../services/redis": MockRedis = function () {
49
+ function MockRedis() {
50
+ _classCallCheck(this, MockRedis);
51
+ }
52
+
53
+ _createClass(MockRedis, null, [{
54
+ key: "createClient",
55
+ value: function createClient() {
56
+ return new MockRedisClient();
57
+ }
58
+ }]);
59
+
60
+ return MockRedis;
61
+ }()
62
+ };
63
+
64
+ proxyquire("../observers/redis.observer", stub);
65
+ }).call(undefined);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notification-processor",
3
- "version": "5.0.2",
3
+ "version": "5.0.3",
4
4
  "description": "notification-processor",
5
5
  "main": "index.js",
6
6
  "scripts": {