@flusys/nestjs-shared 7.1.0 → 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 (338) hide show
  1. package/classes/api-controller.class.d.ts +4 -4
  2. package/classes/api-service.class.d.ts +7 -7
  3. package/classes/disabled-event-bus.class.d.ts +1 -1
  4. package/classes/domain-event.publisher.d.ts +1 -1
  5. package/classes/event-bus.registry.d.ts +1 -1
  6. package/classes/event-transports/event-transport.registry.d.ts +1 -1
  7. package/classes/event-transports/index.d.ts +2 -2
  8. package/classes/event-transports/kafka-transport.optional.d.ts +1 -1
  9. package/classes/event-transports/memory-transport.d.ts +1 -1
  10. package/classes/event-transports/rabbitmq-transport.optional.d.ts +1 -1
  11. package/classes/hybrid-event-bus.class.d.ts +1 -1
  12. package/classes/index.d.ts +11 -11
  13. package/constants/index.d.ts +2 -2
  14. package/decorators/api-response.decorator.d.ts +1 -1
  15. package/decorators/current-user.decorator.d.ts +1 -1
  16. package/decorators/index.d.ts +6 -6
  17. package/decorators/require-permission.decorator.d.ts +1 -1
  18. package/dtos/filter-and-pagination.dto.d.ts +1 -1
  19. package/dtos/index.d.ts +6 -6
  20. package/entities/index.d.ts +3 -3
  21. package/enums/index.d.ts +3 -3
  22. package/exceptions/index.d.ts +2 -2
  23. package/fesm/470.js +2713 -0
  24. package/fesm/509.js +793 -0
  25. package/fesm/667.js +606 -0
  26. package/fesm/67.js +808 -0
  27. package/fesm/720.js +914 -0
  28. package/{cjs/classes/event-transports/rabbitmq-transport.optional.js → fesm/chunks/123.js} +78 -55
  29. package/fesm/{classes/event-transports/kafka-transport.optional.js → chunks/37.js} +74 -41
  30. package/fesm/classes/index.js +62 -12
  31. package/fesm/constants/index.js +445 -17
  32. package/fesm/decorators/index.js +341 -6
  33. package/fesm/dtos/index.js +23 -6
  34. package/fesm/entities/index.js +336 -3
  35. package/fesm/enums/index.js +57 -3
  36. package/fesm/exceptions/index.js +280 -2
  37. package/fesm/guards/index.js +1003 -3
  38. package/fesm/index.js +1524 -14
  39. package/fesm/interceptors/index.js +924 -5
  40. package/fesm/interfaces/index.js +57 -11
  41. package/fesm/middlewares/index.js +33 -1
  42. package/fesm/modules/index.js +432 -9
  43. package/fesm/runtime.js +134 -0
  44. package/fesm/utils/index.js +1045 -8
  45. package/filters/global-exception.filter.d.ts +1 -1
  46. package/filters/index.d.ts +1 -1
  47. package/guards/index.d.ts +3 -3
  48. package/guards/jwt-auth.guard.d.ts +1 -1
  49. package/guards/optional-jwt-auth.guard.d.ts +1 -1
  50. package/guards/permission.guard.d.ts +2 -2
  51. package/index.d.ts +14 -14
  52. package/interceptors/delete-empty-id-from-body.interceptor.d.ts +1 -1
  53. package/interceptors/idempotency.interceptor.d.ts +2 -2
  54. package/interceptors/index.d.ts +5 -5
  55. package/interceptors/response-meta.interceptor.d.ts +1 -1
  56. package/interceptors/set-user-field-on-body.interceptor.d.ts +1 -1
  57. package/interceptors/slug.interceptor.d.ts +2 -2
  58. package/interfaces/ai-assistant-adapter.interface.d.ts +1 -1
  59. package/interfaces/api.interface.d.ts +2 -2
  60. package/interfaces/datasource.interface.d.ts +1 -1
  61. package/interfaces/event-manager-adapter.interface.d.ts +2 -2
  62. package/interfaces/index.d.ts +11 -11
  63. package/interfaces/module-config.interface.d.ts +1 -1
  64. package/interfaces/notification-adapter.interface.d.ts +1 -1
  65. package/middlewares/index.d.ts +1 -1
  66. package/middlewares/logger.middleware.d.ts +2 -2
  67. package/modules/cache/cache.module.d.ts +1 -1
  68. package/modules/datasource/index.d.ts +1 -1
  69. package/modules/datasource/multi-tenant-datasource.service.d.ts +4 -4
  70. package/modules/event-bus/event-bus.explorer.d.ts +2 -2
  71. package/modules/event-bus/event-bus.module.d.ts +2 -2
  72. package/modules/event-bus/module-events.provider.d.ts +1 -1
  73. package/modules/index.d.ts +9 -9
  74. package/modules/shared-permission-cache/shared-permission-cache.service.d.ts +3 -3
  75. package/modules/utils/utils.service.d.ts +1 -1
  76. package/package.json +30 -44
  77. package/utils/index.d.ts +8 -8
  78. package/utils/permission-cache-key.util.d.ts +1 -1
  79. package/utils/query-helpers.util.d.ts +2 -2
  80. package/utils/request.util.d.ts +1 -1
  81. package/cjs/classes/api-controller.class.js +0 -728
  82. package/cjs/classes/api-controller.class.spec.js +0 -684
  83. package/cjs/classes/api-service.class.js +0 -631
  84. package/cjs/classes/api-service.class.spec.js +0 -1137
  85. package/cjs/classes/disabled-event-bus.class.js +0 -15
  86. package/cjs/classes/disabled-event-bus.class.spec.js +0 -40
  87. package/cjs/classes/domain-event.publisher.js +0 -76
  88. package/cjs/classes/domain-event.publisher.spec.js +0 -234
  89. package/cjs/classes/event-bus.registry.js +0 -68
  90. package/cjs/classes/event-bus.registry.spec.js +0 -141
  91. package/cjs/classes/event-transports/event-transport.registry.js +0 -41
  92. package/cjs/classes/event-transports/event-transport.registry.spec.js +0 -31
  93. package/cjs/classes/event-transports/index.js +0 -23
  94. package/cjs/classes/event-transports/kafka-transport.optional.js +0 -151
  95. package/cjs/classes/event-transports/kafka-transport.optional.spec.js +0 -198
  96. package/cjs/classes/event-transports/memory-transport.js +0 -53
  97. package/cjs/classes/event-transports/memory-transport.spec.js +0 -57
  98. package/cjs/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -255
  99. package/cjs/classes/handle-custome-db.js +0 -37
  100. package/cjs/classes/handle-custome-db.spec.js +0 -82
  101. package/cjs/classes/hybrid-cache.class.js +0 -96
  102. package/cjs/classes/hybrid-cache.class.spec.js +0 -168
  103. package/cjs/classes/hybrid-event-bus.class.js +0 -281
  104. package/cjs/classes/hybrid-event-bus.class.spec.js +0 -218
  105. package/cjs/classes/index.js +0 -28
  106. package/cjs/classes/winston.logger.class.js +0 -274
  107. package/cjs/classes/winston.logger.class.spec.js +0 -330
  108. package/cjs/constants/index.js +0 -88
  109. package/cjs/constants/message-keys.js +0 -68
  110. package/cjs/constants/permissions.js +0 -307
  111. package/cjs/decorators/api-response.decorator.js +0 -52
  112. package/cjs/decorators/api-response.decorator.spec.js +0 -126
  113. package/cjs/decorators/current-user.decorator.js +0 -16
  114. package/cjs/decorators/current-user.decorator.spec.js +0 -74
  115. package/cjs/decorators/index.js +0 -23
  116. package/cjs/decorators/log-action.decorator.js +0 -149
  117. package/cjs/decorators/log-action.decorator.spec.js +0 -353
  118. package/cjs/decorators/on-domain-event.decorator.js +0 -13
  119. package/cjs/decorators/on-domain-event.decorator.spec.js +0 -50
  120. package/cjs/decorators/public.decorator.js +0 -13
  121. package/cjs/decorators/public.decorator.spec.js +0 -50
  122. package/cjs/decorators/require-permission.decorator.js +0 -32
  123. package/cjs/decorators/require-permission.decorator.spec.js +0 -147
  124. package/cjs/dtos/delete.dto.js +0 -92
  125. package/cjs/dtos/filter-and-pagination.dto.js +0 -164
  126. package/cjs/dtos/get-by-ids.dto.js +0 -65
  127. package/cjs/dtos/identity-response.dto.js +0 -108
  128. package/cjs/dtos/index.js +0 -23
  129. package/cjs/dtos/pagination.dto.js +0 -67
  130. package/cjs/dtos/response-payload.dto.js +0 -369
  131. package/cjs/entities/identity.js +0 -100
  132. package/cjs/entities/index.js +0 -20
  133. package/cjs/entities/raw-type.js +0 -4
  134. package/cjs/entities/user-root.js +0 -189
  135. package/cjs/enums/index.js +0 -20
  136. package/cjs/enums/notification-type.enum.js +0 -17
  137. package/cjs/enums/participant-status.enum.js +0 -17
  138. package/cjs/enums/recurrence-type.enum.js +0 -18
  139. package/cjs/exceptions/base-app.exception.js +0 -151
  140. package/cjs/exceptions/base-app.exception.spec.js +0 -157
  141. package/cjs/exceptions/index.js +0 -19
  142. package/cjs/exceptions/permission.exception.js +0 -56
  143. package/cjs/exceptions/permission.exception.spec.js +0 -82
  144. package/cjs/filters/global-exception.filter.js +0 -279
  145. package/cjs/filters/global-exception.filter.spec.js +0 -440
  146. package/cjs/filters/index.js +0 -18
  147. package/cjs/guards/index.js +0 -20
  148. package/cjs/guards/jwt-auth.guard.js +0 -75
  149. package/cjs/guards/jwt-auth.guard.spec.js +0 -75
  150. package/cjs/guards/optional-jwt-auth.guard.js +0 -67
  151. package/cjs/guards/optional-jwt-auth.guard.spec.js +0 -55
  152. package/cjs/guards/permission.guard.js +0 -179
  153. package/cjs/guards/permission.guard.spec.js +0 -373
  154. package/cjs/index.js +0 -31
  155. package/cjs/interceptors/delete-empty-id-from-body.interceptor.js +0 -40
  156. package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -107
  157. package/cjs/interceptors/idempotency.interceptor.js +0 -96
  158. package/cjs/interceptors/idempotency.interceptor.spec.js +0 -116
  159. package/cjs/interceptors/index.js +0 -22
  160. package/cjs/interceptors/response-meta.interceptor.js +0 -47
  161. package/cjs/interceptors/response-meta.interceptor.spec.js +0 -89
  162. package/cjs/interceptors/set-user-field-on-body.interceptor.js +0 -60
  163. package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +0 -128
  164. package/cjs/interceptors/slug.interceptor.js +0 -75
  165. package/cjs/interceptors/slug.interceptor.spec.js +0 -110
  166. package/cjs/interfaces/ai-assistant-adapter.interface.js +0 -24
  167. package/cjs/interfaces/api.interface.js +0 -4
  168. package/cjs/interfaces/datasource.interface.js +0 -4
  169. package/cjs/interfaces/event-bus.interface.js +0 -4
  170. package/cjs/interfaces/event-manager-adapter.interface.js +0 -11
  171. package/cjs/interfaces/identity.interface.js +0 -4
  172. package/cjs/interfaces/index.js +0 -28
  173. package/cjs/interfaces/logged-user-info.interface.js +0 -6
  174. package/cjs/interfaces/module-config.interface.js +0 -4
  175. package/cjs/interfaces/notification-adapter.interface.js +0 -11
  176. package/cjs/interfaces/permission-sync-adapter.interface.js +0 -11
  177. package/cjs/interfaces/permission.interface.js +0 -4
  178. package/cjs/middlewares/index.js +0 -18
  179. package/cjs/middlewares/logger.middleware.js +0 -264
  180. package/cjs/middlewares/logger.middleware.spec.js +0 -462
  181. package/cjs/modules/cache/cache.module.js +0 -39
  182. package/cjs/modules/cache/cache.module.spec.js +0 -48
  183. package/cjs/modules/datasource/index.js +0 -18
  184. package/cjs/modules/datasource/multi-tenant-datasource.service.js +0 -222
  185. package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +0 -381
  186. package/cjs/modules/event-bus/event-bus.explorer.js +0 -124
  187. package/cjs/modules/event-bus/event-bus.explorer.spec.js +0 -189
  188. package/cjs/modules/event-bus/event-bus.integration.spec.js +0 -253
  189. package/cjs/modules/event-bus/event-bus.module.js +0 -94
  190. package/cjs/modules/event-bus/event-bus.module.spec.js +0 -149
  191. package/cjs/modules/event-bus/module-events.provider.js +0 -39
  192. package/cjs/modules/event-bus/module-events.provider.spec.js +0 -56
  193. package/cjs/modules/index.js +0 -26
  194. package/cjs/modules/shared-permission-cache/shared-permission-cache.module.js +0 -32
  195. package/cjs/modules/shared-permission-cache/shared-permission-cache.service.js +0 -101
  196. package/cjs/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -165
  197. package/cjs/modules/utils/utils.module.js +0 -32
  198. package/cjs/modules/utils/utils.service.js +0 -76
  199. package/cjs/modules/utils/utils.service.spec.js +0 -169
  200. package/cjs/utils/date-time.util.js +0 -94
  201. package/cjs/utils/date-time.util.spec.js +0 -84
  202. package/cjs/utils/event-pattern.util.js +0 -103
  203. package/cjs/utils/event-pattern.util.spec.js +0 -251
  204. package/cjs/utils/html-sanitizer.util.js +0 -64
  205. package/cjs/utils/html-sanitizer.util.spec.js +0 -76
  206. package/cjs/utils/index.js +0 -25
  207. package/cjs/utils/optional-module.util.js +0 -35
  208. package/cjs/utils/optional-module.util.spec.js +0 -14
  209. package/cjs/utils/permission-cache-key.util.js +0 -31
  210. package/cjs/utils/permission-cache-key.util.spec.js +0 -88
  211. package/cjs/utils/query-helpers.util.js +0 -60
  212. package/cjs/utils/query-helpers.util.spec.js +0 -209
  213. package/cjs/utils/request.util.js +0 -96
  214. package/cjs/utils/request.util.spec.js +0 -249
  215. package/cjs/utils/string.util.js +0 -63
  216. package/cjs/utils/string.util.spec.js +0 -63
  217. package/fesm/classes/api-controller.class.js +0 -718
  218. package/fesm/classes/api-controller.class.spec.js +0 -680
  219. package/fesm/classes/api-service.class.js +0 -621
  220. package/fesm/classes/api-service.class.spec.js +0 -1133
  221. package/fesm/classes/disabled-event-bus.class.js +0 -11
  222. package/fesm/classes/disabled-event-bus.class.spec.js +0 -36
  223. package/fesm/classes/domain-event.publisher.js +0 -63
  224. package/fesm/classes/domain-event.publisher.spec.js +0 -230
  225. package/fesm/classes/event-bus.registry.js +0 -64
  226. package/fesm/classes/event-bus.registry.spec.js +0 -137
  227. package/fesm/classes/event-transports/event-transport.registry.js +0 -35
  228. package/fesm/classes/event-transports/event-transport.registry.spec.js +0 -27
  229. package/fesm/classes/event-transports/index.js +0 -6
  230. package/fesm/classes/event-transports/kafka-transport.optional.spec.js +0 -194
  231. package/fesm/classes/event-transports/memory-transport.js +0 -49
  232. package/fesm/classes/event-transports/memory-transport.spec.js +0 -53
  233. package/fesm/classes/event-transports/rabbitmq-transport.optional.js +0 -174
  234. package/fesm/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -251
  235. package/fesm/classes/handle-custome-db.js +0 -19
  236. package/fesm/classes/handle-custome-db.spec.js +0 -78
  237. package/fesm/classes/hybrid-cache.class.js +0 -81
  238. package/fesm/classes/hybrid-cache.class.spec.js +0 -164
  239. package/fesm/classes/hybrid-event-bus.class.js +0 -237
  240. package/fesm/classes/hybrid-event-bus.class.spec.js +0 -214
  241. package/fesm/classes/winston.logger.class.js +0 -228
  242. package/fesm/classes/winston.logger.class.spec.js +0 -327
  243. package/fesm/constants/message-keys.js +0 -49
  244. package/fesm/constants/permissions.js +0 -215
  245. package/fesm/decorators/api-response.decorator.js +0 -42
  246. package/fesm/decorators/api-response.decorator.spec.js +0 -122
  247. package/fesm/decorators/current-user.decorator.js +0 -18
  248. package/fesm/decorators/current-user.decorator.spec.js +0 -73
  249. package/fesm/decorators/log-action.decorator.js +0 -139
  250. package/fesm/decorators/log-action.decorator.spec.js +0 -349
  251. package/fesm/decorators/on-domain-event.decorator.js +0 -10
  252. package/fesm/decorators/on-domain-event.decorator.spec.js +0 -46
  253. package/fesm/decorators/public.decorator.js +0 -20
  254. package/fesm/decorators/public.decorator.spec.js +0 -46
  255. package/fesm/decorators/require-permission.decorator.js +0 -27
  256. package/fesm/decorators/require-permission.decorator.spec.js +0 -143
  257. package/fesm/dtos/delete.dto.js +0 -82
  258. package/fesm/dtos/filter-and-pagination.dto.js +0 -148
  259. package/fesm/dtos/get-by-ids.dto.js +0 -55
  260. package/fesm/dtos/identity-response.dto.js +0 -98
  261. package/fesm/dtos/pagination.dto.js +0 -57
  262. package/fesm/dtos/response-payload.dto.js +0 -336
  263. package/fesm/entities/identity.js +0 -90
  264. package/fesm/entities/raw-type.js +0 -1
  265. package/fesm/entities/user-root.js +0 -179
  266. package/fesm/enums/notification-type.enum.js +0 -7
  267. package/fesm/enums/participant-status.enum.js +0 -7
  268. package/fesm/enums/recurrence-type.enum.js +0 -8
  269. package/fesm/exceptions/base-app.exception.js +0 -115
  270. package/fesm/exceptions/base-app.exception.spec.js +0 -153
  271. package/fesm/exceptions/permission.exception.js +0 -35
  272. package/fesm/exceptions/permission.exception.spec.js +0 -78
  273. package/fesm/filters/global-exception.filter.js +0 -269
  274. package/fesm/filters/global-exception.filter.spec.js +0 -436
  275. package/fesm/filters/index.js +0 -1
  276. package/fesm/guards/jwt-auth.guard.js +0 -65
  277. package/fesm/guards/jwt-auth.guard.spec.js +0 -71
  278. package/fesm/guards/optional-jwt-auth.guard.js +0 -57
  279. package/fesm/guards/optional-jwt-auth.guard.spec.js +0 -51
  280. package/fesm/guards/permission.guard.js +0 -169
  281. package/fesm/guards/permission.guard.spec.js +0 -369
  282. package/fesm/interceptors/delete-empty-id-from-body.interceptor.js +0 -30
  283. package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -103
  284. package/fesm/interceptors/idempotency.interceptor.js +0 -86
  285. package/fesm/interceptors/idempotency.interceptor.spec.js +0 -112
  286. package/fesm/interceptors/response-meta.interceptor.js +0 -37
  287. package/fesm/interceptors/response-meta.interceptor.spec.js +0 -85
  288. package/fesm/interceptors/set-user-field-on-body.interceptor.js +0 -39
  289. package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +0 -124
  290. package/fesm/interceptors/slug.interceptor.js +0 -65
  291. package/fesm/interceptors/slug.interceptor.spec.js +0 -106
  292. package/fesm/interfaces/ai-assistant-adapter.interface.js +0 -3
  293. package/fesm/interfaces/api.interface.js +0 -1
  294. package/fesm/interfaces/datasource.interface.js +0 -1
  295. package/fesm/interfaces/event-bus.interface.js +0 -1
  296. package/fesm/interfaces/event-manager-adapter.interface.js +0 -1
  297. package/fesm/interfaces/identity.interface.js +0 -1
  298. package/fesm/interfaces/logged-user-info.interface.js +0 -3
  299. package/fesm/interfaces/module-config.interface.js +0 -5
  300. package/fesm/interfaces/notification-adapter.interface.js +0 -1
  301. package/fesm/interfaces/permission-sync-adapter.interface.js +0 -1
  302. package/fesm/interfaces/permission.interface.js +0 -1
  303. package/fesm/middlewares/logger.middleware.js +0 -235
  304. package/fesm/middlewares/logger.middleware.spec.js +0 -459
  305. package/fesm/modules/cache/cache.module.js +0 -29
  306. package/fesm/modules/cache/cache.module.spec.js +0 -44
  307. package/fesm/modules/datasource/index.js +0 -1
  308. package/fesm/modules/datasource/multi-tenant-datasource.service.js +0 -212
  309. package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +0 -377
  310. package/fesm/modules/event-bus/event-bus.explorer.js +0 -114
  311. package/fesm/modules/event-bus/event-bus.explorer.spec.js +0 -185
  312. package/fesm/modules/event-bus/event-bus.integration.spec.js +0 -249
  313. package/fesm/modules/event-bus/event-bus.module.js +0 -84
  314. package/fesm/modules/event-bus/event-bus.module.spec.js +0 -145
  315. package/fesm/modules/event-bus/module-events.provider.js +0 -26
  316. package/fesm/modules/event-bus/module-events.provider.spec.js +0 -52
  317. package/fesm/modules/shared-permission-cache/shared-permission-cache.module.js +0 -22
  318. package/fesm/modules/shared-permission-cache/shared-permission-cache.service.js +0 -91
  319. package/fesm/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -161
  320. package/fesm/modules/utils/utils.module.js +0 -22
  321. package/fesm/modules/utils/utils.service.js +0 -66
  322. package/fesm/modules/utils/utils.service.spec.js +0 -165
  323. package/fesm/utils/date-time.util.js +0 -70
  324. package/fesm/utils/date-time.util.spec.js +0 -80
  325. package/fesm/utils/event-pattern.util.js +0 -91
  326. package/fesm/utils/event-pattern.util.spec.js +0 -247
  327. package/fesm/utils/html-sanitizer.util.js +0 -69
  328. package/fesm/utils/html-sanitizer.util.spec.js +0 -72
  329. package/fesm/utils/optional-module.util.js +0 -25
  330. package/fesm/utils/optional-module.util.spec.js +0 -10
  331. package/fesm/utils/permission-cache-key.util.js +0 -10
  332. package/fesm/utils/permission-cache-key.util.spec.js +0 -84
  333. package/fesm/utils/query-helpers.util.js +0 -85
  334. package/fesm/utils/query-helpers.util.spec.js +0 -205
  335. package/fesm/utils/request.util.js +0 -81
  336. package/fesm/utils/request.util.spec.js +0 -245
  337. package/fesm/utils/string.util.js +0 -71
  338. package/fesm/utils/string.util.spec.js +0 -59
@@ -1,209 +0,0 @@
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
- });
@@ -1,96 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: Object.getOwnPropertyDescriptor(all, name).get
9
- });
10
- }
11
- _export(exports, {
12
- get buildCookieOptions () {
13
- return buildCookieOptions;
14
- },
15
- get buildFrontendUrl () {
16
- return buildFrontendUrl;
17
- },
18
- get extractFrontendUrl () {
19
- return extractFrontendUrl;
20
- },
21
- get isBrowserRequest () {
22
- return isBrowserRequest;
23
- },
24
- get parseDurationToMs () {
25
- return parseDurationToMs;
26
- }
27
- });
28
- const _config = require("@flusys/nestjs-core/config");
29
- const _constants = require("../constants");
30
- /** Time unit multipliers in milliseconds */ const TIME_UNIT_MS = {
31
- s: 1000,
32
- m: 60 * 1000,
33
- h: 60 * 60 * 1000,
34
- d: 24 * 60 * 60 * 1000,
35
- w: 7 * 24 * 60 * 60 * 1000
36
- };
37
- /** Get normalized client type from request header */ function getClientType(req) {
38
- const clientType = req.headers[_constants.CLIENT_TYPE_HEADER];
39
- return clientType ? clientType.toLowerCase() : null;
40
- }
41
- function isBrowserRequest(req) {
42
- const clientType = getClientType(req);
43
- if (clientType) {
44
- return clientType === 'browser' || clientType === 'web';
45
- }
46
- const accept = req.headers['accept'] || '';
47
- if (accept.includes('text/html')) return true;
48
- const userAgent = req.headers['user-agent'] || '';
49
- const browserPatterns = /mozilla|chrome|safari|firefox|edge|opera|msie/i;
50
- return browserPatterns.test(userAgent) && !userAgent.includes('Postman');
51
- }
52
- function buildCookieOptions(req) {
53
- const hostname = req.hostname || '';
54
- const origin = req.headers.origin || '';
55
- const isProduction = _config.envConfig.isProduction();
56
- const forwardedProto = req.headers['x-forwarded-proto'];
57
- const isHttps = isProduction || forwardedProto === 'https' || origin.startsWith('https://') || req.secure;
58
- let domain;
59
- const domainParts = hostname.split('.');
60
- if (domainParts.length > 2 && !hostname.includes('localhost')) {
61
- domain = '.' + domainParts.slice(-2).join('.');
62
- }
63
- return {
64
- secure: isHttps,
65
- sameSite: isHttps ? 'strict' : 'lax',
66
- ...domain && {
67
- domain
68
- }
69
- };
70
- }
71
- function parseDurationToMs(duration, defaultMs = TIME_UNIT_MS.w) {
72
- const match = duration.match(/^(\d+)(s|m|h|d|w)$/);
73
- if (!match) return defaultMs;
74
- const value = parseInt(match[1], 10);
75
- const unit = match[2];
76
- return value * (TIME_UNIT_MS[unit] ?? TIME_UNIT_MS.d);
77
- }
78
- function extractFrontendUrl(req) {
79
- const allowedOrigins = _config.envConfig.getOrigins();
80
- const origin = (req.get('origin') ?? req.get('referer') ?? '').trim();
81
- if (origin && allowedOrigins.length) {
82
- const matched = allowedOrigins.find((u)=>origin.startsWith(u));
83
- if (matched) {
84
- return matched;
85
- }
86
- }
87
- const host = req.get('host');
88
- if (!host) {
89
- throw new Error('Unable to determine frontend URL from request');
90
- }
91
- return `${req.protocol}://${host}`;
92
- }
93
- function buildFrontendUrl(req, path) {
94
- const baseUrl = extractFrontendUrl(req);
95
- return `${baseUrl}${path}`;
96
- }
@@ -1,249 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- const _requestutil = require("./request.util");
6
- const isProductionMock = jest.fn();
7
- const getOriginsMock = jest.fn();
8
- jest.mock('@flusys/nestjs-core/config', ()=>({
9
- envConfig: {
10
- isProduction: ()=>isProductionMock(),
11
- getOrigins: ()=>getOriginsMock()
12
- }
13
- }));
14
- function buildRequest(overrides = {}) {
15
- return {
16
- headers: {},
17
- hostname: 'app.example.com',
18
- secure: false,
19
- get: jest.fn((name)=>overrides.headers?.[name.toLowerCase()]),
20
- ...overrides
21
- };
22
- }
23
- describe('request.util', ()=>{
24
- beforeEach(()=>{
25
- isProductionMock.mockReset().mockReturnValue(false);
26
- getOriginsMock.mockReset().mockReturnValue([]);
27
- });
28
- describe('isBrowserRequest', ()=>{
29
- it('should return true when x-client-type header is "browser"', ()=>{
30
- const req = buildRequest({
31
- headers: {
32
- 'x-client-type': 'browser'
33
- }
34
- });
35
- expect((0, _requestutil.isBrowserRequest)(req)).toBe(true);
36
- });
37
- it('should return true when x-client-type header is "web"', ()=>{
38
- const req = buildRequest({
39
- headers: {
40
- 'x-client-type': 'WEB'
41
- }
42
- });
43
- expect((0, _requestutil.isBrowserRequest)(req)).toBe(true);
44
- });
45
- it('should return false when x-client-type header is a non-browser value', ()=>{
46
- const req = buildRequest({
47
- headers: {
48
- 'x-client-type': 'mobile'
49
- }
50
- });
51
- expect((0, _requestutil.isBrowserRequest)(req)).toBe(false);
52
- });
53
- it('should return true when accept header includes text/html', ()=>{
54
- const req = buildRequest({
55
- headers: {
56
- accept: 'text/html,application/xhtml+xml'
57
- }
58
- });
59
- expect((0, _requestutil.isBrowserRequest)(req)).toBe(true);
60
- });
61
- it('should return true for a recognized browser user-agent', ()=>{
62
- const req = buildRequest({
63
- headers: {
64
- 'user-agent': 'Mozilla/5.0 Chrome/100.0 Safari/537.36'
65
- }
66
- });
67
- expect((0, _requestutil.isBrowserRequest)(req)).toBe(true);
68
- });
69
- it('should return false for Postman user-agent even if it matches browser patterns', ()=>{
70
- const req = buildRequest({
71
- headers: {
72
- 'user-agent': 'PostmanRuntime/7.0 chrome'
73
- }
74
- });
75
- expect((0, _requestutil.isBrowserRequest)(req)).toBe(false);
76
- });
77
- it('should return false when no headers indicate a browser', ()=>{
78
- const req = buildRequest({
79
- headers: {
80
- 'user-agent': 'curl/7.79.1'
81
- }
82
- });
83
- expect((0, _requestutil.isBrowserRequest)(req)).toBe(false);
84
- });
85
- });
86
- describe('buildCookieOptions', ()=>{
87
- it('should mark cookies secure and sameSite strict in production', ()=>{
88
- isProductionMock.mockReturnValue(true);
89
- const req = buildRequest({
90
- headers: {}
91
- });
92
- const options = (0, _requestutil.buildCookieOptions)(req);
93
- expect(options.secure).toBe(true);
94
- expect(options.sameSite).toBe('strict');
95
- });
96
- it('should mark cookies insecure and sameSite lax in dev with no forwarded proto', ()=>{
97
- isProductionMock.mockReturnValue(false);
98
- const req = buildRequest({
99
- headers: {}
100
- });
101
- const options = (0, _requestutil.buildCookieOptions)(req);
102
- expect(options.secure).toBe(false);
103
- expect(options.sameSite).toBe('lax');
104
- });
105
- it('should treat x-forwarded-proto: https as secure', ()=>{
106
- isProductionMock.mockReturnValue(false);
107
- const req = buildRequest({
108
- headers: {
109
- 'x-forwarded-proto': 'https'
110
- }
111
- });
112
- const options = (0, _requestutil.buildCookieOptions)(req);
113
- expect(options.secure).toBe(true);
114
- expect(options.sameSite).toBe('strict');
115
- });
116
- it('should treat an https origin header as secure', ()=>{
117
- isProductionMock.mockReturnValue(false);
118
- const req = buildRequest({
119
- headers: {
120
- origin: 'https://app.example.com'
121
- }
122
- });
123
- const options = (0, _requestutil.buildCookieOptions)(req);
124
- expect(options.secure).toBe(true);
125
- });
126
- it('should treat req.secure as secure', ()=>{
127
- isProductionMock.mockReturnValue(false);
128
- const req = buildRequest({
129
- headers: {},
130
- secure: true
131
- });
132
- const options = (0, _requestutil.buildCookieOptions)(req);
133
- expect(options.secure).toBe(true);
134
- });
135
- it('should not set a domain for a localhost hostname', ()=>{
136
- isProductionMock.mockReturnValue(false);
137
- const req = buildRequest({
138
- headers: {},
139
- hostname: 'localhost'
140
- });
141
- const options = (0, _requestutil.buildCookieOptions)(req);
142
- expect(options.domain).toBeUndefined();
143
- });
144
- it('should not set a domain for a bare two-part hostname', ()=>{
145
- isProductionMock.mockReturnValue(false);
146
- const req = buildRequest({
147
- headers: {},
148
- hostname: 'example.com'
149
- });
150
- const options = (0, _requestutil.buildCookieOptions)(req);
151
- expect(options.domain).toBeUndefined();
152
- });
153
- it('should set a leading-dot domain for a subdomain hostname', ()=>{
154
- isProductionMock.mockReturnValue(false);
155
- const req = buildRequest({
156
- headers: {},
157
- hostname: 'app.example.com'
158
- });
159
- const options = (0, _requestutil.buildCookieOptions)(req);
160
- expect(options.domain).toBe('.example.com');
161
- });
162
- });
163
- describe('parseDurationToMs', ()=>{
164
- it('should parse seconds', ()=>{
165
- expect((0, _requestutil.parseDurationToMs)('30s')).toBe(30 * 1000);
166
- });
167
- it('should parse minutes', ()=>{
168
- expect((0, _requestutil.parseDurationToMs)('15m')).toBe(15 * 60 * 1000);
169
- });
170
- it('should parse hours', ()=>{
171
- expect((0, _requestutil.parseDurationToMs)('2h')).toBe(2 * 60 * 60 * 1000);
172
- });
173
- it('should parse days', ()=>{
174
- expect((0, _requestutil.parseDurationToMs)('7d')).toBe(7 * 24 * 60 * 60 * 1000);
175
- });
176
- it('should parse weeks', ()=>{
177
- expect((0, _requestutil.parseDurationToMs)('2w')).toBe(2 * 7 * 24 * 60 * 60 * 1000);
178
- });
179
- it('should return the default when the string does not match the pattern', ()=>{
180
- expect((0, _requestutil.parseDurationToMs)('garbage')).toBe(7 * 24 * 60 * 60 * 1000);
181
- });
182
- it('should return a custom default when provided and pattern does not match', ()=>{
183
- expect((0, _requestutil.parseDurationToMs)('garbage', 12345)).toBe(12345);
184
- });
185
- });
186
- describe('extractFrontendUrl', ()=>{
187
- it('should return the matched allowed origin when the request origin starts with it', ()=>{
188
- getOriginsMock.mockReturnValue([
189
- 'https://app.example.com'
190
- ]);
191
- const req = buildRequest({
192
- headers: {
193
- origin: 'https://app.example.com'
194
- },
195
- get: jest.fn((name)=>{
196
- if (name === 'origin') return 'https://app.example.com';
197
- return undefined;
198
- })
199
- });
200
- expect((0, _requestutil.extractFrontendUrl)(req)).toBe('https://app.example.com');
201
- });
202
- it('should fall back to referer when origin header is absent', ()=>{
203
- getOriginsMock.mockReturnValue([
204
- 'https://app.example.com'
205
- ]);
206
- const req = buildRequest({
207
- get: jest.fn((name)=>{
208
- if (name === 'referer') return 'https://app.example.com/some/path';
209
- return undefined;
210
- })
211
- });
212
- expect((0, _requestutil.extractFrontendUrl)(req)).toBe('https://app.example.com');
213
- });
214
- it('should fall back to protocol + host when no origin matches an allowed origin', ()=>{
215
- getOriginsMock.mockReturnValue([
216
- 'https://other.example.com'
217
- ]);
218
- const req = buildRequest({
219
- get: jest.fn((name)=>{
220
- if (name === 'host') return 'app.example.com';
221
- return undefined;
222
- }),
223
- protocol: 'https'
224
- });
225
- expect((0, _requestutil.extractFrontendUrl)(req)).toBe('https://app.example.com');
226
- });
227
- it('should throw when no origin/referer matches and no host header is present', ()=>{
228
- getOriginsMock.mockReturnValue([]);
229
- const req = buildRequest({
230
- get: jest.fn(()=>undefined)
231
- });
232
- expect(()=>(0, _requestutil.extractFrontendUrl)(req)).toThrow('Unable to determine frontend URL from request');
233
- });
234
- });
235
- describe('buildFrontendUrl', ()=>{
236
- it('should append the path to the resolved frontend base URL', ()=>{
237
- getOriginsMock.mockReturnValue([
238
- 'https://app.example.com'
239
- ]);
240
- const req = buildRequest({
241
- get: jest.fn((name)=>{
242
- if (name === 'origin') return 'https://app.example.com';
243
- return undefined;
244
- })
245
- });
246
- expect((0, _requestutil.buildFrontendUrl)(req, '/reset-password?token=abc')).toBe('https://app.example.com/reset-password?token=abc');
247
- });
248
- });
249
- });
@@ -1,63 +0,0 @@
1
- /**
2
- * String utility functions
3
- */ /**
4
- * Generate URL-friendly slug from a string
5
- * @param text - The text to convert to slug
6
- * @param maxLength - Maximum length of the slug (default: 100)
7
- * @returns URL-friendly slug
8
- *
9
- * @example
10
- * generateSlug('My Company Name') // 'my-company-name'
11
- * generateSlug('Hello World!') // 'hello-world'
12
- */ "use strict";
13
- Object.defineProperty(exports, "__esModule", {
14
- value: true
15
- });
16
- function _export(target, all) {
17
- for(var name in all)Object.defineProperty(target, name, {
18
- enumerable: true,
19
- get: Object.getOwnPropertyDescriptor(all, name).get
20
- });
21
- }
22
- _export(exports, {
23
- get generateSlug () {
24
- return generateSlug;
25
- },
26
- get generateUniqueSlug () {
27
- return generateUniqueSlug;
28
- }
29
- });
30
- function generateSlug(text, maxLength = 100) {
31
- return text.toLowerCase().trim().replace(/[^a-z0-9\s-]/g, '') // Remove special characters
32
- .replace(/\s+/g, '-') // Replace spaces with hyphens
33
- .replace(/-+/g, '-') // Replace multiple hyphens with single
34
- .replace(/^-+|-+$/g, '') // Remove leading/trailing hyphens
35
- .substring(0, maxLength);
36
- }
37
- async function generateUniqueSlug(text, findMatchingSlugs, maxLength = 100) {
38
- const baseSlug = generateSlug(text, maxLength);
39
- // Single query to get all matching slugs
40
- const existingSlugs = await findMatchingSlugs(baseSlug);
41
- // No collisions - base slug is available
42
- if (existingSlugs.length === 0) {
43
- return baseSlug;
44
- }
45
- // Build set for O(1) lookup
46
- const existingSet = new Set(existingSlugs);
47
- // Base slug not taken (only suffixed versions exist)
48
- if (!existingSet.has(baseSlug)) {
49
- return baseSlug;
50
- }
51
- // Find next available suffix in memory (fast)
52
- let counter = 1;
53
- while(counter < 10000){
54
- const slugWithSuffix = `${baseSlug}-${counter}`;
55
- if (!existingSet.has(slugWithSuffix)) {
56
- return slugWithSuffix;
57
- }
58
- counter++;
59
- }
60
- // Fallback: append random string (extremely rare edge case)
61
- const randomSuffix = Math.random().toString(36).substring(2, 8);
62
- return `${baseSlug}-${randomSuffix}`;
63
- }
@@ -1,63 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- const _stringutil = require("./string.util");
6
- describe('string.util', ()=>{
7
- describe('generateSlug', ()=>{
8
- it('should lowercase and hyphenate a plain string', ()=>{
9
- expect((0, _stringutil.generateSlug)('My Company Name')).toBe('my-company-name');
10
- });
11
- it('should strip special characters', ()=>{
12
- expect((0, _stringutil.generateSlug)('Hello World!')).toBe('hello-world');
13
- });
14
- it('should collapse repeated whitespace and hyphens', ()=>{
15
- expect((0, _stringutil.generateSlug)('Foo Bar--Baz')).toBe('foo-bar-baz');
16
- });
17
- it('should trim leading/trailing hyphens produced by punctuation', ()=>{
18
- expect((0, _stringutil.generateSlug)(' -Hello- ')).toBe('hello');
19
- });
20
- it('should truncate to maxLength', ()=>{
21
- expect((0, _stringutil.generateSlug)('abcdefghij', 5)).toBe('abcde');
22
- });
23
- it('should return an empty string for input that is only special characters', ()=>{
24
- expect((0, _stringutil.generateSlug)('!!!???')).toBe('');
25
- });
26
- });
27
- describe('generateUniqueSlug', ()=>{
28
- it('should return the base slug when there are no collisions', async ()=>{
29
- const findMatchingSlugs = jest.fn().mockResolvedValue([]);
30
- const slug = await (0, _stringutil.generateUniqueSlug)('My Company', findMatchingSlugs);
31
- expect(slug).toBe('my-company');
32
- expect(findMatchingSlugs).toHaveBeenCalledWith('my-company');
33
- });
34
- it('should return the base slug when only suffixed variants exist', async ()=>{
35
- const findMatchingSlugs = jest.fn().mockResolvedValue([
36
- 'my-company-1'
37
- ]);
38
- const slug = await (0, _stringutil.generateUniqueSlug)('My Company', findMatchingSlugs);
39
- expect(slug).toBe('my-company');
40
- });
41
- it('should append the next available numeric suffix on collision', async ()=>{
42
- const findMatchingSlugs = jest.fn().mockResolvedValue([
43
- 'my-company',
44
- 'my-company-1',
45
- 'my-company-2'
46
- ]);
47
- const slug = await (0, _stringutil.generateUniqueSlug)('My Company', findMatchingSlugs);
48
- expect(slug).toBe('my-company-3');
49
- });
50
- it('should fall back to a random suffix when all 9999 slots are taken', async ()=>{
51
- const taken = [
52
- 'my-company',
53
- ...Array.from({
54
- length: 9999
55
- }, (_, i)=>`my-company-${i + 1}`)
56
- ];
57
- const findMatchingSlugs = jest.fn().mockResolvedValue(taken);
58
- const slug = await (0, _stringutil.generateUniqueSlug)('My Company', findMatchingSlugs);
59
- expect(slug).toMatch(/^my-company-[a-z0-9]+$/);
60
- expect(taken).not.toContain(slug);
61
- });
62
- });
63
- });