@flusys/nestjs-shared 7.1.0 → 8.0.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.
- package/classes/api-controller.class.d.ts +4 -4
- package/classes/api-service.class.d.ts +7 -7
- package/classes/disabled-event-bus.class.d.ts +1 -1
- package/classes/domain-event.publisher.d.ts +1 -1
- package/classes/event-bus.registry.d.ts +1 -1
- package/classes/event-transports/event-transport.registry.d.ts +1 -1
- package/classes/event-transports/index.d.ts +2 -2
- package/classes/event-transports/kafka-transport.optional.d.ts +1 -1
- package/classes/event-transports/memory-transport.d.ts +1 -1
- package/classes/event-transports/rabbitmq-transport.optional.d.ts +1 -1
- package/classes/hybrid-event-bus.class.d.ts +1 -1
- package/classes/index.d.ts +11 -11
- package/constants/index.d.ts +2 -2
- package/decorators/api-response.decorator.d.ts +1 -1
- package/decorators/current-user.decorator.d.ts +1 -1
- package/decorators/index.d.ts +6 -6
- package/decorators/require-permission.decorator.d.ts +1 -1
- package/dtos/filter-and-pagination.dto.d.ts +1 -1
- package/dtos/index.d.ts +6 -6
- package/entities/index.d.ts +3 -3
- package/enums/index.d.ts +3 -3
- package/exceptions/index.d.ts +2 -2
- package/fesm/470.js +2713 -0
- package/fesm/509.js +793 -0
- package/fesm/667.js +606 -0
- package/fesm/67.js +808 -0
- package/fesm/720.js +914 -0
- package/{cjs/classes/event-transports/rabbitmq-transport.optional.js → fesm/chunks/123.js} +78 -55
- package/fesm/{classes/event-transports/kafka-transport.optional.js → chunks/37.js} +74 -41
- package/fesm/classes/index.js +62 -12
- package/fesm/constants/index.js +445 -17
- package/fesm/decorators/index.js +341 -6
- package/fesm/dtos/index.js +23 -6
- package/fesm/entities/index.js +336 -3
- package/fesm/enums/index.js +57 -3
- package/fesm/exceptions/index.js +280 -2
- package/fesm/guards/index.js +1003 -3
- package/fesm/index.js +1524 -14
- package/fesm/interceptors/index.js +924 -5
- package/fesm/interfaces/index.js +57 -11
- package/fesm/middlewares/index.js +33 -1
- package/fesm/modules/index.js +432 -9
- package/fesm/runtime.js +134 -0
- package/fesm/utils/index.js +1045 -8
- package/filters/global-exception.filter.d.ts +1 -1
- package/filters/index.d.ts +1 -1
- package/guards/index.d.ts +3 -3
- package/guards/jwt-auth.guard.d.ts +1 -1
- package/guards/optional-jwt-auth.guard.d.ts +1 -1
- package/guards/permission.guard.d.ts +2 -2
- package/index.d.ts +14 -14
- package/interceptors/delete-empty-id-from-body.interceptor.d.ts +1 -1
- package/interceptors/idempotency.interceptor.d.ts +2 -2
- package/interceptors/index.d.ts +5 -5
- package/interceptors/response-meta.interceptor.d.ts +1 -1
- package/interceptors/set-user-field-on-body.interceptor.d.ts +1 -1
- package/interceptors/slug.interceptor.d.ts +2 -2
- package/interfaces/ai-assistant-adapter.interface.d.ts +1 -1
- package/interfaces/api.interface.d.ts +2 -2
- package/interfaces/datasource.interface.d.ts +1 -1
- package/interfaces/event-manager-adapter.interface.d.ts +2 -2
- package/interfaces/index.d.ts +11 -11
- package/interfaces/module-config.interface.d.ts +1 -1
- package/interfaces/notification-adapter.interface.d.ts +1 -1
- package/middlewares/index.d.ts +1 -1
- package/middlewares/logger.middleware.d.ts +2 -2
- package/modules/cache/cache.module.d.ts +1 -1
- package/modules/datasource/index.d.ts +1 -1
- package/modules/datasource/multi-tenant-datasource.service.d.ts +4 -4
- package/modules/event-bus/event-bus.explorer.d.ts +2 -2
- package/modules/event-bus/event-bus.module.d.ts +2 -2
- package/modules/event-bus/module-events.provider.d.ts +1 -1
- package/modules/index.d.ts +9 -9
- package/modules/shared-permission-cache/shared-permission-cache.service.d.ts +3 -3
- package/modules/utils/utils.service.d.ts +1 -1
- package/package.json +30 -44
- package/utils/index.d.ts +8 -8
- package/utils/permission-cache-key.util.d.ts +1 -1
- package/utils/query-helpers.util.d.ts +2 -2
- package/utils/request.util.d.ts +1 -1
- package/cjs/classes/api-controller.class.js +0 -728
- package/cjs/classes/api-controller.class.spec.js +0 -684
- package/cjs/classes/api-service.class.js +0 -631
- package/cjs/classes/api-service.class.spec.js +0 -1137
- package/cjs/classes/disabled-event-bus.class.js +0 -15
- package/cjs/classes/disabled-event-bus.class.spec.js +0 -40
- package/cjs/classes/domain-event.publisher.js +0 -76
- package/cjs/classes/domain-event.publisher.spec.js +0 -234
- package/cjs/classes/event-bus.registry.js +0 -68
- package/cjs/classes/event-bus.registry.spec.js +0 -141
- package/cjs/classes/event-transports/event-transport.registry.js +0 -41
- package/cjs/classes/event-transports/event-transport.registry.spec.js +0 -31
- package/cjs/classes/event-transports/index.js +0 -23
- package/cjs/classes/event-transports/kafka-transport.optional.js +0 -151
- package/cjs/classes/event-transports/kafka-transport.optional.spec.js +0 -198
- package/cjs/classes/event-transports/memory-transport.js +0 -53
- package/cjs/classes/event-transports/memory-transport.spec.js +0 -57
- package/cjs/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -255
- package/cjs/classes/handle-custome-db.js +0 -37
- package/cjs/classes/handle-custome-db.spec.js +0 -82
- package/cjs/classes/hybrid-cache.class.js +0 -96
- package/cjs/classes/hybrid-cache.class.spec.js +0 -168
- package/cjs/classes/hybrid-event-bus.class.js +0 -281
- package/cjs/classes/hybrid-event-bus.class.spec.js +0 -218
- package/cjs/classes/index.js +0 -28
- package/cjs/classes/winston.logger.class.js +0 -274
- package/cjs/classes/winston.logger.class.spec.js +0 -330
- package/cjs/constants/index.js +0 -88
- package/cjs/constants/message-keys.js +0 -68
- package/cjs/constants/permissions.js +0 -307
- package/cjs/decorators/api-response.decorator.js +0 -52
- package/cjs/decorators/api-response.decorator.spec.js +0 -126
- package/cjs/decorators/current-user.decorator.js +0 -16
- package/cjs/decorators/current-user.decorator.spec.js +0 -74
- package/cjs/decorators/index.js +0 -23
- package/cjs/decorators/log-action.decorator.js +0 -149
- package/cjs/decorators/log-action.decorator.spec.js +0 -353
- package/cjs/decorators/on-domain-event.decorator.js +0 -13
- package/cjs/decorators/on-domain-event.decorator.spec.js +0 -50
- package/cjs/decorators/public.decorator.js +0 -13
- package/cjs/decorators/public.decorator.spec.js +0 -50
- package/cjs/decorators/require-permission.decorator.js +0 -32
- package/cjs/decorators/require-permission.decorator.spec.js +0 -147
- package/cjs/dtos/delete.dto.js +0 -92
- package/cjs/dtos/filter-and-pagination.dto.js +0 -164
- package/cjs/dtos/get-by-ids.dto.js +0 -65
- package/cjs/dtos/identity-response.dto.js +0 -108
- package/cjs/dtos/index.js +0 -23
- package/cjs/dtos/pagination.dto.js +0 -67
- package/cjs/dtos/response-payload.dto.js +0 -369
- package/cjs/entities/identity.js +0 -100
- package/cjs/entities/index.js +0 -20
- package/cjs/entities/raw-type.js +0 -4
- package/cjs/entities/user-root.js +0 -189
- package/cjs/enums/index.js +0 -20
- package/cjs/enums/notification-type.enum.js +0 -17
- package/cjs/enums/participant-status.enum.js +0 -17
- package/cjs/enums/recurrence-type.enum.js +0 -18
- package/cjs/exceptions/base-app.exception.js +0 -151
- package/cjs/exceptions/base-app.exception.spec.js +0 -157
- package/cjs/exceptions/index.js +0 -19
- package/cjs/exceptions/permission.exception.js +0 -56
- package/cjs/exceptions/permission.exception.spec.js +0 -82
- package/cjs/filters/global-exception.filter.js +0 -279
- package/cjs/filters/global-exception.filter.spec.js +0 -440
- package/cjs/filters/index.js +0 -18
- package/cjs/guards/index.js +0 -20
- package/cjs/guards/jwt-auth.guard.js +0 -75
- package/cjs/guards/jwt-auth.guard.spec.js +0 -75
- package/cjs/guards/optional-jwt-auth.guard.js +0 -67
- package/cjs/guards/optional-jwt-auth.guard.spec.js +0 -55
- package/cjs/guards/permission.guard.js +0 -179
- package/cjs/guards/permission.guard.spec.js +0 -373
- package/cjs/index.js +0 -31
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.js +0 -40
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -107
- package/cjs/interceptors/idempotency.interceptor.js +0 -96
- package/cjs/interceptors/idempotency.interceptor.spec.js +0 -116
- package/cjs/interceptors/index.js +0 -22
- package/cjs/interceptors/response-meta.interceptor.js +0 -47
- package/cjs/interceptors/response-meta.interceptor.spec.js +0 -89
- package/cjs/interceptors/set-user-field-on-body.interceptor.js +0 -60
- package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +0 -128
- package/cjs/interceptors/slug.interceptor.js +0 -75
- package/cjs/interceptors/slug.interceptor.spec.js +0 -110
- package/cjs/interfaces/ai-assistant-adapter.interface.js +0 -24
- package/cjs/interfaces/api.interface.js +0 -4
- package/cjs/interfaces/datasource.interface.js +0 -4
- package/cjs/interfaces/event-bus.interface.js +0 -4
- package/cjs/interfaces/event-manager-adapter.interface.js +0 -11
- package/cjs/interfaces/identity.interface.js +0 -4
- package/cjs/interfaces/index.js +0 -28
- package/cjs/interfaces/logged-user-info.interface.js +0 -6
- package/cjs/interfaces/module-config.interface.js +0 -4
- package/cjs/interfaces/notification-adapter.interface.js +0 -11
- package/cjs/interfaces/permission-sync-adapter.interface.js +0 -11
- package/cjs/interfaces/permission.interface.js +0 -4
- package/cjs/middlewares/index.js +0 -18
- package/cjs/middlewares/logger.middleware.js +0 -264
- package/cjs/middlewares/logger.middleware.spec.js +0 -462
- package/cjs/modules/cache/cache.module.js +0 -39
- package/cjs/modules/cache/cache.module.spec.js +0 -48
- package/cjs/modules/datasource/index.js +0 -18
- package/cjs/modules/datasource/multi-tenant-datasource.service.js +0 -222
- package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +0 -381
- package/cjs/modules/event-bus/event-bus.explorer.js +0 -124
- package/cjs/modules/event-bus/event-bus.explorer.spec.js +0 -189
- package/cjs/modules/event-bus/event-bus.integration.spec.js +0 -253
- package/cjs/modules/event-bus/event-bus.module.js +0 -94
- package/cjs/modules/event-bus/event-bus.module.spec.js +0 -149
- package/cjs/modules/event-bus/module-events.provider.js +0 -39
- package/cjs/modules/event-bus/module-events.provider.spec.js +0 -56
- package/cjs/modules/index.js +0 -26
- package/cjs/modules/shared-permission-cache/shared-permission-cache.module.js +0 -32
- package/cjs/modules/shared-permission-cache/shared-permission-cache.service.js +0 -101
- package/cjs/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -165
- package/cjs/modules/utils/utils.module.js +0 -32
- package/cjs/modules/utils/utils.service.js +0 -76
- package/cjs/modules/utils/utils.service.spec.js +0 -169
- package/cjs/utils/date-time.util.js +0 -94
- package/cjs/utils/date-time.util.spec.js +0 -84
- package/cjs/utils/event-pattern.util.js +0 -103
- package/cjs/utils/event-pattern.util.spec.js +0 -251
- package/cjs/utils/html-sanitizer.util.js +0 -64
- package/cjs/utils/html-sanitizer.util.spec.js +0 -76
- package/cjs/utils/index.js +0 -25
- package/cjs/utils/optional-module.util.js +0 -35
- package/cjs/utils/optional-module.util.spec.js +0 -14
- package/cjs/utils/permission-cache-key.util.js +0 -31
- package/cjs/utils/permission-cache-key.util.spec.js +0 -88
- package/cjs/utils/query-helpers.util.js +0 -60
- package/cjs/utils/query-helpers.util.spec.js +0 -209
- package/cjs/utils/request.util.js +0 -96
- package/cjs/utils/request.util.spec.js +0 -249
- package/cjs/utils/string.util.js +0 -63
- package/cjs/utils/string.util.spec.js +0 -63
- package/fesm/classes/api-controller.class.js +0 -718
- package/fesm/classes/api-controller.class.spec.js +0 -680
- package/fesm/classes/api-service.class.js +0 -621
- package/fesm/classes/api-service.class.spec.js +0 -1133
- package/fesm/classes/disabled-event-bus.class.js +0 -11
- package/fesm/classes/disabled-event-bus.class.spec.js +0 -36
- package/fesm/classes/domain-event.publisher.js +0 -63
- package/fesm/classes/domain-event.publisher.spec.js +0 -230
- package/fesm/classes/event-bus.registry.js +0 -64
- package/fesm/classes/event-bus.registry.spec.js +0 -137
- package/fesm/classes/event-transports/event-transport.registry.js +0 -35
- package/fesm/classes/event-transports/event-transport.registry.spec.js +0 -27
- package/fesm/classes/event-transports/index.js +0 -6
- package/fesm/classes/event-transports/kafka-transport.optional.spec.js +0 -194
- package/fesm/classes/event-transports/memory-transport.js +0 -49
- package/fesm/classes/event-transports/memory-transport.spec.js +0 -53
- package/fesm/classes/event-transports/rabbitmq-transport.optional.js +0 -174
- package/fesm/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -251
- package/fesm/classes/handle-custome-db.js +0 -19
- package/fesm/classes/handle-custome-db.spec.js +0 -78
- package/fesm/classes/hybrid-cache.class.js +0 -81
- package/fesm/classes/hybrid-cache.class.spec.js +0 -164
- package/fesm/classes/hybrid-event-bus.class.js +0 -237
- package/fesm/classes/hybrid-event-bus.class.spec.js +0 -214
- package/fesm/classes/winston.logger.class.js +0 -228
- package/fesm/classes/winston.logger.class.spec.js +0 -327
- package/fesm/constants/message-keys.js +0 -49
- package/fesm/constants/permissions.js +0 -215
- package/fesm/decorators/api-response.decorator.js +0 -42
- package/fesm/decorators/api-response.decorator.spec.js +0 -122
- package/fesm/decorators/current-user.decorator.js +0 -18
- package/fesm/decorators/current-user.decorator.spec.js +0 -73
- package/fesm/decorators/log-action.decorator.js +0 -139
- package/fesm/decorators/log-action.decorator.spec.js +0 -349
- package/fesm/decorators/on-domain-event.decorator.js +0 -10
- package/fesm/decorators/on-domain-event.decorator.spec.js +0 -46
- package/fesm/decorators/public.decorator.js +0 -20
- package/fesm/decorators/public.decorator.spec.js +0 -46
- package/fesm/decorators/require-permission.decorator.js +0 -27
- package/fesm/decorators/require-permission.decorator.spec.js +0 -143
- package/fesm/dtos/delete.dto.js +0 -82
- package/fesm/dtos/filter-and-pagination.dto.js +0 -148
- package/fesm/dtos/get-by-ids.dto.js +0 -55
- package/fesm/dtos/identity-response.dto.js +0 -98
- package/fesm/dtos/pagination.dto.js +0 -57
- package/fesm/dtos/response-payload.dto.js +0 -336
- package/fesm/entities/identity.js +0 -90
- package/fesm/entities/raw-type.js +0 -1
- package/fesm/entities/user-root.js +0 -179
- package/fesm/enums/notification-type.enum.js +0 -7
- package/fesm/enums/participant-status.enum.js +0 -7
- package/fesm/enums/recurrence-type.enum.js +0 -8
- package/fesm/exceptions/base-app.exception.js +0 -115
- package/fesm/exceptions/base-app.exception.spec.js +0 -153
- package/fesm/exceptions/permission.exception.js +0 -35
- package/fesm/exceptions/permission.exception.spec.js +0 -78
- package/fesm/filters/global-exception.filter.js +0 -269
- package/fesm/filters/global-exception.filter.spec.js +0 -436
- package/fesm/filters/index.js +0 -1
- package/fesm/guards/jwt-auth.guard.js +0 -65
- package/fesm/guards/jwt-auth.guard.spec.js +0 -71
- package/fesm/guards/optional-jwt-auth.guard.js +0 -57
- package/fesm/guards/optional-jwt-auth.guard.spec.js +0 -51
- package/fesm/guards/permission.guard.js +0 -169
- package/fesm/guards/permission.guard.spec.js +0 -369
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.js +0 -30
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -103
- package/fesm/interceptors/idempotency.interceptor.js +0 -86
- package/fesm/interceptors/idempotency.interceptor.spec.js +0 -112
- package/fesm/interceptors/response-meta.interceptor.js +0 -37
- package/fesm/interceptors/response-meta.interceptor.spec.js +0 -85
- package/fesm/interceptors/set-user-field-on-body.interceptor.js +0 -39
- package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +0 -124
- package/fesm/interceptors/slug.interceptor.js +0 -65
- package/fesm/interceptors/slug.interceptor.spec.js +0 -106
- package/fesm/interfaces/ai-assistant-adapter.interface.js +0 -3
- package/fesm/interfaces/api.interface.js +0 -1
- package/fesm/interfaces/datasource.interface.js +0 -1
- package/fesm/interfaces/event-bus.interface.js +0 -1
- package/fesm/interfaces/event-manager-adapter.interface.js +0 -1
- package/fesm/interfaces/identity.interface.js +0 -1
- package/fesm/interfaces/logged-user-info.interface.js +0 -3
- package/fesm/interfaces/module-config.interface.js +0 -5
- package/fesm/interfaces/notification-adapter.interface.js +0 -1
- package/fesm/interfaces/permission-sync-adapter.interface.js +0 -1
- package/fesm/interfaces/permission.interface.js +0 -1
- package/fesm/middlewares/logger.middleware.js +0 -235
- package/fesm/middlewares/logger.middleware.spec.js +0 -459
- package/fesm/modules/cache/cache.module.js +0 -29
- package/fesm/modules/cache/cache.module.spec.js +0 -44
- package/fesm/modules/datasource/index.js +0 -1
- package/fesm/modules/datasource/multi-tenant-datasource.service.js +0 -212
- package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +0 -377
- package/fesm/modules/event-bus/event-bus.explorer.js +0 -114
- package/fesm/modules/event-bus/event-bus.explorer.spec.js +0 -185
- package/fesm/modules/event-bus/event-bus.integration.spec.js +0 -249
- package/fesm/modules/event-bus/event-bus.module.js +0 -84
- package/fesm/modules/event-bus/event-bus.module.spec.js +0 -145
- package/fesm/modules/event-bus/module-events.provider.js +0 -26
- package/fesm/modules/event-bus/module-events.provider.spec.js +0 -52
- package/fesm/modules/shared-permission-cache/shared-permission-cache.module.js +0 -22
- package/fesm/modules/shared-permission-cache/shared-permission-cache.service.js +0 -91
- package/fesm/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -161
- package/fesm/modules/utils/utils.module.js +0 -22
- package/fesm/modules/utils/utils.service.js +0 -66
- package/fesm/modules/utils/utils.service.spec.js +0 -165
- package/fesm/utils/date-time.util.js +0 -70
- package/fesm/utils/date-time.util.spec.js +0 -80
- package/fesm/utils/event-pattern.util.js +0 -91
- package/fesm/utils/event-pattern.util.spec.js +0 -247
- package/fesm/utils/html-sanitizer.util.js +0 -69
- package/fesm/utils/html-sanitizer.util.spec.js +0 -72
- package/fesm/utils/optional-module.util.js +0 -25
- package/fesm/utils/optional-module.util.spec.js +0 -10
- package/fesm/utils/permission-cache-key.util.js +0 -10
- package/fesm/utils/permission-cache-key.util.spec.js +0 -84
- package/fesm/utils/query-helpers.util.js +0 -85
- package/fesm/utils/query-helpers.util.spec.js +0 -205
- package/fesm/utils/request.util.js +0 -81
- package/fesm/utils/request.util.spec.js +0 -245
- package/fesm/utils/string.util.js +0 -71
- package/fesm/utils/string.util.spec.js +0 -59
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTML Sanitizer Utilities
|
|
3
|
-
*
|
|
4
|
-
* Provides functions for escaping HTML content to prevent XSS attacks.
|
|
5
|
-
* Use these utilities when interpolating user-provided variables into HTML content.
|
|
6
|
-
*/ /**
|
|
7
|
-
* HTML entity mapping for escaping special characters
|
|
8
|
-
*/ const HTML_ESCAPE_MAP = {
|
|
9
|
-
'&': '&',
|
|
10
|
-
'<': '<',
|
|
11
|
-
'>': '>',
|
|
12
|
-
'"': '"',
|
|
13
|
-
"'": ''',
|
|
14
|
-
'/': '/',
|
|
15
|
-
'`': '`',
|
|
16
|
-
'=': '='
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Regex pattern matching characters that need HTML escaping
|
|
20
|
-
*/ const HTML_ESCAPE_REGEX = /[&<>"'`=/]/g;
|
|
21
|
-
/**
|
|
22
|
-
* Escapes HTML special characters to prevent XSS attacks.
|
|
23
|
-
*
|
|
24
|
-
* @param str - The string to escape
|
|
25
|
-
* @returns The escaped string safe for HTML insertion
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* escapeHtml('<script>alert("xss")</script>')
|
|
30
|
-
* // Returns: '<script>alert("xss")</script>'
|
|
31
|
-
* ```
|
|
32
|
-
*/ export function escapeHtml(str) {
|
|
33
|
-
if (!str || typeof str !== 'string') {
|
|
34
|
-
return str ?? '';
|
|
35
|
-
}
|
|
36
|
-
return str.replace(HTML_ESCAPE_REGEX, (char)=>HTML_ESCAPE_MAP[char]);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Escapes all string values in a variables object for safe HTML interpolation.
|
|
40
|
-
* Non-string values are converted to strings and escaped.
|
|
41
|
-
*
|
|
42
|
-
* @param variables - Object containing template variables
|
|
43
|
-
* @returns New object with all string values HTML-escaped
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* escapeHtmlVariables({ name: '<script>xss</script>', count: 5 })
|
|
48
|
-
* // Returns: { name: '<script>xss</script>', count: '5' }
|
|
49
|
-
* ```
|
|
50
|
-
*/ export function escapeHtmlVariables(variables) {
|
|
51
|
-
if (!variables || typeof variables !== 'object') {
|
|
52
|
-
return {};
|
|
53
|
-
}
|
|
54
|
-
const escaped = {};
|
|
55
|
-
for (const [key, value] of Object.entries(variables)){
|
|
56
|
-
if (value === null || value === undefined) {
|
|
57
|
-
escaped[key] = '';
|
|
58
|
-
} else if (typeof value === 'string') {
|
|
59
|
-
escaped[key] = escapeHtml(value);
|
|
60
|
-
} else if (typeof value === 'object') {
|
|
61
|
-
// For objects/arrays, stringify and escape
|
|
62
|
-
escaped[key] = escapeHtml(JSON.stringify(value));
|
|
63
|
-
} else {
|
|
64
|
-
// For numbers, booleans, etc., convert to string (no escaping needed)
|
|
65
|
-
escaped[key] = String(value);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return escaped;
|
|
69
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { escapeHtml, escapeHtmlVariables } from './html-sanitizer.util';
|
|
2
|
-
describe('html-sanitizer.util', ()=>{
|
|
3
|
-
describe('escapeHtml', ()=>{
|
|
4
|
-
it('should escape all special HTML characters', ()=>{
|
|
5
|
-
expect(escapeHtml('<script>alert("xss")</script>')).toBe('<script>alert("xss")</script>');
|
|
6
|
-
});
|
|
7
|
-
it('should escape ampersands', ()=>{
|
|
8
|
-
expect(escapeHtml('a & b')).toBe('a & b');
|
|
9
|
-
});
|
|
10
|
-
it('should escape single quotes, backticks, and equals signs', ()=>{
|
|
11
|
-
expect(escapeHtml(`a='b' c=\`d\``)).toBe('a='b' c=`d`');
|
|
12
|
-
});
|
|
13
|
-
it('should return an empty string for empty input', ()=>{
|
|
14
|
-
expect(escapeHtml('')).toBe('');
|
|
15
|
-
});
|
|
16
|
-
it('should return input unchanged when it has no special characters', ()=>{
|
|
17
|
-
expect(escapeHtml('Hello World 123')).toBe('Hello World 123');
|
|
18
|
-
});
|
|
19
|
-
it('should return non-string input coerced via nullish fallback', ()=>{
|
|
20
|
-
expect(escapeHtml(null)).toBe('');
|
|
21
|
-
expect(escapeHtml(undefined)).toBe('');
|
|
22
|
-
});
|
|
23
|
-
it('should return non-string, truthy input unchanged (not throw)', ()=>{
|
|
24
|
-
// e.g. a number passed at runtime despite the TS signature
|
|
25
|
-
expect(escapeHtml(42)).toBe(42);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
describe('escapeHtmlVariables', ()=>{
|
|
29
|
-
it('should escape string values', ()=>{
|
|
30
|
-
expect(escapeHtmlVariables({
|
|
31
|
-
name: '<b>bold</b>'
|
|
32
|
-
})).toEqual({
|
|
33
|
-
name: '<b>bold</b>'
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
it('should convert numbers and booleans to strings without escaping', ()=>{
|
|
37
|
-
expect(escapeHtmlVariables({
|
|
38
|
-
count: 5,
|
|
39
|
-
active: true
|
|
40
|
-
})).toEqual({
|
|
41
|
-
count: '5',
|
|
42
|
-
active: 'true'
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
it('should stringify and escape object/array values', ()=>{
|
|
46
|
-
const result = escapeHtmlVariables({
|
|
47
|
-
meta: {
|
|
48
|
-
a: '<x>'
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
expect(result.meta).toBe(escapeHtml(JSON.stringify({
|
|
52
|
-
a: '<x>'
|
|
53
|
-
})));
|
|
54
|
-
});
|
|
55
|
-
it('should convert null/undefined values to empty string', ()=>{
|
|
56
|
-
expect(escapeHtmlVariables({
|
|
57
|
-
a: null,
|
|
58
|
-
b: undefined
|
|
59
|
-
})).toEqual({
|
|
60
|
-
a: '',
|
|
61
|
-
b: ''
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
it('should return an empty object for null/non-object input', ()=>{
|
|
65
|
-
expect(escapeHtmlVariables(null)).toEqual({});
|
|
66
|
-
expect(escapeHtmlVariables(undefined)).toEqual({});
|
|
67
|
-
});
|
|
68
|
-
it('should return an empty object for an empty input object', ()=>{
|
|
69
|
-
expect(escapeHtmlVariables({})).toEqual({});
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Loads an optional peer dependency at runtime.
|
|
3
|
-
*
|
|
4
|
-
* The specifier has to stay invisible to the bundler. A plain `import(name)`
|
|
5
|
-
* looks dynamic in source but webpack rewrites it into a context module rooted
|
|
6
|
-
* at this directory - one that can only ever resolve files sitting next to this
|
|
7
|
-
* one - so `amqplib` and `kafkajs` fail to load in the bundled app no matter how
|
|
8
|
-
* correctly they are installed. Both lookups below are assembled at runtime,
|
|
9
|
-
* where no bundler can rewrite them.
|
|
10
|
-
*
|
|
11
|
-
* `require` is used first because it is what the CommonJS builds actually run
|
|
12
|
-
* on; native ESM has no `require`, and there `import()` is the only way in.
|
|
13
|
-
*/ export async function loadOptionalModule(name) {
|
|
14
|
-
const runtimeRequire = resolveRuntimeRequire();
|
|
15
|
-
if (runtimeRequire) return runtimeRequire(name);
|
|
16
|
-
const runtimeImport = new Function('specifier', 'return import(specifier);');
|
|
17
|
-
return runtimeImport(name);
|
|
18
|
-
}
|
|
19
|
-
/** Undefined under native ESM and inside a bundle that shadows nothing back */ function resolveRuntimeRequire() {
|
|
20
|
-
try {
|
|
21
|
-
return eval('typeof require === "function" ? require : undefined');
|
|
22
|
-
} catch {
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { loadOptionalModule } from './optional-module.util';
|
|
2
|
-
describe('loadOptionalModule', ()=>{
|
|
3
|
-
it('should resolve an installed module', async ()=>{
|
|
4
|
-
const events = await loadOptionalModule('events');
|
|
5
|
-
expect(typeof events.EventEmitter).toBe('function');
|
|
6
|
-
});
|
|
7
|
-
it('should reject for a module that is not installed', async ()=>{
|
|
8
|
-
await expect(loadOptionalModule('definitely-not-installed-pkg')).rejects.toThrow();
|
|
9
|
-
});
|
|
10
|
-
});
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { PERMISSIONS_CACHE_PREFIX } from '../constants';
|
|
2
|
-
export const DEFAULT_USER_PERMISSION_KEY_FORMAT = `${PERMISSIONS_CACHE_PREFIX}:user:{userId}`;
|
|
3
|
-
export const DEFAULT_COMPANY_PERMISSION_KEY_FORMAT = `${PERMISSIONS_CACHE_PREFIX}:company:{companyId}:branch:{branchId}:user:{userId}`;
|
|
4
|
-
/** Single source of truth for permission cache keys, shared by the guard (reader) and cache service (writer) so a config override can't desync them; company scope requires enableCompanyFeature, not just a companyId */ export function buildPermissionCacheKey(options, config) {
|
|
5
|
-
const isCompanyScope = Boolean(config?.enableCompanyFeature && options.companyId);
|
|
6
|
-
if (isCompanyScope) {
|
|
7
|
-
return (config?.companyPermissionKeyFormat || DEFAULT_COMPANY_PERMISSION_KEY_FORMAT).replace('{userId}', options.userId).replace('{companyId}', options.companyId).replace('{branchId}', options.branchId || 'null');
|
|
8
|
-
}
|
|
9
|
-
return (config?.userPermissionKeyFormat || DEFAULT_USER_PERMISSION_KEY_FORMAT).replace('{userId}', options.userId);
|
|
10
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { buildPermissionCacheKey, DEFAULT_COMPANY_PERMISSION_KEY_FORMAT, DEFAULT_USER_PERMISSION_KEY_FORMAT } from './permission-cache-key.util';
|
|
2
|
-
describe('permission-cache-key.util', ()=>{
|
|
3
|
-
describe('buildPermissionCacheKey', ()=>{
|
|
4
|
-
it('should build a user-scoped key when no config is provided', ()=>{
|
|
5
|
-
const key = buildPermissionCacheKey({
|
|
6
|
-
userId: 'user-1'
|
|
7
|
-
});
|
|
8
|
-
expect(key).toBe(DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
|
|
9
|
-
});
|
|
10
|
-
it('should build a user-scoped key when company feature is disabled', ()=>{
|
|
11
|
-
const key = buildPermissionCacheKey({
|
|
12
|
-
userId: 'user-1',
|
|
13
|
-
companyId: 'company-1'
|
|
14
|
-
}, {
|
|
15
|
-
enableCompanyFeature: false
|
|
16
|
-
});
|
|
17
|
-
expect(key).toBe(DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
|
|
18
|
-
});
|
|
19
|
-
it('should build a user-scoped key when company feature enabled but no companyId given', ()=>{
|
|
20
|
-
const key = buildPermissionCacheKey({
|
|
21
|
-
userId: 'user-1'
|
|
22
|
-
}, {
|
|
23
|
-
enableCompanyFeature: true
|
|
24
|
-
});
|
|
25
|
-
expect(key).toBe(DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
|
|
26
|
-
});
|
|
27
|
-
it('should build a company-scoped key when company feature is enabled and companyId is present', ()=>{
|
|
28
|
-
const key = buildPermissionCacheKey({
|
|
29
|
-
userId: 'user-1',
|
|
30
|
-
companyId: 'company-1',
|
|
31
|
-
branchId: 'branch-1'
|
|
32
|
-
}, {
|
|
33
|
-
enableCompanyFeature: true
|
|
34
|
-
});
|
|
35
|
-
expect(key).toBe(DEFAULT_COMPANY_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1').replace('{companyId}', 'company-1').replace('{branchId}', 'branch-1'));
|
|
36
|
-
});
|
|
37
|
-
it('should default branchId placeholder to "null" when branchId is missing', ()=>{
|
|
38
|
-
const key = buildPermissionCacheKey({
|
|
39
|
-
userId: 'user-1',
|
|
40
|
-
companyId: 'company-1'
|
|
41
|
-
}, {
|
|
42
|
-
enableCompanyFeature: true
|
|
43
|
-
});
|
|
44
|
-
expect(key).toContain(':branch:null:');
|
|
45
|
-
});
|
|
46
|
-
it('should default branchId placeholder to "null" when branchId is explicitly null', ()=>{
|
|
47
|
-
const key = buildPermissionCacheKey({
|
|
48
|
-
userId: 'user-1',
|
|
49
|
-
companyId: 'company-1',
|
|
50
|
-
branchId: null
|
|
51
|
-
}, {
|
|
52
|
-
enableCompanyFeature: true
|
|
53
|
-
});
|
|
54
|
-
expect(key).toContain(':branch:null:');
|
|
55
|
-
});
|
|
56
|
-
it('should use a custom userPermissionKeyFormat when provided', ()=>{
|
|
57
|
-
const key = buildPermissionCacheKey({
|
|
58
|
-
userId: 'user-1'
|
|
59
|
-
}, {
|
|
60
|
-
userPermissionKeyFormat: 'custom:{userId}'
|
|
61
|
-
});
|
|
62
|
-
expect(key).toBe('custom:user-1');
|
|
63
|
-
});
|
|
64
|
-
it('should use a custom companyPermissionKeyFormat when provided', ()=>{
|
|
65
|
-
const key = buildPermissionCacheKey({
|
|
66
|
-
userId: 'user-1',
|
|
67
|
-
companyId: 'company-1'
|
|
68
|
-
}, {
|
|
69
|
-
enableCompanyFeature: true,
|
|
70
|
-
companyPermissionKeyFormat: 'custom:{companyId}:{userId}:{branchId}'
|
|
71
|
-
});
|
|
72
|
-
expect(key).toBe('custom:company-1:user-1:null');
|
|
73
|
-
});
|
|
74
|
-
it('should treat an empty-string companyId as no company scope', ()=>{
|
|
75
|
-
const key = buildPermissionCacheKey({
|
|
76
|
-
userId: 'user-1',
|
|
77
|
-
companyId: ''
|
|
78
|
-
}, {
|
|
79
|
-
enableCompanyFeature: true
|
|
80
|
-
});
|
|
81
|
-
expect(key).toBe(DEFAULT_USER_PERMISSION_KEY_FORMAT.replace('{userId}', 'user-1'));
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
});
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { BadRequestException } from '@nestjs/common';
|
|
2
|
-
import { AUTH_MESSAGES } from '../constants';
|
|
3
|
-
/**
|
|
4
|
-
* Apply company filter to a query builder if company feature is enabled.
|
|
5
|
-
* Centralizes the common pattern of filtering by user's company.
|
|
6
|
-
*
|
|
7
|
-
* @param query - TypeORM SelectQueryBuilder
|
|
8
|
-
* @param config - Company filter configuration
|
|
9
|
-
* @param user - Current logged user (may be null)
|
|
10
|
-
* @returns The modified query builder
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* applyCompanyFilter(query, {
|
|
15
|
-
* isCompanyFeatureEnabled: this.config.isCompanyFeatureEnabled(),
|
|
16
|
-
* entityAlias: 'file_manager',
|
|
17
|
-
* }, user);
|
|
18
|
-
* ```
|
|
19
|
-
*/ export function applyCompanyFilter(query, config, user) {
|
|
20
|
-
const columnName = config.columnName ?? 'companyId';
|
|
21
|
-
if (config.isCompanyFeatureEnabled && user?.companyId) {
|
|
22
|
-
query.andWhere(`${config.entityAlias}.${columnName} = :companyId`, {
|
|
23
|
-
companyId: user.companyId
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
return query;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Build a where condition object with optional company filter.
|
|
30
|
-
*
|
|
31
|
-
* @param baseWhere - Base where condition object
|
|
32
|
-
* @param isCompanyFeatureEnabled - Whether company feature is enabled
|
|
33
|
-
* @param user - Current logged user
|
|
34
|
-
* @returns Where condition with companyId added if applicable
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```typescript
|
|
38
|
-
* const where = buildCompanyWhereCondition(
|
|
39
|
-
* { id: dto.id },
|
|
40
|
-
* this.config.isCompanyFeatureEnabled(),
|
|
41
|
-
* user
|
|
42
|
-
* );
|
|
43
|
-
* // Returns { id: dto.id, companyId: user.companyId } if company feature enabled
|
|
44
|
-
* ```
|
|
45
|
-
*/ export function buildCompanyWhereCondition(baseWhere, isCompanyFeatureEnabled, user) {
|
|
46
|
-
if (isCompanyFeatureEnabled && user?.companyId) {
|
|
47
|
-
return {
|
|
48
|
-
...baseWhere,
|
|
49
|
-
companyId: user.companyId
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
return baseWhere;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Type guard to check if entity has companyId property
|
|
56
|
-
*/ export function hasCompanyId(entity) {
|
|
57
|
-
return entity !== null && typeof entity === 'object' && 'companyId' in entity;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Validates that an entity with companyId belongs to the user's company.
|
|
61
|
-
* Throws BadRequestException if the entity belongs to another company.
|
|
62
|
-
*
|
|
63
|
-
* @param entity - Entity to validate
|
|
64
|
-
* @param user - Current logged user
|
|
65
|
-
* @param isCompanyFeatureEnabled - Whether company feature is enabled
|
|
66
|
-
* @param entityName - Name of entity for error message (e.g., 'Email configuration')
|
|
67
|
-
* @throws BadRequestException if entity belongs to another company
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```typescript
|
|
71
|
-
* validateCompanyOwnership(config, user, this.config.isCompanyFeatureEnabled(), 'Email configuration');
|
|
72
|
-
* ```
|
|
73
|
-
*/ export function validateCompanyOwnership(entity, user, isCompanyFeatureEnabled, entityName) {
|
|
74
|
-
if (isCompanyFeatureEnabled && user?.companyId && hasCompanyId(entity)) {
|
|
75
|
-
if (entity.companyId && entity.companyId !== user.companyId) {
|
|
76
|
-
throw new BadRequestException({
|
|
77
|
-
message: `${entityName} belongs to another company`,
|
|
78
|
-
messageKey: AUTH_MESSAGES.ENTITY_BELOG_ANOTHER_COMPANY,
|
|
79
|
-
messageVariables: {
|
|
80
|
-
entity: entityName
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import { BadRequestException } from '@nestjs/common';
|
|
2
|
-
import { buildMockUser } from '@test-utils/mocks/logged-user.mock';
|
|
3
|
-
import { applyCompanyFilter, buildCompanyWhereCondition, hasCompanyId, validateCompanyOwnership } from './query-helpers.util';
|
|
4
|
-
function createMockQueryBuilder() {
|
|
5
|
-
return {
|
|
6
|
-
andWhere: jest.fn().mockReturnThis()
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
describe('query-helpers.util', ()=>{
|
|
10
|
-
describe('applyCompanyFilter', ()=>{
|
|
11
|
-
it('should apply the company filter when feature enabled and user has companyId', ()=>{
|
|
12
|
-
const query = createMockQueryBuilder();
|
|
13
|
-
const user = buildMockUser({
|
|
14
|
-
companyId: 'company-1'
|
|
15
|
-
});
|
|
16
|
-
applyCompanyFilter(query, {
|
|
17
|
-
isCompanyFeatureEnabled: true,
|
|
18
|
-
entityAlias: 'file'
|
|
19
|
-
}, user);
|
|
20
|
-
expect(query.andWhere).toHaveBeenCalledWith('file.companyId = :companyId', {
|
|
21
|
-
companyId: 'company-1'
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
it('should use a custom column name when provided', ()=>{
|
|
25
|
-
const query = createMockQueryBuilder();
|
|
26
|
-
const user = buildMockUser({
|
|
27
|
-
companyId: 'company-1'
|
|
28
|
-
});
|
|
29
|
-
applyCompanyFilter(query, {
|
|
30
|
-
isCompanyFeatureEnabled: true,
|
|
31
|
-
entityAlias: 'file',
|
|
32
|
-
columnName: 'company_id'
|
|
33
|
-
}, user);
|
|
34
|
-
expect(query.andWhere).toHaveBeenCalledWith('file.company_id = :companyId', {
|
|
35
|
-
companyId: 'company-1'
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
it('should not apply filter when feature disabled', ()=>{
|
|
39
|
-
const query = createMockQueryBuilder();
|
|
40
|
-
const user = buildMockUser({
|
|
41
|
-
companyId: 'company-1'
|
|
42
|
-
});
|
|
43
|
-
applyCompanyFilter(query, {
|
|
44
|
-
isCompanyFeatureEnabled: false,
|
|
45
|
-
entityAlias: 'file'
|
|
46
|
-
}, user);
|
|
47
|
-
expect(query.andWhere).not.toHaveBeenCalled();
|
|
48
|
-
});
|
|
49
|
-
it('should not apply filter when user is null', ()=>{
|
|
50
|
-
const query = createMockQueryBuilder();
|
|
51
|
-
applyCompanyFilter(query, {
|
|
52
|
-
isCompanyFeatureEnabled: true,
|
|
53
|
-
entityAlias: 'file'
|
|
54
|
-
}, null);
|
|
55
|
-
expect(query.andWhere).not.toHaveBeenCalled();
|
|
56
|
-
});
|
|
57
|
-
it('should not apply filter when user has no companyId', ()=>{
|
|
58
|
-
const query = createMockQueryBuilder();
|
|
59
|
-
const user = buildMockUser({
|
|
60
|
-
companyId: undefined
|
|
61
|
-
});
|
|
62
|
-
applyCompanyFilter(query, {
|
|
63
|
-
isCompanyFeatureEnabled: true,
|
|
64
|
-
entityAlias: 'file'
|
|
65
|
-
}, user);
|
|
66
|
-
expect(query.andWhere).not.toHaveBeenCalled();
|
|
67
|
-
});
|
|
68
|
-
it('should return the same query builder instance', ()=>{
|
|
69
|
-
const query = createMockQueryBuilder();
|
|
70
|
-
const result = applyCompanyFilter(query, {
|
|
71
|
-
isCompanyFeatureEnabled: false,
|
|
72
|
-
entityAlias: 'file'
|
|
73
|
-
}, null);
|
|
74
|
-
expect(result).toBe(query);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
describe('buildCompanyWhereCondition', ()=>{
|
|
78
|
-
it('should add companyId when feature enabled and user has one', ()=>{
|
|
79
|
-
const user = buildMockUser({
|
|
80
|
-
companyId: 'company-1'
|
|
81
|
-
});
|
|
82
|
-
const result = buildCompanyWhereCondition({
|
|
83
|
-
id: '1'
|
|
84
|
-
}, true, user);
|
|
85
|
-
expect(result).toEqual({
|
|
86
|
-
id: '1',
|
|
87
|
-
companyId: 'company-1'
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
it('should return base where unchanged when feature disabled', ()=>{
|
|
91
|
-
const user = buildMockUser({
|
|
92
|
-
companyId: 'company-1'
|
|
93
|
-
});
|
|
94
|
-
const result = buildCompanyWhereCondition({
|
|
95
|
-
id: '1'
|
|
96
|
-
}, false, user);
|
|
97
|
-
expect(result).toEqual({
|
|
98
|
-
id: '1'
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
it('should return base where unchanged when user is null', ()=>{
|
|
102
|
-
const result = buildCompanyWhereCondition({
|
|
103
|
-
id: '1'
|
|
104
|
-
}, true, null);
|
|
105
|
-
expect(result).toEqual({
|
|
106
|
-
id: '1'
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
it('should return base where unchanged when user has no companyId', ()=>{
|
|
110
|
-
const user = buildMockUser({
|
|
111
|
-
companyId: undefined
|
|
112
|
-
});
|
|
113
|
-
const result = buildCompanyWhereCondition({
|
|
114
|
-
id: '1'
|
|
115
|
-
}, true, user);
|
|
116
|
-
expect(result).toEqual({
|
|
117
|
-
id: '1'
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
describe('hasCompanyId', ()=>{
|
|
122
|
-
it('should return true for an object with a companyId property', ()=>{
|
|
123
|
-
expect(hasCompanyId({
|
|
124
|
-
companyId: 'company-1'
|
|
125
|
-
})).toBe(true);
|
|
126
|
-
});
|
|
127
|
-
it('should return true even when companyId is explicitly undefined but present', ()=>{
|
|
128
|
-
expect(hasCompanyId({
|
|
129
|
-
companyId: undefined
|
|
130
|
-
})).toBe(true);
|
|
131
|
-
});
|
|
132
|
-
it('should return false for an object without a companyId property', ()=>{
|
|
133
|
-
expect(hasCompanyId({
|
|
134
|
-
id: '1'
|
|
135
|
-
})).toBe(false);
|
|
136
|
-
});
|
|
137
|
-
it('should return false for null', ()=>{
|
|
138
|
-
expect(hasCompanyId(null)).toBe(false);
|
|
139
|
-
});
|
|
140
|
-
it('should return false for non-object values', ()=>{
|
|
141
|
-
expect(hasCompanyId('string')).toBe(false);
|
|
142
|
-
expect(hasCompanyId(42)).toBe(false);
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
describe('validateCompanyOwnership', ()=>{
|
|
146
|
-
it('should not throw when feature disabled', ()=>{
|
|
147
|
-
const user = buildMockUser({
|
|
148
|
-
companyId: 'company-1'
|
|
149
|
-
});
|
|
150
|
-
expect(()=>validateCompanyOwnership({
|
|
151
|
-
companyId: 'company-2'
|
|
152
|
-
}, user, false, 'Entity')).not.toThrow();
|
|
153
|
-
});
|
|
154
|
-
it('should not throw when user is null', ()=>{
|
|
155
|
-
expect(()=>validateCompanyOwnership({
|
|
156
|
-
companyId: 'company-2'
|
|
157
|
-
}, null, true, 'Entity')).not.toThrow();
|
|
158
|
-
});
|
|
159
|
-
it('should not throw when entity has no companyId', ()=>{
|
|
160
|
-
const user = buildMockUser({
|
|
161
|
-
companyId: 'company-1'
|
|
162
|
-
});
|
|
163
|
-
expect(()=>validateCompanyOwnership({
|
|
164
|
-
id: '1'
|
|
165
|
-
}, user, true, 'Entity')).not.toThrow();
|
|
166
|
-
});
|
|
167
|
-
it('should not throw when entity companyId is falsy', ()=>{
|
|
168
|
-
const user = buildMockUser({
|
|
169
|
-
companyId: 'company-1'
|
|
170
|
-
});
|
|
171
|
-
expect(()=>validateCompanyOwnership({
|
|
172
|
-
companyId: null
|
|
173
|
-
}, user, true, 'Entity')).not.toThrow();
|
|
174
|
-
});
|
|
175
|
-
it('should not throw when entity companyId matches user companyId', ()=>{
|
|
176
|
-
const user = buildMockUser({
|
|
177
|
-
companyId: 'company-1'
|
|
178
|
-
});
|
|
179
|
-
expect(()=>validateCompanyOwnership({
|
|
180
|
-
companyId: 'company-1'
|
|
181
|
-
}, user, true, 'Entity')).not.toThrow();
|
|
182
|
-
});
|
|
183
|
-
it('should throw BadRequestException when entity belongs to a different company', ()=>{
|
|
184
|
-
const user = buildMockUser({
|
|
185
|
-
companyId: 'company-1'
|
|
186
|
-
});
|
|
187
|
-
expect(()=>validateCompanyOwnership({
|
|
188
|
-
companyId: 'company-2'
|
|
189
|
-
}, user, true, 'Email configuration')).toThrow(BadRequestException);
|
|
190
|
-
});
|
|
191
|
-
it('should include the entity name in the thrown exception message', ()=>{
|
|
192
|
-
const user = buildMockUser({
|
|
193
|
-
companyId: 'company-1'
|
|
194
|
-
});
|
|
195
|
-
try {
|
|
196
|
-
validateCompanyOwnership({
|
|
197
|
-
companyId: 'company-2'
|
|
198
|
-
}, user, true, 'Email configuration');
|
|
199
|
-
fail('expected to throw');
|
|
200
|
-
} catch (error) {
|
|
201
|
-
expect(error.getResponse().message).toContain('Email configuration');
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
});
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { envConfig } from '@flusys/nestjs-core/config';
|
|
2
|
-
import { CLIENT_TYPE_HEADER } from '../constants';
|
|
3
|
-
/** Time unit multipliers in milliseconds */ const TIME_UNIT_MS = {
|
|
4
|
-
s: 1000,
|
|
5
|
-
m: 60 * 1000,
|
|
6
|
-
h: 60 * 60 * 1000,
|
|
7
|
-
d: 24 * 60 * 60 * 1000,
|
|
8
|
-
w: 7 * 24 * 60 * 60 * 1000
|
|
9
|
-
};
|
|
10
|
-
/** Get normalized client type from request header */ function getClientType(req) {
|
|
11
|
-
const clientType = req.headers[CLIENT_TYPE_HEADER];
|
|
12
|
-
return clientType ? clientType.toLowerCase() : null;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Detect if request is from a browser client.
|
|
16
|
-
* Uses x-client-type header first, then falls back to user-agent detection.
|
|
17
|
-
*/ export function isBrowserRequest(req) {
|
|
18
|
-
const clientType = getClientType(req);
|
|
19
|
-
if (clientType) {
|
|
20
|
-
return clientType === 'browser' || clientType === 'web';
|
|
21
|
-
}
|
|
22
|
-
const accept = req.headers['accept'] || '';
|
|
23
|
-
if (accept.includes('text/html')) return true;
|
|
24
|
-
const userAgent = req.headers['user-agent'] || '';
|
|
25
|
-
const browserPatterns = /mozilla|chrome|safari|firefox|edge|opera|msie/i;
|
|
26
|
-
return browserPatterns.test(userAgent) && !userAgent.includes('Postman');
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Build secure cookie options based on request context.
|
|
30
|
-
* Handles HTTPS detection for TLS-terminating proxies.
|
|
31
|
-
*/ export function buildCookieOptions(req) {
|
|
32
|
-
const hostname = req.hostname || '';
|
|
33
|
-
const origin = req.headers.origin || '';
|
|
34
|
-
const isProduction = envConfig.isProduction();
|
|
35
|
-
const forwardedProto = req.headers['x-forwarded-proto'];
|
|
36
|
-
const isHttps = isProduction || forwardedProto === 'https' || origin.startsWith('https://') || req.secure;
|
|
37
|
-
let domain;
|
|
38
|
-
const domainParts = hostname.split('.');
|
|
39
|
-
if (domainParts.length > 2 && !hostname.includes('localhost')) {
|
|
40
|
-
domain = '.' + domainParts.slice(-2).join('.');
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
secure: isHttps,
|
|
44
|
-
sameSite: isHttps ? 'strict' : 'lax',
|
|
45
|
-
...domain && {
|
|
46
|
-
domain
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Parse duration string to milliseconds.
|
|
52
|
-
* @example '7d' → 604800000, '24h' → 86400000, '30m' → 1800000
|
|
53
|
-
*/ export function parseDurationToMs(duration, defaultMs = TIME_UNIT_MS.w) {
|
|
54
|
-
const match = duration.match(/^(\d+)(s|m|h|d|w)$/);
|
|
55
|
-
if (!match) return defaultMs;
|
|
56
|
-
const value = parseInt(match[1], 10);
|
|
57
|
-
const unit = match[2];
|
|
58
|
-
return value * (TIME_UNIT_MS[unit] ?? TIME_UNIT_MS.d);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Determine the frontend base URL for auth-related links.
|
|
62
|
-
* Uses allowed origins from envConfig first, then falls back to request host.
|
|
63
|
-
*/ export function extractFrontendUrl(req) {
|
|
64
|
-
const allowedOrigins = envConfig.getOrigins();
|
|
65
|
-
const origin = (req.get('origin') ?? req.get('referer') ?? '').trim();
|
|
66
|
-
if (origin && allowedOrigins.length) {
|
|
67
|
-
const matched = allowedOrigins.find((u)=>origin.startsWith(u));
|
|
68
|
-
if (matched) {
|
|
69
|
-
return matched;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const host = req.get('host');
|
|
73
|
-
if (!host) {
|
|
74
|
-
throw new Error('Unable to determine frontend URL from request');
|
|
75
|
-
}
|
|
76
|
-
return `${req.protocol}://${host}`;
|
|
77
|
-
}
|
|
78
|
-
export function buildFrontendUrl(req, path) {
|
|
79
|
-
const baseUrl = extractFrontendUrl(req);
|
|
80
|
-
return `${baseUrl}${path}`;
|
|
81
|
-
}
|