@konplit-services/common 1.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/build/Schema/CFLESCHEMA.d.ts +138 -0
- package/build/Schema/CFLESCHEMA.js +144 -0
- package/build/errors/axios-request-error.d.ts +17 -0
- package/build/errors/axios-request-error.js +32 -0
- package/build/errors/bad-request-error.d.ts +17 -0
- package/build/errors/bad-request-error.js +28 -0
- package/build/errors/custom-error.d.ts +15 -0
- package/build/errors/custom-error.js +11 -0
- package/build/errors/database-error.d.ts +17 -0
- package/build/errors/database-error.js +28 -0
- package/build/errors/error.interface.d.ts +4 -0
- package/build/errors/error.interface.js +2 -0
- package/build/errors/forbidden-error.d.ts +17 -0
- package/build/errors/forbidden-error.js +28 -0
- package/build/errors/invalid-input-error.d.ts +19 -0
- package/build/errors/invalid-input-error.js +31 -0
- package/build/errors/mongoose-validation-error.d.ts +17 -0
- package/build/errors/mongoose-validation-error.js +28 -0
- package/build/errors/not-found.d.ts +17 -0
- package/build/errors/not-found.js +28 -0
- package/build/errors/notAuthorized.d.ts +17 -0
- package/build/errors/notAuthorized.js +28 -0
- package/build/errors/request-validation-error.d.ts +17 -0
- package/build/errors/request-validation-error.js +28 -0
- package/build/errors/service-not-available.d.ts +17 -0
- package/build/errors/service-not-available.js +28 -0
- package/build/events/accounts-events/interfaces/account-created.interface.d.ts +11 -0
- package/build/events/accounts-events/interfaces/account-created.interface.js +2 -0
- package/build/events/accounts-events/interfaces/account-updated.interface.d.ts +0 -0
- package/build/events/accounts-events/interfaces/account-updated.interface.js +1 -0
- package/build/events/accounts-events/interfaces/permission-created.interface.d.ts +12 -0
- package/build/events/accounts-events/interfaces/permission-created.interface.js +2 -0
- package/build/events/accounts-events/interfaces/permission-removed.interface.d.ts +12 -0
- package/build/events/accounts-events/interfaces/permission-removed.interface.js +2 -0
- package/build/events/base-events/base-events.listener.d.ts +103 -0
- package/build/events/base-events/base-events.listener.js +164 -0
- package/build/events/base-events/base-events.publisher.d.ts +75 -0
- package/build/events/base-events/base-events.publisher.js +110 -0
- package/build/events/logs-events/interfaces/admins-logs.created.interface.d.ts +15 -0
- package/build/events/logs-events/interfaces/admins-logs.created.interface.js +2 -0
- package/build/events/logs-events/interfaces/merchants-logs-created.interface.d.ts +15 -0
- package/build/events/logs-events/interfaces/merchants-logs-created.interface.js +2 -0
- package/build/events/subjects.d.ts +26 -0
- package/build/events/subjects.js +37 -0
- package/build/events/types/order-statu.types.d.ts +22 -0
- package/build/events/types/order-statu.types.js +26 -0
- package/build/helper/Two-fa-type.d.ts +7 -0
- package/build/helper/Two-fa-type.js +11 -0
- package/build/helper/account-balance-types.d.ts +4 -0
- package/build/helper/account-balance-types.js +8 -0
- package/build/helper/account-status.d.ts +4 -0
- package/build/helper/account-status.js +8 -0
- package/build/helper/bank-code.d.ts +1 -0
- package/build/helper/bank-code.js +16 -0
- package/build/helper/business-category.d.ts +12 -0
- package/build/helper/business-category.js +17 -0
- package/build/helper/bvn-validation-type.d.ts +4 -0
- package/build/helper/bvn-validation-type.js +8 -0
- package/build/helper/card-types.d.ts +10 -0
- package/build/helper/card-types.js +14 -0
- package/build/helper/card_auth_type.d.ts +5 -0
- package/build/helper/card_auth_type.js +9 -0
- package/build/helper/chargeback.d.ts +7 -0
- package/build/helper/chargeback.js +11 -0
- package/build/helper/charges.d.ts +7 -0
- package/build/helper/charges.js +11 -0
- package/build/helper/currency-types.d.ts +4 -0
- package/build/helper/currency-types.js +8 -0
- package/build/helper/custom-paginate.d.ts +19 -0
- package/build/helper/custom-paginate.js +50 -0
- package/build/helper/email-types.d.ts +22 -0
- package/build/helper/email-types.js +26 -0
- package/build/helper/errorCodes/error-codes.d.ts +92 -0
- package/build/helper/errorCodes/error-codes.js +99 -0
- package/build/helper/extract-card-details.d.ts +4 -0
- package/build/helper/extract-card-details.js +20 -0
- package/build/helper/invoice.d.ts +19 -0
- package/build/helper/invoice.js +26 -0
- package/build/helper/lang/email.d.ts +12 -0
- package/build/helper/lang/email.js +19 -0
- package/build/helper/lang/error-text.d.ts +32 -0
- package/build/helper/lang/error-text.js +35 -0
- package/build/helper/lang/error.d.ts +0 -0
- package/build/helper/lang/error.js +1 -0
- package/build/helper/lang/language.d.ts +113 -0
- package/build/helper/lang/language.js +146 -0
- package/build/helper/last-three.d.ts +1 -0
- package/build/helper/last-three.js +8 -0
- package/build/helper/location.d.ts +2 -0
- package/build/helper/location.js +20 -0
- package/build/helper/media-types.d.ts +6 -0
- package/build/helper/media-types.js +10 -0
- package/build/helper/nibss-scopes.d.ts +9 -0
- package/build/helper/nibss-scopes.js +13 -0
- package/build/helper/payment-options.d.ts +7 -0
- package/build/helper/payment-options.js +11 -0
- package/build/helper/pdf-generator.d.ts +9 -0
- package/build/helper/pdf-generator.js +73 -0
- package/build/helper/permistions.d.ts +126 -0
- package/build/helper/permistions.js +129 -0
- package/build/helper/plan-types.d.ts +9 -0
- package/build/helper/plan-types.js +13 -0
- package/build/helper/query/query-option.d.ts +23 -0
- package/build/helper/query/query-option.js +40 -0
- package/build/helper/refund.d.ts +21 -0
- package/build/helper/refund.js +28 -0
- package/build/helper/roles.d.ts +15 -0
- package/build/helper/roles.js +19 -0
- package/build/helper/settlement.d.ts +14 -0
- package/build/helper/settlement.js +20 -0
- package/build/helper/status.d.ts +15 -0
- package/build/helper/status.js +19 -0
- package/build/helper/subscriptions-types.d.ts +16 -0
- package/build/helper/subscriptions-types.js +22 -0
- package/build/helper/success-response.d.ts +9 -0
- package/build/helper/success-response.js +13 -0
- package/build/helper/transaction-authentication-type.d.ts +5 -0
- package/build/helper/transaction-authentication-type.js +9 -0
- package/build/helper/transaction-type.d.ts +15 -0
- package/build/helper/transaction-type.js +20 -0
- package/build/helper/transfer.d.ts +9 -0
- package/build/helper/transfer.js +13 -0
- package/build/helper/user-types.d.ts +4 -0
- package/build/helper/user-types.js +8 -0
- package/build/helper/util/gender.d.ts +4 -0
- package/build/helper/util/gender.js +8 -0
- package/build/helper/util/get-transaction-id.d.ts +9 -0
- package/build/helper/util/get-transaction-id.js +17 -0
- package/build/helper/util/getobject-keys.d.ts +10 -0
- package/build/helper/util/getobject-keys.js +22 -0
- package/build/helper/util/pagination.d.ts +25 -0
- package/build/helper/util/pagination.js +19 -0
- package/build/helper/util/remove-transactionId.d.ts +14 -0
- package/build/helper/util/remove-transactionId.js +16 -0
- package/build/helper/util/sort-by.d.ts +8 -0
- package/build/helper/util/sort-by.js +11 -0
- package/build/helper/validateParams.d.ts +7 -0
- package/build/helper/validateParams.js +13 -0
- package/build/helper/worker-task-types.d.ts +8 -0
- package/build/helper/worker-task-types.js +12 -0
- package/build/index.d.ts +79 -0
- package/build/index.js +103 -0
- package/build/log-activities/actions.d.ts +17 -0
- package/build/log-activities/actions.js +25 -0
- package/build/middlewares/check-inputs.d.ts +2 -0
- package/build/middlewares/check-inputs.js +24 -0
- package/build/middlewares/current-user.d.ts +10 -0
- package/build/middlewares/current-user.js +41 -0
- package/build/middlewares/error-handler.d.ts +3 -0
- package/build/middlewares/error-handler.js +61 -0
- package/build/middlewares/permission.d.ts +10 -0
- package/build/middlewares/permission.js +46 -0
- package/build/middlewares/require-auth.d.ts +2 -0
- package/build/middlewares/require-auth.js +13 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +13 -0
- package/build/redis/base/index.d.ts +13 -0
- package/build/redis/base/index.js +104 -0
- package/build/redis/constants/index.d.ts +5 -0
- package/build/redis/constants/index.js +12 -0
- package/build/repository/interface/baseRepository.d.ts +9 -0
- package/build/repository/interface/baseRepository.js +8 -0
- package/build/repository/interface/based.d.ts +9 -0
- package/build/repository/interface/based.js +2 -0
- package/build/services/Jwt.d.ts +17 -0
- package/build/services/Jwt.js +44 -0
- package/build/services/base.service.d.ts +14 -0
- package/build/services/base.service.js +20 -0
- package/build/services/mailersend.d.ts +25 -0
- package/build/services/mailersend.js +56 -0
- package/build/util/jwt.validation.d.ts +1 -0
- package/build/util/jwt.validation.js +27 -0
- package/build/util/lib/nats/nat-error-codes.d.ts +4 -0
- package/build/util/lib/nats/nat-error-codes.js +7 -0
- package/build/util/parse-query-string.d.ts +4 -0
- package/build/util/parse-query-string.js +22 -0
- package/package.json +46 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotAuthorizedError = void 0;
|
|
4
|
+
const status_1 = require("../helper/status");
|
|
5
|
+
const custom_error_1 = require("./custom-error");
|
|
6
|
+
class NotAuthorizedError extends custom_error_1.CustomError {
|
|
7
|
+
constructor(message, error_data) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.message = message;
|
|
10
|
+
this.error_data = error_data;
|
|
11
|
+
this.statusCode = 401;
|
|
12
|
+
this.error_data = error_data;
|
|
13
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
|
14
|
+
}
|
|
15
|
+
sequalizeErrors() {
|
|
16
|
+
return {
|
|
17
|
+
status: status_1.Status.Failed,
|
|
18
|
+
errors: [
|
|
19
|
+
{
|
|
20
|
+
message: this.message,
|
|
21
|
+
error_code: this.error_data.error_code,
|
|
22
|
+
code: this.error_data.code,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.NotAuthorizedError = NotAuthorizedError;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ValidationError } from "express-validator";
|
|
2
|
+
import { Status } from "../helper/status";
|
|
3
|
+
import { CustomError } from "./custom-error";
|
|
4
|
+
export declare class RequestValidationError extends CustomError {
|
|
5
|
+
private errors;
|
|
6
|
+
readonly statusCode = 400;
|
|
7
|
+
constructor(errors: ValidationError[]);
|
|
8
|
+
sequalizeErrors(): {
|
|
9
|
+
status: Status;
|
|
10
|
+
errors: {
|
|
11
|
+
message: any;
|
|
12
|
+
error_code: any;
|
|
13
|
+
field: string;
|
|
14
|
+
code: any;
|
|
15
|
+
}[];
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestValidationError = void 0;
|
|
4
|
+
const status_1 = require("../helper/status");
|
|
5
|
+
const custom_error_1 = require("./custom-error");
|
|
6
|
+
class RequestValidationError extends custom_error_1.CustomError {
|
|
7
|
+
constructor(errors) {
|
|
8
|
+
super("validation error");
|
|
9
|
+
this.errors = errors;
|
|
10
|
+
this.statusCode = 400;
|
|
11
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
12
|
+
}
|
|
13
|
+
sequalizeErrors() {
|
|
14
|
+
const errors = this.errors.map((error) => {
|
|
15
|
+
return {
|
|
16
|
+
message: error.msg.message,
|
|
17
|
+
error_code: error.msg.error_code,
|
|
18
|
+
field: error.param,
|
|
19
|
+
code: error.msg.error_code,
|
|
20
|
+
};
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
status: status_1.Status.Failed,
|
|
24
|
+
errors,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.RequestValidationError = RequestValidationError;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Status } from "../helper/status";
|
|
2
|
+
import { CustomError } from "./custom-error";
|
|
3
|
+
import { ErrorReturn } from "./error.interface";
|
|
4
|
+
export declare class ServiceNotAvailableError extends CustomError {
|
|
5
|
+
message: string;
|
|
6
|
+
private error_data;
|
|
7
|
+
readonly statusCode = 503;
|
|
8
|
+
constructor(message: string, error_data: ErrorReturn);
|
|
9
|
+
sequalizeErrors(): {
|
|
10
|
+
status: Status;
|
|
11
|
+
errors: {
|
|
12
|
+
message: string;
|
|
13
|
+
code: number;
|
|
14
|
+
error_code: string;
|
|
15
|
+
}[];
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServiceNotAvailableError = void 0;
|
|
4
|
+
const status_1 = require("../helper/status");
|
|
5
|
+
const custom_error_1 = require("./custom-error");
|
|
6
|
+
class ServiceNotAvailableError extends custom_error_1.CustomError {
|
|
7
|
+
constructor(message, error_data) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.message = message;
|
|
10
|
+
this.error_data = error_data;
|
|
11
|
+
this.statusCode = 503;
|
|
12
|
+
this.error_data = error_data;
|
|
13
|
+
Object.setPrototypeOf(this, ServiceNotAvailableError.prototype);
|
|
14
|
+
}
|
|
15
|
+
sequalizeErrors() {
|
|
16
|
+
return {
|
|
17
|
+
status: status_1.Status.Failed,
|
|
18
|
+
errors: [
|
|
19
|
+
{
|
|
20
|
+
message: this.message,
|
|
21
|
+
code: this.error_data.code,
|
|
22
|
+
error_code: this.error_data.error_code,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.ServiceNotAvailableError = ServiceNotAvailableError;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { StreamEvent, StreamName, Subjects } from "../../subjects";
|
|
2
|
+
export interface AccountCreatedEvent {
|
|
3
|
+
subject: Subjects.AccountCreated;
|
|
4
|
+
streamName: StreamName.name;
|
|
5
|
+
streamEvents: StreamEvent.Event;
|
|
6
|
+
data: {
|
|
7
|
+
title: string;
|
|
8
|
+
id: string;
|
|
9
|
+
price: number;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Roles } from "../../../helper/roles";
|
|
2
|
+
import { StreamEvent, StreamName, Subjects } from "../../subjects";
|
|
3
|
+
export interface PermissionCreatedEvent {
|
|
4
|
+
subject: Subjects.PermissionCreated;
|
|
5
|
+
streamName: StreamName.name;
|
|
6
|
+
streamEvents: StreamEvent.Event;
|
|
7
|
+
data: {
|
|
8
|
+
role: Roles;
|
|
9
|
+
id: string;
|
|
10
|
+
permission: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Roles } from "../../../helper/roles";
|
|
2
|
+
import { StreamEvent, StreamName, Subjects } from "../../subjects";
|
|
3
|
+
export interface PermissionRemovedEvent {
|
|
4
|
+
subject: Subjects.PermissionRemoved;
|
|
5
|
+
streamName: StreamName.name;
|
|
6
|
+
streamEvents: StreamEvent.Event;
|
|
7
|
+
data: {
|
|
8
|
+
role: Roles;
|
|
9
|
+
id: string;
|
|
10
|
+
permission: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listener Module
|
|
3
|
+
*
|
|
4
|
+
* This module defines an abstract `Listener` class for listening to events from a NATS JetStream.
|
|
5
|
+
* It ensures that the required stream and consumer exist before consuming events.
|
|
6
|
+
*
|
|
7
|
+
* @module Listener
|
|
8
|
+
*/
|
|
9
|
+
import { JetStreamClient, JsMsg } from "nats";
|
|
10
|
+
import { StreamEvent, StreamName } from "../subjects";
|
|
11
|
+
/**
|
|
12
|
+
* Event Interface
|
|
13
|
+
*
|
|
14
|
+
* Represents an event that will be consumed from a NATS stream.
|
|
15
|
+
*
|
|
16
|
+
* @interface
|
|
17
|
+
*/
|
|
18
|
+
interface Event {
|
|
19
|
+
subject: string;
|
|
20
|
+
streamEvents: StreamEvent;
|
|
21
|
+
streamName: StreamName;
|
|
22
|
+
data: any;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Listener Abstract Class
|
|
26
|
+
*
|
|
27
|
+
* Abstract class for consuming events from a NATS JetStream.
|
|
28
|
+
* Subclasses should implement the specific subject, streamEvents, stream, and durableName properties.
|
|
29
|
+
*
|
|
30
|
+
* @abstract
|
|
31
|
+
* @template T - The type of event to be consumed
|
|
32
|
+
*/
|
|
33
|
+
export declare abstract class Listener<T extends Event> {
|
|
34
|
+
private client;
|
|
35
|
+
private jc;
|
|
36
|
+
/**
|
|
37
|
+
* The subject associated with the event.
|
|
38
|
+
* @abstract
|
|
39
|
+
*/
|
|
40
|
+
abstract subject: T["subject"];
|
|
41
|
+
/**
|
|
42
|
+
* The stream events associated with the event.
|
|
43
|
+
* @abstract
|
|
44
|
+
*/
|
|
45
|
+
abstract streamEvents: T["streamEvents"];
|
|
46
|
+
/**
|
|
47
|
+
* The stream name associated with the event.
|
|
48
|
+
* @abstract
|
|
49
|
+
*/
|
|
50
|
+
abstract stream: T["streamName"];
|
|
51
|
+
/**
|
|
52
|
+
* The durable name for the consumer.
|
|
53
|
+
* @abstract
|
|
54
|
+
*/
|
|
55
|
+
abstract durableName: string;
|
|
56
|
+
/**
|
|
57
|
+
* Acknowledgment wait time of 5 seconds before retrying.
|
|
58
|
+
* @protected
|
|
59
|
+
*/
|
|
60
|
+
protected ackWait: number;
|
|
61
|
+
/**
|
|
62
|
+
* Handles the incoming message.
|
|
63
|
+
* @abstract
|
|
64
|
+
* @param {T["data"]} data - The decoded data from the message
|
|
65
|
+
* @param {JsMsg} msg - The raw NATS message
|
|
66
|
+
*/
|
|
67
|
+
abstract onMessage(data: T["data"], msg: JsMsg): void;
|
|
68
|
+
/**
|
|
69
|
+
* Constructor for the Listener class.
|
|
70
|
+
*
|
|
71
|
+
* @param {JetStreamClient} client - The NATS JetStream client
|
|
72
|
+
*/
|
|
73
|
+
constructor(client: JetStreamClient);
|
|
74
|
+
/**
|
|
75
|
+
* Sets up the listener by ensuring the stream and consumer exist,
|
|
76
|
+
* then starts consuming messages.
|
|
77
|
+
*
|
|
78
|
+
* @returns {Promise<void>} - Resolves when the listener is set up and consuming messages
|
|
79
|
+
*/
|
|
80
|
+
listen(): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Ensures the stream exists in the NATS JetStream.
|
|
83
|
+
* Creates the stream if it does not exist.
|
|
84
|
+
*
|
|
85
|
+
* @private
|
|
86
|
+
* @param {StreamName} streamName - The name of the stream
|
|
87
|
+
* @param {string[]} subjects - The subjects associated with the stream
|
|
88
|
+
* @returns {Promise<void>} - Resolves when the stream is ensured to exist
|
|
89
|
+
*/
|
|
90
|
+
private ensureStream;
|
|
91
|
+
/**
|
|
92
|
+
* Ensures the consumer exists in the NATS JetStream.
|
|
93
|
+
* Creates the consumer if it does not exist.
|
|
94
|
+
*
|
|
95
|
+
* @private
|
|
96
|
+
* @param {StreamName} streamName - The name of the stream
|
|
97
|
+
* @param {string} durableName - The durable name of the consumer
|
|
98
|
+
* @param {string} subject - The subject to filter for the consumer
|
|
99
|
+
* @returns {Promise<void>} - Resolves when the consumer is ensured to exist
|
|
100
|
+
*/
|
|
101
|
+
private ensureConsumer;
|
|
102
|
+
}
|
|
103
|
+
export {};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Listener Module
|
|
4
|
+
*
|
|
5
|
+
* This module defines an abstract `Listener` class for listening to events from a NATS JetStream.
|
|
6
|
+
* It ensures that the required stream and consumer exist before consuming events.
|
|
7
|
+
*
|
|
8
|
+
* @module Listener
|
|
9
|
+
*/
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.Listener = void 0;
|
|
21
|
+
const nats_1 = require("nats");
|
|
22
|
+
const nat_error_codes_1 = require("../../util/lib/nats/nat-error-codes");
|
|
23
|
+
/**
|
|
24
|
+
* Listener Abstract Class
|
|
25
|
+
*
|
|
26
|
+
* Abstract class for consuming events from a NATS JetStream.
|
|
27
|
+
* Subclasses should implement the specific subject, streamEvents, stream, and durableName properties.
|
|
28
|
+
*
|
|
29
|
+
* @abstract
|
|
30
|
+
* @template T - The type of event to be consumed
|
|
31
|
+
*/
|
|
32
|
+
class Listener {
|
|
33
|
+
/**
|
|
34
|
+
* Constructor for the Listener class.
|
|
35
|
+
*
|
|
36
|
+
* @param {JetStreamClient} client - The NATS JetStream client
|
|
37
|
+
*/
|
|
38
|
+
constructor(client) {
|
|
39
|
+
this.jc = (0, nats_1.JSONCodec)();
|
|
40
|
+
/**
|
|
41
|
+
* Acknowledgment wait time of 5 seconds before retrying.
|
|
42
|
+
* @protected
|
|
43
|
+
*/
|
|
44
|
+
this.ackWait = (0, nats_1.nanos)(5 * 1000); // acknowledge time of 5 seconds before retry
|
|
45
|
+
this.client = client;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Sets up the listener by ensuring the stream and consumer exist,
|
|
49
|
+
* then starts consuming messages.
|
|
50
|
+
*
|
|
51
|
+
* @returns {Promise<void>} - Resolves when the listener is set up and consuming messages
|
|
52
|
+
*/
|
|
53
|
+
listen() {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
try {
|
|
56
|
+
// Ensure the stream exists
|
|
57
|
+
yield this.ensureStream(this.stream, [this.streamEvents]);
|
|
58
|
+
// Ensure the consumer exists
|
|
59
|
+
yield this.ensureConsumer(this.stream, this.durableName, this.subject);
|
|
60
|
+
const consumer = yield this.client.consumers.get(this.stream, this.durableName);
|
|
61
|
+
yield consumer.consume({
|
|
62
|
+
max_messages: 1,
|
|
63
|
+
callback: (message) => {
|
|
64
|
+
console.log("sequence number", message.seq);
|
|
65
|
+
const data = this.jc.decode(message.data);
|
|
66
|
+
this.onMessage(data, message);
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
console.error("Error setting up listener:", err);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Ensures the stream exists in the NATS JetStream.
|
|
77
|
+
* Creates the stream if it does not exist.
|
|
78
|
+
*
|
|
79
|
+
* @private
|
|
80
|
+
* @param {StreamName} streamName - The name of the stream
|
|
81
|
+
* @param {string[]} subjects - The subjects associated with the stream
|
|
82
|
+
* @returns {Promise<void>} - Resolves when the stream is ensured to exist
|
|
83
|
+
*/
|
|
84
|
+
ensureStream(streamName, subjects) {
|
|
85
|
+
var _a, _b;
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
try {
|
|
88
|
+
const jsm = yield this.client.jetstreamManager();
|
|
89
|
+
yield jsm.streams.info(streamName);
|
|
90
|
+
console.log(`Stream '${streamName}' already exists`);
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
console.log("Stream info retrieval error:", err.api_error);
|
|
94
|
+
if (((_a = err === null || err === void 0 ? void 0 : err.api_error) === null || _a === void 0 ? void 0 : _a.code) === 404 &&
|
|
95
|
+
((_b = err === null || err === void 0 ? void 0 : err.api_error) === null || _b === void 0 ? void 0 : _b.err_code) === nat_error_codes_1.NATS_ERROR.STREAM_NOT_FOUND) {
|
|
96
|
+
try {
|
|
97
|
+
const jsm = yield this.client.jetstreamManager();
|
|
98
|
+
const streamConfig = {
|
|
99
|
+
name: streamName,
|
|
100
|
+
subjects,
|
|
101
|
+
storage: nats_1.StorageType.File,
|
|
102
|
+
max_consumers: -1,
|
|
103
|
+
retention: nats_1.RetentionPolicy.Workqueue,
|
|
104
|
+
};
|
|
105
|
+
yield jsm.streams.add(streamConfig);
|
|
106
|
+
console.log(`Stream '${streamName}' created`);
|
|
107
|
+
}
|
|
108
|
+
catch (addErr) {
|
|
109
|
+
console.error("Error creating stream:", addErr);
|
|
110
|
+
throw addErr;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
throw err;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Ensures the consumer exists in the NATS JetStream.
|
|
121
|
+
* Creates the consumer if it does not exist.
|
|
122
|
+
*
|
|
123
|
+
* @private
|
|
124
|
+
* @param {StreamName} streamName - The name of the stream
|
|
125
|
+
* @param {string} durableName - The durable name of the consumer
|
|
126
|
+
* @param {string} subject - The subject to filter for the consumer
|
|
127
|
+
* @returns {Promise<void>} - Resolves when the consumer is ensured to exist
|
|
128
|
+
*/
|
|
129
|
+
ensureConsumer(streamName, durableName, subject) {
|
|
130
|
+
var _a, _b;
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
const jsm = yield this.client.jetstreamManager();
|
|
133
|
+
try {
|
|
134
|
+
yield jsm.consumers.info(streamName, durableName);
|
|
135
|
+
console.log(`Consumer '${durableName}' already exists`);
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
if (((_a = err === null || err === void 0 ? void 0 : err.api_error) === null || _a === void 0 ? void 0 : _a.code) === 404 &&
|
|
139
|
+
((_b = err === null || err === void 0 ? void 0 : err.api_error) === null || _b === void 0 ? void 0 : _b.err_code) === nat_error_codes_1.NATS_ERROR.CONSUMER_NOT_FOUND) {
|
|
140
|
+
try {
|
|
141
|
+
const consumerConfig = {
|
|
142
|
+
durable_name: durableName,
|
|
143
|
+
ack_policy: nats_1.AckPolicy.Explicit,
|
|
144
|
+
filter_subject: subject,
|
|
145
|
+
deliver_policy: nats_1.DeliverPolicy.All,
|
|
146
|
+
ack_wait: this.ackWait,
|
|
147
|
+
max_deliver: 20,
|
|
148
|
+
};
|
|
149
|
+
yield jsm.consumers.add(streamName, consumerConfig);
|
|
150
|
+
console.log(`Consumer '${durableName}' created`);
|
|
151
|
+
}
|
|
152
|
+
catch (addConsumerError) {
|
|
153
|
+
console.error("Error creating stream:", addConsumerError);
|
|
154
|
+
throw addConsumerError;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
throw err;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
exports.Listener = Listener;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publisher Module
|
|
3
|
+
*
|
|
4
|
+
* This module defines an abstract `Publisher` class for publishing events to a NATS JetStream.
|
|
5
|
+
* It ensures that the required stream exists before publishing events.
|
|
6
|
+
*
|
|
7
|
+
* @module Publisher
|
|
8
|
+
*/
|
|
9
|
+
import { JetStreamClient } from "nats";
|
|
10
|
+
import { StreamEvent, StreamName } from "../subjects";
|
|
11
|
+
/**
|
|
12
|
+
* Event Interface
|
|
13
|
+
*
|
|
14
|
+
* Represents an event that will be published to a NATS stream.
|
|
15
|
+
*
|
|
16
|
+
* @interface
|
|
17
|
+
*/
|
|
18
|
+
interface Event {
|
|
19
|
+
subject: string;
|
|
20
|
+
streamEvents: StreamEvent;
|
|
21
|
+
streamName: StreamName;
|
|
22
|
+
data: any;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Publisher Abstract Class
|
|
26
|
+
*
|
|
27
|
+
* Abstract class for publishing events to a NATS JetStream.
|
|
28
|
+
* Subclasses should implement the specific subject, streamEvents, and stream properties.
|
|
29
|
+
*
|
|
30
|
+
* @abstract
|
|
31
|
+
* @template T - The type of event to be published
|
|
32
|
+
*/
|
|
33
|
+
export declare abstract class Publisher<T extends Event> {
|
|
34
|
+
private client;
|
|
35
|
+
private jc;
|
|
36
|
+
/**
|
|
37
|
+
* The subject associated with the event.
|
|
38
|
+
* @abstract
|
|
39
|
+
*/
|
|
40
|
+
abstract subject: T["subject"];
|
|
41
|
+
/**
|
|
42
|
+
* The stream events associated with the event.
|
|
43
|
+
* @abstract
|
|
44
|
+
*/
|
|
45
|
+
abstract streamEvents: T["streamEvents"];
|
|
46
|
+
/**
|
|
47
|
+
* The stream name associated with the event.
|
|
48
|
+
* @abstract
|
|
49
|
+
*/
|
|
50
|
+
abstract stream: T["streamName"];
|
|
51
|
+
/**
|
|
52
|
+
* Constructor for the Publisher class.
|
|
53
|
+
*
|
|
54
|
+
* @param {JetStreamClient} client - The NATS JetStream client
|
|
55
|
+
*/
|
|
56
|
+
constructor(client: JetStreamClient);
|
|
57
|
+
/**
|
|
58
|
+
* Publishes data to the NATS JetStream.
|
|
59
|
+
*
|
|
60
|
+
* @param {T["data"]} data - The data to be published
|
|
61
|
+
* @returns {Promise<void>} - Resolves when the data is published successfully
|
|
62
|
+
*/
|
|
63
|
+
publish(data: T["data"]): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Ensures the stream exists in the NATS JetStream.
|
|
66
|
+
* Creates the stream if it does not exist.
|
|
67
|
+
*
|
|
68
|
+
* @private
|
|
69
|
+
* @param {StreamName} streamName - The name of the stream
|
|
70
|
+
* @param {string[]} subjects - The subjects associated with the stream
|
|
71
|
+
* @returns {Promise<void>} - Resolves when the stream is ensured to exist
|
|
72
|
+
*/
|
|
73
|
+
private ensureStream;
|
|
74
|
+
}
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Publisher Module
|
|
4
|
+
*
|
|
5
|
+
* This module defines an abstract `Publisher` class for publishing events to a NATS JetStream.
|
|
6
|
+
* It ensures that the required stream exists before publishing events.
|
|
7
|
+
*
|
|
8
|
+
* @module Publisher
|
|
9
|
+
*/
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.Publisher = void 0;
|
|
21
|
+
const nats_1 = require("nats");
|
|
22
|
+
const nat_error_codes_1 = require("../../util/lib/nats/nat-error-codes");
|
|
23
|
+
/**
|
|
24
|
+
* Publisher Abstract Class
|
|
25
|
+
*
|
|
26
|
+
* Abstract class for publishing events to a NATS JetStream.
|
|
27
|
+
* Subclasses should implement the specific subject, streamEvents, and stream properties.
|
|
28
|
+
*
|
|
29
|
+
* @abstract
|
|
30
|
+
* @template T - The type of event to be published
|
|
31
|
+
*/
|
|
32
|
+
class Publisher {
|
|
33
|
+
/**
|
|
34
|
+
* Constructor for the Publisher class.
|
|
35
|
+
*
|
|
36
|
+
* @param {JetStreamClient} client - The NATS JetStream client
|
|
37
|
+
*/
|
|
38
|
+
constructor(client) {
|
|
39
|
+
this.jc = (0, nats_1.JSONCodec)();
|
|
40
|
+
this.client = client;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Publishes data to the NATS JetStream.
|
|
44
|
+
*
|
|
45
|
+
* @param {T["data"]} data - The data to be published
|
|
46
|
+
* @returns {Promise<void>} - Resolves when the data is published successfully
|
|
47
|
+
*/
|
|
48
|
+
publish(data) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
try {
|
|
52
|
+
// Ensure the stream exists
|
|
53
|
+
yield this.ensureStream(this.stream, [this.streamEvents]);
|
|
54
|
+
yield this.client.publish(this.subject, this.jc.encode(data));
|
|
55
|
+
console.log("Event Published");
|
|
56
|
+
resolve();
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
console.error("Error publishing event:", err);
|
|
60
|
+
reject(err);
|
|
61
|
+
}
|
|
62
|
+
}));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Ensures the stream exists in the NATS JetStream.
|
|
67
|
+
* Creates the stream if it does not exist.
|
|
68
|
+
*
|
|
69
|
+
* @private
|
|
70
|
+
* @param {StreamName} streamName - The name of the stream
|
|
71
|
+
* @param {string[]} subjects - The subjects associated with the stream
|
|
72
|
+
* @returns {Promise<void>} - Resolves when the stream is ensured to exist
|
|
73
|
+
*/
|
|
74
|
+
ensureStream(streamName, subjects) {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
try {
|
|
78
|
+
const jsm = yield this.client.jetstreamManager();
|
|
79
|
+
yield jsm.streams.info(streamName);
|
|
80
|
+
console.log(`Stream '${streamName}' already exists`);
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
console.log("Stream info retrieval error:", err.api_error);
|
|
84
|
+
if (((_a = err === null || err === void 0 ? void 0 : err.api_error) === null || _a === void 0 ? void 0 : _a.code) === 404 &&
|
|
85
|
+
((_b = err === null || err === void 0 ? void 0 : err.api_error) === null || _b === void 0 ? void 0 : _b.err_code) === nat_error_codes_1.NATS_ERROR.STREAM_NOT_FOUND) {
|
|
86
|
+
try {
|
|
87
|
+
const jsm = yield this.client.jetstreamManager();
|
|
88
|
+
const streamConfig = {
|
|
89
|
+
name: streamName,
|
|
90
|
+
subjects,
|
|
91
|
+
storage: nats_1.StorageType.File,
|
|
92
|
+
retention: nats_1.RetentionPolicy.Workqueue,
|
|
93
|
+
max_consumers: -1,
|
|
94
|
+
};
|
|
95
|
+
yield jsm.streams.add(streamConfig);
|
|
96
|
+
console.log(`Stream '${streamName}' created`);
|
|
97
|
+
}
|
|
98
|
+
catch (addErr) {
|
|
99
|
+
console.error("Error creating stream:", addErr);
|
|
100
|
+
throw addErr;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.Publisher = Publisher;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { LOGACTIONS } from "../../../log-activities/actions";
|
|
2
|
+
import { StreamEvent, StreamName, Subjects } from "../../subjects";
|
|
3
|
+
export interface AdminLogsCreatedEvent {
|
|
4
|
+
subject: Subjects.AdminLogCreated;
|
|
5
|
+
streamName: StreamName.logName;
|
|
6
|
+
streamEvents: StreamEvent.LOG;
|
|
7
|
+
data: {
|
|
8
|
+
id?: string;
|
|
9
|
+
role?: string;
|
|
10
|
+
email?: string;
|
|
11
|
+
action: LOGACTIONS;
|
|
12
|
+
data: any;
|
|
13
|
+
accountId?: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { LOGACTIONS } from "../../../log-activities/actions";
|
|
2
|
+
import { StreamEvent, StreamName, Subjects } from "../../subjects";
|
|
3
|
+
export interface MerchantLogsCreatedEvent {
|
|
4
|
+
subject: Subjects.MerchantLogCreated;
|
|
5
|
+
streamName: StreamName.logName;
|
|
6
|
+
streamEvents: StreamEvent.LOG;
|
|
7
|
+
data: {
|
|
8
|
+
id?: string;
|
|
9
|
+
email?: string;
|
|
10
|
+
role?: string;
|
|
11
|
+
action: LOGACTIONS;
|
|
12
|
+
data: any;
|
|
13
|
+
accountId?: string;
|
|
14
|
+
};
|
|
15
|
+
}
|