@aldb2b/common 1.0.721 → 1.0.723

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.
@@ -0,0 +1,9 @@
1
+ import { ClientProxy } from '@nestjs/microservices';
2
+ import { HeaderUser } from "../types/header-user.interface";
3
+ export declare class SubscriptionValidatorService {
4
+ private readonly client;
5
+ private cacheManager;
6
+ constructor(client: ClientProxy);
7
+ validate(method: string, path: string, header: HeaderUser, totalRecipients?: number, fromMiddleware?: boolean): Promise<any>;
8
+ private isPermitted;
9
+ }
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.SubscriptionValidatorService = void 0;
16
+ const rxjs_1 = require("rxjs");
17
+ const common_1 = require("@nestjs/common");
18
+ const microservices_1 = require("@nestjs/microservices");
19
+ const redis_client_1 = require("../utils/redis-client");
20
+ const subjects_1 = require("../events/subjects");
21
+ const getEndpoint_1 = require("../utils/getEndpoint");
22
+ const endpoint_type_enum_1 = require("../types/endpoint-type.enum");
23
+ let SubscriptionValidatorService = class SubscriptionValidatorService {
24
+ constructor(client) {
25
+ this.client = client;
26
+ this.cacheManager = new redis_client_1.RedisClient();
27
+ }
28
+ async validate(method, path, header, totalRecipients, fromMiddleware) {
29
+ const user = {
30
+ subject: subjects_1.Subjects.HasSubscription,
31
+ data: {
32
+ user: header.id,
33
+ company: header.company,
34
+ eventId: header.eventId,
35
+ },
36
+ };
37
+ const redisKey = JSON.stringify(user.data);
38
+ const redisResponse = undefined;
39
+ console.log('redisKey: ', redisKey);
40
+ console.log('redisResponse: ', redisResponse);
41
+ let resp;
42
+ if (redisResponse) {
43
+ resp = JSON.parse(redisResponse);
44
+ return this.isPermitted(resp, method, path, totalRecipients, fromMiddleware);
45
+ }
46
+ else {
47
+ resp = await (0, rxjs_1.firstValueFrom)(this.client.send(user.subject, user.data));
48
+ if (resp.hasSubscription === false) {
49
+ throw new common_1.ForbiddenException('Subscription is required');
50
+ }
51
+ else {
52
+ await this.cacheManager.setData(redisKey, JSON.stringify(resp), 60);
53
+ return this.isPermitted(resp, method, path, totalRecipients, fromMiddleware);
54
+ }
55
+ }
56
+ }
57
+ async isPermitted(subscription, method, path, totalRecipients, fromMiddleware) {
58
+ const { features, remainingPoints } = subscription;
59
+ const { endpoint, exceptionResp } = (0, getEndpoint_1.getEndpoint)(features, method, path, fromMiddleware);
60
+ console.log('exceptionResp: ', exceptionResp);
61
+ if (exceptionResp)
62
+ return;
63
+ if (!endpoint || endpoint?.isFree) {
64
+ return true;
65
+ }
66
+ console.log('endpoint.endpointType: ', endpoint.endpointType);
67
+ console.log('remainingPoints: ', remainingPoints);
68
+ console.log('Number((totalRecipients ?? 0) * endpoint.point): ', Number((totalRecipients ?? 0) * endpoint.point));
69
+ console.log('remainingPoints < Number((totalRecipients ?? 0) * endpoint.point): ', remainingPoints < Number((totalRecipients ?? 0) * endpoint.point));
70
+ let resp = undefined;
71
+ if (endpoint.endpointType === endpoint_type_enum_1.EndpointType.BINARY && !endpoint.isFree) {
72
+ resp = {
73
+ code: 'C-103',
74
+ statusCode: 403,
75
+ message: 'Please upgrade your subscription',
76
+ error: 'Forbidden',
77
+ description: 'Please upgrade your subscription',
78
+ };
79
+ }
80
+ else if (endpoint.endpointType === endpoint_type_enum_1.EndpointType.LIMIT &&
81
+ endpoint.limit === 0) {
82
+ resp = {
83
+ code: 'C-104',
84
+ statusCode: 402,
85
+ message: 'Limits exceeded',
86
+ error: 'Forbidden',
87
+ description: 'Limits exceeded, please renew your subscription or upgrade your plan',
88
+ };
89
+ }
90
+ else if (endpoint.endpointType === endpoint_type_enum_1.EndpointType.CREDIT &&
91
+ remainingPoints < endpoint.point) {
92
+ resp = {
93
+ code: 'C-105',
94
+ statusCode: 402,
95
+ message: 'Insufficient credit',
96
+ error: 'Forbidden',
97
+ description: 'Insufficient credits, please renew your subscription or upgrade your plan',
98
+ };
99
+ }
100
+ else if (endpoint.endpointType === endpoint_type_enum_1.EndpointType.CREDIT &&
101
+ remainingPoints < Number((totalRecipients ?? 0) * endpoint.point)) {
102
+ resp = {
103
+ code: 'C-106',
104
+ statusCode: 402,
105
+ message: 'Insufficient credit',
106
+ error: 'Forbidden',
107
+ description: 'Insufficient credits, please renew your subscription or upgrade your plan',
108
+ };
109
+ }
110
+ if (fromMiddleware)
111
+ throw new common_1.ForbiddenException(resp);
112
+ return resp;
113
+ }
114
+ };
115
+ exports.SubscriptionValidatorService = SubscriptionValidatorService;
116
+ exports.SubscriptionValidatorService = SubscriptionValidatorService = __decorate([
117
+ (0, common_1.Injectable)(),
118
+ __param(0, (0, common_1.Inject)('SUBSCRIPTION_SERVICE')),
119
+ __metadata("design:paramtypes", [microservices_1.ClientProxy])
120
+ ], SubscriptionValidatorService);
121
+ //# sourceMappingURL=subscription-validator.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscription-validator.service.js","sourceRoot":"","sources":["../../src/services/subscription-validator.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAsC;AACtC,2CAAwE;AACxE,yDAAoD;AACpD,wDAAkD;AAElD,iDAA4C;AAC5C,sDAAiD;AACjD,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