@flusys/nestjs-shared 6.0.2 → 6.1.0

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 (63) hide show
  1. package/cjs/classes/api-controller.class.spec.js +684 -0
  2. package/cjs/classes/api-service.class.spec.js +809 -0
  3. package/cjs/classes/handle-custome-db.spec.js +82 -0
  4. package/cjs/classes/hybrid-cache.class.spec.js +168 -0
  5. package/cjs/classes/winston.logger.class.spec.js +330 -0
  6. package/cjs/decorators/api-response.decorator.spec.js +126 -0
  7. package/cjs/decorators/current-user.decorator.spec.js +74 -0
  8. package/cjs/decorators/log-action.decorator.spec.js +353 -0
  9. package/cjs/decorators/public.decorator.spec.js +50 -0
  10. package/cjs/decorators/require-permission.decorator.spec.js +147 -0
  11. package/cjs/exceptions/base-app.exception.spec.js +157 -0
  12. package/cjs/exceptions/permission.exception.spec.js +82 -0
  13. package/cjs/filters/global-exception.filter.spec.js +440 -0
  14. package/cjs/guards/jwt-auth.guard.spec.js +75 -0
  15. package/cjs/guards/optional-jwt-auth.guard.spec.js +55 -0
  16. package/cjs/guards/permission.guard.spec.js +358 -0
  17. package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +107 -0
  18. package/cjs/interceptors/idempotency.interceptor.spec.js +116 -0
  19. package/cjs/interceptors/response-meta.interceptor.spec.js +89 -0
  20. package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +128 -0
  21. package/cjs/interceptors/slug.interceptor.spec.js +110 -0
  22. package/cjs/middlewares/logger.middleware.spec.js +462 -0
  23. package/cjs/modules/cache/cache.module.spec.js +48 -0
  24. package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +381 -0
  25. package/cjs/modules/utils/utils.service.spec.js +169 -0
  26. package/cjs/utils/date-time.util.spec.js +84 -0
  27. package/cjs/utils/html-sanitizer.util.spec.js +76 -0
  28. package/cjs/utils/permission-cache-key.util.spec.js +88 -0
  29. package/cjs/utils/query-helpers.util.spec.js +209 -0
  30. package/cjs/utils/request.util.spec.js +249 -0
  31. package/cjs/utils/string.util.spec.js +63 -0
  32. package/fesm/classes/api-controller.class.spec.js +680 -0
  33. package/fesm/classes/api-service.class.spec.js +805 -0
  34. package/fesm/classes/handle-custome-db.spec.js +78 -0
  35. package/fesm/classes/hybrid-cache.class.spec.js +164 -0
  36. package/fesm/classes/winston.logger.class.spec.js +327 -0
  37. package/fesm/decorators/api-response.decorator.spec.js +122 -0
  38. package/fesm/decorators/current-user.decorator.spec.js +73 -0
  39. package/fesm/decorators/log-action.decorator.spec.js +349 -0
  40. package/fesm/decorators/public.decorator.spec.js +46 -0
  41. package/fesm/decorators/require-permission.decorator.spec.js +143 -0
  42. package/fesm/exceptions/base-app.exception.spec.js +153 -0
  43. package/fesm/exceptions/permission.exception.spec.js +78 -0
  44. package/fesm/filters/global-exception.filter.spec.js +436 -0
  45. package/fesm/guards/jwt-auth.guard.spec.js +71 -0
  46. package/fesm/guards/optional-jwt-auth.guard.spec.js +51 -0
  47. package/fesm/guards/permission.guard.spec.js +354 -0
  48. package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +103 -0
  49. package/fesm/interceptors/idempotency.interceptor.spec.js +112 -0
  50. package/fesm/interceptors/response-meta.interceptor.spec.js +85 -0
  51. package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +124 -0
  52. package/fesm/interceptors/slug.interceptor.spec.js +106 -0
  53. package/fesm/middlewares/logger.middleware.spec.js +459 -0
  54. package/fesm/modules/cache/cache.module.spec.js +44 -0
  55. package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +377 -0
  56. package/fesm/modules/utils/utils.service.spec.js +165 -0
  57. package/fesm/utils/date-time.util.spec.js +80 -0
  58. package/fesm/utils/html-sanitizer.util.spec.js +72 -0
  59. package/fesm/utils/permission-cache-key.util.spec.js +84 -0
  60. package/fesm/utils/query-helpers.util.spec.js +205 -0
  61. package/fesm/utils/request.util.spec.js +245 -0
  62. package/fesm/utils/string.util.spec.js +59 -0
  63. package/package.json +2 -2
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _datetimeutil = require("./date-time.util");
6
+ describe('date-time.util', ()=>{
7
+ describe('localToUtc', ()=>{
8
+ it('should convert a local date/time in a fixed-offset timezone to UTC', ()=>{
9
+ // Asia/Dhaka is UTC+6, no DST
10
+ const result = (0, _datetimeutil.localToUtc)('2026-01-15', '10:00', 'Asia/Dhaka');
11
+ expect(result.toISOString()).toBe('2026-01-15T04:00:00.000Z');
12
+ });
13
+ it('should convert local time in UTC timezone unchanged', ()=>{
14
+ const result = (0, _datetimeutil.localToUtc)('2026-06-01', '12:30', 'UTC');
15
+ expect(result.toISOString()).toBe('2026-06-01T12:30:00.000Z');
16
+ });
17
+ it('should handle a negative UTC offset timezone (America/New_York in winter, no DST)', ()=>{
18
+ // America/New_York is UTC-5 in January (standard time, no DST)
19
+ const result = (0, _datetimeutil.localToUtc)('2026-01-15', '09:00', 'America/New_York');
20
+ expect(result.toISOString()).toBe('2026-01-15T14:00:00.000Z');
21
+ });
22
+ it('should account for DST when converting a summer date in America/New_York (UTC-4)', ()=>{
23
+ const result = (0, _datetimeutil.localToUtc)('2026-07-15', '09:00', 'America/New_York');
24
+ expect(result.toISOString()).toBe('2026-07-15T13:00:00.000Z');
25
+ });
26
+ });
27
+ describe('utcToLocal', ()=>{
28
+ it('should convert a UTC Date to local date/time/display parts', ()=>{
29
+ const result = (0, _datetimeutil.utcToLocal)(new Date('2026-01-15T04:00:00.000Z'), 'Asia/Dhaka');
30
+ expect(result.date).toBe('2026-01-15');
31
+ expect(result.time).toBe('10:00');
32
+ expect(result.display).toEqual(expect.any(String));
33
+ });
34
+ it('should accept a UTC date string as input', ()=>{
35
+ const result = (0, _datetimeutil.utcToLocal)('2026-01-15T04:00:00.000Z', 'Asia/Dhaka');
36
+ expect(result.date).toBe('2026-01-15');
37
+ expect(result.time).toBe('10:00');
38
+ });
39
+ it('should normalize midnight (en-CA "24") to "00"', ()=>{
40
+ // 00:00 Dhaka time == 18:00 UTC the previous day
41
+ const result = (0, _datetimeutil.utcToLocal)('2026-01-14T18:00:00.000Z', 'Asia/Dhaka');
42
+ expect(result.date).toBe('2026-01-15');
43
+ expect(result.time).toBe('00:00');
44
+ });
45
+ });
46
+ describe('formatInTimezone', ()=>{
47
+ it('should format a date in the given timezone with default options', ()=>{
48
+ const result = (0, _datetimeutil.formatInTimezone)('2026-01-15T04:00:00.000Z', 'Asia/Dhaka');
49
+ expect(typeof result).toBe('string');
50
+ expect(result.length).toBeGreaterThan(0);
51
+ });
52
+ it('should respect custom Intl.DateTimeFormatOptions', ()=>{
53
+ const result = (0, _datetimeutil.formatInTimezone)('2026-01-15T04:00:00.000Z', 'UTC', {
54
+ year: 'numeric'
55
+ });
56
+ expect(result).toBe('2026');
57
+ });
58
+ it('should accept a Date object as input', ()=>{
59
+ const result = (0, _datetimeutil.formatInTimezone)(new Date('2026-01-15T04:00:00.000Z'), 'UTC', {
60
+ year: 'numeric'
61
+ });
62
+ expect(result).toBe('2026');
63
+ });
64
+ });
65
+ describe('getTodayInTimezone', ()=>{
66
+ it('should return a date string in YYYY-MM-DD format', ()=>{
67
+ const result = (0, _datetimeutil.getTodayInTimezone)('UTC');
68
+ expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
69
+ });
70
+ it('should match utcToLocal(now, timezone).date', ()=>{
71
+ const now = new Date();
72
+ const expected = (0, _datetimeutil.utcToLocal)(now, 'Asia/Dhaka').date;
73
+ expect((0, _datetimeutil.getTodayInTimezone)('Asia/Dhaka')).toBe(expected);
74
+ });
75
+ });
76
+ describe('round-trip', ()=>{
77
+ it('should round-trip localToUtc -> utcToLocal for a fixed-offset zone', ()=>{
78
+ const utc = (0, _datetimeutil.localToUtc)('2026-03-10', '08:15', 'Asia/Dhaka');
79
+ const back = (0, _datetimeutil.utcToLocal)(utc, 'Asia/Dhaka');
80
+ expect(back.date).toBe('2026-03-10');
81
+ expect(back.time).toBe('08:15');
82
+ });
83
+ });
84
+ });
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _htmlsanitizerutil = require("./html-sanitizer.util");
6
+ describe('html-sanitizer.util', ()=>{
7
+ describe('escapeHtml', ()=>{
8
+ it('should escape all special HTML characters', ()=>{
9
+ expect((0, _htmlsanitizerutil.escapeHtml)('<script>alert("xss")</script>')).toBe('&lt;script&gt;alert(&quot;xss&quot;)&lt;&#x2F;script&gt;');
10
+ });
11
+ it('should escape ampersands', ()=>{
12
+ expect((0, _htmlsanitizerutil.escapeHtml)('a & b')).toBe('a &amp; b');
13
+ });
14
+ it('should escape single quotes, backticks, and equals signs', ()=>{
15
+ expect((0, _htmlsanitizerutil.escapeHtml)(`a='b' c=\`d\``)).toBe('a&#x3D;&#x27;b&#x27; c&#x3D;&#x60;d&#x60;');
16
+ });
17
+ it('should return an empty string for empty input', ()=>{
18
+ expect((0, _htmlsanitizerutil.escapeHtml)('')).toBe('');
19
+ });
20
+ it('should return input unchanged when it has no special characters', ()=>{
21
+ expect((0, _htmlsanitizerutil.escapeHtml)('Hello World 123')).toBe('Hello World 123');
22
+ });
23
+ it('should return non-string input coerced via nullish fallback', ()=>{
24
+ expect((0, _htmlsanitizerutil.escapeHtml)(null)).toBe('');
25
+ expect((0, _htmlsanitizerutil.escapeHtml)(undefined)).toBe('');
26
+ });
27
+ it('should return non-string, truthy input unchanged (not throw)', ()=>{
28
+ // e.g. a number passed at runtime despite the TS signature
29
+ expect((0, _htmlsanitizerutil.escapeHtml)(42)).toBe(42);
30
+ });
31
+ });
32
+ describe('escapeHtmlVariables', ()=>{
33
+ it('should escape string values', ()=>{
34
+ expect((0, _htmlsanitizerutil.escapeHtmlVariables)({
35
+ name: '<b>bold</b>'
36
+ })).toEqual({
37
+ name: '&lt;b&gt;bold&lt;&#x2F;b&gt;'
38
+ });
39
+ });
40
+ it('should convert numbers and booleans to strings without escaping', ()=>{
41
+ expect((0, _htmlsanitizerutil.escapeHtmlVariables)({
42
+ count: 5,
43
+ active: true
44
+ })).toEqual({
45
+ count: '5',
46
+ active: 'true'
47
+ });
48
+ });
49
+ it('should stringify and escape object/array values', ()=>{
50
+ const result = (0, _htmlsanitizerutil.escapeHtmlVariables)({
51
+ meta: {
52
+ a: '<x>'
53
+ }
54
+ });
55
+ expect(result.meta).toBe((0, _htmlsanitizerutil.escapeHtml)(JSON.stringify({
56
+ a: '<x>'
57
+ })));
58
+ });
59
+ it('should convert null/undefined values to empty string', ()=>{
60
+ expect((0, _htmlsanitizerutil.escapeHtmlVariables)({
61
+ a: null,
62
+ b: undefined
63
+ })).toEqual({
64
+ a: '',
65
+ b: ''
66
+ });
67
+ });
68
+ it('should return an empty object for null/non-object input', ()=>{
69
+ expect((0, _htmlsanitizerutil.escapeHtmlVariables)(null)).toEqual({});
70
+ expect((0, _htmlsanitizerutil.escapeHtmlVariables)(undefined)).toEqual({});
71
+ });
72
+ it('should return an empty object for an empty input object', ()=>{
73
+ expect((0, _htmlsanitizerutil.escapeHtmlVariables)({})).toEqual({});
74
+ });
75
+ });
76
+ });
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _permissioncachekeyutil = require("./permission-cache-key.util");
6
+ describe('permission-cache-key.util', ()=>{
7
+ describe('buildPermissionCacheKey', ()=>{
8
+ it('should build a user-scoped key when no config is provided', ()=>{
9
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
10
+ userId: 'user-1'
11
+ });
12
+ expect(key).toBe(_permissioncachekeyutil.DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
13
+ });
14
+ it('should build a user-scoped key when company feature is disabled', ()=>{
15
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
16
+ userId: 'user-1',
17
+ companyId: 'company-1'
18
+ }, {
19
+ enableCompanyFeature: false
20
+ });
21
+ expect(key).toBe(_permissioncachekeyutil.DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
22
+ });
23
+ it('should build a user-scoped key when company feature enabled but no companyId given', ()=>{
24
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
25
+ userId: 'user-1'
26
+ }, {
27
+ enableCompanyFeature: true
28
+ });
29
+ expect(key).toBe(_permissioncachekeyutil.DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
30
+ });
31
+ it('should build a company-scoped key when company feature is enabled and companyId is present', ()=>{
32
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
33
+ userId: 'user-1',
34
+ companyId: 'company-1',
35
+ branchId: 'branch-1'
36
+ }, {
37
+ enableCompanyFeature: true
38
+ });
39
+ expect(key).toBe(_permissioncachekeyutil.DEFAULT_COMPANY_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1').replace('{companyId}', 'company-1').replace('{branchId}', 'branch-1'));
40
+ });
41
+ it('should default branchId placeholder to "null" when branchId is missing', ()=>{
42
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
43
+ userId: 'user-1',
44
+ companyId: 'company-1'
45
+ }, {
46
+ enableCompanyFeature: true
47
+ });
48
+ expect(key).toContain(':branch:null:');
49
+ });
50
+ it('should default branchId placeholder to "null" when branchId is explicitly null', ()=>{
51
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
52
+ userId: 'user-1',
53
+ companyId: 'company-1',
54
+ branchId: null
55
+ }, {
56
+ enableCompanyFeature: true
57
+ });
58
+ expect(key).toContain(':branch:null:');
59
+ });
60
+ it('should use a custom userPermissionKeyFormat when provided', ()=>{
61
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
62
+ userId: 'user-1'
63
+ }, {
64
+ userPermissionKeyFormat: 'custom:{userId}'
65
+ });
66
+ expect(key).toBe('custom:user-1');
67
+ });
68
+ it('should use a custom companyPermissionKeyFormat when provided', ()=>{
69
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
70
+ userId: 'user-1',
71
+ companyId: 'company-1'
72
+ }, {
73
+ enableCompanyFeature: true,
74
+ companyPermissionKeyFormat: 'custom:{companyId}:{userId}:{branchId}'
75
+ });
76
+ expect(key).toBe('custom:company-1:user-1:null');
77
+ });
78
+ it('should treat an empty-string companyId as no company scope', ()=>{
79
+ const key = (0, _permissioncachekeyutil.buildPermissionCacheKey)({
80
+ userId: 'user-1',
81
+ companyId: ''
82
+ }, {
83
+ enableCompanyFeature: true
84
+ });
85
+ expect(key).toBe(_permissioncachekeyutil.DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
86
+ });
87
+ });
88
+ });
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _common = require("@nestjs/common");
6
+ const _loggedusermock = require("@test-utils/mocks/logged-user.mock");
7
+ const _queryhelpersutil = require("./query-helpers.util");
8
+ function createMockQueryBuilder() {
9
+ return {
10
+ andWhere: jest.fn().mockReturnThis()
11
+ };
12
+ }
13
+ describe('query-helpers.util', ()=>{
14
+ describe('applyCompanyFilter', ()=>{
15
+ it('should apply the company filter when feature enabled and user has companyId', ()=>{
16
+ const query = createMockQueryBuilder();
17
+ const user = (0, _loggedusermock.buildMockUser)({
18
+ companyId: 'company-1'
19
+ });
20
+ (0, _queryhelpersutil.applyCompanyFilter)(query, {
21
+ isCompanyFeatureEnabled: true,
22
+ entityAlias: 'file'
23
+ }, user);
24
+ expect(query.andWhere).toHaveBeenCalledWith('file.companyId = :companyId', {
25
+ companyId: 'company-1'
26
+ });
27
+ });
28
+ it('should use a custom column name when provided', ()=>{
29
+ const query = createMockQueryBuilder();
30
+ const user = (0, _loggedusermock.buildMockUser)({
31
+ companyId: 'company-1'
32
+ });
33
+ (0, _queryhelpersutil.applyCompanyFilter)(query, {
34
+ isCompanyFeatureEnabled: true,
35
+ entityAlias: 'file',
36
+ columnName: 'company_id'
37
+ }, user);
38
+ expect(query.andWhere).toHaveBeenCalledWith('file.company_id = :companyId', {
39
+ companyId: 'company-1'
40
+ });
41
+ });
42
+ it('should not apply filter when feature disabled', ()=>{
43
+ const query = createMockQueryBuilder();
44
+ const user = (0, _loggedusermock.buildMockUser)({
45
+ companyId: 'company-1'
46
+ });
47
+ (0, _queryhelpersutil.applyCompanyFilter)(query, {
48
+ isCompanyFeatureEnabled: false,
49
+ entityAlias: 'file'
50
+ }, user);
51
+ expect(query.andWhere).not.toHaveBeenCalled();
52
+ });
53
+ it('should not apply filter when user is null', ()=>{
54
+ const query = createMockQueryBuilder();
55
+ (0, _queryhelpersutil.applyCompanyFilter)(query, {
56
+ isCompanyFeatureEnabled: true,
57
+ entityAlias: 'file'
58
+ }, null);
59
+ expect(query.andWhere).not.toHaveBeenCalled();
60
+ });
61
+ it('should not apply filter when user has no companyId', ()=>{
62
+ const query = createMockQueryBuilder();
63
+ const user = (0, _loggedusermock.buildMockUser)({
64
+ companyId: undefined
65
+ });
66
+ (0, _queryhelpersutil.applyCompanyFilter)(query, {
67
+ isCompanyFeatureEnabled: true,
68
+ entityAlias: 'file'
69
+ }, user);
70
+ expect(query.andWhere).not.toHaveBeenCalled();
71
+ });
72
+ it('should return the same query builder instance', ()=>{
73
+ const query = createMockQueryBuilder();
74
+ const result = (0, _queryhelpersutil.applyCompanyFilter)(query, {
75
+ isCompanyFeatureEnabled: false,
76
+ entityAlias: 'file'
77
+ }, null);
78
+ expect(result).toBe(query);
79
+ });
80
+ });
81
+ describe('buildCompanyWhereCondition', ()=>{
82
+ it('should add companyId when feature enabled and user has one', ()=>{
83
+ const user = (0, _loggedusermock.buildMockUser)({
84
+ companyId: 'company-1'
85
+ });
86
+ const result = (0, _queryhelpersutil.buildCompanyWhereCondition)({
87
+ id: '1'
88
+ }, true, user);
89
+ expect(result).toEqual({
90
+ id: '1',
91
+ companyId: 'company-1'
92
+ });
93
+ });
94
+ it('should return base where unchanged when feature disabled', ()=>{
95
+ const user = (0, _loggedusermock.buildMockUser)({
96
+ companyId: 'company-1'
97
+ });
98
+ const result = (0, _queryhelpersutil.buildCompanyWhereCondition)({
99
+ id: '1'
100
+ }, false, user);
101
+ expect(result).toEqual({
102
+ id: '1'
103
+ });
104
+ });
105
+ it('should return base where unchanged when user is null', ()=>{
106
+ const result = (0, _queryhelpersutil.buildCompanyWhereCondition)({
107
+ id: '1'
108
+ }, true, null);
109
+ expect(result).toEqual({
110
+ id: '1'
111
+ });
112
+ });
113
+ it('should return base where unchanged when user has no companyId', ()=>{
114
+ const user = (0, _loggedusermock.buildMockUser)({
115
+ companyId: undefined
116
+ });
117
+ const result = (0, _queryhelpersutil.buildCompanyWhereCondition)({
118
+ id: '1'
119
+ }, true, user);
120
+ expect(result).toEqual({
121
+ id: '1'
122
+ });
123
+ });
124
+ });
125
+ describe('hasCompanyId', ()=>{
126
+ it('should return true for an object with a companyId property', ()=>{
127
+ expect((0, _queryhelpersutil.hasCompanyId)({
128
+ companyId: 'company-1'
129
+ })).toBe(true);
130
+ });
131
+ it('should return true even when companyId is explicitly undefined but present', ()=>{
132
+ expect((0, _queryhelpersutil.hasCompanyId)({
133
+ companyId: undefined
134
+ })).toBe(true);
135
+ });
136
+ it('should return false for an object without a companyId property', ()=>{
137
+ expect((0, _queryhelpersutil.hasCompanyId)({
138
+ id: '1'
139
+ })).toBe(false);
140
+ });
141
+ it('should return false for null', ()=>{
142
+ expect((0, _queryhelpersutil.hasCompanyId)(null)).toBe(false);
143
+ });
144
+ it('should return false for non-object values', ()=>{
145
+ expect((0, _queryhelpersutil.hasCompanyId)('string')).toBe(false);
146
+ expect((0, _queryhelpersutil.hasCompanyId)(42)).toBe(false);
147
+ });
148
+ });
149
+ describe('validateCompanyOwnership', ()=>{
150
+ it('should not throw when feature disabled', ()=>{
151
+ const user = (0, _loggedusermock.buildMockUser)({
152
+ companyId: 'company-1'
153
+ });
154
+ expect(()=>(0, _queryhelpersutil.validateCompanyOwnership)({
155
+ companyId: 'company-2'
156
+ }, user, false, 'Entity')).not.toThrow();
157
+ });
158
+ it('should not throw when user is null', ()=>{
159
+ expect(()=>(0, _queryhelpersutil.validateCompanyOwnership)({
160
+ companyId: 'company-2'
161
+ }, null, true, 'Entity')).not.toThrow();
162
+ });
163
+ it('should not throw when entity has no companyId', ()=>{
164
+ const user = (0, _loggedusermock.buildMockUser)({
165
+ companyId: 'company-1'
166
+ });
167
+ expect(()=>(0, _queryhelpersutil.validateCompanyOwnership)({
168
+ id: '1'
169
+ }, user, true, 'Entity')).not.toThrow();
170
+ });
171
+ it('should not throw when entity companyId is falsy', ()=>{
172
+ const user = (0, _loggedusermock.buildMockUser)({
173
+ companyId: 'company-1'
174
+ });
175
+ expect(()=>(0, _queryhelpersutil.validateCompanyOwnership)({
176
+ companyId: null
177
+ }, user, true, 'Entity')).not.toThrow();
178
+ });
179
+ it('should not throw when entity companyId matches user companyId', ()=>{
180
+ const user = (0, _loggedusermock.buildMockUser)({
181
+ companyId: 'company-1'
182
+ });
183
+ expect(()=>(0, _queryhelpersutil.validateCompanyOwnership)({
184
+ companyId: 'company-1'
185
+ }, user, true, 'Entity')).not.toThrow();
186
+ });
187
+ it('should throw BadRequestException when entity belongs to a different company', ()=>{
188
+ const user = (0, _loggedusermock.buildMockUser)({
189
+ companyId: 'company-1'
190
+ });
191
+ expect(()=>(0, _queryhelpersutil.validateCompanyOwnership)({
192
+ companyId: 'company-2'
193
+ }, user, true, 'Email configuration')).toThrow(_common.BadRequestException);
194
+ });
195
+ it('should include the entity name in the thrown exception message', ()=>{
196
+ const user = (0, _loggedusermock.buildMockUser)({
197
+ companyId: 'company-1'
198
+ });
199
+ try {
200
+ (0, _queryhelpersutil.validateCompanyOwnership)({
201
+ companyId: 'company-2'
202
+ }, user, true, 'Email configuration');
203
+ fail('expected to throw');
204
+ } catch (error) {
205
+ expect(error.getResponse().message).toContain('Email configuration');
206
+ }
207
+ });
208
+ });
209
+ });