@misalon/common 1.0.4 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ import { Message, Stan } from 'node-nats-streaming';
2
+ import { Subjects } from './subjects';
3
+ interface Event {
4
+ subject: Subjects;
5
+ data: any;
6
+ }
7
+ export declare abstract class Listener<T extends Event> {
8
+ abstract subject: T['subject'];
9
+ abstract queueGroupName: string;
10
+ abstract onMessage(data: T['data'], msg: Message): void;
11
+ protected client: Stan;
12
+ protected ackWait: number;
13
+ constructor(client: Stan);
14
+ subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
15
+ listen(): void;
16
+ parseMessage(msg: Message): any;
17
+ }
18
+ export {};
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Listener = void 0;
4
+ var Listener = /** @class */ (function () {
5
+ function Listener(client) {
6
+ this.ackWait = 5 * 1000;
7
+ this.client = client;
8
+ }
9
+ Listener.prototype.subscriptionOptions = function () {
10
+ return this.client
11
+ .subscriptionOptions()
12
+ .setDeliverAllAvailable()
13
+ .setManualAckMode(true)
14
+ .setAckWait(this.ackWait)
15
+ .setDurableName(this.queueGroupName);
16
+ };
17
+ Listener.prototype.listen = function () {
18
+ var _this = this;
19
+ var subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
20
+ subscription.on('message', function (msg) {
21
+ console.log("Message received: ".concat(_this.subject, " / ").concat(_this.queueGroupName));
22
+ var parsedData = _this.parseMessage(msg);
23
+ _this.onMessage(parsedData, msg);
24
+ });
25
+ };
26
+ Listener.prototype.parseMessage = function (msg) {
27
+ var data = msg.getData();
28
+ return typeof data === 'string'
29
+ ? JSON.parse(data)
30
+ : JSON.parse(data.toString('utf8'));
31
+ };
32
+ return Listener;
33
+ }());
34
+ exports.Listener = Listener;
@@ -0,0 +1,13 @@
1
+ import { Stan } from 'node-nats-streaming';
2
+ import { Subjects } from './subjects';
3
+ interface Event {
4
+ subject: Subjects;
5
+ data: any;
6
+ }
7
+ export declare abstract class Publisher<T extends Event> {
8
+ abstract subject: T['subject'];
9
+ protected client: Stan;
10
+ constructor(client: Stan);
11
+ publish(data: T['data']): Promise<void>;
12
+ }
13
+ export {};
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Publisher = void 0;
4
+ var Publisher = /** @class */ (function () {
5
+ function Publisher(client) {
6
+ this.client = client;
7
+ }
8
+ Publisher.prototype.publish = function (data) {
9
+ var _this = this;
10
+ return new Promise(function (resolve, reject) {
11
+ _this.client.publish(_this.subject, JSON.stringify(data), function (err) {
12
+ if (err) {
13
+ return reject(err);
14
+ }
15
+ console.log('Event published to subject', _this.subject);
16
+ resolve();
17
+ });
18
+ });
19
+ };
20
+ return Publisher;
21
+ }());
22
+ exports.Publisher = Publisher;
@@ -0,0 +1,17 @@
1
+ import { Subjects } from './subjects';
2
+ import { ServiceCategory } from './type/service-category';
3
+ export interface SalonServiceCreatedEvent {
4
+ subject: Subjects.ServiceCreated;
5
+ data: {
6
+ id: string;
7
+ name: string;
8
+ category: ServiceCategory;
9
+ price: number;
10
+ duration: number;
11
+ description?: string;
12
+ isActive: boolean;
13
+ userId: string;
14
+ createdAt: string;
15
+ updatedAt: string;
16
+ };
17
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
1
+ import { Subjects } from './subjects';
2
+ import { ServiceCategory } from './type/service-category';
3
+ export interface SalonServiceUpdatedEvent {
4
+ subject: Subjects.ServiceUpdated;
5
+ data: {
6
+ id: string;
7
+ name: string;
8
+ category: ServiceCategory;
9
+ price: number;
10
+ duration: number;
11
+ description?: string;
12
+ isActive: boolean;
13
+ userId: string;
14
+ updatedAt: string;
15
+ };
16
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export declare enum Subjects {
2
+ ServiceCreated = "service:created",
3
+ ServiceUpdated = "service:updated"
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Subjects = void 0;
4
+ var Subjects;
5
+ (function (Subjects) {
6
+ Subjects["ServiceCreated"] = "service:created";
7
+ Subjects["ServiceUpdated"] = "service:updated";
8
+ })(Subjects || (exports.Subjects = Subjects = {}));
@@ -0,0 +1,7 @@
1
+ export declare enum ServiceCategory {
2
+ HAIR = "Hair",
3
+ NAILS = "Nails",
4
+ SKINCARE = "Skincare",
5
+ MASSAGE = "Massage",
6
+ OTHER = "Other"
7
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServiceCategory = void 0;
4
+ var ServiceCategory;
5
+ (function (ServiceCategory) {
6
+ ServiceCategory["HAIR"] = "Hair";
7
+ ServiceCategory["NAILS"] = "Nails";
8
+ ServiceCategory["SKINCARE"] = "Skincare";
9
+ ServiceCategory["MASSAGE"] = "Massage";
10
+ ServiceCategory["OTHER"] = "Other";
11
+ })(ServiceCategory || (exports.ServiceCategory = ServiceCategory = {}));
package/build/index.d.ts CHANGED
@@ -8,3 +8,10 @@ export * from './middlewares/current-user';
8
8
  export * from './middlewares/error-handler';
9
9
  export * from './middlewares/require-auth';
10
10
  export * from './middlewares/validate-request';
11
+ export * from './middlewares/admin-auth';
12
+ export * from './events/base-listener';
13
+ export * from './events/base-publisher';
14
+ export * from './events/subjects';
15
+ export * from './events/salon-service-created-event';
16
+ export * from './events/salon-service-updated-event';
17
+ export * from './events/type/service-category';
package/build/index.js CHANGED
@@ -24,3 +24,10 @@ __exportStar(require("./middlewares/current-user"), exports);
24
24
  __exportStar(require("./middlewares/error-handler"), exports);
25
25
  __exportStar(require("./middlewares/require-auth"), exports);
26
26
  __exportStar(require("./middlewares/validate-request"), exports);
27
+ __exportStar(require("./middlewares/admin-auth"), exports);
28
+ __exportStar(require("./events/base-listener"), exports);
29
+ __exportStar(require("./events/base-publisher"), exports);
30
+ __exportStar(require("./events/subjects"), exports);
31
+ __exportStar(require("./events/salon-service-created-event"), exports);
32
+ __exportStar(require("./events/salon-service-updated-event"), exports);
33
+ __exportStar(require("./events/type/service-category"), exports);
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const requireAdmin: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireAdmin = void 0;
4
+ var not_authorized_error_1 = require("../errors/not-authorized-error");
5
+ var requireAdmin = function (req, res, next) {
6
+ var user = req.currentUser;
7
+ if (!user || user.role !== "admin") {
8
+ // Explicitly terminate the function with `return` after sending a response
9
+ throw new not_authorized_error_1.NotAuthorizedError();
10
+ }
11
+ // Call next() to pass control to the next middleware
12
+ next();
13
+ };
14
+ exports.requireAdmin = requireAdmin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@misalon/common",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
@@ -26,6 +26,7 @@
26
26
  "cookie-session": "^2.1.0",
27
27
  "express": "^4.21.1",
28
28
  "express-validator": "^7.2.0",
29
- "jsonwebtoken": "^9.0.2"
29
+ "jsonwebtoken": "^9.0.2",
30
+ "node-nats-streaming": "^0.3.2"
30
31
  }
31
32
  }