@flusys/nestjs-notification 7.0.2 → 8.0.0-beta.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.
Files changed (103) hide show
  1. package/adapters/index.d.ts +1 -1
  2. package/adapters/notification.adapter.d.ts +2 -2
  3. package/config/index.d.ts +3 -3
  4. package/controllers/index.d.ts +1 -1
  5. package/controllers/notification.controller.d.ts +4 -4
  6. package/docs/index.d.ts +1 -1
  7. package/docs/notification-swagger.config.d.ts +2 -2
  8. package/dtos/index.d.ts +1 -1
  9. package/entities/index.d.ts +5 -5
  10. package/entities/notification-with-company.entity.d.ts +1 -1
  11. package/fesm/244.js +816 -0
  12. package/fesm/446.js +939 -0
  13. package/fesm/adapters/index.js +79 -1
  14. package/fesm/config/index.js +84 -3
  15. package/fesm/controllers/index.js +13 -1
  16. package/fesm/docs/index.js +112 -1
  17. package/fesm/dtos/index.js +311 -1
  18. package/fesm/entities/index.js +211 -8
  19. package/fesm/gateways/index.js +286 -1
  20. package/fesm/index.js +467 -10
  21. package/fesm/interfaces/index.js +16 -2
  22. package/fesm/modules/index.js +269 -1
  23. package/fesm/runtime.js +85 -0
  24. package/fesm/services/index.js +357 -4
  25. package/gateways/index.d.ts +1 -1
  26. package/gateways/notification.gateway.d.ts +2 -2
  27. package/index.d.ts +10 -10
  28. package/interfaces/index.d.ts +2 -2
  29. package/interfaces/notification-module.interface.d.ts +3 -3
  30. package/interfaces/notification.interface.d.ts +1 -1
  31. package/modules/index.d.ts +1 -1
  32. package/modules/notification.module.d.ts +2 -2
  33. package/package.json +33 -44
  34. package/services/index.d.ts +4 -4
  35. package/services/notification-config.service.d.ts +3 -3
  36. package/services/notification-datasource.provider.d.ts +3 -3
  37. package/services/notification-helper.service.d.ts +5 -5
  38. package/services/notification.service.d.ts +8 -8
  39. package/cjs/adapters/index.js +0 -18
  40. package/cjs/adapters/notification.adapter.js +0 -62
  41. package/cjs/adapters/notification.adapter.spec.js +0 -73
  42. package/cjs/config/event-actions.js +0 -33
  43. package/cjs/config/index.js +0 -20
  44. package/cjs/config/message-keys.js +0 -19
  45. package/cjs/config/notification.constants.js +0 -11
  46. package/cjs/controllers/index.js +0 -18
  47. package/cjs/controllers/notification.controller.js +0 -299
  48. package/cjs/controllers/notification.controller.spec.js +0 -214
  49. package/cjs/docs/index.js +0 -18
  50. package/cjs/docs/notification-swagger.config.js +0 -97
  51. package/cjs/docs/notification-swagger.config.spec.js +0 -68
  52. package/cjs/dtos/index.js +0 -18
  53. package/cjs/dtos/notification.dto.js +0 -295
  54. package/cjs/entities/index.js +0 -48
  55. package/cjs/entities/index.spec.js +0 -13
  56. package/cjs/entities/notification-with-company.entity.js +0 -57
  57. package/cjs/entities/notification.entity.js +0 -112
  58. package/cjs/gateways/index.js +0 -18
  59. package/cjs/gateways/notification.gateway.js +0 -172
  60. package/cjs/gateways/notification.gateway.spec.js +0 -359
  61. package/cjs/index.js +0 -28
  62. package/cjs/interfaces/index.js +0 -19
  63. package/cjs/interfaces/notification-module.interface.js +0 -4
  64. package/cjs/interfaces/notification.interface.js +0 -4
  65. package/cjs/modules/index.js +0 -18
  66. package/cjs/modules/notification.module.js +0 -139
  67. package/cjs/modules/notification.module.spec.js +0 -152
  68. package/cjs/services/index.js +0 -21
  69. package/cjs/services/notification-config.service.js +0 -69
  70. package/cjs/services/notification-config.service.spec.js +0 -113
  71. package/cjs/services/notification-datasource.provider.js +0 -133
  72. package/cjs/services/notification-datasource.provider.spec.js +0 -136
  73. package/cjs/services/notification-helper.service.js +0 -197
  74. package/cjs/services/notification-helper.service.spec.js +0 -392
  75. package/cjs/services/notification.service.js +0 -281
  76. package/cjs/services/notification.service.spec.js +0 -560
  77. package/fesm/adapters/notification.adapter.js +0 -52
  78. package/fesm/adapters/notification.adapter.spec.js +0 -69
  79. package/fesm/config/event-actions.js +0 -12
  80. package/fesm/config/message-keys.js +0 -9
  81. package/fesm/config/notification.constants.js +0 -1
  82. package/fesm/controllers/notification.controller.js +0 -289
  83. package/fesm/controllers/notification.controller.spec.js +0 -210
  84. package/fesm/docs/notification-swagger.config.js +0 -87
  85. package/fesm/docs/notification-swagger.config.spec.js +0 -64
  86. package/fesm/dtos/notification.dto.js +0 -262
  87. package/fesm/entities/index.spec.js +0 -9
  88. package/fesm/entities/notification-with-company.entity.js +0 -47
  89. package/fesm/entities/notification.entity.js +0 -102
  90. package/fesm/gateways/notification.gateway.js +0 -162
  91. package/fesm/gateways/notification.gateway.spec.js +0 -355
  92. package/fesm/interfaces/notification-module.interface.js +0 -1
  93. package/fesm/interfaces/notification.interface.js +0 -1
  94. package/fesm/modules/notification.module.js +0 -129
  95. package/fesm/modules/notification.module.spec.js +0 -148
  96. package/fesm/services/notification-config.service.js +0 -59
  97. package/fesm/services/notification-config.service.spec.js +0 -109
  98. package/fesm/services/notification-datasource.provider.js +0 -82
  99. package/fesm/services/notification-datasource.provider.spec.js +0 -132
  100. package/fesm/services/notification-helper.service.js +0 -187
  101. package/fesm/services/notification-helper.service.spec.js +0 -388
  102. package/fesm/services/notification.service.js +0 -271
  103. package/fesm/services/notification.service.spec.js +0 -556
@@ -1,136 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- const _modules = require("@flusys/nestjs-shared/modules");
6
- const _notificationdatasourceprovider = require("./notification-datasource.provider");
7
- const _entities = require("../entities");
8
- function buildConfigService(overrides = {}) {
9
- return {
10
- isCompanyFeatureEnabled: jest.fn().mockReturnValue(false),
11
- getDatabaseMode: jest.fn().mockReturnValue('single'),
12
- isMultiTenant: jest.fn().mockReturnValue(false),
13
- getJwtSecret: jest.fn().mockReturnValue(undefined),
14
- getOptions: jest.fn().mockReturnValue({
15
- bootstrapAppConfig: {
16
- databaseMode: 'single',
17
- enableCompanyFeature: false
18
- },
19
- config: {
20
- defaultDatabaseConfig: {
21
- type: 'mysql',
22
- host: 'localhost',
23
- port: 3306,
24
- username: 'root',
25
- password: 'pass',
26
- database: 'flusys'
27
- },
28
- tenants: []
29
- }
30
- }),
31
- ...overrides
32
- };
33
- }
34
- describe('NotificationDataSourceProvider', ()=>{
35
- let configService;
36
- let provider;
37
- beforeEach(()=>{
38
- configService = buildConfigService();
39
- provider = new _notificationdatasourceprovider.NotificationDataSourceProvider(configService, undefined);
40
- });
41
- describe('getEnableCompanyFeatureForTenant', ()=>{
42
- it('returns the tenant override when the tenant explicitly enables the company feature', ()=>{
43
- const result = provider.getEnableCompanyFeatureForTenant({
44
- id: 'tenant-1',
45
- database: 'tenant_db',
46
- enableCompanyFeature: true
47
- });
48
- expect(result).toBe(true);
49
- expect(configService.isCompanyFeatureEnabled).not.toHaveBeenCalled();
50
- });
51
- it('returns the tenant override when the tenant explicitly disables the company feature', ()=>{
52
- const result = provider.getEnableCompanyFeatureForTenant({
53
- id: 'tenant-1',
54
- database: 'tenant_db',
55
- enableCompanyFeature: false
56
- });
57
- expect(result).toBe(false);
58
- });
59
- it('falls back to the config service when the tenant has no override', ()=>{
60
- configService.isCompanyFeatureEnabled.mockReturnValue(true);
61
- const result = provider.getEnableCompanyFeatureForTenant(undefined);
62
- expect(result).toBe(true);
63
- expect(configService.isCompanyFeatureEnabled).toHaveBeenCalled();
64
- });
65
- });
66
- describe('getNotificationEntities', ()=>{
67
- it('returns [NotificationWithCompany] when enableCompanyFeature is explicitly true', async ()=>{
68
- const entities = await provider.getNotificationEntities(true);
69
- expect(entities).toEqual([
70
- _entities.NotificationWithCompany
71
- ]);
72
- });
73
- it('returns [Notification] when enableCompanyFeature is explicitly false', async ()=>{
74
- const entities = await provider.getNotificationEntities(false);
75
- expect(entities).toEqual([
76
- _entities.Notification
77
- ]);
78
- });
79
- it('falls back to the config service when enableCompanyFeature is not provided', async ()=>{
80
- configService.isCompanyFeatureEnabled.mockReturnValue(true);
81
- const entities = await provider.getNotificationEntities();
82
- expect(entities).toEqual([
83
- _entities.NotificationWithCompany
84
- ]);
85
- expect(configService.isCompanyFeatureEnabled).toHaveBeenCalled();
86
- });
87
- });
88
- describe('createDataSourceFromConfig', ()=>{
89
- it('resolves company-aware entities and delegates to the parent implementation', async ()=>{
90
- configService.isCompanyFeatureEnabled.mockReturnValue(true);
91
- const fakeDataSource = {};
92
- const superSpy = jest.spyOn(_modules.MultiTenantDataSourceService.prototype, 'createDataSourceFromConfig').mockResolvedValue(fakeDataSource);
93
- const config = {
94
- type: 'mysql',
95
- host: 'localhost',
96
- port: 3306,
97
- username: 'root',
98
- password: 'pass',
99
- database: 'flusys'
100
- };
101
- const result = await provider.createDataSourceFromConfig(config);
102
- expect(result).toBe(fakeDataSource);
103
- expect(superSpy).toHaveBeenCalledWith(config, [
104
- _entities.NotificationWithCompany
105
- ]);
106
- superSpy.mockRestore();
107
- });
108
- it('resolves core entities when the company feature is disabled', async ()=>{
109
- configService.isCompanyFeatureEnabled.mockReturnValue(false);
110
- const fakeDataSource = {};
111
- const superSpy = jest.spyOn(_modules.MultiTenantDataSourceService.prototype, 'createDataSourceFromConfig').mockResolvedValue(fakeDataSource);
112
- const config = {
113
- type: 'mysql',
114
- host: 'localhost',
115
- port: 3306,
116
- username: 'root',
117
- password: 'pass',
118
- database: 'flusys'
119
- };
120
- await provider.createDataSourceFromConfig(config);
121
- expect(superSpy).toHaveBeenCalledWith(config, [
122
- _entities.Notification
123
- ]);
124
- superSpy.mockRestore();
125
- });
126
- });
127
- describe('inherited configuration accessors', ()=>{
128
- it('reports single database mode by default', ()=>{
129
- expect(provider.getDatabaseMode()).toBe('single');
130
- expect(provider.isMultiTenant()).toBe(false);
131
- });
132
- it('returns null for the current tenant when no request is bound', ()=>{
133
- expect(provider.getCurrentTenant()).toBeNull();
134
- });
135
- });
136
- });
@@ -1,197 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "NotificationHelperService", {
6
- enumerable: true,
7
- get: function() {
8
- return NotificationHelperService;
9
- }
10
- });
11
- const _nestjsshared = require("@flusys/nestjs-shared");
12
- const _classes = require("@flusys/nestjs-shared/classes");
13
- const _enums = require("@flusys/nestjs-shared/enums");
14
- const _modules = require("@flusys/nestjs-shared/modules");
15
- const _common = require("@nestjs/common");
16
- const _config = require("../config");
17
- const _entities = require("../entities");
18
- const _gateways = require("../gateways");
19
- const _notificationconfigservice = require("./notification-config.service");
20
- const _notificationdatasourceprovider = require("./notification-datasource.provider");
21
- function _define_property(obj, key, value) {
22
- if (key in obj) {
23
- Object.defineProperty(obj, key, {
24
- value: value,
25
- enumerable: true,
26
- configurable: true,
27
- writable: true
28
- });
29
- } else {
30
- obj[key] = value;
31
- }
32
- return obj;
33
- }
34
- function _ts_decorate(decorators, target, key, desc) {
35
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
36
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
37
- 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;
38
- return c > 3 && r && Object.defineProperty(target, key, r), r;
39
- }
40
- function _ts_metadata(k, v) {
41
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
42
- }
43
- function _ts_param(paramIndex, decorator) {
44
- return function(target, key) {
45
- decorator(target, key, paramIndex);
46
- };
47
- }
48
- let NotificationHelperService = class NotificationHelperService {
49
- async send(options) {
50
- const repo = await this.getRepository();
51
- const entity = this.buildEntity(options.userId, options);
52
- const saved = await repo.save(entity);
53
- await this.clearCache();
54
- this.emitToUser(saved, options.realtime !== false);
55
- await this.publishSentEvent([
56
- saved
57
- ], [
58
- options.userId
59
- ]);
60
- return saved;
61
- }
62
- async sendToMany(options) {
63
- const repo = await this.getRepository();
64
- const entities = options.userIds.map((userId)=>this.buildEntity(userId, options));
65
- const saved = await repo.save(entities);
66
- await this.clearCache();
67
- if (options.realtime !== false) {
68
- setImmediate(()=>{
69
- for (const notification of saved){
70
- this.emitToUser(notification, true);
71
- }
72
- });
73
- }
74
- await this.publishSentEvent(saved, options.userIds);
75
- return saved;
76
- }
77
- isUserOnline(userId) {
78
- return this.gateway?.isUserOnline(userId) ?? false;
79
- }
80
- getOnlineUserCount() {
81
- return this.gateway?.getOnlineUserCount() ?? 0;
82
- }
83
- async publishSentEvent(notifications, userIds) {
84
- await (0, _classes.publishDomainEvent)({
85
- module: _config.NOTIFICATION_EVENT_MODULE,
86
- entity: _config.NOTIFICATION_EVENT_ENTITIES.NOTIFICATION,
87
- action: _config.NOTIFICATION_EVENT_ACTIONS.SENT,
88
- ids: notifications.map((notification)=>notification.id),
89
- metadata: {
90
- recipientCount: userIds.length
91
- }
92
- });
93
- }
94
- async getDataSource() {
95
- if (!this.dataSource) {
96
- this.dataSource = await this.dataSourceProvider.getDataSource();
97
- }
98
- return this.dataSource;
99
- }
100
- async getRepository() {
101
- const ds = await this.getDataSource();
102
- const EntityClass = this.configService.isCompanyFeatureEnabled() ? _entities.NotificationWithCompany : _entities.Notification;
103
- return ds.getRepository(EntityClass);
104
- }
105
- buildEntity(userId, content) {
106
- const entity = {
107
- userId,
108
- type: content.type ?? _enums.NotificationType.INFO,
109
- title: content.title,
110
- message: content.message ?? null,
111
- data: content.data ?? null,
112
- isRead: false
113
- };
114
- if (this.configService.isCompanyFeatureEnabled() && content.companyId) {
115
- entity.companyId = content.companyId;
116
- }
117
- return entity;
118
- }
119
- emitToUser(notification, realtime) {
120
- if (!realtime || !this.gateway) {
121
- return;
122
- }
123
- const ext = notification;
124
- const payload = this.toPayload(notification);
125
- if (this.configService.isCompanyFeatureEnabled() && ext.companyId) {
126
- this.gateway.emitToUserInCompany(notification.userId, ext.companyId, 'notification:new', payload);
127
- } else {
128
- this.gateway.emitToUser(notification.userId, 'notification:new', payload);
129
- }
130
- }
131
- toPayload(notification) {
132
- return {
133
- id: notification.id,
134
- type: notification.type,
135
- title: notification.title,
136
- message: notification.message,
137
- data: notification.data,
138
- createdAt: notification.createdAt
139
- };
140
- }
141
- async clearCache() {
142
- await this.utilsService.clearCache(_config.NOTIFICATION_EVENT_ENTITIES.NOTIFICATION, this.cacheManager);
143
- }
144
- constructor(cacheManager, utilsService, configService, dataSourceProvider, gateway){
145
- _define_property(this, "cacheManager", void 0);
146
- _define_property(this, "utilsService", void 0);
147
- _define_property(this, "configService", void 0);
148
- _define_property(this, "dataSourceProvider", void 0);
149
- _define_property(this, "gateway", void 0);
150
- _define_property(this, "dataSource", void 0);
151
- this.cacheManager = cacheManager;
152
- this.utilsService = utilsService;
153
- this.configService = configService;
154
- this.dataSourceProvider = dataSourceProvider;
155
- this.gateway = gateway;
156
- this.dataSource = null;
157
- }
158
- };
159
- _ts_decorate([
160
- (0, _nestjsshared.LogAction)({
161
- action: 'notification.send',
162
- module: 'notification'
163
- }),
164
- _ts_metadata("design:type", Function),
165
- _ts_metadata("design:paramtypes", [
166
- typeof _nestjsshared.INotificationSendOptions === "undefined" ? Object : _nestjsshared.INotificationSendOptions
167
- ]),
168
- _ts_metadata("design:returntype", Promise)
169
- ], NotificationHelperService.prototype, "send", null);
170
- _ts_decorate([
171
- (0, _nestjsshared.LogAction)({
172
- action: 'notification.sendToMany',
173
- module: 'notification'
174
- }),
175
- _ts_metadata("design:type", Function),
176
- _ts_metadata("design:paramtypes", [
177
- typeof _nestjsshared.INotificationBulkSendOptions === "undefined" ? Object : _nestjsshared.INotificationBulkSendOptions
178
- ]),
179
- _ts_metadata("design:returntype", Promise)
180
- ], NotificationHelperService.prototype, "sendToMany", null);
181
- NotificationHelperService = _ts_decorate([
182
- (0, _common.Injectable)(),
183
- _ts_param(0, (0, _common.Inject)('CACHE_INSTANCE')),
184
- _ts_param(1, (0, _common.Inject)(_modules.UtilsService)),
185
- _ts_param(2, (0, _common.Inject)(_notificationconfigservice.NotificationConfigService)),
186
- _ts_param(3, (0, _common.Inject)(_notificationdatasourceprovider.NotificationDataSourceProvider)),
187
- _ts_param(4, (0, _common.Optional)()),
188
- _ts_param(4, (0, _common.Inject)(_gateways.NotificationGateway)),
189
- _ts_metadata("design:type", Function),
190
- _ts_metadata("design:paramtypes", [
191
- typeof _classes.HybridCache === "undefined" ? Object : _classes.HybridCache,
192
- typeof _modules.UtilsService === "undefined" ? Object : _modules.UtilsService,
193
- typeof _notificationconfigservice.NotificationConfigService === "undefined" ? Object : _notificationconfigservice.NotificationConfigService,
194
- typeof _notificationdatasourceprovider.NotificationDataSourceProvider === "undefined" ? Object : _notificationdatasourceprovider.NotificationDataSourceProvider,
195
- typeof _gateways.NotificationGateway === "undefined" ? Object : _gateways.NotificationGateway
196
- ])
197
- ], NotificationHelperService);