@modernlock/common 1.0.8 → 1.0.9
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/build/errors/auth-error.d.ts +8 -0
- package/build/errors/auth-error.js +17 -0
- package/build/errors/authorization-error.d.ts +8 -0
- package/build/errors/authorization-error.js +17 -0
- package/build/errors/custom-error.d.ts +8 -0
- package/build/errors/custom-error.js +12 -0
- package/build/errors/internal-server-error.d.ts +8 -0
- package/build/errors/internal-server-error.js +17 -0
- package/build/errors/not-found-error.d.ts +8 -0
- package/build/errors/not-found-error.js +16 -0
- package/build/events/Group-notification-publisher.d.ts +10 -0
- package/build/events/Group-notification-publisher.js +17 -0
- package/build/events/base-consumer.d.ts +16 -0
- package/build/events/base-consumer.js +46 -0
- package/build/events/base-producer.d.ts +16 -0
- package/build/events/base-producer.js +33 -0
- package/build/events/chat-notification-publisher.d.ts +10 -0
- package/build/events/chat-notification-publisher.js +17 -0
- package/build/events/events.d.ts +20 -0
- package/build/events/events.js +2 -0
- package/build/events/exchange-types.d.ts +6 -0
- package/build/events/exchange-types.js +11 -0
- package/build/events/exchanges.d.ts +3 -0
- package/build/events/exchanges.js +8 -0
- package/build/events/mappings.d.ts +22 -0
- package/build/events/mappings.js +25 -0
- package/build/events/queues.d.ts +18 -0
- package/build/events/queues.js +26 -0
- package/build/events/rabbitmq-wrapper.d.ts +14 -0
- package/build/events/rabbitmq-wrapper.js +117 -0
- package/build/events/routing-keys.d.ts +20 -0
- package/build/events/routing-keys.js +28 -0
- package/build/events/single-notification-publisher.d.ts +10 -0
- package/build/events/single-notification-publisher.js +17 -0
- package/build/events/templates.d.ts +14 -0
- package/build/events/templates.js +18 -0
- package/build/index.d.ts +23 -0
- package/build/index.js +35 -0
- package/build/middlewares/checkAuth.d.ts +2 -0
- package/build/middlewares/checkAuth.js +19 -0
- package/build/middlewares/checkPermissions.d.ts +3 -0
- package/build/middlewares/checkPermissions.js +60 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +13 -0
- package/build/middlewares/session.d.ts +4 -0
- package/build/middlewares/session.js +21 -0
- package/build/utils/jwtEncryption.d.ts +2 -0
- package/build/utils/jwtEncryption.js +29 -0
- package/build/utils/logger.d.ts +2 -0
- package/build/utils/logger.js +40 -0
- package/package.json +2 -3
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthenticationError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class AuthenticationError extends custom_error_1.CustomError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Not Authenticated");
|
|
8
|
+
this.statusCode = 401;
|
|
9
|
+
Object.setPrototypeOf(this, AuthenticationError.prototype);
|
|
10
|
+
}
|
|
11
|
+
;
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: "Not Authenticated" }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.AuthenticationError = AuthenticationError;
|
|
17
|
+
;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthorizationError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class AuthorizationError extends custom_error_1.CustomError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Not Authorized to perform this operation");
|
|
8
|
+
this.statusCode = 403;
|
|
9
|
+
Object.setPrototypeOf(this, AuthorizationError.prototype);
|
|
10
|
+
}
|
|
11
|
+
;
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: "Not Authorized to perform this operation" }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.AuthorizationError = AuthorizationError;
|
|
17
|
+
;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomError = void 0;
|
|
4
|
+
class CustomError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
Object.setPrototypeOf(this, CustomError.prototype);
|
|
8
|
+
}
|
|
9
|
+
;
|
|
10
|
+
}
|
|
11
|
+
exports.CustomError = CustomError;
|
|
12
|
+
;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InternalServerError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class InternalServerError extends custom_error_1.CustomError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Internal server error");
|
|
8
|
+
this.statusCode = 500;
|
|
9
|
+
Object.setPrototypeOf(this, InternalServerError.prototype);
|
|
10
|
+
}
|
|
11
|
+
;
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: this.message }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.InternalServerError = InternalServerError;
|
|
17
|
+
;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotFoundError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class NotFoundError extends custom_error_1.CustomError {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Route not found");
|
|
8
|
+
this.statusCode = 404;
|
|
9
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
10
|
+
}
|
|
11
|
+
;
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: "Not found error" }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.NotFoundError = NotFoundError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ExchangeTypes } from './exchange-types';
|
|
2
|
+
import { Exchanges } from './exchanges';
|
|
3
|
+
import { RoutingKeys } from "./routing-keys";
|
|
4
|
+
import { GroupNotificationEvent } from "./events";
|
|
5
|
+
import { Producer } from "./base-producer";
|
|
6
|
+
export declare class GroupNotificationPublisher extends Producer<GroupNotificationEvent> {
|
|
7
|
+
exchange: Exchanges.mainExchange;
|
|
8
|
+
exchangeType: ExchangeTypes.direct;
|
|
9
|
+
routingKey: RoutingKeys.groupNotification;
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GroupNotificationPublisher = void 0;
|
|
4
|
+
const exchange_types_1 = require("./exchange-types");
|
|
5
|
+
const exchanges_1 = require("./exchanges");
|
|
6
|
+
const routing_keys_1 = require("./routing-keys");
|
|
7
|
+
const base_producer_1 = require("./base-producer");
|
|
8
|
+
class GroupNotificationPublisher extends base_producer_1.Producer {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
this.exchange = exchanges_1.Exchanges.mainExchange;
|
|
12
|
+
this.exchangeType = exchange_types_1.ExchangeTypes.direct;
|
|
13
|
+
this.routingKey = routing_keys_1.RoutingKeys.groupNotification;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.GroupNotificationPublisher = GroupNotificationPublisher;
|
|
17
|
+
;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
|
+
import { RoutingKeys } from './routing-keys';
|
|
3
|
+
import { ExchangeTypes } from './exchange-types';
|
|
4
|
+
import { Exchanges } from './exchanges';
|
|
5
|
+
import { Queues } from './queues';
|
|
6
|
+
export declare abstract class Consumer {
|
|
7
|
+
abstract exchange: Exchanges;
|
|
8
|
+
abstract exchangeType: ExchangeTypes;
|
|
9
|
+
abstract routingKey: RoutingKeys;
|
|
10
|
+
abstract queue: Queues;
|
|
11
|
+
abstract onMessage(msg: amqp.Message, data: any, channel: amqp.Channel): void;
|
|
12
|
+
private channel;
|
|
13
|
+
constructor(channel: amqp.Channel);
|
|
14
|
+
listen(): Promise<void>;
|
|
15
|
+
parseMessage(msg: amqp.Message): any;
|
|
16
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Consumer = void 0;
|
|
13
|
+
class Consumer {
|
|
14
|
+
constructor(channel) {
|
|
15
|
+
this.channel = channel;
|
|
16
|
+
}
|
|
17
|
+
;
|
|
18
|
+
listen() {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
try {
|
|
21
|
+
yield this.channel.assertExchange(this.exchange, this.exchangeType);
|
|
22
|
+
const q = yield this.channel.assertQueue(this.queue);
|
|
23
|
+
yield this.channel.bindQueue(q.queue, this.exchange, this.routingKey);
|
|
24
|
+
this.channel.consume(q.queue, (msg) => {
|
|
25
|
+
if (!msg)
|
|
26
|
+
throw new Error('Message is required');
|
|
27
|
+
const data = this.parseMessage(msg);
|
|
28
|
+
this.onMessage(msg, data, this.channel);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error('Failed to publish message:', error);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
;
|
|
37
|
+
parseMessage(msg) {
|
|
38
|
+
const data = msg.content;
|
|
39
|
+
return typeof data === 'string'
|
|
40
|
+
? JSON.parse(data)
|
|
41
|
+
: JSON.parse(data.toString('utf8'));
|
|
42
|
+
}
|
|
43
|
+
;
|
|
44
|
+
}
|
|
45
|
+
exports.Consumer = Consumer;
|
|
46
|
+
;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
|
+
import { ExchangeTypes } from './exchange-types';
|
|
3
|
+
import { Exchanges } from './exchanges';
|
|
4
|
+
import { RoutingKeys } from "./routing-keys";
|
|
5
|
+
interface Event {
|
|
6
|
+
data: any;
|
|
7
|
+
}
|
|
8
|
+
export declare abstract class Producer<T extends Event> {
|
|
9
|
+
abstract exchange: Exchanges;
|
|
10
|
+
abstract exchangeType: ExchangeTypes;
|
|
11
|
+
abstract routingKey: RoutingKeys;
|
|
12
|
+
private channel;
|
|
13
|
+
constructor(channel: amqp.Channel);
|
|
14
|
+
publish(data: T["data"]): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Producer = void 0;
|
|
13
|
+
class Producer {
|
|
14
|
+
constructor(channel) {
|
|
15
|
+
this.channel = channel;
|
|
16
|
+
}
|
|
17
|
+
;
|
|
18
|
+
publish(data) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
try {
|
|
21
|
+
yield this.channel.assertExchange(this.exchange, this.exchangeType, { durable: true });
|
|
22
|
+
yield this.channel.publish(this.exchange, this.routingKey, Buffer.from(JSON.stringify(data)));
|
|
23
|
+
console.log('message published:', data);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.error('Failed to publish data:', error);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.Producer = Producer;
|
|
33
|
+
;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ExchangeTypes } from './exchange-types';
|
|
2
|
+
import { Exchanges } from './exchanges';
|
|
3
|
+
import { RoutingKeys } from "./routing-keys";
|
|
4
|
+
import { ChatNotificationEvent } from "./events";
|
|
5
|
+
import { Producer } from "./base-producer";
|
|
6
|
+
export declare class ChatNotificationPublisher extends Producer<ChatNotificationEvent> {
|
|
7
|
+
exchange: Exchanges.mainExchange;
|
|
8
|
+
exchangeType: ExchangeTypes.direct;
|
|
9
|
+
routingKey: RoutingKeys.chatNotification;
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChatNotificationPublisher = void 0;
|
|
4
|
+
const exchange_types_1 = require("./exchange-types");
|
|
5
|
+
const exchanges_1 = require("./exchanges");
|
|
6
|
+
const routing_keys_1 = require("./routing-keys");
|
|
7
|
+
const base_producer_1 = require("./base-producer");
|
|
8
|
+
class ChatNotificationPublisher extends base_producer_1.Producer {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
this.exchange = exchanges_1.Exchanges.mainExchange;
|
|
12
|
+
this.exchangeType = exchange_types_1.ExchangeTypes.direct;
|
|
13
|
+
this.routingKey = routing_keys_1.RoutingKeys.chatNotification;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.ChatNotificationPublisher = ChatNotificationPublisher;
|
|
17
|
+
;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Templates } from "./templates";
|
|
2
|
+
export interface SingleNotificationEvent {
|
|
3
|
+
data: {
|
|
4
|
+
template: Templates;
|
|
5
|
+
args?: any;
|
|
6
|
+
userId: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export interface GroupNotificationEvent {
|
|
10
|
+
data: {
|
|
11
|
+
template: Templates;
|
|
12
|
+
args?: any;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export interface ChatNotificationEvent {
|
|
16
|
+
data: {
|
|
17
|
+
ids: string[];
|
|
18
|
+
message: any;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExchangeTypes = void 0;
|
|
4
|
+
var ExchangeTypes;
|
|
5
|
+
(function (ExchangeTypes) {
|
|
6
|
+
ExchangeTypes["direct"] = "direct";
|
|
7
|
+
ExchangeTypes["topic"] = "topic";
|
|
8
|
+
ExchangeTypes["headers"] = "headers";
|
|
9
|
+
ExchangeTypes["fanout"] = "fanout";
|
|
10
|
+
})(ExchangeTypes = exports.ExchangeTypes || (exports.ExchangeTypes = {}));
|
|
11
|
+
;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Exchanges = void 0;
|
|
4
|
+
var Exchanges;
|
|
5
|
+
(function (Exchanges) {
|
|
6
|
+
Exchanges["mainExchange"] = "mainExchange";
|
|
7
|
+
})(Exchanges = exports.Exchanges || (exports.Exchanges = {}));
|
|
8
|
+
;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare enum Categories {
|
|
2
|
+
platformUpdates = "platformUpdates",
|
|
3
|
+
profileCompletion = "profileCompletion",
|
|
4
|
+
accountManagement = "accountManagement",
|
|
5
|
+
tipsAndBestPractices = "tipsAndBestPractices",
|
|
6
|
+
mandatory = "mandatory"
|
|
7
|
+
}
|
|
8
|
+
export declare const templateCategoryMapping: {
|
|
9
|
+
accountVerificationActivation: Categories;
|
|
10
|
+
accountDeletingConfirmation: Categories;
|
|
11
|
+
passwordRecoveryInstructions: Categories;
|
|
12
|
+
notificationOfPlatformMaintenanceOrDowntime: Categories;
|
|
13
|
+
accountOnboardingInstructions: Categories;
|
|
14
|
+
reminderToCompleteProfile: Categories;
|
|
15
|
+
notificationOfNewLinkedWallet: Categories;
|
|
16
|
+
notificationOfNewLinkedEmail: Categories;
|
|
17
|
+
notificationOfModifiedLinkedWallet: Categories;
|
|
18
|
+
notificationOfModifiedEmail: Categories;
|
|
19
|
+
notificationOfModifiedPassword: Categories;
|
|
20
|
+
tipsAndBestPractices: Categories;
|
|
21
|
+
};
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.templateCategoryMapping = void 0;
|
|
4
|
+
var Categories;
|
|
5
|
+
(function (Categories) {
|
|
6
|
+
Categories["platformUpdates"] = "platformUpdates";
|
|
7
|
+
Categories["profileCompletion"] = "profileCompletion";
|
|
8
|
+
Categories["accountManagement"] = "accountManagement";
|
|
9
|
+
Categories["tipsAndBestPractices"] = "tipsAndBestPractices";
|
|
10
|
+
Categories["mandatory"] = "mandatory";
|
|
11
|
+
})(Categories || (Categories = {}));
|
|
12
|
+
exports.templateCategoryMapping = {
|
|
13
|
+
accountVerificationActivation: Categories.mandatory,
|
|
14
|
+
accountDeletingConfirmation: Categories.mandatory,
|
|
15
|
+
passwordRecoveryInstructions: Categories.mandatory,
|
|
16
|
+
notificationOfPlatformMaintenanceOrDowntime: Categories.mandatory,
|
|
17
|
+
accountOnboardingInstructions: Categories.mandatory,
|
|
18
|
+
reminderToCompleteProfile: Categories.profileCompletion,
|
|
19
|
+
notificationOfNewLinkedWallet: Categories.accountManagement,
|
|
20
|
+
notificationOfNewLinkedEmail: Categories.accountManagement,
|
|
21
|
+
notificationOfModifiedLinkedWallet: Categories.accountManagement,
|
|
22
|
+
notificationOfModifiedEmail: Categories.accountManagement,
|
|
23
|
+
notificationOfModifiedPassword: Categories.mandatory,
|
|
24
|
+
tipsAndBestPractices: Categories.tipsAndBestPractices,
|
|
25
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare enum Queues {
|
|
2
|
+
userCreatedQueue = "userCreatedQueue",
|
|
3
|
+
userUpdatedQueue = "userUpdatedQueue",
|
|
4
|
+
orderQueue = "orderQueue",
|
|
5
|
+
contractQueue = "contractQueue",
|
|
6
|
+
FileConfirmQeueu = "FileConfirmQeueu",
|
|
7
|
+
FileRemoveQeueu = "FileRemoveQeueu",
|
|
8
|
+
severalUserCreatedQeueu = "severalUserCreatedQeueu",
|
|
9
|
+
singleNotificationQeueu = "singleNotificationQeueu",
|
|
10
|
+
groupNotificationsQeueu = "groupNotificationsQeueu",
|
|
11
|
+
chatNotificationsQueue = "chatNotificationsQueue",
|
|
12
|
+
notificationAppQueue = "notificationAppQueue",
|
|
13
|
+
groupNotificationAppQueue = "groupNotificationAppQueue",
|
|
14
|
+
chatNotificationAppQueue = "chatNotificationAppQueue",
|
|
15
|
+
notificationEmailQueue = "notificationEmailQueue",
|
|
16
|
+
groupNotificationEmailQueue = "groupNotificationEmailQueue",
|
|
17
|
+
chatNotificationEmailQueue = "chatNotificationEmailQueue"
|
|
18
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Queues = void 0;
|
|
4
|
+
var Queues;
|
|
5
|
+
(function (Queues) {
|
|
6
|
+
Queues["userCreatedQueue"] = "userCreatedQueue";
|
|
7
|
+
Queues["userUpdatedQueue"] = "userUpdatedQueue";
|
|
8
|
+
Queues["orderQueue"] = "orderQueue";
|
|
9
|
+
Queues["contractQueue"] = "contractQueue";
|
|
10
|
+
Queues["FileConfirmQeueu"] = "FileConfirmQeueu";
|
|
11
|
+
Queues["FileRemoveQeueu"] = "FileRemoveQeueu";
|
|
12
|
+
Queues["severalUserCreatedQeueu"] = "severalUserCreatedQeueu";
|
|
13
|
+
// notification service queues
|
|
14
|
+
Queues["singleNotificationQeueu"] = "singleNotificationQeueu";
|
|
15
|
+
Queues["groupNotificationsQeueu"] = "groupNotificationsQeueu";
|
|
16
|
+
Queues["chatNotificationsQueue"] = "chatNotificationsQueue";
|
|
17
|
+
// app workers queue
|
|
18
|
+
Queues["notificationAppQueue"] = "notificationAppQueue";
|
|
19
|
+
Queues["groupNotificationAppQueue"] = "groupNotificationAppQueue";
|
|
20
|
+
Queues["chatNotificationAppQueue"] = "chatNotificationAppQueue";
|
|
21
|
+
// email worker queue
|
|
22
|
+
Queues["notificationEmailQueue"] = "notificationEmailQueue";
|
|
23
|
+
Queues["groupNotificationEmailQueue"] = "groupNotificationEmailQueue";
|
|
24
|
+
Queues["chatNotificationEmailQueue"] = "chatNotificationEmailQueue";
|
|
25
|
+
})(Queues = exports.Queues || (exports.Queues = {}));
|
|
26
|
+
;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
|
+
export declare class RabbitmqWrapper {
|
|
3
|
+
private connection;
|
|
4
|
+
private _channel;
|
|
5
|
+
private url;
|
|
6
|
+
private isConnecting;
|
|
7
|
+
private isConnected;
|
|
8
|
+
private reconnectRetries;
|
|
9
|
+
get channel(): amqp.Channel;
|
|
10
|
+
connect(url: string): Promise<void>;
|
|
11
|
+
connectWithRetries(url: string): Promise<void>;
|
|
12
|
+
private reconnect;
|
|
13
|
+
}
|
|
14
|
+
export declare const rabbitmqWrapper: RabbitmqWrapper;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.rabbitmqWrapper = exports.RabbitmqWrapper = void 0;
|
|
32
|
+
const amqp = __importStar(require("amqplib"));
|
|
33
|
+
const MAX_RETRIES = 5; // Maximum number of reconnection attempts
|
|
34
|
+
const INITIAL_DELAY_MS = 1000; //
|
|
35
|
+
class RabbitmqWrapper {
|
|
36
|
+
constructor() {
|
|
37
|
+
this.isConnecting = false;
|
|
38
|
+
this.isConnected = false;
|
|
39
|
+
this.reconnectRetries = 0; // Number of reconnection attempts
|
|
40
|
+
}
|
|
41
|
+
get channel() {
|
|
42
|
+
if (!this._channel) {
|
|
43
|
+
throw new Error("Cannot access RabbitMQ channel before connecting");
|
|
44
|
+
}
|
|
45
|
+
return this._channel;
|
|
46
|
+
}
|
|
47
|
+
connect(url) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
if (this.isConnecting || this.isConnected)
|
|
50
|
+
return;
|
|
51
|
+
this.isConnecting = true;
|
|
52
|
+
try {
|
|
53
|
+
this.connection = yield amqp.connect(url);
|
|
54
|
+
this._channel = yield this.connection.createChannel();
|
|
55
|
+
console.log("Connected to RabbitMQ");
|
|
56
|
+
this.url = url;
|
|
57
|
+
this.isConnected = true;
|
|
58
|
+
// Handle close event (e.g., network issues or RabbitMQ server restart)
|
|
59
|
+
this.connection.on("close", () => {
|
|
60
|
+
console.error("RabbitMQ connection closed unexpectedly");
|
|
61
|
+
this.isConnected = false;
|
|
62
|
+
this.isConnecting = false;
|
|
63
|
+
// Implement reconnection logic here
|
|
64
|
+
this.reconnect();
|
|
65
|
+
});
|
|
66
|
+
// Handle errors
|
|
67
|
+
this.connection.on("error", (err) => {
|
|
68
|
+
console.error("RabbitMQ connection error:", err.message);
|
|
69
|
+
this.isConnected = false;
|
|
70
|
+
this.isConnecting = false;
|
|
71
|
+
// Implement error handling logic here
|
|
72
|
+
// You can choose to reconnect or take other actions based on the error
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.error("Failed to connect to RabbitMQ:", error);
|
|
77
|
+
this.isConnecting = false;
|
|
78
|
+
// Implement error handling logic here
|
|
79
|
+
// You can choose to retry the connection or take other actions based on the error
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
connectWithRetries(url) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
try {
|
|
86
|
+
yield this.connect(url);
|
|
87
|
+
console.log('Connected to RabbitMQ');
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error('Failed to connect to RabbitMQ:', error);
|
|
91
|
+
// Implement reconnection logic with exponential backoff
|
|
92
|
+
if (this.reconnectRetries < MAX_RETRIES) {
|
|
93
|
+
const delay = INITIAL_DELAY_MS * Math.pow(2, this.reconnectRetries);
|
|
94
|
+
console.log(`Retrying connection in ${delay} ms (attempt ${this.reconnectRetries + 1} of ${MAX_RETRIES})`);
|
|
95
|
+
this.reconnectRetries++;
|
|
96
|
+
setTimeout(() => {
|
|
97
|
+
this.connectWithRetries(url);
|
|
98
|
+
}, delay);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
console.error(`Max reconnection attempts (${MAX_RETRIES}) reached. Could not reconnect to RabbitMQ.`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
reconnect() {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
if (!this.isConnecting && !this.isConnected) {
|
|
109
|
+
console.log("Attempting to reconnect to RabbitMQ...");
|
|
110
|
+
this.connectWithRetries(this.url);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.RabbitmqWrapper = RabbitmqWrapper;
|
|
116
|
+
;
|
|
117
|
+
exports.rabbitmqWrapper = new RabbitmqWrapper();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare enum RoutingKeys {
|
|
2
|
+
orderCreated = "order:created",
|
|
3
|
+
orderUpdated = "order:updated",
|
|
4
|
+
orderCanceled = "order:canceled",
|
|
5
|
+
userCreated = "user:created",
|
|
6
|
+
userUpdated = "user:updated",
|
|
7
|
+
userDeleted = "User:deleted",
|
|
8
|
+
severalUsersCreated = "severalUsers:created",
|
|
9
|
+
fileConfirmed = "file:confirmed",
|
|
10
|
+
fileRemoved = "file:removed",
|
|
11
|
+
groupNotification = "notification:groupNotification",
|
|
12
|
+
singleNotification = "notification:singleNotification",
|
|
13
|
+
chatNotification = "notification:chatNotification",
|
|
14
|
+
notificationEmailWorkers = "notification:email-workers",
|
|
15
|
+
groupNotificationEmailWorkers = "groupNotification:email-workers",
|
|
16
|
+
chatNotificationEmailWorkers = "chatNotification:email-workers",
|
|
17
|
+
notificationAppWorkers = "notification:app-workers",
|
|
18
|
+
groupNotificationAppWorkers = "groupNotification:app-workers",
|
|
19
|
+
chatNotificationAppWorkers = "chatNotification:app-workers"
|
|
20
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RoutingKeys = void 0;
|
|
4
|
+
var RoutingKeys;
|
|
5
|
+
(function (RoutingKeys) {
|
|
6
|
+
RoutingKeys["orderCreated"] = "order:created";
|
|
7
|
+
RoutingKeys["orderUpdated"] = "order:updated";
|
|
8
|
+
RoutingKeys["orderCanceled"] = "order:canceled";
|
|
9
|
+
RoutingKeys["userCreated"] = "user:created";
|
|
10
|
+
RoutingKeys["userUpdated"] = "user:updated";
|
|
11
|
+
RoutingKeys["userDeleted"] = "User:deleted";
|
|
12
|
+
RoutingKeys["severalUsersCreated"] = "severalUsers:created";
|
|
13
|
+
RoutingKeys["fileConfirmed"] = "file:confirmed";
|
|
14
|
+
RoutingKeys["fileRemoved"] = "file:removed";
|
|
15
|
+
// notification service routing keys
|
|
16
|
+
RoutingKeys["groupNotification"] = "notification:groupNotification";
|
|
17
|
+
RoutingKeys["singleNotification"] = "notification:singleNotification";
|
|
18
|
+
RoutingKeys["chatNotification"] = "notification:chatNotification";
|
|
19
|
+
// Email workers routing keys
|
|
20
|
+
RoutingKeys["notificationEmailWorkers"] = "notification:email-workers";
|
|
21
|
+
RoutingKeys["groupNotificationEmailWorkers"] = "groupNotification:email-workers";
|
|
22
|
+
RoutingKeys["chatNotificationEmailWorkers"] = "chatNotification:email-workers";
|
|
23
|
+
// App workers routing keys
|
|
24
|
+
RoutingKeys["notificationAppWorkers"] = "notification:app-workers";
|
|
25
|
+
RoutingKeys["groupNotificationAppWorkers"] = "groupNotification:app-workers";
|
|
26
|
+
RoutingKeys["chatNotificationAppWorkers"] = "chatNotification:app-workers";
|
|
27
|
+
})(RoutingKeys = exports.RoutingKeys || (exports.RoutingKeys = {}));
|
|
28
|
+
;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ExchangeTypes } from './exchange-types';
|
|
2
|
+
import { Exchanges } from './exchanges';
|
|
3
|
+
import { RoutingKeys } from "./routing-keys";
|
|
4
|
+
import { SingleNotificationEvent } from "./events";
|
|
5
|
+
import { Producer } from "./base-producer";
|
|
6
|
+
export declare class SingleNotificationPublisher extends Producer<SingleNotificationEvent> {
|
|
7
|
+
exchange: Exchanges.mainExchange;
|
|
8
|
+
exchangeType: ExchangeTypes.direct;
|
|
9
|
+
routingKey: RoutingKeys.singleNotification;
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SingleNotificationPublisher = void 0;
|
|
4
|
+
const exchange_types_1 = require("./exchange-types");
|
|
5
|
+
const exchanges_1 = require("./exchanges");
|
|
6
|
+
const routing_keys_1 = require("./routing-keys");
|
|
7
|
+
const base_producer_1 = require("./base-producer");
|
|
8
|
+
class SingleNotificationPublisher extends base_producer_1.Producer {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
this.exchange = exchanges_1.Exchanges.mainExchange;
|
|
12
|
+
this.exchangeType = exchange_types_1.ExchangeTypes.direct;
|
|
13
|
+
this.routingKey = routing_keys_1.RoutingKeys.singleNotification;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.SingleNotificationPublisher = SingleNotificationPublisher;
|
|
17
|
+
;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare enum Templates {
|
|
2
|
+
accountVerificationActivation = "accountVerificationActivation",
|
|
3
|
+
accountOnboardingInstructions = "accountOnboardingInstructions",
|
|
4
|
+
reminderToCompleteProfile = "reminderToCompleteProfile",
|
|
5
|
+
accountDeletingConfirmation = "accountDeletingConfirmation",
|
|
6
|
+
passwordRecoveryInstructions = "passwordRecoveryInstructions",
|
|
7
|
+
notificationOfNewLinkedWallet = "notificationOfNewLinkedWallet",
|
|
8
|
+
notificationOfNewLinkedEmail = "notificationOfNewLinkedEmail",
|
|
9
|
+
notificationOfModifiedLinkedWallet = "notificationOfModifiedLinkedWallet",
|
|
10
|
+
notificationOfModifiedEmail = "notificationOfModifiedEmail",
|
|
11
|
+
notificationOfModifiedPassword = "notificationOfModifiedPassword",
|
|
12
|
+
tipsAndBestPractices = "tipsAndBestPractices",
|
|
13
|
+
notificationOfPlatformMaintenanceOrDowntime = "notificationOfPlatformMaintenanceOrDowntime"
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Templates = void 0;
|
|
4
|
+
var Templates;
|
|
5
|
+
(function (Templates) {
|
|
6
|
+
Templates["accountVerificationActivation"] = "accountVerificationActivation";
|
|
7
|
+
Templates["accountOnboardingInstructions"] = "accountOnboardingInstructions";
|
|
8
|
+
Templates["reminderToCompleteProfile"] = "reminderToCompleteProfile";
|
|
9
|
+
Templates["accountDeletingConfirmation"] = "accountDeletingConfirmation";
|
|
10
|
+
Templates["passwordRecoveryInstructions"] = "passwordRecoveryInstructions";
|
|
11
|
+
Templates["notificationOfNewLinkedWallet"] = "notificationOfNewLinkedWallet";
|
|
12
|
+
Templates["notificationOfNewLinkedEmail"] = "notificationOfNewLinkedEmail";
|
|
13
|
+
Templates["notificationOfModifiedLinkedWallet"] = "notificationOfModifiedLinkedWallet";
|
|
14
|
+
Templates["notificationOfModifiedEmail"] = "notificationOfModifiedEmail";
|
|
15
|
+
Templates["notificationOfModifiedPassword"] = "notificationOfModifiedPassword";
|
|
16
|
+
Templates["tipsAndBestPractices"] = "tipsAndBestPractices";
|
|
17
|
+
Templates["notificationOfPlatformMaintenanceOrDowntime"] = "notificationOfPlatformMaintenanceOrDowntime";
|
|
18
|
+
})(Templates = exports.Templates || (exports.Templates = {}));
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export * from "./events/base-consumer";
|
|
2
|
+
export * from "./events/base-producer";
|
|
3
|
+
export * from "./events/exchanges";
|
|
4
|
+
export * from "./events/exchange-types";
|
|
5
|
+
export * from "./events/queues";
|
|
6
|
+
export * from "./events/routing-keys";
|
|
7
|
+
export * from "./events/rabbitmq-wrapper";
|
|
8
|
+
export * from "./events/templates";
|
|
9
|
+
export * from "./events/single-notification-publisher";
|
|
10
|
+
export * from "./events/Group-notification-publisher";
|
|
11
|
+
export * from "./events/chat-notification-publisher";
|
|
12
|
+
export * from "./events/events";
|
|
13
|
+
export * from "./events/mappings";
|
|
14
|
+
export * from "./errors/custom-error";
|
|
15
|
+
export * from "./errors/internal-server-error";
|
|
16
|
+
export * from "./errors/not-found-error";
|
|
17
|
+
export * from "./errors/auth-error";
|
|
18
|
+
export * from "./middlewares/checkPermissions";
|
|
19
|
+
export * from "./middlewares/checkAuth";
|
|
20
|
+
export * from "./middlewares/error-handler";
|
|
21
|
+
export * from "./middlewares/session";
|
|
22
|
+
export * from "./utils/jwtEncryption";
|
|
23
|
+
export * from "./utils/logger";
|
package/build/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./events/base-consumer"), exports);
|
|
14
|
+
__exportStar(require("./events/base-producer"), exports);
|
|
15
|
+
__exportStar(require("./events/exchanges"), exports);
|
|
16
|
+
__exportStar(require("./events/exchange-types"), exports);
|
|
17
|
+
__exportStar(require("./events/queues"), exports);
|
|
18
|
+
__exportStar(require("./events/routing-keys"), exports);
|
|
19
|
+
__exportStar(require("./events/rabbitmq-wrapper"), exports);
|
|
20
|
+
__exportStar(require("./events/templates"), exports);
|
|
21
|
+
__exportStar(require("./events/single-notification-publisher"), exports);
|
|
22
|
+
__exportStar(require("./events/Group-notification-publisher"), exports);
|
|
23
|
+
__exportStar(require("./events/chat-notification-publisher"), exports);
|
|
24
|
+
__exportStar(require("./events/events"), exports);
|
|
25
|
+
__exportStar(require("./events/mappings"), exports);
|
|
26
|
+
__exportStar(require("./errors/custom-error"), exports);
|
|
27
|
+
__exportStar(require("./errors/internal-server-error"), exports);
|
|
28
|
+
__exportStar(require("./errors/not-found-error"), exports);
|
|
29
|
+
__exportStar(require("./errors/auth-error"), exports);
|
|
30
|
+
__exportStar(require("./middlewares/checkPermissions"), exports);
|
|
31
|
+
__exportStar(require("./middlewares/checkAuth"), exports);
|
|
32
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
|
33
|
+
__exportStar(require("./middlewares/session"), exports);
|
|
34
|
+
__exportStar(require("./utils/jwtEncryption"), exports);
|
|
35
|
+
__exportStar(require("./utils/logger"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.authenticateToken = void 0;
|
|
4
|
+
const auth_error_1 = require("../errors/auth-error");
|
|
5
|
+
const jwtEncryption_1 = require("../utils/jwtEncryption");
|
|
6
|
+
const authenticateToken = (req, res, next) => {
|
|
7
|
+
const { token } = req.session;
|
|
8
|
+
if (!token)
|
|
9
|
+
throw new auth_error_1.AuthenticationError();
|
|
10
|
+
const user = (0, jwtEncryption_1.jwtDecrypt)(token);
|
|
11
|
+
if (!user)
|
|
12
|
+
throw new auth_error_1.AuthenticationError();
|
|
13
|
+
else {
|
|
14
|
+
req.user = user;
|
|
15
|
+
next();
|
|
16
|
+
}
|
|
17
|
+
;
|
|
18
|
+
};
|
|
19
|
+
exports.authenticateToken = authenticateToken;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
export declare function strongCheck(read: boolean, write: boolean): (req: Request, res: Response, next: NextFunction) => void;
|
|
3
|
+
export declare function weakCheck(read: boolean, write: boolean): (req: Request, res: Response, next: NextFunction) => void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.weakCheck = exports.strongCheck = void 0;
|
|
4
|
+
const auth_error_1 = require("../errors/auth-error");
|
|
5
|
+
const authorization_error_1 = require("../errors/authorization-error");
|
|
6
|
+
const internal_server_error_1 = require("../errors/internal-server-error");
|
|
7
|
+
const jwtEncryption_1 = require("../utils/jwtEncryption");
|
|
8
|
+
function strongCheck(read, write) {
|
|
9
|
+
return (req, res, next) => {
|
|
10
|
+
var _a, _b;
|
|
11
|
+
if (!(write && read))
|
|
12
|
+
throw new internal_server_error_1.InternalServerError();
|
|
13
|
+
const { token } = req.session;
|
|
14
|
+
if (!token)
|
|
15
|
+
throw new auth_error_1.AuthenticationError();
|
|
16
|
+
const user = (0, jwtEncryption_1.jwtDecrypt)(token);
|
|
17
|
+
if (!user)
|
|
18
|
+
throw new auth_error_1.AuthenticationError();
|
|
19
|
+
if (!user.isAdmin)
|
|
20
|
+
throw new authorization_error_1.AuthorizationError();
|
|
21
|
+
if (write && !((_a = user.permissions) === null || _a === void 0 ? void 0 : _a.write))
|
|
22
|
+
throw new authorization_error_1.AuthorizationError();
|
|
23
|
+
if (read && !((_b = user.permissions) === null || _b === void 0 ? void 0 : _b.read))
|
|
24
|
+
throw new authorization_error_1.AuthorizationError();
|
|
25
|
+
req.user = user;
|
|
26
|
+
next();
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
exports.strongCheck = strongCheck;
|
|
30
|
+
;
|
|
31
|
+
function weakCheck(read, write) {
|
|
32
|
+
return (req, res, next) => {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
if (!(write && read))
|
|
35
|
+
throw new internal_server_error_1.InternalServerError();
|
|
36
|
+
const { token } = req.session;
|
|
37
|
+
if (!token)
|
|
38
|
+
throw new auth_error_1.AuthenticationError();
|
|
39
|
+
const user = (0, jwtEncryption_1.jwtDecrypt)(token);
|
|
40
|
+
if (!user)
|
|
41
|
+
throw new auth_error_1.AuthenticationError();
|
|
42
|
+
req.user = user;
|
|
43
|
+
if (!user.isAdmin) {
|
|
44
|
+
req.user.allowed = false;
|
|
45
|
+
return next();
|
|
46
|
+
}
|
|
47
|
+
if (write && !((_a = user.permissions) === null || _a === void 0 ? void 0 : _a.write)) {
|
|
48
|
+
req.user.allowed = false;
|
|
49
|
+
return next();
|
|
50
|
+
}
|
|
51
|
+
if (read && !((_b = user.permissions) === null || _b === void 0 ? void 0 : _b.read)) {
|
|
52
|
+
req.user.allowed = false;
|
|
53
|
+
return next();
|
|
54
|
+
}
|
|
55
|
+
req.user.allowed = true;
|
|
56
|
+
next();
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
exports.weakCheck = weakCheck;
|
|
60
|
+
;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorHandler = void 0;
|
|
4
|
+
const custom_error_1 = require("../errors/custom-error");
|
|
5
|
+
const errorHandler = (err, req, res, next) => {
|
|
6
|
+
if (err instanceof custom_error_1.CustomError) {
|
|
7
|
+
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
8
|
+
}
|
|
9
|
+
res.status(400).send({
|
|
10
|
+
errors: [{ message: err.message ? err.message : "Something went wrong" }]
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
exports.errorHandler = errorHandler;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.session = void 0;
|
|
7
|
+
const cookie_session_1 = __importDefault(require("cookie-session"));
|
|
8
|
+
const session = () => {
|
|
9
|
+
return (0, cookie_session_1.default)({
|
|
10
|
+
secret: process.env.SESSION_SECRET,
|
|
11
|
+
name: "session",
|
|
12
|
+
signed: false,
|
|
13
|
+
secure: false,
|
|
14
|
+
sameSite: "lax",
|
|
15
|
+
httpOnly: true,
|
|
16
|
+
path: "/",
|
|
17
|
+
maxAge: 60 * 60 * 24 * 100 * 365,
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
exports.session = session;
|
|
21
|
+
exports.default = exports.session;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.jwtDecrypt = exports.jwtEncrypt = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
function jwtEncrypt(data, expiration = "5m") {
|
|
9
|
+
if (!process.env.ACCESS_TOKEN)
|
|
10
|
+
throw new Error("ACCESS_TOKEN must be defined");
|
|
11
|
+
let accessToken = jsonwebtoken_1.default.sign(data, process.env.ACCESS_TOKEN, {
|
|
12
|
+
expiresIn: expiration,
|
|
13
|
+
});
|
|
14
|
+
return accessToken;
|
|
15
|
+
}
|
|
16
|
+
exports.jwtEncrypt = jwtEncrypt;
|
|
17
|
+
function jwtDecrypt(token) {
|
|
18
|
+
if (!process.env.ACCESS_TOKEN)
|
|
19
|
+
throw new Error("ACCESS_TOKEN must be defined");
|
|
20
|
+
let data = null;
|
|
21
|
+
jsonwebtoken_1.default.verify(token, process.env.ACCESS_TOKEN, (err, user) => {
|
|
22
|
+
if (err)
|
|
23
|
+
return;
|
|
24
|
+
else
|
|
25
|
+
data = user;
|
|
26
|
+
});
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
exports.jwtDecrypt = jwtDecrypt;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.winstonLogger = void 0;
|
|
7
|
+
const winston_1 = __importDefault(require("winston"));
|
|
8
|
+
const winston_elasticsearch_1 = require("winston-elasticsearch");
|
|
9
|
+
const esTransformer = (logData) => {
|
|
10
|
+
return (0, winston_elasticsearch_1.ElasticsearchTransformer)(logData);
|
|
11
|
+
};
|
|
12
|
+
const winstonLogger = (elasticsearchNode, name, level) => {
|
|
13
|
+
const options = {
|
|
14
|
+
console: {
|
|
15
|
+
level,
|
|
16
|
+
handleExceptions: true,
|
|
17
|
+
json: false,
|
|
18
|
+
colorize: true
|
|
19
|
+
},
|
|
20
|
+
elasticsearch: {
|
|
21
|
+
level,
|
|
22
|
+
transformer: esTransformer,
|
|
23
|
+
clientOpts: {
|
|
24
|
+
node: elasticsearchNode,
|
|
25
|
+
log: level,
|
|
26
|
+
maxRetries: 2,
|
|
27
|
+
requestTimeout: 10000,
|
|
28
|
+
sniffOnStart: false
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const esTransport = new winston_elasticsearch_1.ElasticsearchTransport(options.elasticsearch);
|
|
33
|
+
const logger = winston_1.default.createLogger({
|
|
34
|
+
exitOnError: false,
|
|
35
|
+
defaultMeta: { service: name },
|
|
36
|
+
transports: [new winston_1.default.transports.Console(options.console), esTransport]
|
|
37
|
+
});
|
|
38
|
+
return logger;
|
|
39
|
+
};
|
|
40
|
+
exports.winstonLogger = winstonLogger;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modernlock/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"main": "./build/index.js",
|
|
5
5
|
"types": "./build/index.d.ts",
|
|
6
6
|
"files": [
|
|
7
|
-
"build/**/*"
|
|
7
|
+
"./build/**/*"
|
|
8
8
|
],
|
|
9
9
|
"scripts": {
|
|
10
10
|
"clean": "del ./build/*",
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
"@types/express": "4.17.17",
|
|
26
26
|
"amqplib": "0.10.3",
|
|
27
27
|
"cookie-session": "2.0.0",
|
|
28
|
-
"del": "^7.1.0",
|
|
29
28
|
"express": "4.18.2",
|
|
30
29
|
"jsonwebtoken": "9.0.0",
|
|
31
30
|
"winston": "^3.12.0",
|