@aldb2b/common 1.0.685 → 1.0.686

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/index.d.ts CHANGED
@@ -254,3 +254,5 @@ export * from './common/dto/email-validation-response.dto';
254
254
  export * from './common/interfaces/email-validation-option.interface';
255
255
  export * from './common/services/common.service';
256
256
  export * from './common/utils';
257
+ export * from './services/subscription-validator.service';
258
+ export * from './services/subscription-subtractor.service';
package/build/index.js CHANGED
@@ -270,4 +270,6 @@ __exportStar(require("./common/dto/email-validation-response.dto"), exports);
270
270
  __exportStar(require("./common/interfaces/email-validation-option.interface"), exports);
271
271
  __exportStar(require("./common/services/common.service"), exports);
272
272
  __exportStar(require("./common/utils"), exports);
273
+ __exportStar(require("./services/subscription-validator.service"), exports);
274
+ __exportStar(require("./services/subscription-subtractor.service"), exports);
273
275
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oEAAiD;AACjD,oDAAiC;AACjC,iEAA8C;AAC9C,mEAAgD;AAChD,+DAA4C;AAC5C,oEAAiD;AACjD,qEAAkD;AAClD,oEAAiD;AACjD,sEAAmD;AACnD,6EAA0D;AAC1D,uEAAoD;AACpD,sEAAmD;AACnD,6EAA0D;AAC1D,2EAAwD;AACxD,4EAAyD;AACzD,iEAA8C;AAC9C,4EAAyD;AACzD,4EAAyD;AACzD,wEAAqD;AACrD,yEAAsD;AACtD,yEAAsD;AACtD,oEAAiD;AACjD,6EAA0D;AAC1D,2EAAwD;AACxD,8EAA2D;AAC3D,mEAAgD;AAChD,oEAAiD;AACjD,oEAAiD;AACjD,oEAAiD;AACjD,wEAAqD;AACrD,2EAAwD;AACxD,0EAAuD;AACvD,mEAAgD;AAChD,kDAA+B;AAC/B,oEAAiD;AACjD,2EAAwD;AACxD,oEAAiD;AACjD,qEAAkD;AAClD,qEAAk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/D,0EAAuD;AAEvD,qEAAkD;AAClD,6DAA0C;AAC1C,uDAAoC;AACpC,sDAAmC;AACnC,qDAAkC;AAClC,mDAAgC;AAChC,4DAAyC;AACzC,2DAAwC;AACxC,oDAAiC;AACjC,uDAAoC;AACpC,yDAAsC;AACtC,sDAAmC;AACnC,wDAAqC;AACrC,mDAAgC;AAChC,oDAAiC;AACjC,gDAA6B;AAE7B,qEAAkD;AAClD,kEAA+C;AAC/C,oEAAiD;AACjD,yDAAsC;AACtC,gEAA6C;AAC7C,8DAA2C;AAC3C,gEAA6C;AAC7C,mEAAgD;AAChD,8DAA2C;AAC3C,gEAA6C;AAC7C,kEAA+C;AAC/C,+DAA4C;AAE5C,iEAA8C;AAC9C,mEAAgD;AAChD,iEAA8C;AAE9C,qEAAkD;AAClD,qEAAkD;AAElD,6DAA0C;AAC1C,wFAAqE;AACrE,gEAA6C;AAC7C,oEAAiD;AACjD,8DAA2C;AAC3C,0DAAuC;AACvC,2DAAwC;AACxC,2DAAwC;AACxC,sDAAmC;AAEnC,yDAAsC;AACtC,iDAA8B;AAC9B,yEAAsD;AACtD,4EAAyD;AACzD,6EAA0D;AAC1D,wFAAqE;AACrE,mEAAgD;AAChD,iDAA8B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oEAAiD;AACjD,oDAAiC;AACjC,iEAA8C;AAC9C,mEAAgD;AAChD,+DAA4C;AAC5C,oEAAiD;AACjD,qEAAkD;AAClD,oEAAiD;AACjD,sEAAmD;AACnD,6EAA0D;AAC1D,uEAAoD;AACpD,sEAAmD;AACnD,6EAA0D;AAC1D,2EAAwD;AACxD,4EAAyD;AACzD,iEAA8C;AAC9C,4EAAyD;AACzD,4EAAyD;AACzD,wEAAqD;AACrD,yEAAsD;AACtD,yEAAsD;AACtD,oEAAiD;AACjD,6EAA0D;AAC1D,2EAAwD;AACxD,8EAA2D;AAC3D,mEAAgD;AAChD,oEA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,yDAAsC;AACtC,2DAAwC;AACxC,yDAAsC;AAEtC,+DAA4C;AAC5C,sEAAmD;AACnD,mFAAgE;AAChE,kFAA+D;AAC/D,wEAAqD;AACrD,qGAAkF;AAClF,gEAA6C;AAC7C,0EAAuD;AACvD,kFAA+D;AAC/D,0EAAuD;AAEvD,qEAAkD;AAClD,6DAA0C;AAC1C,uDAAoC;AACpC,sDAAmC;AACnC,qDAAkC;AAClC,mDAAgC;AAChC,4DAAyC;AACzC,2DAAwC;AACxC,oDAAiC;AACjC,uDAAoC;AACpC,yDAAsC;AACtC,sDAAmC;AACnC,wDAAqC;AACrC,mDAAgC;AAChC,oDAAiC;AACjC,gDAA6B;AAE7B,qEAAkD;AAClD,kEAA+C;AAC/C,oEAAiD;AACjD,yDAAsC;AACtC,gEAA6C;AAC7C,8DAA2C;AAC3C,gEAA6C;AAC7C,mEAAgD;AAChD,8DAA2C;AAC3C,gEAA6C;AAC7C,kEAA+C;AAC/C,+DAA4C;AAE5C,iEAA8C;AAC9C,mEAAgD;AAChD,iEAA8C;AAE9C,qEAAkD;AAClD,qEAAkD;AAElD,6DAA0C;AAC1C,wFAAqE;AACrE,gEAA6C;AAC7C,oEAAiD;AACjD,8DAA2C;AAC3C,0DAAuC;AACvC,2DAAwC;AACxC,2DAAwC;AACxC,sDAAmC;AAEnC,yDAAsC;AACtC,iDAA8B;AAC9B,yEAAsD;AACtD,4EAAyD;AACzD,6EAA0D;AAC1D,wFAAqE;AACrE,mEAAgD;AAChD,iDAA8B;AAE9B,4EAAyD;AACzD,6EAA0D"}
@@ -0,0 +1,9 @@
1
+ import { ClientProxy } from '@nestjs/microservices';
2
+ import { HeaderUser } from "../types/header-user.interface";
3
+ export declare class SubscriptionSubtractorService {
4
+ private readonly client;
5
+ private cachManager;
6
+ constructor(client: ClientProxy);
7
+ subtract(method: string, path: string, header: HeaderUser, recordId: string, description: string): Promise<any>;
8
+ private subtractSubscription;
9
+ }
@@ -0,0 +1,122 @@
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.SubscriptionSubtractorService = void 0;
16
+ const rxjs_1 = require("rxjs");
17
+ const subjects_1 = require("../events/subjects");
18
+ const getEndpoint_1 = require("../utils/getEndpoint");
19
+ const redis_client_1 = require("../utils/redis-client");
20
+ const common_1 = require("@nestjs/common");
21
+ const microservices_1 = require("@nestjs/microservices");
22
+ const endpoint_type_enum_1 = require("../types/endpoint-type.enum");
23
+ let SubscriptionSubtractorService = class SubscriptionSubtractorService {
24
+ constructor(client) {
25
+ this.client = client;
26
+ this.cachManager = new redis_client_1.RedisClient();
27
+ }
28
+ async subtract(method = 'POST', path = '/campaigns/wish-campaigns', header, recordId, description) {
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
+ let resp;
40
+ if (redisResponse) {
41
+ resp = JSON.parse(redisResponse);
42
+ console.log('FROM REDIS--------------------');
43
+ await this.subtractSubscription(resp, method, path, redisKey, recordId, description, header.contactId, header.eventId);
44
+ }
45
+ else {
46
+ resp = await (0, rxjs_1.firstValueFrom)(this.client.send(user.subject, user.data));
47
+ console.log('FROM MESSAGE BROKER--------------------');
48
+ await this.cachManager.setData(redisKey, JSON.stringify(resp), 60);
49
+ await this.subtractSubscription(resp, method, path, redisKey, recordId, description, header.contactId, header.eventId);
50
+ }
51
+ }
52
+ async subtractSubscription(subscription, method, path, key, recordId, description, contactId, event) {
53
+ console.log('endpoint: ', JSON.stringify(subscription));
54
+ const { features, remainingPoints } = subscription;
55
+ console.log('features: ', JSON.stringify(features));
56
+ const { endpoint, feature: subscriptionFeature } = (0, getEndpoint_1.getEndpoint)(features, method, path);
57
+ if (!endpoint || endpoint?.isFree) {
58
+ return true;
59
+ }
60
+ console.log('endpoint: ', JSON.stringify(endpoint));
61
+ description += `Deduct ${endpoint.point} ${endpoint.endpointType} for ${subscriptionFeature.featureType}`;
62
+ console.log('description: ', description);
63
+ if (endpoint?.endpointType === endpoint_type_enum_1.EndpointType.BINARY) {
64
+ console.log('InsideBinaryEndpoint');
65
+ return;
66
+ }
67
+ else if (endpoint?.endpointType === endpoint_type_enum_1.EndpointType.LIMIT) {
68
+ console.log('InsideLimitEndpoint');
69
+ subscription.features = subscription.features.map((feature) => {
70
+ if (feature._id === subscriptionFeature._id) {
71
+ feature.endpoints = feature.endpoints.map((existingEndpoint) => {
72
+ if (existingEndpoint._id === endpoint._id) {
73
+ existingEndpoint.limit -= 1;
74
+ }
75
+ return existingEndpoint;
76
+ });
77
+ }
78
+ return feature;
79
+ });
80
+ await this.cachManager.setData(key, JSON.stringify(subscription), 60);
81
+ (0, rxjs_1.firstValueFrom)(this.client.send(subjects_1.Subjects.SubtractSubscriptionLimit, {
82
+ limit: endpoint.limit,
83
+ id: subscription._id,
84
+ featureId: subscriptionFeature._id,
85
+ endpointId: endpoint._id,
86
+ method: endpoint.method,
87
+ action: 'endpoint-called',
88
+ description: description,
89
+ createdBy: contactId,
90
+ relatedRecord: recordId,
91
+ relatedRecordType: subscriptionFeature.featureType,
92
+ event,
93
+ }));
94
+ }
95
+ else if (endpoint?.endpointType === endpoint_type_enum_1.EndpointType.CREDIT) {
96
+ console.log('InsideCreditEndpoint');
97
+ const modifiedSubscription = {
98
+ ...subscription,
99
+ remainingPoints: remainingPoints - endpoint.point,
100
+ };
101
+ await this.cachManager.setData(key, JSON.stringify(modifiedSubscription), 60);
102
+ (0, rxjs_1.firstValueFrom)(this.client.send(subjects_1.Subjects.SubtractSubscriptionPoint, {
103
+ point: endpoint.point,
104
+ id: subscription._id,
105
+ method: endpoint.method,
106
+ action: 'endpoint-called',
107
+ description: description,
108
+ createdBy: contactId,
109
+ relatedRecord: recordId,
110
+ relatedRecordType: subscriptionFeature.featureType,
111
+ event,
112
+ }));
113
+ }
114
+ }
115
+ };
116
+ exports.SubscriptionSubtractorService = SubscriptionSubtractorService;
117
+ exports.SubscriptionSubtractorService = SubscriptionSubtractorService = __decorate([
118
+ (0, common_1.Injectable)(),
119
+ __param(0, (0, common_1.Inject)('SUBSCRIPTION_SERVICE')),
120
+ __metadata("design:paramtypes", [microservices_1.ClientProxy])
121
+ ], SubscriptionSubtractorService);
122
+ //# sourceMappingURL=subscription-subtractor.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscription-subtractor.service.js","sourceRoot":"","sources":["../../src/services/subscription-subtractor.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAsC;AACtC,iDAA4C;AAC5C,sDAAiD;AACjD,wDAAkD;AAClD,2CAAoD;AACpD,yDAAoD;AACpD,oEAAyD;AAIlD,IAAM,6BAA6B,GAAnC,MAAM,6BAA6B;IAGxC,YACmD,MAAmB;QAAnB,WAAM,GAAN,MAAM,CAAa;QAEpE,IAAI,CAAC,WAAW,GAAG,IAAI,0BAAW,EAAE,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,QAAQ,CACZ,MAAM,GAAG,MAAM,EACf,IAAI,GAAG,2BAA2B,EAClC,MAAkB,EAClB,QAAgB,EAChB,WAA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AS,CAAC,oBAAoB,CAAC,EACpC,EAAE,CACH,CAAC;YACF,IAAA,qBAAc,EACZ,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,mBAAQ,CAAC,yBAAyB,EAAE;gBACnD,KAAK,EAAE,QAAQ,CAAC,KAAK;gBACrB,EAAE,EAAE,YAAY,CAAC,GAAG;gBACpB,MAAM,EAAE,QAAQ,CAAC,MAAM;gBACvB,MAAM,EAAE,iBAAiB;gBACzB,WAAW,EAAE,WAAW;gBACxB,SAAS,EAAE,SAAS;gBACpB,aAAa,EAAE,QAAQ;gBACvB,iBAAiB,EAAE,mBAAmB,CAAC,WAAW;gBAClD,KAAK;aACN,CAAC,CACH,CAAC;QACJ,CAAC;IACH,CAAC;CACF,CAAA;AAnJY,sEAA6B;wCAA7B,6BAA6B;IADzC,IAAA,mBAAU,GAAE;IAKR,WAAA,IAAA,eAAM,EAAC,sBAAsB,CAAC,CAAA;qCAA0B,2BAAW;GAJ3D,6BAA6B,CAmJzC"}
@@ -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): Promise<any>;
8
+ private isPermitted;
9
+ }
@@ -0,0 +1,109 @@
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 subjects_1 = require("../events/subjects");
18
+ const getEndpoint_1 = require("../utils/getEndpoint");
19
+ const redis_client_1 = require("../utils/redis-client");
20
+ const microservices_1 = require("@nestjs/microservices");
21
+ const endpoint_type_enum_1 = require("../types/endpoint-type.enum");
22
+ const common_1 = require("@nestjs/common");
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) {
29
+ console.log('*************** validate **************');
30
+ console.log('method: ', method);
31
+ console.log('path: ', path);
32
+ console.log('header: ', header);
33
+ console.log('totalRecipients: ', totalRecipients);
34
+ const user = {
35
+ subject: subjects_1.Subjects.HasSubscription,
36
+ data: {
37
+ user: header.id,
38
+ company: header.company,
39
+ eventId: header.eventId,
40
+ },
41
+ };
42
+ const redisKey = JSON.stringify(user.data);
43
+ const redisResponse = undefined;
44
+ let resp;
45
+ if (redisResponse) {
46
+ resp = JSON.parse(redisResponse);
47
+ await this.isPermitted(resp, method, path, totalRecipients);
48
+ }
49
+ else {
50
+ resp = await (0, rxjs_1.firstValueFrom)(this.client.send(user.subject, user.data));
51
+ if (resp.hasSubscription === false) {
52
+ throw new common_1.ForbiddenException('Subscription is required');
53
+ }
54
+ else {
55
+ await this.cacheManager.setData(redisKey, JSON.stringify(resp), 60);
56
+ await this.isPermitted(resp, method, path, totalRecipients);
57
+ }
58
+ }
59
+ }
60
+ async isPermitted(subscription, method, path, totalRecipients) {
61
+ const { features, remainingPoints } = subscription;
62
+ const { endpoint } = (0, getEndpoint_1.getEndpoint)(features, method, path);
63
+ if (!endpoint || endpoint?.isFree) {
64
+ return true;
65
+ }
66
+ if (endpoint.endpointType === endpoint_type_enum_1.EndpointType.BINARY && !endpoint.isFree) {
67
+ throw new common_1.ForbiddenException({
68
+ statusCode: 403,
69
+ message: 'Please upgrade your subscription',
70
+ error: 'Forbidden',
71
+ description: 'Please upgrade your subscription',
72
+ });
73
+ }
74
+ else if (endpoint.endpointType === endpoint_type_enum_1.EndpointType.LIMIT &&
75
+ endpoint.limit === 0) {
76
+ throw new common_1.ForbiddenException({
77
+ statusCode: 402,
78
+ message: 'Limits exceeded',
79
+ error: 'Forbidden',
80
+ description: 'Limits exceeded, please renew your subscription or upgrade your plan',
81
+ });
82
+ }
83
+ else if (endpoint.endpointType === endpoint_type_enum_1.EndpointType.CREDIT &&
84
+ remainingPoints < endpoint.point) {
85
+ throw new common_1.ForbiddenException({
86
+ statusCode: 402,
87
+ message: 'Insufficient credit',
88
+ error: 'Forbidden',
89
+ description: 'Insufficient credits, please renew your subscription or upgrade your plan',
90
+ });
91
+ }
92
+ else if (endpoint.endpointType === endpoint_type_enum_1.EndpointType.CREDIT &&
93
+ remainingPoints < Number((totalRecipients ?? 0) * endpoint.point)) {
94
+ throw new common_1.ForbiddenException({
95
+ statusCode: 402,
96
+ message: 'Insufficient credit',
97
+ error: 'Forbidden',
98
+ description: 'Insufficient credits, please renew your subscription or upgrade your plan',
99
+ });
100
+ }
101
+ }
102
+ };
103
+ exports.SubscriptionValidatorService = SubscriptionValidatorService;
104
+ exports.SubscriptionValidatorService = SubscriptionValidatorService = __decorate([
105
+ (0, common_1.Injectable)(),
106
+ __param(0, (0, common_1.Inject)('SUBSCRIPTION_SERVICE')),
107
+ __metadata("design:paramtypes", [microservices_1.ClientProxy])
108
+ ], SubscriptionValidatorService);
109
+ //# sourceMappingURL=subscription-validator.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscription-validator.service.js","sourceRoot":"","sources":["../../src/services/subscription-validator.service.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