@orangewall/customer-notifications-util 0.0.1

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Snyk Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # monitoring-utils
2
+
3
+ An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format (CJS). It can be used in Node.js and browser applications.
4
+
5
+ ## Get Started
6
+
7
+ 1. Run `npm install` in your terminal
8
+ 1. Then run `npm run build`
9
+ 1. Update the `package.json` with the new version that needs to be published
10
+ 1. Create an account with [npm](https://www.npmjs.com/signup) if you don't have one already. Also be sure to enable [two-factor authentication](https://docs.npmjs.com/configuring-two-factor-authentication)
11
+ 1. Sign in to your npm account in your terminal with `npm login`
12
+ 1. Run `npm publish --access=public` to publish your package
13
+
14
+ ### Testing
15
+
16
+ 1. Install developer dependencies using the following command in your terminal `npm i -D mocha @types/mocha chai @types/chai ts-node`
17
+ 1. Create a new file `.mocharc.json` in the root directory with the following contents:
18
+ ```json
19
+ {
20
+ "extension": ["ts"],
21
+ "spec": "./**/*.spec.ts",
22
+ "require": "ts-node/register"
23
+ }
24
+ ```
25
+ 1. Create a `tests` folder
26
+ 1. Create an `index.spec.ts` file in the `tests` folder
27
+ 1. Write unit tests in the `index.spec.ts` file to test the code in `index.ts`
28
+ 1. Add a `"test"` property in the `package.json` file and give it a value of `"mocha"`
29
+ 1. Run `npm test` in your terminal from the root folder of the project
30
+
31
+
32
+
33
+
34
+ ### References
35
+
36
+ https://snyk.io/blog/best-practices-create-modern-npm-package
37
+ https://github.com/snyk-snippets/modern-npm-package
38
+ https://github.com/goldbergyoni/javascript-testing-best-practices
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.sendCustomerNotification = void 0;
16
+ const client_sns_1 = require("@aws-sdk/client-sns");
17
+ const config_1 = __importDefault(require("config"));
18
+ /**
19
+ * Posts a message to customer-notifications SNS
20
+ * @param {string} messages - Extra parameters to add to description for context.
21
+ * @returns {Promise} - Resolves with the SNS publish result.
22
+ */
23
+ function sendCustomerNotification(messages, topicArn) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ try {
26
+ const snsClient = new client_sns_1.SNSClient(); // Default credentials and region from the environment
27
+ if (!topicArn) {
28
+ config_1.default === null || config_1.default === void 0 ? void 0 : config_1.default.get('notifications.customer-notifications-topic-arn');
29
+ if (!topicArn) {
30
+ throw new Error("if topic is not passed as a parameter, you need to install 'config' dependency and define 'notifications.customer-notifications-topic-arn'!");
31
+ }
32
+ }
33
+ const requests = [];
34
+ for (const message of messages) {
35
+ requests.push(_sendMessage(message, topicArn, snsClient));
36
+ }
37
+ return yield Promise.all(requests);
38
+ }
39
+ catch (err) {
40
+ console.error("Error sending the customer notification:", err);
41
+ }
42
+ });
43
+ }
44
+ exports.sendCustomerNotification = sendCustomerNotification;
45
+ function _sendMessage(message, topicArn, snsClient) {
46
+ validateMessage(message);
47
+ const params = {
48
+ TopicArn: topicArn,
49
+ Message: JSON.stringify(message),
50
+ MessageAttributes: {},
51
+ };
52
+ const command = new client_sns_1.PublishCommand(params);
53
+ return snsClient.send(command);
54
+ }
55
+ function validateMessage(message) {
56
+ var _a, _b, _c, _d;
57
+ if (!message.organizationId) {
58
+ throw new Error(`Organization Id is required!`);
59
+ }
60
+ if (!((_a = message.recipient) === null || _a === void 0 ? void 0 : _a.address)) {
61
+ throw new Error(`recipient.address is required!`);
62
+ }
63
+ if (!message.type) {
64
+ throw new Error(`type is required!`);
65
+ }
66
+ if (!((_b = message.template) === null || _b === void 0 ? void 0 : _b.name)) {
67
+ throw new Error(`template.name is required!`);
68
+ }
69
+ if (!((_c = message.template) === null || _c === void 0 ? void 0 : _c.channelType)) {
70
+ throw new Error(`template.channelType is required!`);
71
+ }
72
+ if (!((_d = message.template) === null || _d === void 0 ? void 0 : _d.version)) {
73
+ throw new Error(`template.version is required!`);
74
+ }
75
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./customer-notifications/index.mjs"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { Message } from '../models/message.model.mjs';
2
+ /**
3
+ * Posts a message to customer-notifications SNS
4
+ * @param {string} messages - Extra parameters to add to description for context.
5
+ * @returns {Promise} - Resolves with the SNS publish result.
6
+ */
7
+ export declare function sendCustomerNotification(messages: Message[], topicArn?: string): Promise<any>;
8
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/customer-notifications/index.mts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEtD;;;;GAIG;AACH,wBAAsB,wBAAwB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAmBnG"}
@@ -0,0 +1,2 @@
1
+ export * from './customer-notifications/index.mjs';
2
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAA"}
@@ -0,0 +1,18 @@
1
+ export type Message = {
2
+ organizationId: String;
3
+ recipient: Recipient;
4
+ type: String;
5
+ template: Template;
6
+ substitutions?: String;
7
+ };
8
+ export type Recipient = {
9
+ employeeId?: String;
10
+ employeeName?: String;
11
+ address: String;
12
+ };
13
+ export type Template = {
14
+ name: String;
15
+ channelType: String;
16
+ version: Number;
17
+ };
18
+ //# sourceMappingURL=message.model.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.model.d.mts","sourceRoot":"","sources":["../../../../src/models/message.model.mts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GAAG;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,SAAS,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAA;AAED,MAAM,MAAM,SAAS,GAAG;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACnB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;CACnB,CAAA"}
@@ -0,0 +1,56 @@
1
+ import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
2
+ import config from 'config';
3
+ /**
4
+ * Posts a message to customer-notifications SNS
5
+ * @param {string} messages - Extra parameters to add to description for context.
6
+ * @returns {Promise} - Resolves with the SNS publish result.
7
+ */
8
+ export async function sendCustomerNotification(messages, topicArn) {
9
+ try {
10
+ const snsClient = new SNSClient(); // Default credentials and region from the environment
11
+ if (!topicArn) {
12
+ config?.get('notifications.customer-notifications-topic-arn');
13
+ if (!topicArn) {
14
+ throw new Error("if topic is not passed as a parameter, you need to install 'config' dependency and define 'notifications.customer-notifications-topic-arn'!");
15
+ }
16
+ }
17
+ const requests = [];
18
+ for (const message of messages) {
19
+ requests.push(_sendMessage(message, topicArn, snsClient));
20
+ }
21
+ return await Promise.all(requests);
22
+ }
23
+ catch (err) {
24
+ console.error("Error sending the customer notification:", err);
25
+ }
26
+ }
27
+ function _sendMessage(message, topicArn, snsClient) {
28
+ validateMessage(message);
29
+ const params = {
30
+ TopicArn: topicArn,
31
+ Message: JSON.stringify(message),
32
+ MessageAttributes: {},
33
+ };
34
+ const command = new PublishCommand(params);
35
+ return snsClient.send(command);
36
+ }
37
+ function validateMessage(message) {
38
+ if (!message.organizationId) {
39
+ throw new Error(`Organization Id is required!`);
40
+ }
41
+ if (!message.recipient?.address) {
42
+ throw new Error(`recipient.address is required!`);
43
+ }
44
+ if (!message.type) {
45
+ throw new Error(`type is required!`);
46
+ }
47
+ if (!message.template?.name) {
48
+ throw new Error(`template.name is required!`);
49
+ }
50
+ if (!message.template?.channelType) {
51
+ throw new Error(`template.channelType is required!`);
52
+ }
53
+ if (!message.template?.version) {
54
+ throw new Error(`template.version is required!`);
55
+ }
56
+ }
@@ -0,0 +1 @@
1
+ export * from './customer-notifications/index.mjs';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { Message } from '../models/message.model.mjs';
2
+ /**
3
+ * Posts a message to customer-notifications SNS
4
+ * @param {string} messages - Extra parameters to add to description for context.
5
+ * @returns {Promise} - Resolves with the SNS publish result.
6
+ */
7
+ export declare function sendCustomerNotification(messages: Message[], topicArn?: string): Promise<any>;
8
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/customer-notifications/index.mts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEtD;;;;GAIG;AACH,wBAAsB,wBAAwB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAmBnG"}
@@ -0,0 +1,2 @@
1
+ export * from './customer-notifications/index.mjs';
2
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAA"}
@@ -0,0 +1,18 @@
1
+ export type Message = {
2
+ organizationId: String;
3
+ recipient: Recipient;
4
+ type: String;
5
+ template: Template;
6
+ substitutions?: String;
7
+ };
8
+ export type Recipient = {
9
+ employeeId?: String;
10
+ employeeName?: String;
11
+ address: String;
12
+ };
13
+ export type Template = {
14
+ name: String;
15
+ channelType: String;
16
+ version: Number;
17
+ };
18
+ //# sourceMappingURL=message.model.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.model.d.mts","sourceRoot":"","sources":["../../../../src/models/message.model.mts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GAAG;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,SAAS,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAA;AAED,MAAM,MAAM,SAAS,GAAG;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACnB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;CACnB,CAAA"}
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "@orangewall/customer-notifications-util",
3
+ "version": "0.0.1",
4
+ "description": "library to send customer notifications ",
5
+ "exports": {
6
+ ".": {
7
+ "import": {
8
+ "types": "./lib/esm/types/index.d.ts",
9
+ "default": "./lib/esm/index.mjs"
10
+ },
11
+ "require": {
12
+ "types": "./lib/cjs/types/index.d.ts",
13
+ "default": "./lib/cjs/index.js"
14
+ }
15
+ }
16
+ },
17
+ "types": "./lib/cjs/types/index.d.ts",
18
+ "main": "./lib/cjs/index.js",
19
+ "files": [
20
+ "lib/**/*"
21
+ ],
22
+ "scripts": {
23
+ "clean": "rm -rf ./lib",
24
+ "build": "npm run clean && npm run build:esm && npm run build:cjs",
25
+ "build:esm": "tsc -p ./configs/tsconfig.esm.json ",
26
+ "build:cjs": "tsc -p ./configs/tsconfig.cjs.json",
27
+ "test": "mocha",
28
+ "semantic-release": "semantic-release",
29
+ "prepack": "npm run build",
30
+ "publish": "npm run build && npm publish --access public"
31
+ },
32
+ "release": {
33
+ "branches": [
34
+ "master"
35
+ ]
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://gitlab.com/orangewall-trucking/public/customer-notifications-util"
43
+ },
44
+ "keywords": [
45
+ "npm",
46
+ "javascript",
47
+ "typescript",
48
+ "esm",
49
+ "cjs",
50
+ "nodejs",
51
+ "commonjs",
52
+ "ecmascript",
53
+ "beginner",
54
+ "example",
55
+ "demonstration"
56
+ ],
57
+ "author": "Orangewall",
58
+ "license": "MIT",
59
+ "bugs": {
60
+ "url": "https://gitlab.com/orangewall-trucking/public/customer-notifications-util/issues"
61
+ },
62
+ "homepage": "https://gitlab.com/orangewall-trucking/public/customer-notifications-util#readme",
63
+ "devDependencies": {
64
+ "@aws-sdk/client-sns": "^3.716.0",
65
+ "@types/chai": "^4.3.3",
66
+ "@types/config": "^3.3.5",
67
+ "@types/mocha": "^9.1.1",
68
+ "chai": "^4.3.6",
69
+ "config": "^3.3.12",
70
+ "mocha": "^10.0.0",
71
+ "semantic-release": "^19.0.3",
72
+ "ts-node": "^10.9.1",
73
+ "typescript": "^4.7.4"
74
+ }
75
+ }