@futdevpro/nts-dynamo 1.15.13 → 1.15.14

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 (352) hide show
  1. package/.c8rc.json +26 -26
  2. package/.copilot/patterns.json +7 -7
  3. package/.cursor/rules/__assistant_guide.mdc +30 -30
  4. package/.cursor/rules/_ag_backend-structure.mdc +85 -85
  5. package/.cursor/rules/_ag_backend.mdc +16 -16
  6. package/.cursor/rules/_ag_frontend-structure.mdc +86 -86
  7. package/.cursor/rules/_ag_frontend.mdc +39 -39
  8. package/.cursor/rules/_ag_import-rules.mdc +44 -44
  9. package/.cursor/rules/_ag_naming.mdc +115 -115
  10. package/.cursor/rules/_ag_should-be.mdc +6 -6
  11. package/.cursor/rules/ai_development_guide.md +60 -60
  12. package/.cursor/rules/cursor-rules.md +160 -160
  13. package/.cursor/rules/default-command.mdc +464 -464
  14. package/.cursor/rules/error_code_pattern.md +39 -39
  15. package/.cursor/rules/saved rule mcp server use.md +15 -15
  16. package/.vscode/settings.json +10 -10
  17. package/HOWTO.md +15 -15
  18. package/LICENSE +21 -21
  19. package/__documentations/nts-integration-tests-2026-03-17.md +26 -26
  20. package/_specifications/BACKLOG.md +50 -22
  21. package/_specifications/TODO.md +15 -15
  22. package/_specifications/agent.md +138 -138
  23. package/build/_collections/global-settings.const.d.ts.map +1 -1
  24. package/build/_collections/global-settings.const.js +4 -0
  25. package/build/_collections/global-settings.const.js.map +1 -1
  26. package/build/_models/interfaces/global-log-settings.interface.d.ts +11 -0
  27. package/build/_models/interfaces/global-log-settings.interface.d.ts.map +1 -1
  28. package/build/_modules/logs/get-logs-routing-module.util.d.ts +19 -0
  29. package/build/_modules/logs/get-logs-routing-module.util.d.ts.map +1 -0
  30. package/build/_modules/logs/get-logs-routing-module.util.js +32 -0
  31. package/build/_modules/logs/get-logs-routing-module.util.js.map +1 -0
  32. package/build/_modules/logs/index.d.ts +4 -0
  33. package/build/_modules/logs/index.d.ts.map +1 -0
  34. package/build/_modules/logs/index.js +10 -0
  35. package/build/_modules/logs/index.js.map +1 -0
  36. package/build/_modules/logs/log-buffer.service.d.ts +38 -0
  37. package/build/_modules/logs/log-buffer.service.d.ts.map +1 -0
  38. package/build/_modules/logs/log-buffer.service.js +97 -0
  39. package/build/_modules/logs/log-buffer.service.js.map +1 -0
  40. package/build/_modules/logs/logs.controller.d.ts +27 -0
  41. package/build/_modules/logs/logs.controller.d.ts.map +1 -0
  42. package/build/_modules/logs/logs.controller.js +90 -0
  43. package/build/_modules/logs/logs.controller.js.map +1 -0
  44. package/build/_modules/logs/logs.service.d.ts +40 -0
  45. package/build/_modules/logs/logs.service.d.ts.map +1 -0
  46. package/build/_modules/logs/logs.service.js +97 -0
  47. package/build/_modules/logs/logs.service.js.map +1 -0
  48. package/build/_modules/server/errors/errors.data-service.d.ts.map +1 -1
  49. package/build/_modules/server/errors/errors.data-service.js +2 -1
  50. package/build/_modules/server/errors/errors.data-service.js.map +1 -1
  51. package/eslint.config.js +3 -3
  52. package/nodemon.json +24 -24
  53. package/package.json +359 -353
  54. package/scripts/run-coverage-tests.js +28 -28
  55. package/spec/support/helpers/spec-reporter-loader.js +359 -359
  56. package/spec/support/helpers/ts-node-helper.js +93 -93
  57. package/spec/support/jasmine.coverage.json +24 -24
  58. package/spec/support/jasmine.json +24 -24
  59. package/src/_collections/archive.util.spec.ts +57 -57
  60. package/src/_collections/archive.util.ts +18 -18
  61. package/src/_collections/atlas-default-db-options.const.ts +9 -9
  62. package/src/_collections/default-fallback-cache-max-age.const.spec.ts +11 -11
  63. package/src/_collections/default-fallback-cache-max-age.const.ts +2 -2
  64. package/src/_collections/default-not-found-page.const.spec.ts +19 -19
  65. package/src/_collections/default-not-found-page.const.ts +22 -22
  66. package/src/_collections/default-socket-path.const.spec.ts +12 -12
  67. package/src/_collections/default-socket-path.const.ts +2 -2
  68. package/src/_collections/get-environment-settings.util.spec.ts +210 -210
  69. package/src/_collections/get-environment-settings.util.ts +48 -48
  70. package/src/_collections/global-settings.const.ts +5 -0
  71. package/src/_collections/sample.env +21 -21
  72. package/src/_collections/star.controller.spec.ts +224 -224
  73. package/src/_collections/star.controller.ts +129 -129
  74. package/src/_enums/data-model-type.enum.ts +14 -14
  75. package/src/_enums/data-service-function.enum.ts +24 -24
  76. package/src/_enums/predefined-data-types.enum.ts +16 -16
  77. package/src/_enums/route-security.enum.ts +12 -12
  78. package/src/_models/control-models/api-call-params.control-model.spec.ts +152 -152
  79. package/src/_models/control-models/api-call-params.control-model.ts +142 -142
  80. package/src/_models/control-models/app-ext-system-controls.control-model.spec.ts +52 -52
  81. package/src/_models/control-models/app-ext-system-controls.control-model.ts +9 -9
  82. package/src/_models/control-models/app-params.control-model.spec.ts +225 -225
  83. package/src/_models/control-models/app-params.control-model.ts +136 -136
  84. package/src/_models/control-models/app-system-controls.control-model.spec.ts +31 -31
  85. package/src/_models/control-models/app-system-controls.control-model.ts +9 -9
  86. package/src/_models/control-models/endpoint-params.control-model.spec.ts +578 -578
  87. package/src/_models/control-models/endpoint-params.control-model.ts +526 -526
  88. package/src/_models/control-models/http-settings.control-model.spec.ts +77 -77
  89. package/src/_models/control-models/http-settings.control-model.ts +37 -37
  90. package/src/_models/control-models/system-control.control-model.spec.ts +27 -27
  91. package/src/_models/control-models/system-control.control-model.ts +12 -12
  92. package/src/_models/interfaces/certification-settings.interface.ts +7 -7
  93. package/src/_models/interfaces/environment-settings.interface.ts +59 -59
  94. package/src/_models/interfaces/global-log-settings.interface.ts +108 -96
  95. package/src/_models/interfaces/global-service-settings.interface.ts +47 -47
  96. package/src/_models/interfaces/routing-module-settings.interface.ts +21 -21
  97. package/src/_models/interfaces/static-client-settings.interface.spec.ts +29 -29
  98. package/src/_models/interfaces/static-client-settings.interface.ts +28 -28
  99. package/src/_models/types/db-update.type.ts +100 -100
  100. package/src/_modules/ai/_models/ai-input-interfaces.ts +117 -117
  101. package/src/_modules/ai/_models/ai-test-generation-result.interface.ts +16 -16
  102. package/src/_modules/ai/_modules/anthropic/_services/aai-user-key.control-service.ts +138 -138
  103. package/src/_modules/ai/_modules/anthropic/index.ts +5 -5
  104. package/src/_modules/ai/_modules/document-ai/_collections/dai-chunking.util.spec.ts +242 -242
  105. package/src/_modules/ai/_modules/document-ai/_collections/dai-chunking.util.ts +639 -639
  106. package/src/_modules/ai/_modules/document-ai/_collections/dai-document.util.spec.ts +209 -209
  107. package/src/_modules/ai/_modules/document-ai/_collections/dai-document.util.ts +85 -85
  108. package/src/_modules/ai/_modules/document-ai/_enums/dai-compare-result-type.enum.ts +7 -7
  109. package/src/_modules/ai/_modules/document-ai/_models/data-models/dai-doc-chunk.data-model.ts +146 -146
  110. package/src/_modules/ai/_modules/document-ai/_models/data-models/dai-doc-page.data-model.ts +162 -162
  111. package/src/_modules/ai/_modules/document-ai/_models/data-models/dai-document.data-model.ts +99 -99
  112. package/src/_modules/ai/_modules/document-ai/_models/interfaces/dai-doc-chunk-compare-result.interface.ts +18 -18
  113. package/src/_modules/ai/_modules/document-ai/_models/interfaces/dai-doc-page-compare-result.interface.ts +19 -19
  114. package/src/_modules/ai/_modules/document-ai/_models/interfaces/dai-document-compare-result.interface.ts +25 -25
  115. package/src/_modules/ai/_modules/document-ai/index.ts +28 -28
  116. package/src/_modules/ai/_modules/fdp-ai/_services/fdpai-user-key.control-service.ts +189 -189
  117. package/src/_modules/ai/_modules/fdp-ai/index.ts +5 -5
  118. package/src/_modules/ai/_modules/open-ai/_collections/oai-global-settings.const.ts +9 -9
  119. package/src/_modules/ai/_modules/open-ai/_collections/oai-llm-predefined-requests-hu.conts.ts +82 -82
  120. package/src/_modules/ai/_modules/open-ai/_collections/oai-llm-predefined-requests.conts.ts +75 -75
  121. package/src/_modules/ai/_modules/open-ai/_enums/oai-gpt-message-role.enum.ts +45 -45
  122. package/src/_modules/ai/_modules/open-ai/_models/interfaces/oai-global-settings.interface.ts +7 -7
  123. package/src/_modules/ai/_modules/open-ai/_models/interfaces/oai-gpt-message.interface.ts +7 -7
  124. package/src/_modules/ai/_modules/open-ai/_models/interfaces/oai-llm-predefined-requests.interface.ts +57 -57
  125. package/src/_modules/ai/_modules/open-ai/_services/data-services/oai-doc-chunk-data.service.ts +292 -292
  126. package/src/_modules/ai/_modules/open-ai/_services/data-services/oai-document.data-service.spec.ts +342 -342
  127. package/src/_modules/ai/_modules/open-ai/_services/data-services/oai-vector-data.service.spec.ts +550 -550
  128. package/src/_modules/ai/_modules/open-ai/_services/data-services/oai-vector-data.service.ts +630 -630
  129. package/src/_modules/ai/_modules/open-ai/_services/oai-embedding.control-service.spec.ts +240 -240
  130. package/src/_modules/ai/_modules/open-ai/_services/oai-embedding.control-service.ts +98 -98
  131. package/src/_modules/ai/_modules/open-ai/_services/oai-llm-chat.service-base.spec.ts +462 -462
  132. package/src/_modules/ai/_modules/open-ai/_services/oai-llm-chat.service-base.ts +615 -615
  133. package/src/_modules/ai/_modules/open-ai/_services/oai-llm.service-base.spec.ts +437 -437
  134. package/src/_modules/ai/_modules/open-ai/_services/oai-llm.service-base.ts +833 -833
  135. package/src/_modules/ai/_modules/open-ai/_services/oai-user-key.control-service.ts +157 -157
  136. package/src/_modules/ai/_services/ai-embedding.service-base.spec.ts +98 -98
  137. package/src/_modules/ai/_services/ai-embedding.service-base.ts +48 -48
  138. package/src/_modules/ai/_services/ai-llm-chat.service-base.spec.ts +229 -229
  139. package/src/_modules/ai/_services/ai-llm-chat.service-base.ts +68 -68
  140. package/src/_modules/ai/_services/ai-llm.service-base.spec.ts +250 -250
  141. package/src/_modules/ai/_services/ai-llm.service-base.ts +332 -332
  142. package/src/_modules/ai/_services/ai-provider.service-base.spec.ts +79 -79
  143. package/src/_modules/ai/_services/ai-provider.service-base.ts +29 -29
  144. package/src/_modules/ai/_services/ai-user-key.service-base.ts +59 -59
  145. package/src/_modules/ai/index.ts +13 -13
  146. package/src/_modules/assistant/_collections/ass-global-settings.const.ts +13 -13
  147. package/src/_modules/assistant/_collections/ass.util.spec.ts +176 -176
  148. package/src/_modules/assistant/_collections/ass.util.ts +50 -50
  149. package/src/_modules/assistant/_models/ass-global-settings.interface.ts +15 -15
  150. package/src/_modules/assistant/_services/ass-io.control-service.spec.ts +140 -140
  151. package/src/_modules/assistant/_services/ass-main.control-service.spec.ts +192 -192
  152. package/src/_modules/assistant/_services/ass-main.control-service.ts +107 -107
  153. package/src/_modules/bot/_collections/bot-default-commands.const.ts +12 -12
  154. package/src/_modules/bot/_collections/bot-global-settings.const.ts +39 -39
  155. package/src/_modules/bot/_models/bot-channel-wrapper.interface.ts +62 -62
  156. package/src/_modules/bot/_models/bot-command.interface.ts +8 -8
  157. package/src/_modules/bot/_models/bot-global-settings.interface.ts +96 -96
  158. package/src/_modules/bot/_models/bot-last-mention-date.interface.ts +6 -6
  159. package/src/_modules/bot/_models/bot-last-message-date.interface.ts +5 -5
  160. package/src/_modules/bot/_models/bot-user-wrapper.interface.ts +41 -41
  161. package/src/_modules/bot/_modules/discord-bot/_models/dib-platform.types.ts +9 -9
  162. package/src/_modules/bot/_modules/discord-bot/_services/dib-messaging-provider.control-service.spec.ts +431 -431
  163. package/src/_modules/bot/_modules/dynamo-bot/_collections/dyb-operations.util.spec.ts +160 -160
  164. package/src/_modules/bot/_modules/dynamo-bot/_collections/dyb-operations.util.ts +55 -55
  165. package/src/_modules/bot/_modules/dynamo-bot/_models/dyb-platform.types.ts +15 -15
  166. package/src/_modules/bot/_modules/dynamo-bot/_services/dyb-messaging-provider.control-service.spec.ts +374 -374
  167. package/src/_modules/bot/_modules/dynamo-bot/_services/dyb-messaging-provider.control-service.ts +447 -447
  168. package/src/_modules/bot/_modules/dynamo-bot/index.ts +15 -15
  169. package/src/_modules/bot/_modules/slack-bot/_models/slb-platform.types.ts +9 -9
  170. package/src/_modules/bot/_modules/slack-bot/_services/slb-messaging-provider.control-service.spec.ts +344 -344
  171. package/src/_modules/bot/_modules/slack-bot/_services/slb-messaging-provider.control-service.ts +197 -197
  172. package/src/_modules/bot/_modules/teams-bot/_models/teb-platform.types.ts +9 -9
  173. package/src/_modules/bot/_modules/teams-bot/_services/teb-messaging-provider.control-service.spec.ts +345 -345
  174. package/src/_modules/bot/_modules/teams-bot/_services/teb-messaging-provider.control-service.ts +197 -197
  175. package/src/_modules/bot/_services/bot-commands.control-service.spec.ts +116 -116
  176. package/src/_modules/bot/_services/bot-io.control-service.spec.ts +285 -285
  177. package/src/_modules/bot/_services/bot-main.control-service.spec.ts +208 -208
  178. package/src/_modules/bot/_services/bot-messaging-provider.service-base.spec.ts +349 -349
  179. package/src/_modules/bot/_services/bot-routines.control-service.spec.ts +111 -111
  180. package/src/_modules/custom-data/custom-data.controller.spec.ts +49 -49
  181. package/src/_modules/custom-data/custom-data.controller.ts +67 -67
  182. package/src/_modules/custom-data/custom-data.data-service.spec.ts +54 -54
  183. package/src/_modules/custom-data/custom-data.data-service.ts +21 -21
  184. package/src/_modules/custom-data/get-custom-data-routing-module.util.spec.ts +28 -28
  185. package/src/_modules/custom-data/get-custom-data-routing-module.util.ts +24 -24
  186. package/src/_modules/custom-data/index.ts +9 -9
  187. package/src/_modules/defaults/_collections/default-endpoints.util.ts +487 -487
  188. package/src/_modules/defaults/_models/default-user.data-model.ts +72 -72
  189. package/src/_modules/defaults/_services/default-auth.service.spec.ts +269 -269
  190. package/src/_modules/defaults/_services/default-auth.service.ts +177 -177
  191. package/src/_modules/defaults/_services/default-socket-events.service.spec.ts +42 -42
  192. package/src/_modules/defaults/_services/default-socket-events.service.ts +61 -61
  193. package/src/_modules/defaults/_services/default-user.data-service.spec.ts +187 -187
  194. package/src/_modules/defaults/_services/default-user.data-service.ts +98 -98
  195. package/src/_modules/defaults/index.ts +17 -17
  196. package/src/_modules/discord-assistant/_collections/dias-global-settings.const.ts +19 -19
  197. package/src/_modules/discord-assistant/_collections/dias.util.spec.ts +366 -366
  198. package/src/_modules/discord-assistant/_collections/dias.util.ts +132 -132
  199. package/src/_modules/discord-assistant/_models/dias-global-settings.interface.ts +19 -19
  200. package/src/_modules/discord-assistant/_models/dias-knowledge.data-model.ts +52 -52
  201. package/src/_modules/discord-assistant/_services/dias-chunk.data-service.ts +177 -177
  202. package/src/_modules/discord-assistant/_services/dias-io.control-service.spec.ts +108 -108
  203. package/src/_modules/discord-assistant/_services/dias-io.control-service.ts +69 -69
  204. package/src/_modules/discord-assistant/_services/dias-main.control-service.spec.ts +22 -22
  205. package/src/_modules/discord-assistant/_services/dias-main.control-service.ts +27 -27
  206. package/src/_modules/discord-assistant/_services/dias.service-base.spec.ts +195 -195
  207. package/src/_modules/discord-assistant/_services/dias.service-base.ts +76 -76
  208. package/src/_modules/discord-assistant/index.ts +38 -38
  209. package/src/_modules/discord-assistant-voiced/_services/dias-discord-bot.control-service.spec.ts +34 -34
  210. package/src/_modules/discord-assistant-voiced/_services/dias-discord-bot.control-service.ts +11 -11
  211. package/src/_modules/discord-assistant-voiced/index.ts +36 -36
  212. package/src/_modules/discord-bot/_collections/dibo-default-commands.const.ts +16 -16
  213. package/src/_modules/discord-bot/_collections/dibo-global-settings.conts.ts +55 -55
  214. package/src/_modules/discord-bot/_collections/dibo-operations.util.spec.ts +214 -214
  215. package/src/_modules/discord-bot/_collections/dibo-operations.util.ts +387 -387
  216. package/src/_modules/discord-bot/_models/dibo-command.interface.ts +12 -12
  217. package/src/_modules/discord-bot/_models/dibo-global-settings.interface.ts +98 -98
  218. package/src/_modules/discord-bot/_models/dibo-last-mention-date.inteface.ts +7 -7
  219. package/src/_modules/discord-bot/_models/dibo-last-message-date.interface.ts +6 -6
  220. package/src/_modules/discord-bot/_services/dibo-commands.control-service.spec.ts +154 -154
  221. package/src/_modules/discord-bot/_services/dibo-commands.control-service.ts +153 -153
  222. package/src/_modules/discord-bot/_services/dibo-io.control-service.spec.ts +264 -264
  223. package/src/_modules/discord-bot/_services/dibo-io.control-service.ts +306 -306
  224. package/src/_modules/discord-bot/_services/dibo-main.control-service.spec.ts +408 -408
  225. package/src/_modules/discord-bot/_services/dibo-main.control-service.ts +487 -487
  226. package/src/_modules/discord-bot/_services/dibo-routines.control-service.spec.ts +105 -105
  227. package/src/_modules/discord-bot/index.ts +36 -36
  228. package/src/_modules/local-vector-search/_enums/lvs-search-mode.enum.ts +19 -19
  229. package/src/_modules/local-vector-search/_models/lvs-search-result.interface.ts +17 -17
  230. package/src/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.spec.ts +418 -418
  231. package/src/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.ts +276 -276
  232. package/src/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.spec.ts +345 -345
  233. package/src/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.ts +330 -330
  234. package/src/_modules/local-vector-search/_services/lvs-vector-pool.control-service.spec.ts +393 -393
  235. package/src/_modules/local-vector-search/_services/lvs-vector-pool.control-service.ts +220 -220
  236. package/src/_modules/local-vector-search/index.ts +11 -11
  237. package/src/_modules/logs/get-logs-routing-module.util.ts +36 -0
  238. package/src/_modules/logs/index.ts +3 -0
  239. package/src/_modules/logs/log-buffer.service.ts +101 -0
  240. package/src/_modules/logs/logs.controller.ts +109 -0
  241. package/src/_modules/logs/logs.service.ts +100 -0
  242. package/src/_modules/messaging/README.md +354 -354
  243. package/src/_modules/messaging/_collections/get-messaging-routing-module.util.ts +26 -26
  244. package/src/_modules/messaging/_collections/msg-global-settings.const.ts +22 -22
  245. package/src/_modules/messaging/_collections/msg.util.spec.ts +226 -226
  246. package/src/_modules/messaging/_models/msg-global-settings.interface.ts +37 -37
  247. package/src/_modules/messaging/_services/msg-conversation.data-service.ts +146 -146
  248. package/src/_modules/messaging/_services/msg-events.service.spec.ts +219 -219
  249. package/src/_modules/messaging/_services/msg-events.service.ts +267 -267
  250. package/src/_modules/messaging/_services/msg-integration.control-service.ts +179 -179
  251. package/src/_modules/messaging/_services/msg-main.control-service.spec.ts +147 -147
  252. package/src/_modules/messaging/_services/msg-main.control-service.ts +571 -571
  253. package/src/_modules/messaging/_services/msg-message.data-service.ts +129 -129
  254. package/src/_modules/messaging/_services/msg.controller.spec.ts +201 -201
  255. package/src/_modules/messaging/index.ts +30 -30
  256. package/src/_modules/mock/app-extended-server.mock.ts +201 -201
  257. package/src/_modules/mock/app-integration-test.mock.ts +51 -51
  258. package/src/_modules/mock/app-params.mock.spec.ts +21 -21
  259. package/src/_modules/mock/app-params.mock.ts +9 -9
  260. package/src/_modules/mock/app-server.mock.ts +188 -188
  261. package/src/_modules/mock/auth-service.mock.spec.ts +47 -47
  262. package/src/_modules/mock/auth-service.mock.ts +28 -28
  263. package/src/_modules/mock/controller.mock.spec.ts +26 -26
  264. package/src/_modules/mock/controller.mock.ts +16 -16
  265. package/src/_modules/mock/data-model.mock.spec.ts +111 -111
  266. package/src/_modules/mock/data-model.mock.ts +82 -82
  267. package/src/_modules/mock/email-service-collection.mock.spec.ts +24 -24
  268. package/src/_modules/mock/email-service-collection.mock.ts +15 -15
  269. package/src/_modules/mock/email-service.mock.spec.ts +17 -17
  270. package/src/_modules/mock/email-service.mock.ts +20 -20
  271. package/src/_modules/mock/email-template.mock.html +14 -14
  272. package/src/_modules/mock/endpoint.mock.ts +91 -91
  273. package/src/_modules/mock/socket-client.mock.spec.ts +40 -40
  274. package/src/_modules/mock/socket-client.mock.ts +45 -45
  275. package/src/_modules/mock/socket-server.mock.spec.ts +44 -44
  276. package/src/_modules/mock/socket-server.mock.ts +46 -46
  277. package/src/_modules/oauth2/_routes/oauth2.controller.spec.ts +107 -107
  278. package/src/_modules/oauth2/_routes/oauth2.controller.ts +98 -98
  279. package/src/_modules/oauth2/_services/oauth2.auth-service.spec.ts +254 -254
  280. package/src/_modules/oauth2/_services/oauth2.auth-service.ts +232 -232
  281. package/src/_modules/oauth2/_services/oauth2.control-service.spec.ts +585 -585
  282. package/src/_modules/oauth2/_services/oauth2.control-service.ts +653 -653
  283. package/src/_modules/oauth2/index.ts +17 -17
  284. package/src/_modules/server/errors/errors.control-service.spec.ts +230 -230
  285. package/src/_modules/server/errors/errors.control-service.ts +69 -69
  286. package/src/_modules/server/errors/errors.controller.spec.ts +165 -165
  287. package/src/_modules/server/errors/errors.controller.ts +270 -270
  288. package/src/_modules/server/errors/errors.data-service.spec.ts +355 -355
  289. package/src/_modules/server/errors/errors.data-service.ts +2 -1
  290. package/src/_modules/server/index.ts +30 -30
  291. package/src/_modules/server/server-status/server-status-snapshot.control-service.spec.ts +70 -70
  292. package/src/_modules/server/server-status/server-status-snapshot.control-service.ts +17 -17
  293. package/src/_modules/server/server-status/server-status-snapshot.data-service.spec.ts +77 -77
  294. package/src/_modules/server/server-status/server-status-snapshot.data-service.ts +37 -37
  295. package/src/_modules/server/server-status/server-status.control-service.spec.ts +516 -516
  296. package/src/_modules/server/server-status/server-status.control-service.ts +336 -336
  297. package/src/_modules/server/server-status/server-status.controller.spec.ts +156 -156
  298. package/src/_modules/server/server-status/server-status.controller.ts +131 -131
  299. package/src/_modules/socket/_enums/socket-security.enum.ts +11 -11
  300. package/src/_modules/socket/_models/socket-client-service-params.control-model.spec.ts +32 -32
  301. package/src/_modules/socket/_models/socket-client-service-params.control-model.ts +22 -22
  302. package/src/_modules/socket/_models/socket-presence.control-model.spec.ts +164 -164
  303. package/src/_modules/socket/_models/socket-presence.control-model.ts +210 -210
  304. package/src/_modules/socket/_models/socket-server-service-params.control-model.spec.ts +46 -46
  305. package/src/_modules/socket/_models/socket-server-service-params.control-model.ts +22 -22
  306. package/src/_modules/socket/_services/socket-client.service.spec.ts +15 -15
  307. package/src/_modules/socket/_services/socket-client.service.ts +260 -260
  308. package/src/_modules/socket/_services/socket-server.service.spec.ts +11 -11
  309. package/src/_modules/socket/app-extended.integration.spec.ts +85 -85
  310. package/src/_modules/socket/app-extended.server.ts +630 -630
  311. package/src/_modules/socket/index.ts +42 -42
  312. package/src/_modules/test/get-test-routing-module.util.spec.ts +28 -28
  313. package/src/_modules/test/get-test-routing-module.util.ts +23 -23
  314. package/src/_modules/test/index.ts +11 -11
  315. package/src/_modules/test/test.controller.spec.ts +72 -72
  316. package/src/_modules/test/test.controller.ts +115 -115
  317. package/src/_modules/usage/get-usage-routing-module.util.ts +22 -22
  318. package/src/_modules/usage/index.ts +15 -15
  319. package/src/_modules/usage/usage.controller.spec.ts +81 -81
  320. package/src/_modules/usage/usage.controller.ts +126 -126
  321. package/src/_modules/usage/usage.data-service.spec.ts +332 -332
  322. package/src/_modules/usage/usage.data-service.ts +185 -185
  323. package/src/_services/base/api.service-base.spec.ts +125 -125
  324. package/src/_services/base/api.service-base.ts +74 -74
  325. package/src/_services/base/archive-data.service.spec.ts +196 -196
  326. package/src/_services/base/archive-data.service.ts +216 -216
  327. package/src/_services/base/data.service.spec.ts +493 -493
  328. package/src/_services/base/data.service.ts +2525 -2525
  329. package/src/_services/base/db.service.spec.ts +73 -73
  330. package/src/_services/base/db.service.ts +1575 -1575
  331. package/src/_services/base/singleton.service-base.spec.ts +28 -28
  332. package/src/_services/base/singleton.service-base.ts +24 -24
  333. package/src/_services/base/singleton.service.spec.ts +114 -114
  334. package/src/_services/base/singleton.service.ts +38 -38
  335. package/src/_services/core/api.service.spec.ts +140 -140
  336. package/src/_services/core/auth.service.spec.ts +159 -159
  337. package/src/_services/core/auth.service.ts +174 -174
  338. package/src/_services/core/email.service.spec.ts +85 -85
  339. package/src/_services/core/email.service.ts +742 -742
  340. package/src/_services/core/global.service.spec.ts +275 -275
  341. package/src/_services/core/global.service.ts +461 -461
  342. package/src/_services/core/service-collection.service.spec.ts +46 -46
  343. package/src/_services/core/service-collection.service.ts +6 -6
  344. package/src/_services/route/controller.service.spec.ts +53 -53
  345. package/src/_services/route/controller.service.ts +148 -148
  346. package/src/_services/route/routing-module.service.spec.ts +98 -98
  347. package/src/_services/route/routing-module.service.ts +330 -330
  348. package/src/_services/shared.static-service.spec.ts +99 -99
  349. package/src/_services/shared.static-service.ts +78 -78
  350. package/src/index.ts +94 -94
  351. package/tsconfig.app.json +12 -12
  352. package/tsconfig.json +42 -42
@@ -1,2525 +1,2525 @@
1
-
2
- import {
3
- DyFM_AnyError,
4
- DyFM_Array,
5
- DyFM_BasicProperty_Type,
6
- DyFM_DataModel_Params,
7
- DyFM_DataProperties,
8
- DyFM_DataProperty_Params,
9
- DyFM_DBFilter,
10
- DyFM_DBFilterExpressions,
11
- DyFM_DSNestedPropertySort,
12
- DyFM_DSPropertySort,
13
- DyFM_DSSort,
14
- DyFM_Error,
15
- DyFM_Error_Settings,
16
- DyFM_ErrorLevel,
17
- DyFM_Log,
18
- DyFM_Metadata,
19
- DyFM_NestPropertySearch,
20
- DyFM_Object,
21
- DyFM_RangeValue,
22
- DyFM_SearchQuery,
23
- DyFM_SearchResult,
24
- DyFM_SpecialSearch,
25
- DyFM_String,
26
- DyFM_ValidationError,
27
- DyFM_ValidationErrors
28
- } from '@futdevpro/fsm-dynamo';
29
-
30
- import { DyNTS_getArchivedDBName } from '../../_collections/archive.util';
31
- import { DyNTS_global_settings } from '../../_collections/global-settings.const';
32
- import { DyNTS_DBUpdate } from '../../_models/types/db-update.type';
33
- import { DyNTS_GlobalService } from '../core/global.service';
34
- import { DyNTS_ArchiveDataService } from './archive-data.service';
35
- import { DyNTS_DBService } from './db.service';
36
-
37
- // TODO: 2 type of archiving service system: within list, or separate db elements
38
-
39
- /**
40
- * Basic Data Service that is connected to the relevant DBServices
41
- *
42
- * @example
43
- * export class UserMatchStatisticsService extends DyNTS_DataService<UserMatchStatistics> {
44
- * constructor(
45
- * set: {
46
- * data?: UserMatchStatistics,
47
- * issuer: string,
48
- * }
49
- * ) {
50
- * super(
51
- * new UserMatchStatistics(set?.data),
52
- * userMatchStatisticsModelParams
53
- * );
54
- * this.issuer = set.issuer;
55
- * }
56
- * }
57
- */
58
- export class DyNTS_DataService<T extends DyFM_Metadata> {
59
-
60
- serviceName: string;
61
- serviceNameShortCode: string;
62
- /** error code base */
63
- ecBase: string = `${DyNTS_global_settings.systemShortCodeName}|`;
64
-
65
- dataDBService: DyNTS_DBService<T>;
66
- haveArchiveDataService: boolean;
67
-
68
- /* data: T; */
69
- /** @deprecated */
70
- dataList: T[] = [];
71
- /* issuer: string; */
72
-
73
- readonly depSettings: {
74
- key: string;
75
- dbServiceKey: string;
76
- keyIsUnique: boolean;
77
- keyIsRequired: boolean;
78
- }[] = [];
79
- /* depKeys: string[] = [];
80
- depDBServiceKeys: string[] = [];
81
- depKeyIsUnique: boolean[] = [];
82
- depKeyIsRequired: boolean[] = []; */
83
- private depDataDBService: DyNTS_DBService<any>;
84
-
85
- /* dataParams: DyFM_DataModel_Params<T>; */
86
-
87
- readonly defaultErrorUserMsg: string =
88
- `We encountered an unhandled Data Service Error, ` +
89
- `\nplease contact the responsible development team.`;
90
- readonly defaultValidationErrorUserMsg: string =
91
- `We encountered an unhandled Validation Error, ` +
92
- `\nplease contact the responsible development team.`;
93
-
94
- constructor(
95
- /**
96
- * Initial data, this will be used by functions on default
97
- * @deprecated
98
- */
99
- public data: T,
100
- /**
101
- * DB data prams will be used to connect to usable dbService on GlobalService
102
- */
103
- /* dataParams: DyFM_DataModel_Params, */
104
- public dataParams: DyFM_DataModel_Params<T>,
105
- /**
106
- * Initial set for issuer to be able to follow the issuer's activity
107
- */
108
- public issuer: string
109
- ) {
110
- try {
111
- this.serviceName = this.constructor?.name;
112
- this.serviceNameShortCode = DyFM_String.anyNameToShortCode(this.serviceName);
113
- this.ecBase = `${DyNTS_global_settings.systemShortCodeName}|${this.serviceNameShortCode}|`;
114
- this.dataDBService = DyNTS_GlobalService.getDBService<T>(this.dataParams);
115
- /* this.data = data; */
116
- /* this.dataParams = dataParams; */
117
- this.haveArchiveDataService = this.dataParams.addArchive;
118
- this.lookForDependencyDataSettings();
119
- /* this.issuer = issuer; */
120
- /* DyNTS_GlobalService.addDataService(this); */
121
- } catch (error) {
122
- throw new DyFM_Error({
123
- ...this._getDefaultErrorSettings('constructor', error),
124
-
125
- errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-DS0-C00`,
126
- message:
127
- `The dataService construction failed for "${this.dataParams?.dataName}". ` +
128
- `at "${this.serviceName}" (${this.constructor.name})` +
129
- `\nMaybe you forgot to add the dbService to the GlobalService?` +
130
- `\nOr just trying to use a dependencyDataName that is not present in this db...`,
131
- level: DyFM_ErrorLevel.critical,
132
- });
133
- /* DyFM_Log.error(
134
- `\nDyNTS_DataService ERROR: ` +
135
- `\nThe dataService construction failed for ` +
136
- `${this.dataParams?.dataName}. ${this.serviceName} (${this.constructor.name})` +
137
- `\nMaybe you forgot to add the dbService to the GlobalService?` +
138
- `\n\n`,
139
- new Error()
140
- ); */
141
- }
142
- }
143
-
144
- getArchiveDataService(): DyNTS_ArchiveDataService<T> {
145
- throw new DyFM_Error({
146
- ...this._getDefaultErrorSettings(
147
- 'getArchiveDataService',
148
- new Error('getArchiveDataService is not implemented!')
149
- ),
150
-
151
- errorCode: `${this.ecBase}DyNTS-DS-GAD0`,
152
- });
153
- }
154
-
155
- /**
156
- * returns all data from database to service dataList
157
- */
158
- async getAll(dontSetToService?: boolean): Promise<T[]> {
159
- try {
160
- const dataListExists: T[] = await this.dataDBService.getAll().catch((error): T[] => {
161
- if (error?.errorCode?.includes('DBS-GA1')) {
162
- DyFM_Log.warn(`getAll "${this.dataParams.dataName}" didn't found any.`);
163
-
164
- return [];
165
- } else {
166
- throw error;
167
- }
168
- });
169
-
170
- if (!dontSetToService) {
171
- this.dataList = dataListExists;
172
- }
173
-
174
- return dataListExists;
175
- } catch (error) {
176
- throw new DyFM_Error({
177
- ...this._getDefaultErrorSettings('getAll', error),
178
-
179
- errorCode: `${this.ecBase}DyNTS-DS-GA0`,
180
- });
181
- }
182
- }
183
-
184
- /**
185
- * @description
186
- * returns data from database by id
187
- * also if dontSetToService is false or not setted,
188
- * the data will be saved to the service, even if its not found
189
- *
190
- * if the data is not found, it will try to find it from the archive
191
- * unless skipArchiveLoad is set to true
192
- *
193
- * @remarks
194
- * If you need to get-save a data, if possible,
195
- * use db-service update instead.
196
- *
197
- * @param {string} id
198
- * (using id from service.data, if not provided)
199
- * @param dontSetToService
200
- *
201
- * @return {T} data: T
202
- */
203
- async getDataById(
204
- id?: string,
205
- dontSetToService?: boolean,
206
- skipArchiveLoad?: boolean
207
- ): Promise<T> {
208
- try {
209
- id = id ?? this.data?._id;
210
-
211
- if (!id) {
212
- throw new DyFM_Error({
213
- ...this._getDefaultErrorSettings(
214
- 'getDataById',
215
- new Error(
216
- `getDataById failed, ID is missing! ` +
217
- `(maybe you wanted to use getDataByDependencyId() instead...) ` +
218
- `(${this.dataParams.dataName})`
219
- )
220
- ),
221
-
222
- errorCode: `${this.ecBase}DyNTS-DS-GI1`,
223
- });
224
- }
225
-
226
- let dataExists: T = await this.dataDBService.getDataById(id);
227
-
228
- if (!dataExists && this.haveArchiveDataService && !skipArchiveLoad) {
229
- const archiveDataService: DyNTS_ArchiveDataService<T> = this.getArchiveDataService();
230
-
231
- dataExists = await archiveDataService.getDataByOriginalId(id, true);
232
- }
233
-
234
- if (!dontSetToService) {
235
- this.data = dataExists;
236
- }
237
-
238
- return dataExists;
239
- } catch (error) {
240
- if (error?.errorCode == `${DyNTS_global_settings.systemShortCodeName}|DyNTS-DS0-GI1`) {
241
- throw error;
242
- } else {
243
- throw new DyFM_Error({
244
- ...this._getDefaultErrorSettings('getDataById', error),
245
- errorCode: `${this.ecBase}DyNTS-DS-GI0`,
246
- });
247
- }
248
- }
249
- }
250
-
251
- async getDataByIds(ids: string[], dontSetToService?: boolean): Promise<T[]> {
252
- try {
253
- if (!ids) {
254
- throw new DyFM_Error({
255
- ...this._getDefaultErrorSettings(
256
- 'getDataByIds',
257
- new Error(`getDataByIds failed, ids is missing! (${this.dataParams.dataName})`)
258
- ),
259
-
260
- errorCode: `${this.ecBase}DyNTS-DS-GIS1`,
261
- });
262
- }
263
-
264
- if (ids.length === 0) {
265
- return [];
266
- }
267
-
268
- const dataList: T[] = await this.dataDBService.find({ _id: { $in: ids } });
269
-
270
- if (!dontSetToService) {
271
- this.dataList = dataList;
272
- }
273
-
274
- return dataList;
275
- } catch (error) {
276
- throw new DyFM_Error({
277
- ...this._getDefaultErrorSettings('getDataByIds', error),
278
-
279
- errorCode: `${this.ecBase}DyNTS-DS-GIS0`,
280
- });
281
- }
282
- }
283
-
284
- async getDataListByIds(ids: string[], dontSetToService?: boolean): Promise<T[]> {
285
- try {
286
- const dataList: T[] = await this.dataDBService.getDataListByIds(ids);
287
-
288
- if (!dontSetToService) {
289
- this.dataList = dataList;
290
- }
291
-
292
- return dataList;
293
- } catch (error) {
294
- throw new DyFM_Error({
295
- ...this._getDefaultErrorSettings('getDataListByIds', error),
296
-
297
- errorCode: `${this.ecBase}DyNTS-DS-GIL0`,
298
- });
299
- }
300
- }
301
-
302
- private getDependencyIdsFilter(
303
- dependencyIds?: string | { [key: string]: string }
304
- ): { [key: string]: string } {
305
- if (!this.depSettings.length) {
306
- DyFM_Log.warn(
307
- `❌ getDataByDependencyId failed, dependencyKey is missing from service! ` +
308
- `(${this.dataParams.dataName})` +
309
- `\n 📍 ${this.dataParams.stackLocation}`
310
- );
311
-
312
- throw new DyFM_Error({
313
- ...this._getDefaultErrorSettings(
314
- 'getDataByDependencyId',
315
- new Error(
316
- `getDataByDependencyId failed, dependencyKey is missing from service! ` +
317
- `(${this.dataParams.dataName})`
318
- )
319
- ),
320
-
321
- errorCode: `${this.ecBase}DyNTS-DS-GD1`,
322
- });
323
- }
324
-
325
- if (typeof dependencyIds === 'string') {
326
- if (this.depSettings.length === 1) {
327
- dependencyIds = { [this.depSettings[0].key]: dependencyIds };
328
- } else if (this.depSettings.length > 1) {
329
- throw new DyFM_Error({
330
- ...this._getDefaultErrorSettings(
331
- 'getDataByDependencyId',
332
- new Error(
333
- `getDataByDependencyId failed, there are multiple dependencyKeys, ` +
334
- `so you need to provide a map of dependencyKeys! ` +
335
- `(${this.dataParams.dataName})`
336
- )
337
- ),
338
-
339
- errorCode: `${this.ecBase}DyNTS-DS-GD2`,
340
- });
341
- }
342
- } else if (!dependencyIds) {
343
- dependencyIds = {};
344
- this.depSettings.forEach((depSetting) => {
345
- if (this.data?.[depSetting.key]) {
346
- dependencyIds[depSetting.key] = this.data?.[depSetting.key];
347
- }
348
- });
349
- }
350
-
351
- if (
352
- !dependencyIds ||
353
- this.depSettings.every((depSetting) => !dependencyIds[depSetting.key])
354
- ) {
355
- throw new DyFM_Error({
356
- ...this._getDefaultErrorSettings(
357
- 'getDataByDependencyId',
358
- new Error(
359
- `getDataByDependencyId failed, dependencyId is missing! ` +
360
- `(${this.dataParams.dataName})`
361
- )
362
- ),
363
-
364
- errorCode: `${this.ecBase}DyNTS-DS-GD3`,
365
- });
366
- }
367
-
368
- return dependencyIds as { [key: string]: string };
369
- }
370
-
371
- /**
372
- * returns data from database by dependencyId to the service
373
- * (using id from service.data, if not provided)
374
- * @param dependencyIds
375
- */
376
- async getDataByDependencyId(
377
- dependencyIds?: string | { [key: string]: string },
378
- dontSetToService?: boolean
379
- ): Promise<T> {
380
- try {
381
- const dependencyIdsFilter: { [key: string]: string } =
382
- this.getDependencyIdsFilter(dependencyIds);
383
-
384
- const dataExists: T =
385
- await this.dataDBService.getDataByDependencyId(dependencyIdsFilter).catch(
386
- (error): null => {
387
- if (error?.errorCode?.includes('DBS-GD2')) {
388
- DyFM_Log.warn(
389
- `getDataByDependencyId failed; "${this.dataParams.dataName}" ` +
390
- `(${JSON.stringify(dependencyIds)}) didn't found any.`
391
- );
392
-
393
- return null;
394
- } else {
395
- throw error;
396
- }
397
- }
398
- );
399
-
400
- if (!dontSetToService) {
401
- this.data = dataExists;
402
- }
403
-
404
- return dataExists;
405
- } catch (error) {
406
- if ([
407
- `${this.ecBase}DyNTS-DS0-GD1`,
408
- `${this.ecBase}DyNTS-DS0-GD2`
409
- ].includes(error?.errorCode)) {
410
- throw error;
411
- } else {
412
- throw new DyFM_Error({
413
- ...this._getDefaultErrorSettings('getDataByDependencyId', error),
414
-
415
- errorCode: `${this.ecBase}DyNTS-DS-GD0`,
416
- });
417
- }
418
- }
419
- }
420
-
421
- async getDataListByDependencyIds(
422
- dependencyIds: string[],
423
- dontSetToService?: boolean,
424
- dependencyKey?: string,
425
- ): Promise<T[]> {
426
- try {
427
- if (!this.depSettings.length) {
428
- throw new DyFM_Error({
429
- ...this._getDefaultErrorSettings(
430
- 'getDataListByDependencyIds',
431
- new Error(
432
- `getDataListByDependencyIds failed, dependencyKey is missing from service! ` +
433
- `(${this.dataParams.dataName})`
434
- )
435
- ),
436
-
437
- errorCode: `${this.ecBase}DyNTS-DS-GDS1`,
438
- });
439
- }
440
-
441
- if (!dependencyKey) {
442
- if (this.depSettings.length === 1) {
443
- dependencyKey = this.depSettings[0].key;
444
- } else {
445
- throw new DyFM_Error({
446
- ...this._getDefaultErrorSettings(
447
- 'getDataListByDependencyIds',
448
- new Error(
449
- `getDataListByDependencyIds failed, dependencyKey is missing! ` +
450
- `since there are multiple dependencyKeys, you need to provide one! ` +
451
- `(${this.dataParams.dataName})`
452
- )
453
- ),
454
-
455
- errorCode: `${this.ecBase}DyNTS-DS-GDS2`,
456
- });
457
- }
458
- }
459
-
460
- if (!dependencyIds) {
461
- throw new DyFM_Error({
462
- ...this._getDefaultErrorSettings(
463
- 'getDataListByDependencyIds',
464
- new Error(
465
- `getDataListByDependencyIds failed, dependencyIds is missing! ` +
466
- `(${this.dataParams.dataName})`
467
- )
468
- ),
469
-
470
- errorCode: `${this.ecBase}DyNTS-DS-GDS3`,
471
- });
472
- }
473
-
474
- if (dependencyIds.length === 0) {
475
- return [];
476
- }
477
-
478
- const dataList: T[] = await this.dataDBService.getDataListByDependencyIds(
479
- dependencyKey, dependencyIds
480
- );
481
-
482
- if (!dontSetToService) {
483
- this.dataList = dataList;
484
- }
485
-
486
- return dataList;
487
- } catch (error) {
488
- if ([
489
- `${this.ecBase}DyNTS-DS0-GDS1`,
490
- `${this.ecBase}DyNTS-DS0-GDS2`
491
- ].includes(error?.errorCode)) {
492
- throw error;
493
- } else {
494
- throw new DyFM_Error({
495
- ...this._getDefaultErrorSettings('getDataListByDependencyIds', error),
496
-
497
- errorCode: `${this.ecBase}DyNTS-DS0-GDS0`,
498
- });
499
- }
500
- }
501
- }
502
-
503
- /**
504
- * returns dataList from database by dependencyId to the service
505
- * @param dependencyIds
506
- */
507
- async getDataListByDependencyId(
508
- dependencyIds?: string | { [key: string]: string },
509
- dontSetToService?: boolean
510
- ): Promise<T[]> {
511
- try {
512
- const dependencyIdsFilter: { [key: string]: string } =
513
- this.getDependencyIdsFilter(dependencyIds);
514
-
515
- const dataListExists: T[] =
516
- await this.dataDBService.getDataListByDependencyId(dependencyIdsFilter).catch(
517
- (error): T[] => {
518
- if (error?.errorCode?.includes('DBS-GLD2')) {
519
- DyFM_Log.warn(
520
- `getDataListByDependencyId "${this.dataParams.dataName}" ` +
521
- `(${JSON.stringify(dependencyIdsFilter)}) didn't found any.`
522
- );
523
-
524
- return [];
525
- } else {
526
- throw error;
527
- }
528
- }
529
- );
530
-
531
- if (!dontSetToService) {
532
- this.dataList = dataListExists;
533
- }
534
-
535
- return dataListExists;
536
- } catch (error) {
537
- if ([
538
- `${this.ecBase}DyNTS-DS0-GLD1`,
539
- `${this.ecBase}DyNTS-DS0-GLD2`
540
- ].includes(error?.errorCode)) {
541
- throw error;
542
- } else {
543
- throw new DyFM_Error({
544
- ...this._getDefaultErrorSettings('getDataListByDependencyId', error),
545
-
546
- errorCode: `${this.ecBase}DyNTS-DS0-GLD0`,
547
-
548
- });
549
- }
550
- }
551
- }
552
-
553
- /**
554
- *
555
- * // findOne desc:
556
- *
557
- * Find the data first by any of its parameters,
558
- * also if dontSetToService is false or not setted,
559
- * the data will be saved to the service, even if non found
560
- *
561
- * @param filter if you can, use unique parameters for find!
562
- *
563
- * @example
564
- * // by email:
565
- * { email: email }
566
- * //
567
- * @example
568
- * // or by id that is in list:
569
- * { userIds: { $in: this.userId } }
570
- * // or by userIds:
571
- * { userId: { $in: userIds } }
572
- * //
573
- * @example
574
- * // or by number or Date that is Greater Than AND Less Than:
575
- * { points: { $gt: 2, $lt: 14 } }
576
- * // further tools (syntax matches with $gt):
577
- * $eq: // Matches values that are EQual to a specified value.
578
- * $gte: // Matches values that are Greater Than OR Equal to a specified value.
579
- * $lte: // Matches values that are Less Than or Equal to a specified value.
580
- * $ne: // Matches all values that are Not Equal to a specified value.
581
- * $nin: // Matches None of the values specified IN an array.
582
- * //
583
- * @returns {T} data: T
584
- */
585
- async findData(filterBy: DyFM_DBFilter<T>, dontSetToService?: boolean): Promise<T> {
586
- try {
587
- const dataExists: T = await this.dataDBService.findOne(filterBy).catch((error): null => {
588
- if (error?.errorCode?.includes('DBS-FO1')) {
589
- DyFM_Log.warn(`findData "${this.dataParams.dataName}" didn't found any.`);
590
-
591
- return null;
592
- } else {
593
- throw error;
594
- }
595
- });
596
-
597
- if (!dontSetToService) {
598
- this.data = dataExists;
599
- }
600
-
601
- return dataExists;
602
- } catch (error) {
603
- throw new DyFM_Error({
604
- ...this._getDefaultErrorSettings('findData', error),
605
-
606
- errorCode: `${this.ecBase}DyNTS-DS0-FD0`,
607
- });
608
- }
609
- }
610
- getDataByQuery: typeof this.findData = this.findData;
611
-
612
- /**
613
- *
614
- * // find desc:
615
- *
616
- * Find the data first by any of its parameters,
617
- * also if dontSetToService is false or not setted,
618
- * the data will be saved to the service, even if non found
619
- *
620
- * @param filter if you can, use unique parameters for find!
621
- *
622
- * @example
623
- * // by email:
624
- * { email: email }
625
- * //
626
- * @example
627
- * // or by id that is in list:
628
- * { userIds: { $in: this.userId } }
629
- * // or by userIds:
630
- * { userId: { $in: userIds } }
631
- * //
632
- * @example
633
- * // or by number or Date that is Greater Than AND Less Than:
634
- * { points: { $gt: 2, $lt: 14 } }
635
- * // further tools (syntax matches with $gt):
636
- * $eq: // Matches values that are EQual to a specified value.
637
- * $gte: // Matches values that are Greater Than OR Equal to a specified value.
638
- * $lte: // Matches values that are Less Than or Equal to a specified value.
639
- * $ne: // Matches all values that are Not Equal to a specified value.
640
- * $nin: // Matches None of the values specified IN an array.
641
- * //
642
- * @returns {T[]} dataList: T[]
643
- */
644
- async findDataList(filterBy: DyFM_DBFilter<T>, dontSetToService?: boolean): Promise<T[]> {
645
- try {
646
- const dataListExists: T[] = await this.dataDBService.find(filterBy).catch((error): T[] => {
647
- if (error?.errorCode?.includes('DBS-F1')) {
648
- DyFM_Log.warn(`findDataList "${this.dataParams.dataName}" didn't found any.`);
649
-
650
- return [];
651
- } else {
652
- throw error;
653
- }
654
- });
655
-
656
- if (!dontSetToService) {
657
- this.dataList = dataListExists;
658
- }
659
-
660
- return dataListExists;
661
- } catch (error) {
662
- throw new DyFM_Error({
663
- ...this._getDefaultErrorSettings('findDataList', error),
664
-
665
- errorCode: `${this.ecBase}DyNTS-DS0-FDS0`,
666
- });
667
- }
668
- }
669
-
670
- /**
671
- * This function uses the dataDBService.updateOne function.
672
- * This uses updateBy if setted, or data._id if its setted or this.data[this.dependencyKey]
673
- * @param set
674
- *
675
- * // updateOne desc:
676
- *
677
- * Find the data first by any of its parameters, throws error if not found
678
- * @param filter This uses the basic Mongoose updateOne.
679
- * If you can, use unique parameters for find!
680
- * @example
681
- * // by email:
682
- * { email: email }
683
- * //
684
- * @example
685
- * // or by id that is in list:
686
- * { userIds: { $in: this.userId } }
687
- * // or by userIds:
688
- * { userId: { $in: userIds } }
689
- * //
690
- * @example
691
- * // or by number or Date that is Greater Than AND Less Than:
692
- * { points: { $gt: 2, $lt: 14 } }
693
- * // further tools (syntax matches with $gt):
694
- * $eq: // Matches values that are EQual to a specified value.
695
- * $gte: // Matches values that are Greater Than OR Equal to a specified value.
696
- * $lte: // Matches values that are Less Than or Equal to a specified value.
697
- * $ne: // Matches all values that are Not Equal to a specified value.
698
- * $nin: // Matches None of the values specified IN an array.
699
- * //
700
- *
701
- * @param update this uses the basic Mongoose updateOne
702
- * @example
703
- * // increase a specific value (here by 15):
704
- * { $inc: { popularity: 15 } }
705
- * //
706
- * @example
707
- * // or add element to a list:
708
- * { $push: { reactions: this.newReaction }
709
- * // or add multiple elements to a list
710
- * { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
711
- * //
712
- * @example
713
- * // or all at once
714
- * {
715
- * $inc: { popularity: this.newVote.amount },
716
- * emailVerified: true,
717
- * $push: { reactions: this.newReaction }
718
- * }
719
- * // further tools (syntax matches with $inc):
720
- * $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
721
- * $min: // Only updates the field if the specified value is less than the existing field value.
722
- * $max: // Only updates the field if the specified value is greater than the existing field value.
723
- * $mul: // Multiplies the value of the field by the specified amount.
724
- * $rename: // Renames a field.
725
- * $unset: // Removes the specified field from a document. (set: "" to value)
726
- * //
727
- */
728
- async updateData(
729
- set: { filterBy?: DyFM_DBFilter<T>, update: DyNTS_DBUpdate<T> },
730
- dontUpdateModified?: boolean
731
- ): Promise<void> {
732
- try {
733
- if (set.filterBy) {
734
- await this.dataDBService.updateOne(
735
- set.filterBy,
736
- set.update,
737
- this.issuer,
738
- dontUpdateModified
739
- );
740
-
741
- } else if (this.data._id) {
742
- await this.dataDBService.updateOne(
743
- { _id: this.data._id } as DyFM_DBFilter<T>,
744
- set.update,
745
- this.issuer,
746
- dontUpdateModified
747
- );
748
-
749
- } else if (
750
- this.depSettings.length &&
751
- this.depSettings.some((depSetting) => this.data[depSetting.key])
752
- ) {
753
- const dependencyIdsFilter: { [key: string]: string } = {};
754
-
755
- this.depSettings.forEach((depSetting) => {
756
- if (this.data[depSetting.key]) {
757
- dependencyIdsFilter[depSetting.key] = this.data[depSetting.key];
758
- }
759
- });
760
-
761
- await this.dataDBService.updateOne(
762
- dependencyIdsFilter as DyFM_DBFilter<T>,
763
- set.update,
764
- this.issuer,
765
- dontUpdateModified
766
- );
767
-
768
- } else {
769
- throw new DyFM_Error({
770
- ...this._getDefaultErrorSettings(
771
- 'updateData',
772
- new Error(
773
- `no usable parameter provided for updateData; no updateBy, no id, no dependencyId ` +
774
- `(${this.dataParams.dataName})`
775
- )
776
- ),
777
-
778
- errorCode: `${this.ecBase}DyNTS-DS0-UD1`,
779
- additionalContent: {
780
- data: this.data,
781
- filterBy: set.filterBy,
782
- },
783
- });
784
- }
785
- } catch (error) {
786
- if (error?.errorCode?.includes('DS0-UD1')) {
787
- throw error;
788
- } else {
789
- throw new DyFM_Error({
790
- ...this._getDefaultErrorSettings('updateData', error),
791
-
792
- errorCode: `${this.ecBase}DyNTS-DS0-UD0`,
793
- });
794
- }
795
- }
796
- }
797
-
798
- protected ensureData(data?: T): T {
799
- if (!data && this.data) {
800
- data = this.data;
801
- }
802
-
803
- if (!data) {
804
- throw new DyFM_Error({
805
- ...this._getDefaultErrorSettings(
806
- 'ensureData',
807
- new Error(`no data to save! (${this.dataParams.dataName})`)
808
- ),
809
-
810
- errorCode: `${this.ecBase}DyNTS-DS0-ED0`,
811
- });
812
- }
813
-
814
- return data;
815
- }
816
-
817
- async patchData(data?: Partial<T>): Promise<T> {
818
- try {
819
- if (!data._id) {
820
- throw new DyFM_Error({
821
- ...this._getDefaultErrorSettings(
822
- 'patchData',
823
- new Error(`no ID to patch data! (${this.dataParams.dataName})`)
824
- ),
825
- });
826
- }
827
-
828
- const dataExists = await this.getDataById(data._id, true);
829
-
830
- if (!dataExists) {
831
- throw new DyFM_Error({
832
- ...this._getDefaultErrorSettings(
833
- 'patchData',
834
- new Error(`data not found! (${this.dataParams.dataName})`)
835
- ),
836
- });
837
- }
838
-
839
- for (const key in this.depSettings) {
840
- if (data[key] && data[key] !== dataExists[key]) {
841
- throw new DyFM_Error({
842
- ...this._getDefaultErrorSettings(
843
- 'patchData',
844
- new Error(`Cannot patch data: dependency data mismatch! (${this.dataParams.dataName})`)
845
- ),
846
- });
847
- }
848
- }
849
-
850
- DyFM_Object.cleanAssign(dataExists, data);
851
-
852
- await this.validateForSave(dataExists);
853
-
854
- return await this.dataDBService.modifyData(dataExists, this.issuer);
855
- } catch (error) {
856
- throw new DyFM_Error({
857
- ...this._getDefaultErrorSettings('patchData', error),
858
-
859
- errorCode: `${this.ecBase}DyNTS-DS0-PD0`,
860
- });
861
- }
862
- }
863
-
864
- /**
865
- * modifies data if the data have ID and already exists in the DB,
866
- * creates new if the ID is not present or cant find in DB,
867
- * and if dependency data setted up, will check before creation,
868
- *
869
- * @warning
870
- * but the proper way to update data, if you use update method instead,
871
- * this way, you can avoid data override errors
872
- * (when you simultaneously trying to change the same data's
873
- * different values from different flows)
874
- */
875
- async saveData(data?: T, dontSetToService?: boolean, dontUpdateModified?: boolean): Promise<T> {
876
- try {
877
- data = this.ensureData(data);
878
-
879
- await this.validateForSave(data);
880
-
881
- if (!data._id) {
882
- if (
883
- this.depSettings.length &&
884
- this.depSettings.some((depSetting) => !data[depSetting.key])
885
- ) {
886
- throw new DyFM_Error({
887
- ...this._getDefaultErrorSettings(
888
- 'saveData',
889
- new Error(
890
- `saveData was unsuccessful: dependency data id missing from data ` +
891
- `(key: ${this.depSettings.map((depSetting) => depSetting.key).join(', ')}, ` +
892
- `${this.dataParams.dataName})`
893
- )
894
- ),
895
-
896
- errorCode: `${this.ecBase}DyNTS-DS0-SD1`,
897
- additionalContent: {
898
- data: data,
899
- },
900
- });
901
- }
902
-
903
- // if ID of dependencyID is not present, data not exists, create new data
904
- data = await this.dataDBService.createData(data, this.issuer);
905
-
906
- if (!dontSetToService) {
907
- this.data = data;
908
- }
909
-
910
- return data;
911
- }
912
-
913
- let dataExists: T;
914
-
915
- // check if data already exists with the specific ID
916
- if (data._id) {
917
- dataExists = await this.getDataById(data._id, true);
918
-
919
- if (!dataExists) {
920
- throw new DyFM_Error({
921
- ...this._getDefaultErrorSettings(
922
- 'saveData',
923
- new Error(
924
- `saveData was unsuccessful: provided ID not exists ` +
925
- `(id: "${data._id}", ${this.dataParams.dataName})`
926
- )
927
- ),
928
-
929
- errorCode: `${this.ecBase}DyNTS-DS0-SD2`,
930
- additionalContent: {
931
- data: data,
932
- },
933
- });
934
- }
935
-
936
- // if data exists do modify
937
- data = await this.dataDBService.modifyData(data, this.issuer, dontUpdateModified);
938
-
939
- if (!dontSetToService) {
940
- this.data = data;
941
- }
942
-
943
- return data;
944
- }
945
-
946
- if (this.depSettings.length) {
947
- if (this.depSettings.some((depSetting) => !data[depSetting.key])) {
948
- throw new DyFM_Error({
949
- ...this._getDefaultErrorSettings(
950
- 'saveData',
951
- new Error(
952
- `saveData was unsuccessful: dependency data id missing from data ` +
953
- `(key: ${this.depSettings.map((depSetting) => depSetting.key).join(', ')}, ` +
954
- `${this.dataParams.dataName})`
955
- )
956
- ),
957
-
958
- errorCode: `${this.ecBase}DyNTS-DS0-SD3`,
959
- additionalContent: {
960
- data: data,
961
- },
962
- });
963
- }
964
-
965
- if (this.depSettings.some((depSetting) => depSetting.keyIsRequired)) {
966
- await DyFM_Array.asyncForEach(
967
- this.depSettings,
968
- async (depSetting) => {
969
- if (depSetting.keyIsRequired) {
970
- if (!data[depSetting.key]) {
971
- throw new DyFM_Error({
972
- ...this._getDefaultErrorSettings(
973
- 'saveData',
974
- new Error(
975
- `saveData was unsuccessful: dependency data id missing from data ` +
976
- `(key: ${depSetting.key}, ${this.dataParams.dataName})`
977
- )
978
- ),
979
-
980
- errorCode: `${this.ecBase}DyNTS-DS0-SD4`,
981
- });
982
- }
983
-
984
- const dependencyExists = await this.getDependencyDataDBService(depSetting.dbServiceKey)
985
- .getDataById(data[depSetting.key])
986
- .catch();
987
-
988
- if (!dependencyExists) {
989
- throw new DyFM_Error({
990
- ...this._getDefaultErrorSettings(
991
- 'saveData',
992
- new Error(
993
- `saveData was unsuccessful: dependency data not exists ` +
994
- `(key: ${depSetting.key}, id: "${data[depSetting.key]}", ` +
995
- `${this.dataParams.dataName})`
996
- )
997
- ),
998
-
999
- errorCode: `${this.ecBase}DyNTS-DS0-SD4`,
1000
- });
1001
- }
1002
- }
1003
- }
1004
- );
1005
- }
1006
-
1007
- /* the db is handling the unique key, so no need to check it here
1008
- if (this.depKeyIsUnique) {
1009
- dataExists = await this.getDataByDependencyId(data[this.depKeys], true);
1010
-
1011
- if (dataExists) {
1012
- // if data exists do modify
1013
- data = await this.dataDBService.modifyData(data, this.issuer, dontUpdateModified);
1014
-
1015
- if (!dontSetToService) {
1016
- this.data = data;
1017
- }
1018
-
1019
- return data;
1020
-
1021
- }
1022
- } */
1023
- }
1024
-
1025
- // if data not exists create new data
1026
- data = await this.dataDBService.createData(data, this.issuer);
1027
-
1028
- if (!data._id) {
1029
- throw new DyFM_Error({
1030
- ...this._getDefaultErrorSettings(
1031
- 'saveData',
1032
- new Error(
1033
- `saveData was unsuccessful: data creation failed ` +
1034
- `(${this.dataParams.dataName})`
1035
- )
1036
- ),
1037
-
1038
- errorCode: `${this.ecBase}DyNTS-DS0-SD5`,
1039
- additionalContent: {
1040
- data: data,
1041
- },
1042
- });
1043
- }
1044
-
1045
- if (!dontSetToService) {
1046
- this.data = data;
1047
- }
1048
-
1049
- return data;
1050
-
1051
- } catch (error) {
1052
- if ([
1053
- `${this.ecBase}DyNTS-DS0-SD1`,
1054
- `${this.ecBase}DyNTS-DS0-SD2`,
1055
- `${this.ecBase}DyNTS-DS0-SD3`,
1056
- `${this.ecBase}DyNTS-DS0-SD4`,
1057
- `${this.ecBase}DyNTS-DS0-SD5`,
1058
- ].includes(error?.errorCode)) {
1059
- throw error;
1060
-
1061
- } else {
1062
- throw new DyFM_Error({
1063
- ...this._getDefaultErrorSettings('saveData', error),
1064
-
1065
- errorCode: `${this.ecBase}DyNTS-DS0-SD0`,
1066
- });
1067
- }
1068
- }
1069
- }
1070
-
1071
- /**
1072
- * markes data as deleted, if can be archived, will be archived as well
1073
- * if absolute is true, permanently deletes data from database by data._id
1074
- */
1075
- async deleteData(id?: string, absolute?: boolean): Promise<void> {
1076
- try {
1077
- if (!id && this.data._id) {
1078
- id = this.data._id;
1079
- }
1080
-
1081
- if (!id) {
1082
- throw new DyFM_Error({
1083
- ...this._getDefaultErrorSettings(
1084
- 'deleteData',
1085
- new Error(`deleteData failed, ID is missing! (${this.dataParams.dataName})`)
1086
- ),
1087
-
1088
- errorCode: `${this.ecBase}DyNTS-DS0-DD1`,
1089
- });
1090
- }
1091
-
1092
- this.data = await this.getDataById(id, true);
1093
-
1094
- if (!this.data) {
1095
- throw new DyFM_Error({
1096
- ...this._getDefaultErrorSettings(
1097
- 'deleteData',
1098
- new Error(`deleteData failed, data not found! (${this.dataParams.dataName})`)
1099
- ),
1100
-
1101
- errorCode: `${this.ecBase}DyNTS-DS0-DD2`,
1102
- });
1103
- }
1104
-
1105
- if (this.haveArchiveDataService) {
1106
- const archive_DS: DyNTS_DataService<T> = this.getArchiveDataService();
1107
-
1108
- await this.dataDBService.trueDeleteDataById(id);
1109
-
1110
- this.data._deleted = new Date();
1111
- this.data._deletedBy = this.issuer;
1112
- this.data._originalId = this.data._id;
1113
- this.data._archived = new Date();
1114
- delete this.data._id;
1115
-
1116
- await archive_DS.saveData(this.data, true);
1117
- } else if (absolute) {
1118
- await this.dataDBService.trueDeleteDataById(id);
1119
-
1120
- delete this.data._id;
1121
- } else {
1122
- await this.dataDBService.markDeletedById(id, this.issuer);
1123
- }
1124
- } catch (error) {
1125
- if (error?.errorCode?.includes('DS0-DD1')) {
1126
- throw error;
1127
- } else {
1128
- throw new DyFM_Error({
1129
- ...this._getDefaultErrorSettings('deleteData', error),
1130
-
1131
- errorCode: `${this.ecBase}DyNTS-DS0-DD0`,
1132
- });
1133
- }
1134
- }
1135
- }
1136
-
1137
- async deleteAllData(absolute?: boolean): Promise<void> {
1138
- try {
1139
- this.dataList = await this.getAll(true);
1140
-
1141
- if (this.haveArchiveDataService) {
1142
- const archive_DS: DyNTS_DataService<T> = this.getArchiveDataService();
1143
-
1144
- await this.dataDBService.trueDeleteAllData();
1145
-
1146
- await DyFM_Array.asyncForEachAllAtOnce(
1147
- this.dataList,
1148
- async (data: T) => {
1149
- data._deleted = new Date();
1150
- data._deletedBy = this.issuer;
1151
- data._originalId = data._id;
1152
- data._archived = new Date();
1153
- delete data._id;
1154
-
1155
- await archive_DS.saveData(data, true);
1156
- }
1157
- );
1158
- } else if (absolute) {
1159
- await this.dataDBService.trueDeleteAllData();
1160
-
1161
- this.dataList.forEach((data: T) => {
1162
- delete data._id;
1163
- });
1164
- } else {
1165
- await DyFM_Array.asyncForEachAllAtOnce(
1166
- this.dataList,
1167
- async (data: T) => {
1168
- await this.dataDBService.markDeletedById(data._id, this.issuer);
1169
- }
1170
- );
1171
- }
1172
- } catch (error) {
1173
- throw new DyFM_Error({
1174
- ...this._getDefaultErrorSettings('deleteAllData', error),
1175
-
1176
- errorCode: `${this.ecBase}DyNTS-DS0-DAD0`,
1177
- });
1178
- }
1179
- }
1180
-
1181
- restoreDeletedData(id?: string): Promise<void> {
1182
- try {
1183
- if (!id && this.data._id) {
1184
- id = this.data._id;
1185
- }
1186
-
1187
- if (!id) {
1188
- throw new DyFM_Error({
1189
- ...this._getDefaultErrorSettings(
1190
- 'restoreDeletedData',
1191
- new Error(`restoreDeletedData failed, ID is missing! (${this.dataParams.dataName})`)
1192
- ),
1193
-
1194
- errorCode: `${this.ecBase}DyNTS-DS0-RD1`,
1195
- });
1196
- }
1197
-
1198
- let archive_DS;
1199
-
1200
- try {
1201
- archive_DS = DyNTS_GlobalService.getDBServiceByKey(
1202
- DyNTS_getArchivedDBName(this.dataParams.dataName),
1203
- );
1204
- } catch (error) {}
1205
-
1206
- if (archive_DS) {
1207
- throw new DyFM_Error({
1208
- ...this._getDefaultErrorSettings(
1209
- 'restoreDeletedData',
1210
- new Error(
1211
- `restoreDeletedData failed, archive service is setted up, ` +
1212
- `archived restoration not implemented yet! ` +
1213
- `(request implementation, and restore by yourself)`
1214
- )
1215
- ),
1216
-
1217
- errorCode: `${this.ecBase}DyNTS-DS0-RD2`,
1218
- });
1219
- }
1220
-
1221
- return this.dataDBService.restoreDeletedById(id, this.issuer);
1222
- } catch (error) {
1223
- if (error?.errorCode?.includes('DS0-RD1')) {
1224
- throw error;
1225
- } else {
1226
- throw new DyFM_Error({
1227
- ...this._getDefaultErrorSettings('restoreDeletedData', error),
1228
-
1229
- errorCode: `${this.ecBase}DyNTS-DS0-RD0`,
1230
- });
1231
- }
1232
- }
1233
- }
1234
-
1235
- /**
1236
- * validation of data, for modify and create, by the ModelParams
1237
- */
1238
- async validateForSave(data?: T): Promise<void> {
1239
- try {
1240
- data = this.ensureData(data);
1241
-
1242
- if (!data) {
1243
- throw new DyFM_Error({
1244
- ...this._getDefaultErrorSettings('validateForSave', new Error('No data to validate! (validateForSave)')),
1245
- errorCode: `${this.ecBase}DyNTS-DS0-VBP0`,
1246
- });
1247
- }
1248
-
1249
- await this.validateDataByPropertyParams(data, this.dataParams.properties);
1250
- } catch (error) {
1251
- if (error?.errorCode?.includes?.('DyNTS-DS0-VBP') || error?.errorCode?.includes?.('DyNTS-DS0-VP')) {
1252
- throw error;
1253
- } else {
1254
- throw new DyFM_Error({
1255
- ...this._getDefaultErrorSettings('validateForSave', error),
1256
-
1257
- status: 522,
1258
- errorCode: `${this.ecBase}DyNTS-DS0-VD0`,
1259
- });
1260
- }
1261
- }
1262
- }
1263
-
1264
- private validateDataByPropertyParams(
1265
- data: T,
1266
- properties: DyFM_DataProperties<T>,
1267
- isSubObjectOf?: DyFM_DataProperty_Params<any, T>
1268
- ): void {
1269
- try {
1270
- if (!data) {
1271
- if (isSubObjectOf?.required) {
1272
- throw new DyFM_Error({
1273
- ...this._getDefaultErrorSettings(
1274
- 'validateDataByPropertyParams',
1275
- new Error(
1276
- `validateData failed, data is required! ` +
1277
- `(${isSubObjectOf?.key ? `${isSubObjectOf.key}.` : ''}${this.dataParams.dataName})` +
1278
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1279
- )
1280
- ),
1281
- errorCode: `${this.ecBase}DyNTS-DS0-VDP1`,
1282
- });
1283
- } else {
1284
- return;
1285
- }
1286
- } else if (isSubObjectOf?.forbidden) {
1287
- throw new DyFM_Error({
1288
- ...this._getDefaultErrorSettings(
1289
- 'validateDataByPropertyParams',
1290
- new Error(
1291
- `validateData failed, data is forbidden! ` +
1292
- `(${isSubObjectOf?.key ? `${isSubObjectOf.key}.` : ''}${this.dataParams.dataName})` +
1293
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1294
- )
1295
- ),
1296
- errorCode: `${this.ecBase}DyNTS-DS0-VDP2`,
1297
- additionalContent: {
1298
- data: data,
1299
- },
1300
- });
1301
- }
1302
-
1303
- const validationErrors: DyFM_Error[] = [];
1304
-
1305
- Object.values(properties).forEach(
1306
- (propertyParams: DyFM_DataProperty_Params<any, T>) => {
1307
- try {
1308
- this.validateProperty(data, data[propertyParams.key], propertyParams, isSubObjectOf);
1309
- } catch (error) {
1310
- validationErrors.push(error);
1311
- }
1312
- }
1313
- );
1314
-
1315
- if (validationErrors.length > 0) {
1316
- if (validationErrors.length === 1) {
1317
- throw validationErrors[0];
1318
- } else {
1319
- const validationErrorsSet: DyFM_ValidationErrors = {};
1320
- validationErrors.forEach((error: DyFM_Error) => {
1321
- Object.keys(error.validationErrors).forEach((key: string) => {
1322
- validationErrors[key] = error.validationErrors[key];
1323
- });
1324
- });
1325
-
1326
- throw new DyFM_Error({
1327
- ...this._getDefaultErrorSettings(
1328
- 'validateByPropertyParams',
1329
- new Error(
1330
- `Validation failed! (multiple validation errors for ${this.dataParams.dataName})` +
1331
- validationErrors.map((error: DyFM_Error) => `\n ${DyFM_Error.getAnyMessage(error)}`).join('')
1332
- )
1333
- ),
1334
- errorCode: `${this.ecBase}DyNTS-DS0-VDP3`,
1335
- errors: validationErrors,
1336
- validationErrors: validationErrorsSet,
1337
- });
1338
- }
1339
- }
1340
- } catch (error) {
1341
- if (error?.errorCode?.includes?.('DyNTS-DS0-VDP')) {
1342
- throw error;
1343
- } else {
1344
- throw new DyFM_Error({
1345
- ...this._getDefaultErrorSettings('validateByPropertyParams', error),
1346
-
1347
- status: 522,
1348
- errorCode: `${this.ecBase}DyNTS-DS0-VDP0`,
1349
- });
1350
- }
1351
- }
1352
- }
1353
-
1354
- private getPropertyKey(
1355
- propertyParams: DyFM_DataProperty_Params<any, T>,
1356
- isSubObjectOf?: DyFM_DataProperty_Params<any, T>
1357
- ): string {
1358
- return (isSubObjectOf && isSubObjectOf.key !== propertyParams.key) ?
1359
- `${isSubObjectOf.key}.${propertyParams.key}` : propertyParams.key;
1360
- }
1361
-
1362
- private validateProperty(
1363
- data: T,
1364
- propertyValue: any,
1365
- propertyParams: DyFM_DataProperty_Params<any, T>,
1366
- isSubObjectOf?: DyFM_DataProperty_Params<any, T>
1367
- ): void {
1368
- // basic required validations
1369
- if (
1370
- propertyParams.required && (
1371
- propertyValue === null ||
1372
- propertyValue === undefined
1373
- )
1374
- ) {
1375
- throw new DyFM_Error({
1376
- ...this._getDefaultErrorSettings(
1377
- 'validateProperty',
1378
- new Error(
1379
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is missing! ` +
1380
- `(required in "${this.dataParams.dataName}" dataParams) ` +
1381
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1382
- )
1383
- ),
1384
-
1385
- status: 522,
1386
- errorCode: `${this.ecBase}DyNTS-DS0-VP1`,
1387
- userMessage: this.defaultValidationErrorUserMsg,
1388
- validationErrors: {
1389
- [propertyParams.key]: {
1390
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is missing! ` +
1391
- `(required in "${this.dataParams.dataName}" dataParams) ` +
1392
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1393
- code: `${this.ecBase}DyNTS-DS0-VP1`,
1394
- userMessage: 'The property is required and must be a valid value!',
1395
- },
1396
- },
1397
- __localStack: this.dataParams.stackLocation,
1398
- additionalContent: {
1399
- data: data,
1400
- },
1401
- });
1402
- } else if (
1403
- propertyParams.forbidden &&
1404
- propertyValue !== undefined
1405
- ) {
1406
- throw new DyFM_Error({
1407
- ...this._getDefaultErrorSettings(
1408
- 'validateProperty',
1409
- new Error(
1410
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is forbidden! ` +
1411
- `(${this.dataParams.dataName}) ` +
1412
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1413
- )
1414
- ),
1415
-
1416
- status: 522,
1417
- errorCode: `${this.ecBase}DyNTS-DS0-VP2`,
1418
- userMessage: this.defaultValidationErrorUserMsg,
1419
- validationErrors: {
1420
- [propertyParams.key]: {
1421
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is forbidden! ` +
1422
- `(${this.dataParams.dataName}) ` +
1423
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1424
- code: `${this.ecBase}DyNTS-DS0-VP2`,
1425
- userMessage: 'The property modification is forbidden!',
1426
- },
1427
- },
1428
- __localStack: this.dataParams.stackLocation,
1429
- additionalContent: {
1430
- data: data,
1431
- },
1432
- });
1433
- }
1434
-
1435
- // specific Date validation
1436
- switch (propertyParams.type) {
1437
- case DyFM_BasicProperty_Type.date:
1438
- const date = new Date(propertyValue);
1439
- if (
1440
- propertyParams.required && (
1441
- !(date instanceof Date) ||
1442
- date.toString() === 'Invalid Date' ||
1443
- isNaN(date.getTime()) ||
1444
- date.toString() === new Date(0).toString()
1445
- )
1446
- ) {
1447
- throw new DyFM_Error({
1448
- ...this._getDefaultErrorSettings(
1449
- 'validateProperty',
1450
- new Error(
1451
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid date! ` +
1452
- `(${this.dataParams.dataName})` +
1453
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1454
- )
1455
- ),
1456
-
1457
- status: 522,
1458
- errorCode: `${this.ecBase}DyNTS-DS0-VP3`,
1459
- userMessage: this.defaultValidationErrorUserMsg,
1460
- validationErrors: {
1461
- [propertyParams.key]: {
1462
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid date! ` +
1463
- `(${this.dataParams.dataName})` +
1464
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1465
- code: `${this.ecBase}DyNTS-DS0-VP3`,
1466
- userMessage: 'The property is required and must be a valid date!',
1467
- },
1468
- },
1469
- __localStack: this.dataParams.stackLocation,
1470
- additionalContent: {
1471
- data: data,
1472
- },
1473
- });
1474
- }
1475
- break;
1476
-
1477
- case DyFM_BasicProperty_Type.number:
1478
- if (propertyParams.required && isNaN(propertyValue)) {
1479
- throw new DyFM_Error({
1480
- ...this._getDefaultErrorSettings(
1481
- 'validateProperty',
1482
- new Error(
1483
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid number! ` +
1484
- `(${this.dataParams.dataName})` +
1485
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1486
- )
1487
- ),
1488
-
1489
- status: 522,
1490
- errorCode: `${this.ecBase}DyNTS-DS0-VP4`,
1491
- userMessage: this.defaultValidationErrorUserMsg,
1492
- validationErrors: {
1493
- [propertyParams.key]: {
1494
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid number! ` +
1495
- `(${this.dataParams.dataName})` +
1496
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1497
- code: `${this.ecBase}DyNTS-DS0-VP4`,
1498
- userMessage: 'The property is required and must be a valid number!',
1499
- },
1500
- },
1501
- __localStack: this.dataParams.stackLocation,
1502
- additionalContent: {
1503
- data: data,
1504
- },
1505
- });
1506
- }
1507
- break;
1508
-
1509
- case DyFM_BasicProperty_Type.string:
1510
- if (propertyParams.required && typeof propertyValue !== 'string') {
1511
- throw new DyFM_Error({
1512
- ...this._getDefaultErrorSettings(
1513
- 'validateProperty',
1514
- new Error(
1515
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid string! ` +
1516
- `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1517
- `(${this.dataParams.dataName})` +
1518
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1519
- )
1520
- ),
1521
-
1522
- status: 522,
1523
- errorCode: `${this.ecBase}DyNTS-DS0-VP5`,
1524
- userMessage: this.defaultValidationErrorUserMsg,
1525
- validationErrors: {
1526
- [propertyParams.key]: {
1527
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid string! ` +
1528
- `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1529
- `(${this.dataParams.dataName})` +
1530
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1531
- code: `${this.ecBase}DyNTS-DS0-VP5`,
1532
- userMessage: 'The property is required and must be a valid string!',
1533
- },
1534
- },
1535
- __localStack: this.dataParams.stackLocation,
1536
- additionalContent: {
1537
- data: data,
1538
- },
1539
- });
1540
- }
1541
- break;
1542
-
1543
- case DyFM_BasicProperty_Type.boolean:
1544
- if (propertyParams.required && typeof propertyValue !== 'boolean') {
1545
- throw new DyFM_Error({
1546
- ...this._getDefaultErrorSettings(
1547
- 'validateProperty',
1548
- new Error(
1549
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid boolean! ` +
1550
- `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1551
- `(${this.dataParams.dataName})` +
1552
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1553
- )
1554
- ),
1555
-
1556
- status: 522,
1557
- errorCode: `${this.ecBase}DyNTS-DS0-VP6`,
1558
- userMessage: this.defaultValidationErrorUserMsg,
1559
- validationErrors: {
1560
- [propertyParams.key]: {
1561
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid boolean! ` +
1562
- `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1563
- `(${this.dataParams.dataName})` +
1564
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1565
- code: `${this.ecBase}DyNTS-DS0-VP6`,
1566
- userMessage: 'The property is required and must be a valid boolean!',
1567
- },
1568
- },
1569
- __localStack: this.dataParams.stackLocation,
1570
- });
1571
- }
1572
- break;
1573
-
1574
- case DyFM_BasicProperty_Type.object:
1575
- if (propertyParams.required && typeof propertyValue !== 'object') {
1576
- throw new DyFM_Error({
1577
- ...this._getDefaultErrorSettings(
1578
- 'validateProperty',
1579
- new Error(
1580
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid object! ` +
1581
- `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1582
- `(${this.dataParams.dataName})` +
1583
- (data as any)?.name ? `\n That failed data's name: ${(data as any).name}` : ''
1584
- )
1585
- ),
1586
-
1587
- status: 522,
1588
- errorCode: `${this.ecBase}DyNTS-DS0-VP7`,
1589
- userMessage: this.defaultValidationErrorUserMsg,
1590
- validationErrors: {
1591
- [propertyParams.key]: {
1592
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid object! ` +
1593
- `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1594
- `(${this.dataParams.dataName})` +
1595
- (data as any)?.name ? `\n That failed data's name: ${(data as any).name}` : '',
1596
- code: `${this.ecBase}DyNTS-DS0-VP7`,
1597
- userMessage: 'The property is required and must be an object!',
1598
- },
1599
- },
1600
- __localStack: this.dataParams.stackLocation,
1601
- });
1602
- }
1603
-
1604
- if (propertyParams.subObjectParams) {
1605
- this.validateDataByPropertyParams(propertyValue, propertyParams.subObjectParams, propertyParams);
1606
- }
1607
- break;
1608
-
1609
- case DyFM_BasicProperty_Type.array:
1610
- if (propertyParams.required && (!Array.isArray(propertyValue) || !propertyValue.length)) {
1611
- throw new DyFM_Error({
1612
- ...this._getDefaultErrorSettings(
1613
- 'validateProperty',
1614
- new Error(
1615
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid array! ` +
1616
- `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1617
- `(${this.dataParams.dataName})` +
1618
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1619
- )
1620
- ),
1621
-
1622
- status: 522,
1623
- errorCode: `${this.ecBase}DyNTS-DS0-VP8`,
1624
- userMessage: this.defaultValidationErrorUserMsg,
1625
- validationErrors: {
1626
- [propertyParams.key]: {
1627
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid array! ` +
1628
- `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1629
- `(${this.dataParams.dataName})` +
1630
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1631
- code: `${this.ecBase}DyNTS-DS0-VP8`,
1632
- userMessage: 'The property is required and must be a valid array!',
1633
- },
1634
- },
1635
- __localStack: this.dataParams.stackLocation,
1636
- });
1637
- }
1638
-
1639
- if (propertyParams.subObjectParams) {
1640
- propertyValue.forEach((item: any): void => {
1641
- this.validateDataByPropertyParams(item, propertyParams.subObjectParams, propertyParams);
1642
- });
1643
- }
1644
- break;
1645
-
1646
- case DyFM_BasicProperty_Type.function:
1647
- DyFM_Log.warn(
1648
- 'validateProperty: function type is not supported yet! ' +
1649
- '(it will be skipped, but you still can use it in additionalValidators)'
1650
- );
1651
- /* if (propertyParams.required && typeof propertyValue !== 'function') {
1652
- throw new DyFM_Error({
1653
- ...this._getDefaultErrorSettings(
1654
- 'validateProperty',
1655
- new Error(
1656
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid function! ` +
1657
- `(${this.dataParams.dataName})` +
1658
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1659
- )
1660
- ),
1661
-
1662
- status: 522,
1663
- errorCode: `${this.ecBase}VP9`,
1664
- userMessage: this.defaultValidationErrorUserMsg,
1665
- validationErrors: {
1666
- [propertyParams.key]: {
1667
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid function! ` +
1668
- `(${this.dataParams.dataName})` +
1669
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1670
- code: `${this.ecBase}DyNTS-DS0-VP9`,
1671
- userMessage: 'The property is required and must be a valid function!',
1672
- },
1673
- },
1674
- __localStack: this.dataParams.stackLocation,
1675
- });
1676
- } */
1677
- break;
1678
-
1679
- default:
1680
- throw new DyFM_Error({
1681
- ...this._getDefaultErrorSettings(
1682
- 'validateProperty',
1683
- new Error(
1684
- `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid property type! ` +
1685
- `(type: ${propertyParams.type}, nonBasicType: ${propertyParams.nonBasicType}) ` +
1686
- `(value type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1687
- `(${this.dataParams.dataName})` +
1688
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1689
- )
1690
- ),
1691
-
1692
- status: 522,
1693
- errorCode: `${this.ecBase}DyNTS-DS0-VP10`,
1694
- userMessage: this.defaultValidationErrorUserMsg,
1695
- validationErrors: {
1696
- [propertyParams.key]: {
1697
- message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid property type! ` +
1698
- `(type: ${propertyParams.type}, nonBasicType: ${propertyParams.nonBasicType}) ` +
1699
- `(value type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1700
- `(${this.dataParams.dataName})` +
1701
- ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1702
- code: `${this.ecBase}DyNTS-DS0-VP10`,
1703
- userMessage: 'The property is required and must be a valid property type!',
1704
- },
1705
- },
1706
- __localStack: this.dataParams.stackLocation,
1707
- });
1708
- }
1709
-
1710
- // call additional validators
1711
- if (propertyParams.additionalValidators) {
1712
- for (let j = 0; j < propertyParams.additionalValidators.length; j++) {
1713
- propertyParams.additionalValidators[j](
1714
- propertyValue,
1715
- data
1716
- );
1717
- }
1718
- }
1719
- }
1720
-
1721
- /**
1722
- * Search the data list based on the search query
1723
- * Sorts and filters the data list based on the search query
1724
- *
1725
- * NOTE: For more simple and with better performance and if you don't need sorting, use findDataList instead.
1726
- *
1727
- * @example
1728
- * ```ts
1729
- * const searchResult = await this.searchData({
1730
- * filterBy: {
1731
- * name: 'John Doe',
1732
- * },
1733
- * sortBy: [
1734
- * { key: 'name', order: 1 },
1735
- * ],
1736
- * });
1737
- * ```
1738
- */
1739
- async searchData(query?: DyFM_SearchQuery<T>, dataList?: T[]): Promise<DyFM_SearchResult<T>> {
1740
- try {
1741
- const searchResult = await this.sortAndFilterDataList(query, dataList);
1742
-
1743
- if (query.page !== undefined && query.pageSize !== undefined) {
1744
- const start = query.page * query.pageSize;
1745
- const end = start + query.pageSize;
1746
-
1747
- searchResult.results = searchResult.results.slice(start, end);
1748
- }
1749
-
1750
- return searchResult;
1751
- } catch (error) {
1752
- throw new DyFM_Error({
1753
- ...this._getDefaultErrorSettings('searchData', error),
1754
-
1755
- errorCode: `${this.ecBase}DyNTS-DS0-SD0`,
1756
- });
1757
- }
1758
- }
1759
-
1760
- async sortAndFilterDataList(
1761
- query: DyFM_SearchQuery<T>,
1762
- dataList?: T[]
1763
- ): Promise<DyFM_SearchResult<T>> {
1764
- try {
1765
- if (!query.filterBy) {
1766
- query.filterBy = {};
1767
- }
1768
-
1769
- if (!query.page) {
1770
- query.page = 0;
1771
- }
1772
-
1773
- if (!query.pageSize) {
1774
- DyFM_Log.warn(
1775
- `searchData pageSize is not setted, setting to ${DyNTS_global_settings.defaultPageSize}.`
1776
- );
1777
- query.pageSize = DyNTS_global_settings.defaultPageSize;
1778
- }
1779
-
1780
- if (!query.sortBy?.length) {
1781
- query.sortBy = [{ key: '__lastModified', order: -1 }];
1782
- }
1783
-
1784
- query.sortBy.reverse();
1785
-
1786
- let preFilter: DyFM_DBFilter<T> = {};
1787
-
1788
- Object.keys(query.filterBy).forEach((key: string) => {
1789
- if (
1790
- typeof query.filterBy[key] === 'object' &&
1791
- Object.keys(query.filterBy[key]).includes('$')
1792
- ) {
1793
- (preFilter as any)[key] = query.filterBy[key] as DyFM_DBFilterExpressions<T[keyof T]>;
1794
- delete query.filterBy[key];
1795
- }
1796
- });
1797
-
1798
- if (!dataList) {
1799
- if (Object.keys(preFilter).length) {
1800
- dataList = await this.findDataList(preFilter);
1801
- } else {
1802
- dataList = await this.getAll();
1803
- }
1804
- } else {
1805
- if (Object.keys(preFilter).length) {
1806
- DyFM_Log.H_warn(
1807
- 'prefilters not supported when dataList is set! (will be ignored the following keys: ' +
1808
- Object.keys(preFilter).map((key: string) => `"${key}"`).join(', ') +
1809
- ')'
1810
- );
1811
- }
1812
- }
1813
-
1814
- const filterKeys: string[] = Object.keys(query.filterBy);
1815
- const filterFunctionsByKey: { [key: string]: (dataProperty) => boolean } = {};
1816
-
1817
- const addFilterKeys: string[] = [];
1818
- const removeFilterKeys: string[] = [];
1819
-
1820
- filterKeys.forEach((key: string): void => {
1821
- const filterBy = query.filterBy[key];
1822
-
1823
- if (filterBy.isSpecialNestSearch) {
1824
- filterBy.nestedPropertySearches.forEach((nestSearch: DyFM_NestPropertySearch<any>): void => {
1825
- const nestKey = nestSearch.nestKeys.join('.');
1826
- filterFunctionsByKey[nestKey] = this.getFilterFunctionForKey(
1827
- nestKey,
1828
- nestSearch.search,
1829
- nestSearch.valueType
1830
- );
1831
- query.filterBy[nestKey] = nestSearch;
1832
- addFilterKeys.push(nestKey);
1833
- });
1834
- removeFilterKeys.push(key);
1835
- } else {
1836
- filterFunctionsByKey[key] = this.getFilterFunctionForKey(key, filterBy);
1837
- }
1838
- });
1839
-
1840
- addFilterKeys.forEach((key: string): void => {
1841
- filterKeys.push(key);
1842
- });
1843
- removeFilterKeys.forEach((key: string): void => {
1844
- filterKeys.splice(filterKeys.indexOf(key), 1);
1845
- });
1846
-
1847
- query.sortBy.forEach((sort: DyFM_DSSort): void => {
1848
- dataList.sort(this.getSortFunctionForKey(sort));
1849
- });
1850
-
1851
- if (filterKeys.some((key: string): boolean => key.includes('.'))) {
1852
- const keyResolvers: { [key: string]: (data: T) => any } = {};
1853
-
1854
- filterKeys.forEach((key: string): void => {
1855
- if (key.includes('.')) {
1856
- if (!query.filterBy[key].nestKeys) {
1857
- throw new DyFM_Error({
1858
- ...this._getDefaultErrorSettings(
1859
- 'searchData',
1860
- new Error(
1861
- `searchData failed, nestKeys missing from nestedPropertySearch! ` +
1862
- `(${this.dataParams.dataName})`
1863
- )
1864
- ),
1865
-
1866
- errorCode: `${this.ecBase}DyNTS-DS0-SD1`,
1867
- });
1868
- }
1869
-
1870
- keyResolvers[key] = this.getKeyResolver(query.filterBy[key].nestKeys);
1871
- } else {
1872
- keyResolvers[key] = (data: T): any => data[key];
1873
- }
1874
- });
1875
-
1876
- if (filterKeys.some((key: string): boolean => query.filterBy[key].isSpecialNestSearch)) {
1877
- dataList = dataList.filter(
1878
- (data: T): boolean => filterKeys.every(
1879
- (key: string): boolean => query.filterBy[key].isSpecialNestSearch ?
1880
- keyResolvers[key](data).some(
1881
- (dataProperty: any): boolean => filterFunctionsByKey[key](dataProperty)
1882
- ) :
1883
- filterFunctionsByKey[key](keyResolvers[key](data))
1884
- )
1885
- );
1886
- } else {
1887
- dataList = dataList.filter(
1888
- (data: T): boolean => filterKeys.every(
1889
- (key: string): boolean => filterFunctionsByKey[key](keyResolvers[key](data))
1890
- )
1891
- );
1892
- }
1893
- } else {
1894
- dataList = dataList.filter(
1895
- (data: T): boolean => filterKeys.every(
1896
- (key: string): boolean => filterFunctionsByKey[key](data[key])
1897
- )
1898
- );
1899
- }
1900
-
1901
- return {
1902
- results: dataList,
1903
- totalItems: dataList.length,
1904
- };
1905
- } catch (error) {
1906
- throw new DyFM_Error({
1907
- ...this._getDefaultErrorSettings('searchData', error),
1908
-
1909
- errorCode: `${this.ecBase}DyNTS-DS0-SD0`,
1910
- });
1911
- }
1912
- }
1913
-
1914
- protected getFilterFunctionForKey<T, N>(
1915
- key: string,
1916
- searchValue: T | T[] | DyFM_RangeValue<T> | DyFM_SpecialSearch<N>,
1917
- propertyType?: DyFM_BasicProperty_Type
1918
- ): (dataProperty) => boolean {
1919
- try {
1920
- if (searchValue === undefined) {
1921
- throw new DyFM_Error({
1922
- ...this._getDefaultErrorSettings(
1923
- 'getFilterFunctionForKey',
1924
- new Error(
1925
- `getFilterFunctionForKey failed, searchValue is missing for key: "${key}" ` +
1926
- `(${this.dataParams.dataName})`
1927
- )
1928
- ),
1929
-
1930
- errorCode: `${this.ecBase}DyNTS-DS0-GFF1`,
1931
- });
1932
- }
1933
-
1934
- if (key.includes('.')) {
1935
- if (!propertyType) {
1936
- throw new DyFM_Error({
1937
- ...this._getDefaultErrorSettings(
1938
- 'getFilterFunctionForKey',
1939
- new Error(
1940
- `getFilterFunctionForKey failed, propertyType is missing for key: "${key}" ` +
1941
- `(${this.dataParams.dataName})`
1942
- )
1943
- ),
1944
-
1945
- errorCode: `${this.ecBase}DyNTS-DS0-GFF3`,
1946
- });
1947
- }
1948
- } else {
1949
- if (!this.dataParams.properties[key]) {
1950
- throw new DyFM_Error({
1951
- ...this._getDefaultErrorSettings(
1952
- 'getFilterFunctionForKey',
1953
- new Error(
1954
- `getFilterFunctionForKey failed, key not found in dataParams: "${key}" ` +
1955
- `(${this.dataParams.dataName})`
1956
- )
1957
- ),
1958
-
1959
- errorCode: `${this.ecBase}DyNTS-DS0-GFF2`,
1960
- });
1961
- }
1962
-
1963
- propertyType = this.dataParams.properties[key].type;
1964
- }
1965
-
1966
- switch (propertyType) {
1967
- case DyFM_BasicProperty_Type.string:
1968
- if (typeof searchValue !== 'string') {
1969
- throw new DyFM_Error({
1970
- ...this._getDefaultErrorSettings(
1971
- 'getFilterFunctionForKey',
1972
- new Error(
1973
- `getFilterFunctionForKey failed, searchValue is not a string! ` +
1974
- `(${this.dataParams.dataName})`
1975
- )
1976
- ),
1977
-
1978
- errorCode: `${this.ecBase}DyNTS-DS0-GFF4`,
1979
- });
1980
- }
1981
-
1982
- if (Array.isArray(searchValue)) {
1983
- const lowerCaseSearchValueArray: string[] = (searchValue as string[]).filter(
1984
- (searchString: string): boolean => Boolean(searchString)
1985
- ).map(
1986
- (searchString: string): string => searchString.toLowerCase()
1987
- );
1988
-
1989
- return (dataProperty: string): boolean => {
1990
- // Convert to string if not already a string (handles numbers, etc.)
1991
- const dataPropertyAsString: string =
1992
- dataProperty != null ? String(dataProperty) : '';
1993
- return lowerCaseSearchValueArray.some(
1994
- (searchString: string): boolean =>
1995
- dataPropertyAsString?.toLowerCase().includes(searchString)
1996
- );
1997
- };
1998
- } else {
1999
- const lowerCaseSearchValue: string = (searchValue as string)?.toLowerCase();
2000
-
2001
- if (!lowerCaseSearchValue) {
2002
- return (dataProperty: string): boolean => !dataProperty;
2003
- }
2004
-
2005
- return (dataProperty: string): boolean => {
2006
- // Convert to string if not already a string (handles numbers, etc.)
2007
- const dataPropertyAsString: string =
2008
- dataProperty != null ? String(dataProperty) : '';
2009
- return dataPropertyAsString?.toLowerCase().includes(lowerCaseSearchValue);
2010
- };
2011
- }
2012
-
2013
- case DyFM_BasicProperty_Type.date:
2014
- if ((searchValue as DyFM_RangeValue).isRange) {
2015
- if (
2016
- (searchValue as DyFM_RangeValue).from === undefined ||
2017
- (searchValue as DyFM_RangeValue).from === null
2018
- ) {
2019
- const toAsNumber = +new Date((searchValue as DyFM_RangeValue).to);
2020
-
2021
- return (dataProperty): boolean => +new Date(dataProperty) <= toAsNumber;
2022
- } else if (
2023
- (searchValue as DyFM_RangeValue).to === undefined ||
2024
- (searchValue as DyFM_RangeValue).to === null
2025
- ) {
2026
- const fromAsNumber = +new Date((searchValue as DyFM_RangeValue).from);
2027
-
2028
- return (dataProperty): boolean => +new Date(dataProperty) >= fromAsNumber;
2029
- }
2030
-
2031
- const rangeAsNumber = new DyFM_RangeValue<number>(
2032
- +new Date((searchValue as DyFM_RangeValue).from),
2033
- +new Date((searchValue as DyFM_RangeValue).to)
2034
- );
2035
-
2036
- return (dataProperty): boolean => DyFM_RangeValue.isInRange(
2037
- +new Date(dataProperty), rangeAsNumber
2038
- );
2039
- }
2040
-
2041
- if (Array.isArray(searchValue)) {
2042
- const searchValueAsNumberArray: number[] = (searchValue as Date[]).map(
2043
- (date: Date): number => +new Date(date)
2044
- );
2045
-
2046
- return (dataProperty): boolean => searchValueAsNumberArray.includes(+new Date(dataProperty));
2047
- }
2048
-
2049
- const searchValueAsNumber: number = +new Date(searchValue as Date);
2050
-
2051
- return (dataProperty): boolean => +new Date(dataProperty) === searchValueAsNumber;
2052
-
2053
- case DyFM_BasicProperty_Type.number:
2054
- if ((searchValue as DyFM_RangeValue).isRange) {
2055
- if (
2056
- (searchValue as DyFM_RangeValue).from === undefined ||
2057
- (searchValue as DyFM_RangeValue).from === null
2058
- ) {
2059
- return (dataProperty): boolean => dataProperty <= (searchValue as DyFM_RangeValue).to;
2060
- } else if (
2061
- (searchValue as DyFM_RangeValue).to === undefined ||
2062
- (searchValue as DyFM_RangeValue).to === null
2063
- ) {
2064
- return (dataProperty): boolean => dataProperty >= (searchValue as DyFM_RangeValue).from;
2065
- }
2066
-
2067
- return (dataProperty): boolean => DyFM_RangeValue.isInRange(
2068
- dataProperty, searchValue as DyFM_RangeValue<number>
2069
- );
2070
- }
2071
-
2072
- if (Array.isArray(searchValue)) {
2073
- return (dataProperty): boolean => (searchValue as number[]).includes(dataProperty);
2074
- }
2075
-
2076
- return (dataProperty): boolean => dataProperty === searchValue;
2077
-
2078
- case DyFM_BasicProperty_Type.boolean:
2079
- return (dataProperty): boolean => dataProperty === searchValue;
2080
-
2081
- case DyFM_BasicProperty_Type.object:
2082
- if (Array.isArray(searchValue)) {
2083
- throw new DyFM_Error({
2084
- ...this._getDefaultErrorSettings(
2085
- 'getFilterFunctionForKey',
2086
- new Error(
2087
- `getFilterFunctionForKey failed, array search not implemented for this type ` +
2088
- `(${this.dataParams.dataName})`
2089
- )
2090
- ),
2091
-
2092
- errorCode: `${this.ecBase}DyNTS-DS0-GFF5`,
2093
- });
2094
- }
2095
-
2096
- return (dataProperty): boolean => {
2097
- const searchValueKeys: string[] = Object.keys(searchValue as object);
2098
- const stringifiedSearchValue = {};
2099
-
2100
- try {
2101
- searchValueKeys.forEach(
2102
- (key: string): void => {
2103
- stringifiedSearchValue[key] = JSON.stringify(searchValue[key]);
2104
- }
2105
- );
2106
- } catch (error) {
2107
- throw new DyFM_Error({
2108
- ...this._getDefaultErrorSettings(
2109
- 'getFilterFunctionForKey',
2110
- new Error(
2111
- `getFilterFunctionForKey failed, object search failed! ` +
2112
- `(${this.dataParams.dataName})`
2113
- )
2114
- ),
2115
-
2116
- errorCode: `${this.ecBase}DyNTS-DS0-GFF6`,
2117
- additionalContent: {
2118
- searchValue: searchValue,
2119
- },
2120
- });
2121
- }
2122
-
2123
- return searchValueKeys.every(
2124
- (key: string): boolean => {
2125
- try {
2126
- return stringifiedSearchValue[key] === JSON.stringify(dataProperty[key]);
2127
- } catch (error) {
2128
- console.error(
2129
- 'object filter returning false, bc of an error',
2130
- 'searchValue:', searchValue,
2131
- 'dataProperty:', dataProperty,
2132
- 'key:', key,
2133
- 'error:', error
2134
- );
2135
- return false;
2136
- }
2137
- }
2138
- );
2139
- }
2140
-
2141
- default:
2142
- if ((searchValue as DyFM_RangeValue).isRange) {
2143
- throw new DyFM_Error({
2144
- ...this._getDefaultErrorSettings(
2145
- 'getFilterFunctionForKey',
2146
- new Error(
2147
- `getFilterFunctionForKey failed, range search not implemented for this type ` +
2148
- `(${this.dataParams.dataName})`
2149
- )
2150
- ),
2151
-
2152
- errorCode: `${this.ecBase}DyNTS-DS0-GFF7`,
2153
- });
2154
- }
2155
-
2156
- if (Array.isArray(searchValue)) {
2157
- return (dataProperty): boolean => (searchValue as number[]).includes(dataProperty);
2158
- }
2159
-
2160
- return (dataProperty): boolean => dataProperty === searchValue;
2161
- }
2162
- } catch (error) {
2163
- throw new DyFM_Error({
2164
- ...this._getDefaultErrorSettings(
2165
- 'getFilterFunctionForKey',
2166
- error
2167
- ),
2168
-
2169
- errorCode: `${this.ecBase}DyNTS-DS0-GFF0`,
2170
- additionalContent: {
2171
- key: key,
2172
- searchValue: searchValue,
2173
- propertyType: propertyType,
2174
- },
2175
- });
2176
- }
2177
- }
2178
-
2179
- /**
2180
- * Get the value of a nested key.
2181
- * @param nestKeys - The nested keys to resolve.
2182
- * @returns The value of the nested key.
2183
- */
2184
- private getKeyResolver(nestKeys: string[]): (data: T) => any {
2185
- return (data: T): any => {
2186
- let value: any = data;
2187
-
2188
- nestKeys.forEach((nestKey: string): void => {
2189
- if (!value) {
2190
- return;
2191
- }
2192
-
2193
- if (nestKey === '*') {
2194
- // Wildcard: ha value egy tömb, akkor minden elemet visszaadunk
2195
- // A következő nestKey minden elemre alkalmazva lesz
2196
- if (Array.isArray(value)) {
2197
- // value marad tömb, a következő nestKey minden elemre alkalmazva lesz
2198
- // Nem változtatjuk meg a value-t, hogy a következő iterációban
2199
- // a tömb minden elemére alkalmazva legyen a következő nestKey
2200
- } else if (value && typeof value === 'object') {
2201
- // Ha objektum, akkor az értékeit adja vissza tömbként
2202
- value = Object.values(value);
2203
- } else {
2204
- // Egyébként undefined
2205
- value = undefined;
2206
- }
2207
- } else if (Array.isArray(value)) {
2208
- // Ha value tömb, akkor minden elemre alkalmazzuk a nestKey-t
2209
- if (!isNaN(+nestKey)) {
2210
- value = value[+nestKey];
2211
- } else {
2212
- value = value.map((element: any): any => element ? element[nestKey] : element);
2213
- }
2214
- } else {
2215
- value = value[nestKey];
2216
- }
2217
- });
2218
-
2219
- return value;
2220
- };
2221
- }
2222
-
2223
- /**
2224
- * Get the sort function for a key.
2225
- * @param sortSettings - The sort settings.
2226
- * @returns The sort function for the key.
2227
- */
2228
- protected getSortFunctionForKey<T>(sortSettings: DyFM_DSSort): (a: T, b: T) => number {
2229
- let keyResolver: (data: any) => any;
2230
- let valueType: DyFM_BasicProperty_Type;
2231
-
2232
- if (
2233
- !this.dataParams.properties[(sortSettings as DyFM_DSPropertySort).key] &&
2234
- !(sortSettings as DyFM_DSNestedPropertySort).isNestedPropertySort
2235
- ) {
2236
- throw new DyFM_Error({
2237
- ...this._getDefaultErrorSettings(
2238
- 'getSortFunctionForKey',
2239
- new Error(
2240
- `getSortFunctionForKey failed, key not found in dataParams ` +
2241
- `(${this.dataParams.dataName})`
2242
- )
2243
- ),
2244
-
2245
- errorCode: `${this.ecBase}DyNTS-DS0-GSF1`,
2246
- });
2247
- }
2248
-
2249
- if ((sortSettings as DyFM_DSNestedPropertySort).isNestedPropertySort) {
2250
- keyResolver = this.getKeyResolver((sortSettings as DyFM_DSNestedPropertySort).nestKeys);
2251
- valueType = (sortSettings as DyFM_DSNestedPropertySort).valueType;
2252
- } else {
2253
- keyResolver = (data: T): any => data[(sortSettings as DyFM_DSPropertySort).key];
2254
- valueType = this.dataParams.properties[(sortSettings as DyFM_DSPropertySort).key].type;
2255
- }
2256
-
2257
- if (!valueType) {
2258
- throw new DyFM_Error({
2259
- ...this._getDefaultErrorSettings(
2260
- 'getSortFunctionForKey',
2261
- new Error(
2262
- `getSortFunctionForKey failed, valueType is missing! ` +
2263
- `(${this.dataParams.dataName})`
2264
- )
2265
- ),
2266
-
2267
- errorCode: `${this.ecBase}DyNTS-DS0-GSF1`,
2268
- });
2269
- }
2270
-
2271
- const sortValue: 1 | -1 = (
2272
- sortSettings.order === 1 ||
2273
- sortSettings.order === 'asc' ||
2274
- sortSettings.order === 'ascending'
2275
- ) ? 1 : -1;
2276
-
2277
- switch (valueType) {
2278
- case DyFM_BasicProperty_Type.string:
2279
- return (a: T, b: T): number => {
2280
- a = keyResolver(a);
2281
- b = keyResolver(b);
2282
-
2283
- return (a as string).localeCompare(b as string) * sortValue;
2284
- }
2285
-
2286
- case DyFM_BasicProperty_Type.date:
2287
- return (a: T, b: T): number => {
2288
- const aNum = +new Date(keyResolver(a));
2289
- const bNum = +new Date(keyResolver(b));
2290
-
2291
- if (aNum < bNum) {
2292
- return -sortValue;
2293
- } else if (aNum > bNum) {
2294
- return sortValue;
2295
- } else {
2296
- return 0;
2297
- }
2298
- };
2299
-
2300
- case DyFM_BasicProperty_Type.number:
2301
- return (a: T, b: T): number => {
2302
- a = keyResolver(a);
2303
- b = keyResolver(b);
2304
-
2305
- if (a < b) {
2306
- return -sortValue;
2307
- } else if (a > b) {
2308
- return sortValue;
2309
- } else {
2310
- return 0;
2311
- }
2312
- };
2313
-
2314
- case DyFM_BasicProperty_Type.boolean:
2315
- return (a: T, b: T): number => {
2316
- a = keyResolver(a);
2317
- b = keyResolver(b);
2318
-
2319
- if (a === b) {
2320
- return 0;
2321
- }
2322
-
2323
- if (a) {
2324
- return sortValue;
2325
- }
2326
-
2327
- return -sortValue;
2328
- };
2329
-
2330
- default:
2331
- throw new DyFM_Error({
2332
- ...this._getDefaultErrorSettings(
2333
- 'getSortFunctionForKey',
2334
- new Error(
2335
- `getSortFunctionForKey failed, sorting not implemented for this type ` +
2336
- `(${this.dataParams.dataName}, ${valueType})`
2337
- )
2338
- ),
2339
-
2340
- errorCode: `${this.ecBase}DyNTS-DS0-GSF2`,
2341
- });
2342
- }
2343
- }
2344
-
2345
- /* private getSortFunctionForNestedKeys<T>(
2346
- sortSettings: DyFM_DSNestedPropertySort
2347
- ): (a: T, b: T) => number {
2348
- const nestedPropertyResolver: (data) => any = this.getKeyResolver(sortSettings.nestKeys);
2349
-
2350
- const sortValue: 1 | -1 = (
2351
- sortSettings.order === 1 ||
2352
- sortSettings.order === 'asc' ||
2353
- sortSettings.order === 'ascending'
2354
- ) ? 1 : -1;
2355
-
2356
- switch (sortSettings.valueType) {
2357
- case DyFM_BasicProperty_Type.string:
2358
- return (a: T, b: T): number => {
2359
- a = nestedPropertyResolver(a);
2360
- b = nestedPropertyResolver(b);
2361
-
2362
- return (a as string).localeCompare(b as string) * sortValue
2363
- };
2364
-
2365
- case DyFM_BasicProperty_Type.date:
2366
- return (a: T, b: T): number => {
2367
- a = nestedPropertyResolver(a);
2368
- b = nestedPropertyResolver(b);
2369
-
2370
- if (+new Date(a) < +new Date(b)) {
2371
- return -sortValue;
2372
- } else if (+new Date(a) > +new Date(b)) {
2373
- return sortValue;
2374
- } else {
2375
- return 0;
2376
- }
2377
- };
2378
-
2379
- case DyFM_BasicProperty_Type.number:
2380
- return (a: T, b: T): number => {
2381
- a = nestedPropertyResolver(a);
2382
- b = nestedPropertyResolver(b);
2383
-
2384
- if (a < b) {
2385
- return -sortValue;
2386
- } else if (a > b) {
2387
- return sortValue;
2388
- } else {
2389
- return 0;
2390
- }
2391
- };
2392
-
2393
- case DyFM_BasicProperty_Type.boolean:
2394
- return (a: T, b: T): number => {
2395
- a = nestedPropertyResolver(a);
2396
- b = nestedPropertyResolver(b);
2397
-
2398
- if (a === b) {
2399
- return 0;
2400
- }
2401
-
2402
- if (a) {
2403
- return sortValue;
2404
- }
2405
-
2406
- return -sortValue;
2407
- };
2408
-
2409
- default:
2410
- throw new DyFM_Error({
2411
- ...this._getDefaultErrorSettings(
2412
- 'getSortFunctionForKey',
2413
- new Error(
2414
- `getSortFunctionForKey failed, sorting not implemented for this type ` +
2415
- `(${this.dataParams.dataName}, ${this.dataParams.properties[key].type})`
2416
- )
2417
- ),
2418
-
2419
- errorCode: `${this.ecBase}DyNTS-DS0-GSF2`,
2420
- });
2421
- }
2422
- } */
2423
-
2424
-
2425
- /**
2426
- * setting up dependency dataHook by DynamoNTSDataModelParams
2427
- */
2428
- private lookForDependencyDataSettings(): void {
2429
- const dependencyParams: DyFM_DataProperty_Params<any, T>[] =
2430
- Object.values(this.dataParams.properties).filter(
2431
- (modelParams: DyFM_DataProperty_Params<any, T>): boolean =>
2432
- Boolean(modelParams.dependencyDataName)
2433
- );
2434
-
2435
- this.depSettings.push(...dependencyParams.map((dependencyParams) => ({
2436
- key: dependencyParams.key,
2437
- dbServiceKey: dependencyParams.dependencyDataName,
2438
- keyIsUnique: dependencyParams.unique,
2439
- keyIsRequired: dependencyParams.required,
2440
- })));
2441
-
2442
- if (this.depSettings.length === 1) {
2443
- this.depDataDBService = DyNTS_GlobalService.getDBServiceByKey(
2444
- this.depSettings[0].dbServiceKey
2445
- );
2446
- }
2447
- }
2448
-
2449
- /**
2450
- *
2451
- * @returns
2452
- */
2453
- getDependencyDataDBService(dBServiceKey?: string): DyNTS_DBService<any> {
2454
- if (!dBServiceKey && !this.depDataDBService) {
2455
- throw new DyFM_Error({
2456
- ...this._getDefaultErrorSettings(
2457
- 'getDependencyDataDBService',
2458
- new Error(
2459
- `getDependencyDataDBService was unsuccessful, service key not setted up! ` +
2460
- `(key: ${this.depSettings.map((depSetting) => depSetting.key).join(', ')}, ` +
2461
- `(${this.dataParams.dataName})`
2462
- )
2463
- ),
2464
-
2465
- status: 500,
2466
- errorCode: `${this.ecBase}DyNTS-DS0-GDDB0`,
2467
- });
2468
- }
2469
-
2470
- if (this.depDataDBService) {
2471
- return this.depDataDBService;
2472
- } else {
2473
- this.depDataDBService = DyNTS_GlobalService.getDBServiceByKey(dBServiceKey);
2474
-
2475
- return this.depDataDBService;
2476
- }
2477
- }
2478
-
2479
- getProvidedData(data: T): T {
2480
- return data;
2481
- }
2482
-
2483
- getProvidedDataList(dataList: T[]): T[] {
2484
- return dataList;
2485
- }
2486
-
2487
- private _getDefaultErrorSettings(
2488
- fnName: string,
2489
- error: DyFM_AnyError
2490
- ): DyFM_Error_Settings {
2491
- return {
2492
- status: (error as DyFM_Error)?.___status ?? 500,
2493
- message: (error as Error)?.message ??
2494
- (error as DyFM_Error)?._message ??
2495
- `${fnName} was UNSUCCESSFUL (NTS; ${this.dataParams.dataName})`,
2496
- addECToUserMsg: !(error as DyFM_Error)?.__userMessage,
2497
- userMessage: (error as DyFM_Error)?.__userMessage ?? this.defaultErrorUserMsg,
2498
- issuer: this.issuer,
2499
- issuerService: this.serviceName + ` (${this?.constructor?.name}-DyNTS_DataService)`,
2500
- systemVersion: DyNTS_global_settings.systemVersion,
2501
- error: error,
2502
- };
2503
- }
2504
-
2505
- protected getDefaultErrorSettings(
2506
- fnName: string,
2507
- error: DyFM_AnyError,
2508
- /** @deprecated we wont support the separate user message in the future, use the message instead */
2509
- useMessageAsUserMessage: boolean = true,
2510
- ): DyFM_Error_Settings {
2511
- return {
2512
- status: (error as DyFM_Error)?.___status ?? 500,
2513
- message: (error as Error)?.message ??
2514
- (error as DyFM_Error)?._message ??
2515
- `${fnName} was UNSUCCESSFUL (${DyNTS_global_settings.systemShortCodeName})`,
2516
- addECToUserMsg: !(error as DyFM_Error)?.__userMessage,
2517
- userMessage: (error as DyFM_Error)?.__userMessage ??
2518
- (useMessageAsUserMessage ? (error as Error)?.message : this.defaultErrorUserMsg),
2519
- issuer: this.issuer,
2520
- issuerService: this.constructor?.name,
2521
- systemVersion: DyNTS_global_settings.systemVersion,
2522
- error: error,
2523
- };
2524
- }
2525
- }
1
+
2
+ import {
3
+ DyFM_AnyError,
4
+ DyFM_Array,
5
+ DyFM_BasicProperty_Type,
6
+ DyFM_DataModel_Params,
7
+ DyFM_DataProperties,
8
+ DyFM_DataProperty_Params,
9
+ DyFM_DBFilter,
10
+ DyFM_DBFilterExpressions,
11
+ DyFM_DSNestedPropertySort,
12
+ DyFM_DSPropertySort,
13
+ DyFM_DSSort,
14
+ DyFM_Error,
15
+ DyFM_Error_Settings,
16
+ DyFM_ErrorLevel,
17
+ DyFM_Log,
18
+ DyFM_Metadata,
19
+ DyFM_NestPropertySearch,
20
+ DyFM_Object,
21
+ DyFM_RangeValue,
22
+ DyFM_SearchQuery,
23
+ DyFM_SearchResult,
24
+ DyFM_SpecialSearch,
25
+ DyFM_String,
26
+ DyFM_ValidationError,
27
+ DyFM_ValidationErrors
28
+ } from '@futdevpro/fsm-dynamo';
29
+
30
+ import { DyNTS_getArchivedDBName } from '../../_collections/archive.util';
31
+ import { DyNTS_global_settings } from '../../_collections/global-settings.const';
32
+ import { DyNTS_DBUpdate } from '../../_models/types/db-update.type';
33
+ import { DyNTS_GlobalService } from '../core/global.service';
34
+ import { DyNTS_ArchiveDataService } from './archive-data.service';
35
+ import { DyNTS_DBService } from './db.service';
36
+
37
+ // TODO: 2 type of archiving service system: within list, or separate db elements
38
+
39
+ /**
40
+ * Basic Data Service that is connected to the relevant DBServices
41
+ *
42
+ * @example
43
+ * export class UserMatchStatisticsService extends DyNTS_DataService<UserMatchStatistics> {
44
+ * constructor(
45
+ * set: {
46
+ * data?: UserMatchStatistics,
47
+ * issuer: string,
48
+ * }
49
+ * ) {
50
+ * super(
51
+ * new UserMatchStatistics(set?.data),
52
+ * userMatchStatisticsModelParams
53
+ * );
54
+ * this.issuer = set.issuer;
55
+ * }
56
+ * }
57
+ */
58
+ export class DyNTS_DataService<T extends DyFM_Metadata> {
59
+
60
+ serviceName: string;
61
+ serviceNameShortCode: string;
62
+ /** error code base */
63
+ ecBase: string = `${DyNTS_global_settings.systemShortCodeName}|`;
64
+
65
+ dataDBService: DyNTS_DBService<T>;
66
+ haveArchiveDataService: boolean;
67
+
68
+ /* data: T; */
69
+ /** @deprecated */
70
+ dataList: T[] = [];
71
+ /* issuer: string; */
72
+
73
+ readonly depSettings: {
74
+ key: string;
75
+ dbServiceKey: string;
76
+ keyIsUnique: boolean;
77
+ keyIsRequired: boolean;
78
+ }[] = [];
79
+ /* depKeys: string[] = [];
80
+ depDBServiceKeys: string[] = [];
81
+ depKeyIsUnique: boolean[] = [];
82
+ depKeyIsRequired: boolean[] = []; */
83
+ private depDataDBService: DyNTS_DBService<any>;
84
+
85
+ /* dataParams: DyFM_DataModel_Params<T>; */
86
+
87
+ readonly defaultErrorUserMsg: string =
88
+ `We encountered an unhandled Data Service Error, ` +
89
+ `\nplease contact the responsible development team.`;
90
+ readonly defaultValidationErrorUserMsg: string =
91
+ `We encountered an unhandled Validation Error, ` +
92
+ `\nplease contact the responsible development team.`;
93
+
94
+ constructor(
95
+ /**
96
+ * Initial data, this will be used by functions on default
97
+ * @deprecated
98
+ */
99
+ public data: T,
100
+ /**
101
+ * DB data prams will be used to connect to usable dbService on GlobalService
102
+ */
103
+ /* dataParams: DyFM_DataModel_Params, */
104
+ public dataParams: DyFM_DataModel_Params<T>,
105
+ /**
106
+ * Initial set for issuer to be able to follow the issuer's activity
107
+ */
108
+ public issuer: string
109
+ ) {
110
+ try {
111
+ this.serviceName = this.constructor?.name;
112
+ this.serviceNameShortCode = DyFM_String.anyNameToShortCode(this.serviceName);
113
+ this.ecBase = `${DyNTS_global_settings.systemShortCodeName}|${this.serviceNameShortCode}|`;
114
+ this.dataDBService = DyNTS_GlobalService.getDBService<T>(this.dataParams);
115
+ /* this.data = data; */
116
+ /* this.dataParams = dataParams; */
117
+ this.haveArchiveDataService = this.dataParams.addArchive;
118
+ this.lookForDependencyDataSettings();
119
+ /* this.issuer = issuer; */
120
+ /* DyNTS_GlobalService.addDataService(this); */
121
+ } catch (error) {
122
+ throw new DyFM_Error({
123
+ ...this._getDefaultErrorSettings('constructor', error),
124
+
125
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-DS0-C00`,
126
+ message:
127
+ `The dataService construction failed for "${this.dataParams?.dataName}". ` +
128
+ `at "${this.serviceName}" (${this.constructor.name})` +
129
+ `\nMaybe you forgot to add the dbService to the GlobalService?` +
130
+ `\nOr just trying to use a dependencyDataName that is not present in this db...`,
131
+ level: DyFM_ErrorLevel.critical,
132
+ });
133
+ /* DyFM_Log.error(
134
+ `\nDyNTS_DataService ERROR: ` +
135
+ `\nThe dataService construction failed for ` +
136
+ `${this.dataParams?.dataName}. ${this.serviceName} (${this.constructor.name})` +
137
+ `\nMaybe you forgot to add the dbService to the GlobalService?` +
138
+ `\n\n`,
139
+ new Error()
140
+ ); */
141
+ }
142
+ }
143
+
144
+ getArchiveDataService(): DyNTS_ArchiveDataService<T> {
145
+ throw new DyFM_Error({
146
+ ...this._getDefaultErrorSettings(
147
+ 'getArchiveDataService',
148
+ new Error('getArchiveDataService is not implemented!')
149
+ ),
150
+
151
+ errorCode: `${this.ecBase}DyNTS-DS-GAD0`,
152
+ });
153
+ }
154
+
155
+ /**
156
+ * returns all data from database to service dataList
157
+ */
158
+ async getAll(dontSetToService?: boolean): Promise<T[]> {
159
+ try {
160
+ const dataListExists: T[] = await this.dataDBService.getAll().catch((error): T[] => {
161
+ if (error?.errorCode?.includes('DBS-GA1')) {
162
+ DyFM_Log.warn(`getAll "${this.dataParams.dataName}" didn't found any.`);
163
+
164
+ return [];
165
+ } else {
166
+ throw error;
167
+ }
168
+ });
169
+
170
+ if (!dontSetToService) {
171
+ this.dataList = dataListExists;
172
+ }
173
+
174
+ return dataListExists;
175
+ } catch (error) {
176
+ throw new DyFM_Error({
177
+ ...this._getDefaultErrorSettings('getAll', error),
178
+
179
+ errorCode: `${this.ecBase}DyNTS-DS-GA0`,
180
+ });
181
+ }
182
+ }
183
+
184
+ /**
185
+ * @description
186
+ * returns data from database by id
187
+ * also if dontSetToService is false or not setted,
188
+ * the data will be saved to the service, even if its not found
189
+ *
190
+ * if the data is not found, it will try to find it from the archive
191
+ * unless skipArchiveLoad is set to true
192
+ *
193
+ * @remarks
194
+ * If you need to get-save a data, if possible,
195
+ * use db-service update instead.
196
+ *
197
+ * @param {string} id
198
+ * (using id from service.data, if not provided)
199
+ * @param dontSetToService
200
+ *
201
+ * @return {T} data: T
202
+ */
203
+ async getDataById(
204
+ id?: string,
205
+ dontSetToService?: boolean,
206
+ skipArchiveLoad?: boolean
207
+ ): Promise<T> {
208
+ try {
209
+ id = id ?? this.data?._id;
210
+
211
+ if (!id) {
212
+ throw new DyFM_Error({
213
+ ...this._getDefaultErrorSettings(
214
+ 'getDataById',
215
+ new Error(
216
+ `getDataById failed, ID is missing! ` +
217
+ `(maybe you wanted to use getDataByDependencyId() instead...) ` +
218
+ `(${this.dataParams.dataName})`
219
+ )
220
+ ),
221
+
222
+ errorCode: `${this.ecBase}DyNTS-DS-GI1`,
223
+ });
224
+ }
225
+
226
+ let dataExists: T = await this.dataDBService.getDataById(id);
227
+
228
+ if (!dataExists && this.haveArchiveDataService && !skipArchiveLoad) {
229
+ const archiveDataService: DyNTS_ArchiveDataService<T> = this.getArchiveDataService();
230
+
231
+ dataExists = await archiveDataService.getDataByOriginalId(id, true);
232
+ }
233
+
234
+ if (!dontSetToService) {
235
+ this.data = dataExists;
236
+ }
237
+
238
+ return dataExists;
239
+ } catch (error) {
240
+ if (error?.errorCode == `${DyNTS_global_settings.systemShortCodeName}|DyNTS-DS0-GI1`) {
241
+ throw error;
242
+ } else {
243
+ throw new DyFM_Error({
244
+ ...this._getDefaultErrorSettings('getDataById', error),
245
+ errorCode: `${this.ecBase}DyNTS-DS-GI0`,
246
+ });
247
+ }
248
+ }
249
+ }
250
+
251
+ async getDataByIds(ids: string[], dontSetToService?: boolean): Promise<T[]> {
252
+ try {
253
+ if (!ids) {
254
+ throw new DyFM_Error({
255
+ ...this._getDefaultErrorSettings(
256
+ 'getDataByIds',
257
+ new Error(`getDataByIds failed, ids is missing! (${this.dataParams.dataName})`)
258
+ ),
259
+
260
+ errorCode: `${this.ecBase}DyNTS-DS-GIS1`,
261
+ });
262
+ }
263
+
264
+ if (ids.length === 0) {
265
+ return [];
266
+ }
267
+
268
+ const dataList: T[] = await this.dataDBService.find({ _id: { $in: ids } });
269
+
270
+ if (!dontSetToService) {
271
+ this.dataList = dataList;
272
+ }
273
+
274
+ return dataList;
275
+ } catch (error) {
276
+ throw new DyFM_Error({
277
+ ...this._getDefaultErrorSettings('getDataByIds', error),
278
+
279
+ errorCode: `${this.ecBase}DyNTS-DS-GIS0`,
280
+ });
281
+ }
282
+ }
283
+
284
+ async getDataListByIds(ids: string[], dontSetToService?: boolean): Promise<T[]> {
285
+ try {
286
+ const dataList: T[] = await this.dataDBService.getDataListByIds(ids);
287
+
288
+ if (!dontSetToService) {
289
+ this.dataList = dataList;
290
+ }
291
+
292
+ return dataList;
293
+ } catch (error) {
294
+ throw new DyFM_Error({
295
+ ...this._getDefaultErrorSettings('getDataListByIds', error),
296
+
297
+ errorCode: `${this.ecBase}DyNTS-DS-GIL0`,
298
+ });
299
+ }
300
+ }
301
+
302
+ private getDependencyIdsFilter(
303
+ dependencyIds?: string | { [key: string]: string }
304
+ ): { [key: string]: string } {
305
+ if (!this.depSettings.length) {
306
+ DyFM_Log.warn(
307
+ `❌ getDataByDependencyId failed, dependencyKey is missing from service! ` +
308
+ `(${this.dataParams.dataName})` +
309
+ `\n 📍 ${this.dataParams.stackLocation}`
310
+ );
311
+
312
+ throw new DyFM_Error({
313
+ ...this._getDefaultErrorSettings(
314
+ 'getDataByDependencyId',
315
+ new Error(
316
+ `getDataByDependencyId failed, dependencyKey is missing from service! ` +
317
+ `(${this.dataParams.dataName})`
318
+ )
319
+ ),
320
+
321
+ errorCode: `${this.ecBase}DyNTS-DS-GD1`,
322
+ });
323
+ }
324
+
325
+ if (typeof dependencyIds === 'string') {
326
+ if (this.depSettings.length === 1) {
327
+ dependencyIds = { [this.depSettings[0].key]: dependencyIds };
328
+ } else if (this.depSettings.length > 1) {
329
+ throw new DyFM_Error({
330
+ ...this._getDefaultErrorSettings(
331
+ 'getDataByDependencyId',
332
+ new Error(
333
+ `getDataByDependencyId failed, there are multiple dependencyKeys, ` +
334
+ `so you need to provide a map of dependencyKeys! ` +
335
+ `(${this.dataParams.dataName})`
336
+ )
337
+ ),
338
+
339
+ errorCode: `${this.ecBase}DyNTS-DS-GD2`,
340
+ });
341
+ }
342
+ } else if (!dependencyIds) {
343
+ dependencyIds = {};
344
+ this.depSettings.forEach((depSetting) => {
345
+ if (this.data?.[depSetting.key]) {
346
+ dependencyIds[depSetting.key] = this.data?.[depSetting.key];
347
+ }
348
+ });
349
+ }
350
+
351
+ if (
352
+ !dependencyIds ||
353
+ this.depSettings.every((depSetting) => !dependencyIds[depSetting.key])
354
+ ) {
355
+ throw new DyFM_Error({
356
+ ...this._getDefaultErrorSettings(
357
+ 'getDataByDependencyId',
358
+ new Error(
359
+ `getDataByDependencyId failed, dependencyId is missing! ` +
360
+ `(${this.dataParams.dataName})`
361
+ )
362
+ ),
363
+
364
+ errorCode: `${this.ecBase}DyNTS-DS-GD3`,
365
+ });
366
+ }
367
+
368
+ return dependencyIds as { [key: string]: string };
369
+ }
370
+
371
+ /**
372
+ * returns data from database by dependencyId to the service
373
+ * (using id from service.data, if not provided)
374
+ * @param dependencyIds
375
+ */
376
+ async getDataByDependencyId(
377
+ dependencyIds?: string | { [key: string]: string },
378
+ dontSetToService?: boolean
379
+ ): Promise<T> {
380
+ try {
381
+ const dependencyIdsFilter: { [key: string]: string } =
382
+ this.getDependencyIdsFilter(dependencyIds);
383
+
384
+ const dataExists: T =
385
+ await this.dataDBService.getDataByDependencyId(dependencyIdsFilter).catch(
386
+ (error): null => {
387
+ if (error?.errorCode?.includes('DBS-GD2')) {
388
+ DyFM_Log.warn(
389
+ `getDataByDependencyId failed; "${this.dataParams.dataName}" ` +
390
+ `(${JSON.stringify(dependencyIds)}) didn't found any.`
391
+ );
392
+
393
+ return null;
394
+ } else {
395
+ throw error;
396
+ }
397
+ }
398
+ );
399
+
400
+ if (!dontSetToService) {
401
+ this.data = dataExists;
402
+ }
403
+
404
+ return dataExists;
405
+ } catch (error) {
406
+ if ([
407
+ `${this.ecBase}DyNTS-DS0-GD1`,
408
+ `${this.ecBase}DyNTS-DS0-GD2`
409
+ ].includes(error?.errorCode)) {
410
+ throw error;
411
+ } else {
412
+ throw new DyFM_Error({
413
+ ...this._getDefaultErrorSettings('getDataByDependencyId', error),
414
+
415
+ errorCode: `${this.ecBase}DyNTS-DS-GD0`,
416
+ });
417
+ }
418
+ }
419
+ }
420
+
421
+ async getDataListByDependencyIds(
422
+ dependencyIds: string[],
423
+ dontSetToService?: boolean,
424
+ dependencyKey?: string,
425
+ ): Promise<T[]> {
426
+ try {
427
+ if (!this.depSettings.length) {
428
+ throw new DyFM_Error({
429
+ ...this._getDefaultErrorSettings(
430
+ 'getDataListByDependencyIds',
431
+ new Error(
432
+ `getDataListByDependencyIds failed, dependencyKey is missing from service! ` +
433
+ `(${this.dataParams.dataName})`
434
+ )
435
+ ),
436
+
437
+ errorCode: `${this.ecBase}DyNTS-DS-GDS1`,
438
+ });
439
+ }
440
+
441
+ if (!dependencyKey) {
442
+ if (this.depSettings.length === 1) {
443
+ dependencyKey = this.depSettings[0].key;
444
+ } else {
445
+ throw new DyFM_Error({
446
+ ...this._getDefaultErrorSettings(
447
+ 'getDataListByDependencyIds',
448
+ new Error(
449
+ `getDataListByDependencyIds failed, dependencyKey is missing! ` +
450
+ `since there are multiple dependencyKeys, you need to provide one! ` +
451
+ `(${this.dataParams.dataName})`
452
+ )
453
+ ),
454
+
455
+ errorCode: `${this.ecBase}DyNTS-DS-GDS2`,
456
+ });
457
+ }
458
+ }
459
+
460
+ if (!dependencyIds) {
461
+ throw new DyFM_Error({
462
+ ...this._getDefaultErrorSettings(
463
+ 'getDataListByDependencyIds',
464
+ new Error(
465
+ `getDataListByDependencyIds failed, dependencyIds is missing! ` +
466
+ `(${this.dataParams.dataName})`
467
+ )
468
+ ),
469
+
470
+ errorCode: `${this.ecBase}DyNTS-DS-GDS3`,
471
+ });
472
+ }
473
+
474
+ if (dependencyIds.length === 0) {
475
+ return [];
476
+ }
477
+
478
+ const dataList: T[] = await this.dataDBService.getDataListByDependencyIds(
479
+ dependencyKey, dependencyIds
480
+ );
481
+
482
+ if (!dontSetToService) {
483
+ this.dataList = dataList;
484
+ }
485
+
486
+ return dataList;
487
+ } catch (error) {
488
+ if ([
489
+ `${this.ecBase}DyNTS-DS0-GDS1`,
490
+ `${this.ecBase}DyNTS-DS0-GDS2`
491
+ ].includes(error?.errorCode)) {
492
+ throw error;
493
+ } else {
494
+ throw new DyFM_Error({
495
+ ...this._getDefaultErrorSettings('getDataListByDependencyIds', error),
496
+
497
+ errorCode: `${this.ecBase}DyNTS-DS0-GDS0`,
498
+ });
499
+ }
500
+ }
501
+ }
502
+
503
+ /**
504
+ * returns dataList from database by dependencyId to the service
505
+ * @param dependencyIds
506
+ */
507
+ async getDataListByDependencyId(
508
+ dependencyIds?: string | { [key: string]: string },
509
+ dontSetToService?: boolean
510
+ ): Promise<T[]> {
511
+ try {
512
+ const dependencyIdsFilter: { [key: string]: string } =
513
+ this.getDependencyIdsFilter(dependencyIds);
514
+
515
+ const dataListExists: T[] =
516
+ await this.dataDBService.getDataListByDependencyId(dependencyIdsFilter).catch(
517
+ (error): T[] => {
518
+ if (error?.errorCode?.includes('DBS-GLD2')) {
519
+ DyFM_Log.warn(
520
+ `getDataListByDependencyId "${this.dataParams.dataName}" ` +
521
+ `(${JSON.stringify(dependencyIdsFilter)}) didn't found any.`
522
+ );
523
+
524
+ return [];
525
+ } else {
526
+ throw error;
527
+ }
528
+ }
529
+ );
530
+
531
+ if (!dontSetToService) {
532
+ this.dataList = dataListExists;
533
+ }
534
+
535
+ return dataListExists;
536
+ } catch (error) {
537
+ if ([
538
+ `${this.ecBase}DyNTS-DS0-GLD1`,
539
+ `${this.ecBase}DyNTS-DS0-GLD2`
540
+ ].includes(error?.errorCode)) {
541
+ throw error;
542
+ } else {
543
+ throw new DyFM_Error({
544
+ ...this._getDefaultErrorSettings('getDataListByDependencyId', error),
545
+
546
+ errorCode: `${this.ecBase}DyNTS-DS0-GLD0`,
547
+
548
+ });
549
+ }
550
+ }
551
+ }
552
+
553
+ /**
554
+ *
555
+ * // findOne desc:
556
+ *
557
+ * Find the data first by any of its parameters,
558
+ * also if dontSetToService is false or not setted,
559
+ * the data will be saved to the service, even if non found
560
+ *
561
+ * @param filter if you can, use unique parameters for find!
562
+ *
563
+ * @example
564
+ * // by email:
565
+ * { email: email }
566
+ * //
567
+ * @example
568
+ * // or by id that is in list:
569
+ * { userIds: { $in: this.userId } }
570
+ * // or by userIds:
571
+ * { userId: { $in: userIds } }
572
+ * //
573
+ * @example
574
+ * // or by number or Date that is Greater Than AND Less Than:
575
+ * { points: { $gt: 2, $lt: 14 } }
576
+ * // further tools (syntax matches with $gt):
577
+ * $eq: // Matches values that are EQual to a specified value.
578
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
579
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
580
+ * $ne: // Matches all values that are Not Equal to a specified value.
581
+ * $nin: // Matches None of the values specified IN an array.
582
+ * //
583
+ * @returns {T} data: T
584
+ */
585
+ async findData(filterBy: DyFM_DBFilter<T>, dontSetToService?: boolean): Promise<T> {
586
+ try {
587
+ const dataExists: T = await this.dataDBService.findOne(filterBy).catch((error): null => {
588
+ if (error?.errorCode?.includes('DBS-FO1')) {
589
+ DyFM_Log.warn(`findData "${this.dataParams.dataName}" didn't found any.`);
590
+
591
+ return null;
592
+ } else {
593
+ throw error;
594
+ }
595
+ });
596
+
597
+ if (!dontSetToService) {
598
+ this.data = dataExists;
599
+ }
600
+
601
+ return dataExists;
602
+ } catch (error) {
603
+ throw new DyFM_Error({
604
+ ...this._getDefaultErrorSettings('findData', error),
605
+
606
+ errorCode: `${this.ecBase}DyNTS-DS0-FD0`,
607
+ });
608
+ }
609
+ }
610
+ getDataByQuery: typeof this.findData = this.findData;
611
+
612
+ /**
613
+ *
614
+ * // find desc:
615
+ *
616
+ * Find the data first by any of its parameters,
617
+ * also if dontSetToService is false or not setted,
618
+ * the data will be saved to the service, even if non found
619
+ *
620
+ * @param filter if you can, use unique parameters for find!
621
+ *
622
+ * @example
623
+ * // by email:
624
+ * { email: email }
625
+ * //
626
+ * @example
627
+ * // or by id that is in list:
628
+ * { userIds: { $in: this.userId } }
629
+ * // or by userIds:
630
+ * { userId: { $in: userIds } }
631
+ * //
632
+ * @example
633
+ * // or by number or Date that is Greater Than AND Less Than:
634
+ * { points: { $gt: 2, $lt: 14 } }
635
+ * // further tools (syntax matches with $gt):
636
+ * $eq: // Matches values that are EQual to a specified value.
637
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
638
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
639
+ * $ne: // Matches all values that are Not Equal to a specified value.
640
+ * $nin: // Matches None of the values specified IN an array.
641
+ * //
642
+ * @returns {T[]} dataList: T[]
643
+ */
644
+ async findDataList(filterBy: DyFM_DBFilter<T>, dontSetToService?: boolean): Promise<T[]> {
645
+ try {
646
+ const dataListExists: T[] = await this.dataDBService.find(filterBy).catch((error): T[] => {
647
+ if (error?.errorCode?.includes('DBS-F1')) {
648
+ DyFM_Log.warn(`findDataList "${this.dataParams.dataName}" didn't found any.`);
649
+
650
+ return [];
651
+ } else {
652
+ throw error;
653
+ }
654
+ });
655
+
656
+ if (!dontSetToService) {
657
+ this.dataList = dataListExists;
658
+ }
659
+
660
+ return dataListExists;
661
+ } catch (error) {
662
+ throw new DyFM_Error({
663
+ ...this._getDefaultErrorSettings('findDataList', error),
664
+
665
+ errorCode: `${this.ecBase}DyNTS-DS0-FDS0`,
666
+ });
667
+ }
668
+ }
669
+
670
+ /**
671
+ * This function uses the dataDBService.updateOne function.
672
+ * This uses updateBy if setted, or data._id if its setted or this.data[this.dependencyKey]
673
+ * @param set
674
+ *
675
+ * // updateOne desc:
676
+ *
677
+ * Find the data first by any of its parameters, throws error if not found
678
+ * @param filter This uses the basic Mongoose updateOne.
679
+ * If you can, use unique parameters for find!
680
+ * @example
681
+ * // by email:
682
+ * { email: email }
683
+ * //
684
+ * @example
685
+ * // or by id that is in list:
686
+ * { userIds: { $in: this.userId } }
687
+ * // or by userIds:
688
+ * { userId: { $in: userIds } }
689
+ * //
690
+ * @example
691
+ * // or by number or Date that is Greater Than AND Less Than:
692
+ * { points: { $gt: 2, $lt: 14 } }
693
+ * // further tools (syntax matches with $gt):
694
+ * $eq: // Matches values that are EQual to a specified value.
695
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
696
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
697
+ * $ne: // Matches all values that are Not Equal to a specified value.
698
+ * $nin: // Matches None of the values specified IN an array.
699
+ * //
700
+ *
701
+ * @param update this uses the basic Mongoose updateOne
702
+ * @example
703
+ * // increase a specific value (here by 15):
704
+ * { $inc: { popularity: 15 } }
705
+ * //
706
+ * @example
707
+ * // or add element to a list:
708
+ * { $push: { reactions: this.newReaction }
709
+ * // or add multiple elements to a list
710
+ * { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
711
+ * //
712
+ * @example
713
+ * // or all at once
714
+ * {
715
+ * $inc: { popularity: this.newVote.amount },
716
+ * emailVerified: true,
717
+ * $push: { reactions: this.newReaction }
718
+ * }
719
+ * // further tools (syntax matches with $inc):
720
+ * $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
721
+ * $min: // Only updates the field if the specified value is less than the existing field value.
722
+ * $max: // Only updates the field if the specified value is greater than the existing field value.
723
+ * $mul: // Multiplies the value of the field by the specified amount.
724
+ * $rename: // Renames a field.
725
+ * $unset: // Removes the specified field from a document. (set: "" to value)
726
+ * //
727
+ */
728
+ async updateData(
729
+ set: { filterBy?: DyFM_DBFilter<T>, update: DyNTS_DBUpdate<T> },
730
+ dontUpdateModified?: boolean
731
+ ): Promise<void> {
732
+ try {
733
+ if (set.filterBy) {
734
+ await this.dataDBService.updateOne(
735
+ set.filterBy,
736
+ set.update,
737
+ this.issuer,
738
+ dontUpdateModified
739
+ );
740
+
741
+ } else if (this.data._id) {
742
+ await this.dataDBService.updateOne(
743
+ { _id: this.data._id } as DyFM_DBFilter<T>,
744
+ set.update,
745
+ this.issuer,
746
+ dontUpdateModified
747
+ );
748
+
749
+ } else if (
750
+ this.depSettings.length &&
751
+ this.depSettings.some((depSetting) => this.data[depSetting.key])
752
+ ) {
753
+ const dependencyIdsFilter: { [key: string]: string } = {};
754
+
755
+ this.depSettings.forEach((depSetting) => {
756
+ if (this.data[depSetting.key]) {
757
+ dependencyIdsFilter[depSetting.key] = this.data[depSetting.key];
758
+ }
759
+ });
760
+
761
+ await this.dataDBService.updateOne(
762
+ dependencyIdsFilter as DyFM_DBFilter<T>,
763
+ set.update,
764
+ this.issuer,
765
+ dontUpdateModified
766
+ );
767
+
768
+ } else {
769
+ throw new DyFM_Error({
770
+ ...this._getDefaultErrorSettings(
771
+ 'updateData',
772
+ new Error(
773
+ `no usable parameter provided for updateData; no updateBy, no id, no dependencyId ` +
774
+ `(${this.dataParams.dataName})`
775
+ )
776
+ ),
777
+
778
+ errorCode: `${this.ecBase}DyNTS-DS0-UD1`,
779
+ additionalContent: {
780
+ data: this.data,
781
+ filterBy: set.filterBy,
782
+ },
783
+ });
784
+ }
785
+ } catch (error) {
786
+ if (error?.errorCode?.includes('DS0-UD1')) {
787
+ throw error;
788
+ } else {
789
+ throw new DyFM_Error({
790
+ ...this._getDefaultErrorSettings('updateData', error),
791
+
792
+ errorCode: `${this.ecBase}DyNTS-DS0-UD0`,
793
+ });
794
+ }
795
+ }
796
+ }
797
+
798
+ protected ensureData(data?: T): T {
799
+ if (!data && this.data) {
800
+ data = this.data;
801
+ }
802
+
803
+ if (!data) {
804
+ throw new DyFM_Error({
805
+ ...this._getDefaultErrorSettings(
806
+ 'ensureData',
807
+ new Error(`no data to save! (${this.dataParams.dataName})`)
808
+ ),
809
+
810
+ errorCode: `${this.ecBase}DyNTS-DS0-ED0`,
811
+ });
812
+ }
813
+
814
+ return data;
815
+ }
816
+
817
+ async patchData(data?: Partial<T>): Promise<T> {
818
+ try {
819
+ if (!data._id) {
820
+ throw new DyFM_Error({
821
+ ...this._getDefaultErrorSettings(
822
+ 'patchData',
823
+ new Error(`no ID to patch data! (${this.dataParams.dataName})`)
824
+ ),
825
+ });
826
+ }
827
+
828
+ const dataExists = await this.getDataById(data._id, true);
829
+
830
+ if (!dataExists) {
831
+ throw new DyFM_Error({
832
+ ...this._getDefaultErrorSettings(
833
+ 'patchData',
834
+ new Error(`data not found! (${this.dataParams.dataName})`)
835
+ ),
836
+ });
837
+ }
838
+
839
+ for (const key in this.depSettings) {
840
+ if (data[key] && data[key] !== dataExists[key]) {
841
+ throw new DyFM_Error({
842
+ ...this._getDefaultErrorSettings(
843
+ 'patchData',
844
+ new Error(`Cannot patch data: dependency data mismatch! (${this.dataParams.dataName})`)
845
+ ),
846
+ });
847
+ }
848
+ }
849
+
850
+ DyFM_Object.cleanAssign(dataExists, data);
851
+
852
+ await this.validateForSave(dataExists);
853
+
854
+ return await this.dataDBService.modifyData(dataExists, this.issuer);
855
+ } catch (error) {
856
+ throw new DyFM_Error({
857
+ ...this._getDefaultErrorSettings('patchData', error),
858
+
859
+ errorCode: `${this.ecBase}DyNTS-DS0-PD0`,
860
+ });
861
+ }
862
+ }
863
+
864
+ /**
865
+ * modifies data if the data have ID and already exists in the DB,
866
+ * creates new if the ID is not present or cant find in DB,
867
+ * and if dependency data setted up, will check before creation,
868
+ *
869
+ * @warning
870
+ * but the proper way to update data, if you use update method instead,
871
+ * this way, you can avoid data override errors
872
+ * (when you simultaneously trying to change the same data's
873
+ * different values from different flows)
874
+ */
875
+ async saveData(data?: T, dontSetToService?: boolean, dontUpdateModified?: boolean): Promise<T> {
876
+ try {
877
+ data = this.ensureData(data);
878
+
879
+ await this.validateForSave(data);
880
+
881
+ if (!data._id) {
882
+ if (
883
+ this.depSettings.length &&
884
+ this.depSettings.some((depSetting) => !data[depSetting.key])
885
+ ) {
886
+ throw new DyFM_Error({
887
+ ...this._getDefaultErrorSettings(
888
+ 'saveData',
889
+ new Error(
890
+ `saveData was unsuccessful: dependency data id missing from data ` +
891
+ `(key: ${this.depSettings.map((depSetting) => depSetting.key).join(', ')}, ` +
892
+ `${this.dataParams.dataName})`
893
+ )
894
+ ),
895
+
896
+ errorCode: `${this.ecBase}DyNTS-DS0-SD1`,
897
+ additionalContent: {
898
+ data: data,
899
+ },
900
+ });
901
+ }
902
+
903
+ // if ID of dependencyID is not present, data not exists, create new data
904
+ data = await this.dataDBService.createData(data, this.issuer);
905
+
906
+ if (!dontSetToService) {
907
+ this.data = data;
908
+ }
909
+
910
+ return data;
911
+ }
912
+
913
+ let dataExists: T;
914
+
915
+ // check if data already exists with the specific ID
916
+ if (data._id) {
917
+ dataExists = await this.getDataById(data._id, true);
918
+
919
+ if (!dataExists) {
920
+ throw new DyFM_Error({
921
+ ...this._getDefaultErrorSettings(
922
+ 'saveData',
923
+ new Error(
924
+ `saveData was unsuccessful: provided ID not exists ` +
925
+ `(id: "${data._id}", ${this.dataParams.dataName})`
926
+ )
927
+ ),
928
+
929
+ errorCode: `${this.ecBase}DyNTS-DS0-SD2`,
930
+ additionalContent: {
931
+ data: data,
932
+ },
933
+ });
934
+ }
935
+
936
+ // if data exists do modify
937
+ data = await this.dataDBService.modifyData(data, this.issuer, dontUpdateModified);
938
+
939
+ if (!dontSetToService) {
940
+ this.data = data;
941
+ }
942
+
943
+ return data;
944
+ }
945
+
946
+ if (this.depSettings.length) {
947
+ if (this.depSettings.some((depSetting) => !data[depSetting.key])) {
948
+ throw new DyFM_Error({
949
+ ...this._getDefaultErrorSettings(
950
+ 'saveData',
951
+ new Error(
952
+ `saveData was unsuccessful: dependency data id missing from data ` +
953
+ `(key: ${this.depSettings.map((depSetting) => depSetting.key).join(', ')}, ` +
954
+ `${this.dataParams.dataName})`
955
+ )
956
+ ),
957
+
958
+ errorCode: `${this.ecBase}DyNTS-DS0-SD3`,
959
+ additionalContent: {
960
+ data: data,
961
+ },
962
+ });
963
+ }
964
+
965
+ if (this.depSettings.some((depSetting) => depSetting.keyIsRequired)) {
966
+ await DyFM_Array.asyncForEach(
967
+ this.depSettings,
968
+ async (depSetting) => {
969
+ if (depSetting.keyIsRequired) {
970
+ if (!data[depSetting.key]) {
971
+ throw new DyFM_Error({
972
+ ...this._getDefaultErrorSettings(
973
+ 'saveData',
974
+ new Error(
975
+ `saveData was unsuccessful: dependency data id missing from data ` +
976
+ `(key: ${depSetting.key}, ${this.dataParams.dataName})`
977
+ )
978
+ ),
979
+
980
+ errorCode: `${this.ecBase}DyNTS-DS0-SD4`,
981
+ });
982
+ }
983
+
984
+ const dependencyExists = await this.getDependencyDataDBService(depSetting.dbServiceKey)
985
+ .getDataById(data[depSetting.key])
986
+ .catch();
987
+
988
+ if (!dependencyExists) {
989
+ throw new DyFM_Error({
990
+ ...this._getDefaultErrorSettings(
991
+ 'saveData',
992
+ new Error(
993
+ `saveData was unsuccessful: dependency data not exists ` +
994
+ `(key: ${depSetting.key}, id: "${data[depSetting.key]}", ` +
995
+ `${this.dataParams.dataName})`
996
+ )
997
+ ),
998
+
999
+ errorCode: `${this.ecBase}DyNTS-DS0-SD4`,
1000
+ });
1001
+ }
1002
+ }
1003
+ }
1004
+ );
1005
+ }
1006
+
1007
+ /* the db is handling the unique key, so no need to check it here
1008
+ if (this.depKeyIsUnique) {
1009
+ dataExists = await this.getDataByDependencyId(data[this.depKeys], true);
1010
+
1011
+ if (dataExists) {
1012
+ // if data exists do modify
1013
+ data = await this.dataDBService.modifyData(data, this.issuer, dontUpdateModified);
1014
+
1015
+ if (!dontSetToService) {
1016
+ this.data = data;
1017
+ }
1018
+
1019
+ return data;
1020
+
1021
+ }
1022
+ } */
1023
+ }
1024
+
1025
+ // if data not exists create new data
1026
+ data = await this.dataDBService.createData(data, this.issuer);
1027
+
1028
+ if (!data._id) {
1029
+ throw new DyFM_Error({
1030
+ ...this._getDefaultErrorSettings(
1031
+ 'saveData',
1032
+ new Error(
1033
+ `saveData was unsuccessful: data creation failed ` +
1034
+ `(${this.dataParams.dataName})`
1035
+ )
1036
+ ),
1037
+
1038
+ errorCode: `${this.ecBase}DyNTS-DS0-SD5`,
1039
+ additionalContent: {
1040
+ data: data,
1041
+ },
1042
+ });
1043
+ }
1044
+
1045
+ if (!dontSetToService) {
1046
+ this.data = data;
1047
+ }
1048
+
1049
+ return data;
1050
+
1051
+ } catch (error) {
1052
+ if ([
1053
+ `${this.ecBase}DyNTS-DS0-SD1`,
1054
+ `${this.ecBase}DyNTS-DS0-SD2`,
1055
+ `${this.ecBase}DyNTS-DS0-SD3`,
1056
+ `${this.ecBase}DyNTS-DS0-SD4`,
1057
+ `${this.ecBase}DyNTS-DS0-SD5`,
1058
+ ].includes(error?.errorCode)) {
1059
+ throw error;
1060
+
1061
+ } else {
1062
+ throw new DyFM_Error({
1063
+ ...this._getDefaultErrorSettings('saveData', error),
1064
+
1065
+ errorCode: `${this.ecBase}DyNTS-DS0-SD0`,
1066
+ });
1067
+ }
1068
+ }
1069
+ }
1070
+
1071
+ /**
1072
+ * markes data as deleted, if can be archived, will be archived as well
1073
+ * if absolute is true, permanently deletes data from database by data._id
1074
+ */
1075
+ async deleteData(id?: string, absolute?: boolean): Promise<void> {
1076
+ try {
1077
+ if (!id && this.data._id) {
1078
+ id = this.data._id;
1079
+ }
1080
+
1081
+ if (!id) {
1082
+ throw new DyFM_Error({
1083
+ ...this._getDefaultErrorSettings(
1084
+ 'deleteData',
1085
+ new Error(`deleteData failed, ID is missing! (${this.dataParams.dataName})`)
1086
+ ),
1087
+
1088
+ errorCode: `${this.ecBase}DyNTS-DS0-DD1`,
1089
+ });
1090
+ }
1091
+
1092
+ this.data = await this.getDataById(id, true);
1093
+
1094
+ if (!this.data) {
1095
+ throw new DyFM_Error({
1096
+ ...this._getDefaultErrorSettings(
1097
+ 'deleteData',
1098
+ new Error(`deleteData failed, data not found! (${this.dataParams.dataName})`)
1099
+ ),
1100
+
1101
+ errorCode: `${this.ecBase}DyNTS-DS0-DD2`,
1102
+ });
1103
+ }
1104
+
1105
+ if (this.haveArchiveDataService) {
1106
+ const archive_DS: DyNTS_DataService<T> = this.getArchiveDataService();
1107
+
1108
+ await this.dataDBService.trueDeleteDataById(id);
1109
+
1110
+ this.data._deleted = new Date();
1111
+ this.data._deletedBy = this.issuer;
1112
+ this.data._originalId = this.data._id;
1113
+ this.data._archived = new Date();
1114
+ delete this.data._id;
1115
+
1116
+ await archive_DS.saveData(this.data, true);
1117
+ } else if (absolute) {
1118
+ await this.dataDBService.trueDeleteDataById(id);
1119
+
1120
+ delete this.data._id;
1121
+ } else {
1122
+ await this.dataDBService.markDeletedById(id, this.issuer);
1123
+ }
1124
+ } catch (error) {
1125
+ if (error?.errorCode?.includes('DS0-DD1')) {
1126
+ throw error;
1127
+ } else {
1128
+ throw new DyFM_Error({
1129
+ ...this._getDefaultErrorSettings('deleteData', error),
1130
+
1131
+ errorCode: `${this.ecBase}DyNTS-DS0-DD0`,
1132
+ });
1133
+ }
1134
+ }
1135
+ }
1136
+
1137
+ async deleteAllData(absolute?: boolean): Promise<void> {
1138
+ try {
1139
+ this.dataList = await this.getAll(true);
1140
+
1141
+ if (this.haveArchiveDataService) {
1142
+ const archive_DS: DyNTS_DataService<T> = this.getArchiveDataService();
1143
+
1144
+ await this.dataDBService.trueDeleteAllData();
1145
+
1146
+ await DyFM_Array.asyncForEachAllAtOnce(
1147
+ this.dataList,
1148
+ async (data: T) => {
1149
+ data._deleted = new Date();
1150
+ data._deletedBy = this.issuer;
1151
+ data._originalId = data._id;
1152
+ data._archived = new Date();
1153
+ delete data._id;
1154
+
1155
+ await archive_DS.saveData(data, true);
1156
+ }
1157
+ );
1158
+ } else if (absolute) {
1159
+ await this.dataDBService.trueDeleteAllData();
1160
+
1161
+ this.dataList.forEach((data: T) => {
1162
+ delete data._id;
1163
+ });
1164
+ } else {
1165
+ await DyFM_Array.asyncForEachAllAtOnce(
1166
+ this.dataList,
1167
+ async (data: T) => {
1168
+ await this.dataDBService.markDeletedById(data._id, this.issuer);
1169
+ }
1170
+ );
1171
+ }
1172
+ } catch (error) {
1173
+ throw new DyFM_Error({
1174
+ ...this._getDefaultErrorSettings('deleteAllData', error),
1175
+
1176
+ errorCode: `${this.ecBase}DyNTS-DS0-DAD0`,
1177
+ });
1178
+ }
1179
+ }
1180
+
1181
+ restoreDeletedData(id?: string): Promise<void> {
1182
+ try {
1183
+ if (!id && this.data._id) {
1184
+ id = this.data._id;
1185
+ }
1186
+
1187
+ if (!id) {
1188
+ throw new DyFM_Error({
1189
+ ...this._getDefaultErrorSettings(
1190
+ 'restoreDeletedData',
1191
+ new Error(`restoreDeletedData failed, ID is missing! (${this.dataParams.dataName})`)
1192
+ ),
1193
+
1194
+ errorCode: `${this.ecBase}DyNTS-DS0-RD1`,
1195
+ });
1196
+ }
1197
+
1198
+ let archive_DS;
1199
+
1200
+ try {
1201
+ archive_DS = DyNTS_GlobalService.getDBServiceByKey(
1202
+ DyNTS_getArchivedDBName(this.dataParams.dataName),
1203
+ );
1204
+ } catch (error) {}
1205
+
1206
+ if (archive_DS) {
1207
+ throw new DyFM_Error({
1208
+ ...this._getDefaultErrorSettings(
1209
+ 'restoreDeletedData',
1210
+ new Error(
1211
+ `restoreDeletedData failed, archive service is setted up, ` +
1212
+ `archived restoration not implemented yet! ` +
1213
+ `(request implementation, and restore by yourself)`
1214
+ )
1215
+ ),
1216
+
1217
+ errorCode: `${this.ecBase}DyNTS-DS0-RD2`,
1218
+ });
1219
+ }
1220
+
1221
+ return this.dataDBService.restoreDeletedById(id, this.issuer);
1222
+ } catch (error) {
1223
+ if (error?.errorCode?.includes('DS0-RD1')) {
1224
+ throw error;
1225
+ } else {
1226
+ throw new DyFM_Error({
1227
+ ...this._getDefaultErrorSettings('restoreDeletedData', error),
1228
+
1229
+ errorCode: `${this.ecBase}DyNTS-DS0-RD0`,
1230
+ });
1231
+ }
1232
+ }
1233
+ }
1234
+
1235
+ /**
1236
+ * validation of data, for modify and create, by the ModelParams
1237
+ */
1238
+ async validateForSave(data?: T): Promise<void> {
1239
+ try {
1240
+ data = this.ensureData(data);
1241
+
1242
+ if (!data) {
1243
+ throw new DyFM_Error({
1244
+ ...this._getDefaultErrorSettings('validateForSave', new Error('No data to validate! (validateForSave)')),
1245
+ errorCode: `${this.ecBase}DyNTS-DS0-VBP0`,
1246
+ });
1247
+ }
1248
+
1249
+ await this.validateDataByPropertyParams(data, this.dataParams.properties);
1250
+ } catch (error) {
1251
+ if (error?.errorCode?.includes?.('DyNTS-DS0-VBP') || error?.errorCode?.includes?.('DyNTS-DS0-VP')) {
1252
+ throw error;
1253
+ } else {
1254
+ throw new DyFM_Error({
1255
+ ...this._getDefaultErrorSettings('validateForSave', error),
1256
+
1257
+ status: 522,
1258
+ errorCode: `${this.ecBase}DyNTS-DS0-VD0`,
1259
+ });
1260
+ }
1261
+ }
1262
+ }
1263
+
1264
+ private validateDataByPropertyParams(
1265
+ data: T,
1266
+ properties: DyFM_DataProperties<T>,
1267
+ isSubObjectOf?: DyFM_DataProperty_Params<any, T>
1268
+ ): void {
1269
+ try {
1270
+ if (!data) {
1271
+ if (isSubObjectOf?.required) {
1272
+ throw new DyFM_Error({
1273
+ ...this._getDefaultErrorSettings(
1274
+ 'validateDataByPropertyParams',
1275
+ new Error(
1276
+ `validateData failed, data is required! ` +
1277
+ `(${isSubObjectOf?.key ? `${isSubObjectOf.key}.` : ''}${this.dataParams.dataName})` +
1278
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1279
+ )
1280
+ ),
1281
+ errorCode: `${this.ecBase}DyNTS-DS0-VDP1`,
1282
+ });
1283
+ } else {
1284
+ return;
1285
+ }
1286
+ } else if (isSubObjectOf?.forbidden) {
1287
+ throw new DyFM_Error({
1288
+ ...this._getDefaultErrorSettings(
1289
+ 'validateDataByPropertyParams',
1290
+ new Error(
1291
+ `validateData failed, data is forbidden! ` +
1292
+ `(${isSubObjectOf?.key ? `${isSubObjectOf.key}.` : ''}${this.dataParams.dataName})` +
1293
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1294
+ )
1295
+ ),
1296
+ errorCode: `${this.ecBase}DyNTS-DS0-VDP2`,
1297
+ additionalContent: {
1298
+ data: data,
1299
+ },
1300
+ });
1301
+ }
1302
+
1303
+ const validationErrors: DyFM_Error[] = [];
1304
+
1305
+ Object.values(properties).forEach(
1306
+ (propertyParams: DyFM_DataProperty_Params<any, T>) => {
1307
+ try {
1308
+ this.validateProperty(data, data[propertyParams.key], propertyParams, isSubObjectOf);
1309
+ } catch (error) {
1310
+ validationErrors.push(error);
1311
+ }
1312
+ }
1313
+ );
1314
+
1315
+ if (validationErrors.length > 0) {
1316
+ if (validationErrors.length === 1) {
1317
+ throw validationErrors[0];
1318
+ } else {
1319
+ const validationErrorsSet: DyFM_ValidationErrors = {};
1320
+ validationErrors.forEach((error: DyFM_Error) => {
1321
+ Object.keys(error.validationErrors).forEach((key: string) => {
1322
+ validationErrors[key] = error.validationErrors[key];
1323
+ });
1324
+ });
1325
+
1326
+ throw new DyFM_Error({
1327
+ ...this._getDefaultErrorSettings(
1328
+ 'validateByPropertyParams',
1329
+ new Error(
1330
+ `Validation failed! (multiple validation errors for ${this.dataParams.dataName})` +
1331
+ validationErrors.map((error: DyFM_Error) => `\n ${DyFM_Error.getAnyMessage(error)}`).join('')
1332
+ )
1333
+ ),
1334
+ errorCode: `${this.ecBase}DyNTS-DS0-VDP3`,
1335
+ errors: validationErrors,
1336
+ validationErrors: validationErrorsSet,
1337
+ });
1338
+ }
1339
+ }
1340
+ } catch (error) {
1341
+ if (error?.errorCode?.includes?.('DyNTS-DS0-VDP')) {
1342
+ throw error;
1343
+ } else {
1344
+ throw new DyFM_Error({
1345
+ ...this._getDefaultErrorSettings('validateByPropertyParams', error),
1346
+
1347
+ status: 522,
1348
+ errorCode: `${this.ecBase}DyNTS-DS0-VDP0`,
1349
+ });
1350
+ }
1351
+ }
1352
+ }
1353
+
1354
+ private getPropertyKey(
1355
+ propertyParams: DyFM_DataProperty_Params<any, T>,
1356
+ isSubObjectOf?: DyFM_DataProperty_Params<any, T>
1357
+ ): string {
1358
+ return (isSubObjectOf && isSubObjectOf.key !== propertyParams.key) ?
1359
+ `${isSubObjectOf.key}.${propertyParams.key}` : propertyParams.key;
1360
+ }
1361
+
1362
+ private validateProperty(
1363
+ data: T,
1364
+ propertyValue: any,
1365
+ propertyParams: DyFM_DataProperty_Params<any, T>,
1366
+ isSubObjectOf?: DyFM_DataProperty_Params<any, T>
1367
+ ): void {
1368
+ // basic required validations
1369
+ if (
1370
+ propertyParams.required && (
1371
+ propertyValue === null ||
1372
+ propertyValue === undefined
1373
+ )
1374
+ ) {
1375
+ throw new DyFM_Error({
1376
+ ...this._getDefaultErrorSettings(
1377
+ 'validateProperty',
1378
+ new Error(
1379
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is missing! ` +
1380
+ `(required in "${this.dataParams.dataName}" dataParams) ` +
1381
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1382
+ )
1383
+ ),
1384
+
1385
+ status: 522,
1386
+ errorCode: `${this.ecBase}DyNTS-DS0-VP1`,
1387
+ userMessage: this.defaultValidationErrorUserMsg,
1388
+ validationErrors: {
1389
+ [propertyParams.key]: {
1390
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is missing! ` +
1391
+ `(required in "${this.dataParams.dataName}" dataParams) ` +
1392
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1393
+ code: `${this.ecBase}DyNTS-DS0-VP1`,
1394
+ userMessage: 'The property is required and must be a valid value!',
1395
+ },
1396
+ },
1397
+ __localStack: this.dataParams.stackLocation,
1398
+ additionalContent: {
1399
+ data: data,
1400
+ },
1401
+ });
1402
+ } else if (
1403
+ propertyParams.forbidden &&
1404
+ propertyValue !== undefined
1405
+ ) {
1406
+ throw new DyFM_Error({
1407
+ ...this._getDefaultErrorSettings(
1408
+ 'validateProperty',
1409
+ new Error(
1410
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is forbidden! ` +
1411
+ `(${this.dataParams.dataName}) ` +
1412
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1413
+ )
1414
+ ),
1415
+
1416
+ status: 522,
1417
+ errorCode: `${this.ecBase}DyNTS-DS0-VP2`,
1418
+ userMessage: this.defaultValidationErrorUserMsg,
1419
+ validationErrors: {
1420
+ [propertyParams.key]: {
1421
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is forbidden! ` +
1422
+ `(${this.dataParams.dataName}) ` +
1423
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1424
+ code: `${this.ecBase}DyNTS-DS0-VP2`,
1425
+ userMessage: 'The property modification is forbidden!',
1426
+ },
1427
+ },
1428
+ __localStack: this.dataParams.stackLocation,
1429
+ additionalContent: {
1430
+ data: data,
1431
+ },
1432
+ });
1433
+ }
1434
+
1435
+ // specific Date validation
1436
+ switch (propertyParams.type) {
1437
+ case DyFM_BasicProperty_Type.date:
1438
+ const date = new Date(propertyValue);
1439
+ if (
1440
+ propertyParams.required && (
1441
+ !(date instanceof Date) ||
1442
+ date.toString() === 'Invalid Date' ||
1443
+ isNaN(date.getTime()) ||
1444
+ date.toString() === new Date(0).toString()
1445
+ )
1446
+ ) {
1447
+ throw new DyFM_Error({
1448
+ ...this._getDefaultErrorSettings(
1449
+ 'validateProperty',
1450
+ new Error(
1451
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid date! ` +
1452
+ `(${this.dataParams.dataName})` +
1453
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1454
+ )
1455
+ ),
1456
+
1457
+ status: 522,
1458
+ errorCode: `${this.ecBase}DyNTS-DS0-VP3`,
1459
+ userMessage: this.defaultValidationErrorUserMsg,
1460
+ validationErrors: {
1461
+ [propertyParams.key]: {
1462
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid date! ` +
1463
+ `(${this.dataParams.dataName})` +
1464
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1465
+ code: `${this.ecBase}DyNTS-DS0-VP3`,
1466
+ userMessage: 'The property is required and must be a valid date!',
1467
+ },
1468
+ },
1469
+ __localStack: this.dataParams.stackLocation,
1470
+ additionalContent: {
1471
+ data: data,
1472
+ },
1473
+ });
1474
+ }
1475
+ break;
1476
+
1477
+ case DyFM_BasicProperty_Type.number:
1478
+ if (propertyParams.required && isNaN(propertyValue)) {
1479
+ throw new DyFM_Error({
1480
+ ...this._getDefaultErrorSettings(
1481
+ 'validateProperty',
1482
+ new Error(
1483
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid number! ` +
1484
+ `(${this.dataParams.dataName})` +
1485
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1486
+ )
1487
+ ),
1488
+
1489
+ status: 522,
1490
+ errorCode: `${this.ecBase}DyNTS-DS0-VP4`,
1491
+ userMessage: this.defaultValidationErrorUserMsg,
1492
+ validationErrors: {
1493
+ [propertyParams.key]: {
1494
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid number! ` +
1495
+ `(${this.dataParams.dataName})` +
1496
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1497
+ code: `${this.ecBase}DyNTS-DS0-VP4`,
1498
+ userMessage: 'The property is required and must be a valid number!',
1499
+ },
1500
+ },
1501
+ __localStack: this.dataParams.stackLocation,
1502
+ additionalContent: {
1503
+ data: data,
1504
+ },
1505
+ });
1506
+ }
1507
+ break;
1508
+
1509
+ case DyFM_BasicProperty_Type.string:
1510
+ if (propertyParams.required && typeof propertyValue !== 'string') {
1511
+ throw new DyFM_Error({
1512
+ ...this._getDefaultErrorSettings(
1513
+ 'validateProperty',
1514
+ new Error(
1515
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid string! ` +
1516
+ `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1517
+ `(${this.dataParams.dataName})` +
1518
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1519
+ )
1520
+ ),
1521
+
1522
+ status: 522,
1523
+ errorCode: `${this.ecBase}DyNTS-DS0-VP5`,
1524
+ userMessage: this.defaultValidationErrorUserMsg,
1525
+ validationErrors: {
1526
+ [propertyParams.key]: {
1527
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid string! ` +
1528
+ `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1529
+ `(${this.dataParams.dataName})` +
1530
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1531
+ code: `${this.ecBase}DyNTS-DS0-VP5`,
1532
+ userMessage: 'The property is required and must be a valid string!',
1533
+ },
1534
+ },
1535
+ __localStack: this.dataParams.stackLocation,
1536
+ additionalContent: {
1537
+ data: data,
1538
+ },
1539
+ });
1540
+ }
1541
+ break;
1542
+
1543
+ case DyFM_BasicProperty_Type.boolean:
1544
+ if (propertyParams.required && typeof propertyValue !== 'boolean') {
1545
+ throw new DyFM_Error({
1546
+ ...this._getDefaultErrorSettings(
1547
+ 'validateProperty',
1548
+ new Error(
1549
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid boolean! ` +
1550
+ `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1551
+ `(${this.dataParams.dataName})` +
1552
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1553
+ )
1554
+ ),
1555
+
1556
+ status: 522,
1557
+ errorCode: `${this.ecBase}DyNTS-DS0-VP6`,
1558
+ userMessage: this.defaultValidationErrorUserMsg,
1559
+ validationErrors: {
1560
+ [propertyParams.key]: {
1561
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid boolean! ` +
1562
+ `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1563
+ `(${this.dataParams.dataName})` +
1564
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1565
+ code: `${this.ecBase}DyNTS-DS0-VP6`,
1566
+ userMessage: 'The property is required and must be a valid boolean!',
1567
+ },
1568
+ },
1569
+ __localStack: this.dataParams.stackLocation,
1570
+ });
1571
+ }
1572
+ break;
1573
+
1574
+ case DyFM_BasicProperty_Type.object:
1575
+ if (propertyParams.required && typeof propertyValue !== 'object') {
1576
+ throw new DyFM_Error({
1577
+ ...this._getDefaultErrorSettings(
1578
+ 'validateProperty',
1579
+ new Error(
1580
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid object! ` +
1581
+ `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1582
+ `(${this.dataParams.dataName})` +
1583
+ (data as any)?.name ? `\n That failed data's name: ${(data as any).name}` : ''
1584
+ )
1585
+ ),
1586
+
1587
+ status: 522,
1588
+ errorCode: `${this.ecBase}DyNTS-DS0-VP7`,
1589
+ userMessage: this.defaultValidationErrorUserMsg,
1590
+ validationErrors: {
1591
+ [propertyParams.key]: {
1592
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid object! ` +
1593
+ `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1594
+ `(${this.dataParams.dataName})` +
1595
+ (data as any)?.name ? `\n That failed data's name: ${(data as any).name}` : '',
1596
+ code: `${this.ecBase}DyNTS-DS0-VP7`,
1597
+ userMessage: 'The property is required and must be an object!',
1598
+ },
1599
+ },
1600
+ __localStack: this.dataParams.stackLocation,
1601
+ });
1602
+ }
1603
+
1604
+ if (propertyParams.subObjectParams) {
1605
+ this.validateDataByPropertyParams(propertyValue, propertyParams.subObjectParams, propertyParams);
1606
+ }
1607
+ break;
1608
+
1609
+ case DyFM_BasicProperty_Type.array:
1610
+ if (propertyParams.required && (!Array.isArray(propertyValue) || !propertyValue.length)) {
1611
+ throw new DyFM_Error({
1612
+ ...this._getDefaultErrorSettings(
1613
+ 'validateProperty',
1614
+ new Error(
1615
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid array! ` +
1616
+ `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1617
+ `(${this.dataParams.dataName})` +
1618
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1619
+ )
1620
+ ),
1621
+
1622
+ status: 522,
1623
+ errorCode: `${this.ecBase}DyNTS-DS0-VP8`,
1624
+ userMessage: this.defaultValidationErrorUserMsg,
1625
+ validationErrors: {
1626
+ [propertyParams.key]: {
1627
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid array! ` +
1628
+ `(type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1629
+ `(${this.dataParams.dataName})` +
1630
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1631
+ code: `${this.ecBase}DyNTS-DS0-VP8`,
1632
+ userMessage: 'The property is required and must be a valid array!',
1633
+ },
1634
+ },
1635
+ __localStack: this.dataParams.stackLocation,
1636
+ });
1637
+ }
1638
+
1639
+ if (propertyParams.subObjectParams) {
1640
+ propertyValue.forEach((item: any): void => {
1641
+ this.validateDataByPropertyParams(item, propertyParams.subObjectParams, propertyParams);
1642
+ });
1643
+ }
1644
+ break;
1645
+
1646
+ case DyFM_BasicProperty_Type.function:
1647
+ DyFM_Log.warn(
1648
+ 'validateProperty: function type is not supported yet! ' +
1649
+ '(it will be skipped, but you still can use it in additionalValidators)'
1650
+ );
1651
+ /* if (propertyParams.required && typeof propertyValue !== 'function') {
1652
+ throw new DyFM_Error({
1653
+ ...this._getDefaultErrorSettings(
1654
+ 'validateProperty',
1655
+ new Error(
1656
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid function! ` +
1657
+ `(${this.dataParams.dataName})` +
1658
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1659
+ )
1660
+ ),
1661
+
1662
+ status: 522,
1663
+ errorCode: `${this.ecBase}VP9`,
1664
+ userMessage: this.defaultValidationErrorUserMsg,
1665
+ validationErrors: {
1666
+ [propertyParams.key]: {
1667
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid function! ` +
1668
+ `(${this.dataParams.dataName})` +
1669
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1670
+ code: `${this.ecBase}DyNTS-DS0-VP9`,
1671
+ userMessage: 'The property is required and must be a valid function!',
1672
+ },
1673
+ },
1674
+ __localStack: this.dataParams.stackLocation,
1675
+ });
1676
+ } */
1677
+ break;
1678
+
1679
+ default:
1680
+ throw new DyFM_Error({
1681
+ ...this._getDefaultErrorSettings(
1682
+ 'validateProperty',
1683
+ new Error(
1684
+ `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid property type! ` +
1685
+ `(type: ${propertyParams.type}, nonBasicType: ${propertyParams.nonBasicType}) ` +
1686
+ `(value type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1687
+ `(${this.dataParams.dataName})` +
1688
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : '')
1689
+ )
1690
+ ),
1691
+
1692
+ status: 522,
1693
+ errorCode: `${this.ecBase}DyNTS-DS0-VP10`,
1694
+ userMessage: this.defaultValidationErrorUserMsg,
1695
+ validationErrors: {
1696
+ [propertyParams.key]: {
1697
+ message: `validateProperty failed, "${this.getPropertyKey(propertyParams, isSubObjectOf)}" is not a valid property type! ` +
1698
+ `(type: ${propertyParams.type}, nonBasicType: ${propertyParams.nonBasicType}) ` +
1699
+ `(value type: ${typeof propertyValue}, value: "${propertyValue}") ` +
1700
+ `(${this.dataParams.dataName})` +
1701
+ ((data as any)?.name ? `\n The failed data's name: ${(data as any).name}` : ''),
1702
+ code: `${this.ecBase}DyNTS-DS0-VP10`,
1703
+ userMessage: 'The property is required and must be a valid property type!',
1704
+ },
1705
+ },
1706
+ __localStack: this.dataParams.stackLocation,
1707
+ });
1708
+ }
1709
+
1710
+ // call additional validators
1711
+ if (propertyParams.additionalValidators) {
1712
+ for (let j = 0; j < propertyParams.additionalValidators.length; j++) {
1713
+ propertyParams.additionalValidators[j](
1714
+ propertyValue,
1715
+ data
1716
+ );
1717
+ }
1718
+ }
1719
+ }
1720
+
1721
+ /**
1722
+ * Search the data list based on the search query
1723
+ * Sorts and filters the data list based on the search query
1724
+ *
1725
+ * NOTE: For more simple and with better performance and if you don't need sorting, use findDataList instead.
1726
+ *
1727
+ * @example
1728
+ * ```ts
1729
+ * const searchResult = await this.searchData({
1730
+ * filterBy: {
1731
+ * name: 'John Doe',
1732
+ * },
1733
+ * sortBy: [
1734
+ * { key: 'name', order: 1 },
1735
+ * ],
1736
+ * });
1737
+ * ```
1738
+ */
1739
+ async searchData(query?: DyFM_SearchQuery<T>, dataList?: T[]): Promise<DyFM_SearchResult<T>> {
1740
+ try {
1741
+ const searchResult = await this.sortAndFilterDataList(query, dataList);
1742
+
1743
+ if (query.page !== undefined && query.pageSize !== undefined) {
1744
+ const start = query.page * query.pageSize;
1745
+ const end = start + query.pageSize;
1746
+
1747
+ searchResult.results = searchResult.results.slice(start, end);
1748
+ }
1749
+
1750
+ return searchResult;
1751
+ } catch (error) {
1752
+ throw new DyFM_Error({
1753
+ ...this._getDefaultErrorSettings('searchData', error),
1754
+
1755
+ errorCode: `${this.ecBase}DyNTS-DS0-SD0`,
1756
+ });
1757
+ }
1758
+ }
1759
+
1760
+ async sortAndFilterDataList(
1761
+ query: DyFM_SearchQuery<T>,
1762
+ dataList?: T[]
1763
+ ): Promise<DyFM_SearchResult<T>> {
1764
+ try {
1765
+ if (!query.filterBy) {
1766
+ query.filterBy = {};
1767
+ }
1768
+
1769
+ if (!query.page) {
1770
+ query.page = 0;
1771
+ }
1772
+
1773
+ if (!query.pageSize) {
1774
+ DyFM_Log.warn(
1775
+ `searchData pageSize is not setted, setting to ${DyNTS_global_settings.defaultPageSize}.`
1776
+ );
1777
+ query.pageSize = DyNTS_global_settings.defaultPageSize;
1778
+ }
1779
+
1780
+ if (!query.sortBy?.length) {
1781
+ query.sortBy = [{ key: '__lastModified', order: -1 }];
1782
+ }
1783
+
1784
+ query.sortBy.reverse();
1785
+
1786
+ let preFilter: DyFM_DBFilter<T> = {};
1787
+
1788
+ Object.keys(query.filterBy).forEach((key: string) => {
1789
+ if (
1790
+ typeof query.filterBy[key] === 'object' &&
1791
+ Object.keys(query.filterBy[key]).includes('$')
1792
+ ) {
1793
+ (preFilter as any)[key] = query.filterBy[key] as DyFM_DBFilterExpressions<T[keyof T]>;
1794
+ delete query.filterBy[key];
1795
+ }
1796
+ });
1797
+
1798
+ if (!dataList) {
1799
+ if (Object.keys(preFilter).length) {
1800
+ dataList = await this.findDataList(preFilter);
1801
+ } else {
1802
+ dataList = await this.getAll();
1803
+ }
1804
+ } else {
1805
+ if (Object.keys(preFilter).length) {
1806
+ DyFM_Log.H_warn(
1807
+ 'prefilters not supported when dataList is set! (will be ignored the following keys: ' +
1808
+ Object.keys(preFilter).map((key: string) => `"${key}"`).join(', ') +
1809
+ ')'
1810
+ );
1811
+ }
1812
+ }
1813
+
1814
+ const filterKeys: string[] = Object.keys(query.filterBy);
1815
+ const filterFunctionsByKey: { [key: string]: (dataProperty) => boolean } = {};
1816
+
1817
+ const addFilterKeys: string[] = [];
1818
+ const removeFilterKeys: string[] = [];
1819
+
1820
+ filterKeys.forEach((key: string): void => {
1821
+ const filterBy = query.filterBy[key];
1822
+
1823
+ if (filterBy.isSpecialNestSearch) {
1824
+ filterBy.nestedPropertySearches.forEach((nestSearch: DyFM_NestPropertySearch<any>): void => {
1825
+ const nestKey = nestSearch.nestKeys.join('.');
1826
+ filterFunctionsByKey[nestKey] = this.getFilterFunctionForKey(
1827
+ nestKey,
1828
+ nestSearch.search,
1829
+ nestSearch.valueType
1830
+ );
1831
+ query.filterBy[nestKey] = nestSearch;
1832
+ addFilterKeys.push(nestKey);
1833
+ });
1834
+ removeFilterKeys.push(key);
1835
+ } else {
1836
+ filterFunctionsByKey[key] = this.getFilterFunctionForKey(key, filterBy);
1837
+ }
1838
+ });
1839
+
1840
+ addFilterKeys.forEach((key: string): void => {
1841
+ filterKeys.push(key);
1842
+ });
1843
+ removeFilterKeys.forEach((key: string): void => {
1844
+ filterKeys.splice(filterKeys.indexOf(key), 1);
1845
+ });
1846
+
1847
+ query.sortBy.forEach((sort: DyFM_DSSort): void => {
1848
+ dataList.sort(this.getSortFunctionForKey(sort));
1849
+ });
1850
+
1851
+ if (filterKeys.some((key: string): boolean => key.includes('.'))) {
1852
+ const keyResolvers: { [key: string]: (data: T) => any } = {};
1853
+
1854
+ filterKeys.forEach((key: string): void => {
1855
+ if (key.includes('.')) {
1856
+ if (!query.filterBy[key].nestKeys) {
1857
+ throw new DyFM_Error({
1858
+ ...this._getDefaultErrorSettings(
1859
+ 'searchData',
1860
+ new Error(
1861
+ `searchData failed, nestKeys missing from nestedPropertySearch! ` +
1862
+ `(${this.dataParams.dataName})`
1863
+ )
1864
+ ),
1865
+
1866
+ errorCode: `${this.ecBase}DyNTS-DS0-SD1`,
1867
+ });
1868
+ }
1869
+
1870
+ keyResolvers[key] = this.getKeyResolver(query.filterBy[key].nestKeys);
1871
+ } else {
1872
+ keyResolvers[key] = (data: T): any => data[key];
1873
+ }
1874
+ });
1875
+
1876
+ if (filterKeys.some((key: string): boolean => query.filterBy[key].isSpecialNestSearch)) {
1877
+ dataList = dataList.filter(
1878
+ (data: T): boolean => filterKeys.every(
1879
+ (key: string): boolean => query.filterBy[key].isSpecialNestSearch ?
1880
+ keyResolvers[key](data).some(
1881
+ (dataProperty: any): boolean => filterFunctionsByKey[key](dataProperty)
1882
+ ) :
1883
+ filterFunctionsByKey[key](keyResolvers[key](data))
1884
+ )
1885
+ );
1886
+ } else {
1887
+ dataList = dataList.filter(
1888
+ (data: T): boolean => filterKeys.every(
1889
+ (key: string): boolean => filterFunctionsByKey[key](keyResolvers[key](data))
1890
+ )
1891
+ );
1892
+ }
1893
+ } else {
1894
+ dataList = dataList.filter(
1895
+ (data: T): boolean => filterKeys.every(
1896
+ (key: string): boolean => filterFunctionsByKey[key](data[key])
1897
+ )
1898
+ );
1899
+ }
1900
+
1901
+ return {
1902
+ results: dataList,
1903
+ totalItems: dataList.length,
1904
+ };
1905
+ } catch (error) {
1906
+ throw new DyFM_Error({
1907
+ ...this._getDefaultErrorSettings('searchData', error),
1908
+
1909
+ errorCode: `${this.ecBase}DyNTS-DS0-SD0`,
1910
+ });
1911
+ }
1912
+ }
1913
+
1914
+ protected getFilterFunctionForKey<T, N>(
1915
+ key: string,
1916
+ searchValue: T | T[] | DyFM_RangeValue<T> | DyFM_SpecialSearch<N>,
1917
+ propertyType?: DyFM_BasicProperty_Type
1918
+ ): (dataProperty) => boolean {
1919
+ try {
1920
+ if (searchValue === undefined) {
1921
+ throw new DyFM_Error({
1922
+ ...this._getDefaultErrorSettings(
1923
+ 'getFilterFunctionForKey',
1924
+ new Error(
1925
+ `getFilterFunctionForKey failed, searchValue is missing for key: "${key}" ` +
1926
+ `(${this.dataParams.dataName})`
1927
+ )
1928
+ ),
1929
+
1930
+ errorCode: `${this.ecBase}DyNTS-DS0-GFF1`,
1931
+ });
1932
+ }
1933
+
1934
+ if (key.includes('.')) {
1935
+ if (!propertyType) {
1936
+ throw new DyFM_Error({
1937
+ ...this._getDefaultErrorSettings(
1938
+ 'getFilterFunctionForKey',
1939
+ new Error(
1940
+ `getFilterFunctionForKey failed, propertyType is missing for key: "${key}" ` +
1941
+ `(${this.dataParams.dataName})`
1942
+ )
1943
+ ),
1944
+
1945
+ errorCode: `${this.ecBase}DyNTS-DS0-GFF3`,
1946
+ });
1947
+ }
1948
+ } else {
1949
+ if (!this.dataParams.properties[key]) {
1950
+ throw new DyFM_Error({
1951
+ ...this._getDefaultErrorSettings(
1952
+ 'getFilterFunctionForKey',
1953
+ new Error(
1954
+ `getFilterFunctionForKey failed, key not found in dataParams: "${key}" ` +
1955
+ `(${this.dataParams.dataName})`
1956
+ )
1957
+ ),
1958
+
1959
+ errorCode: `${this.ecBase}DyNTS-DS0-GFF2`,
1960
+ });
1961
+ }
1962
+
1963
+ propertyType = this.dataParams.properties[key].type;
1964
+ }
1965
+
1966
+ switch (propertyType) {
1967
+ case DyFM_BasicProperty_Type.string:
1968
+ if (typeof searchValue !== 'string') {
1969
+ throw new DyFM_Error({
1970
+ ...this._getDefaultErrorSettings(
1971
+ 'getFilterFunctionForKey',
1972
+ new Error(
1973
+ `getFilterFunctionForKey failed, searchValue is not a string! ` +
1974
+ `(${this.dataParams.dataName})`
1975
+ )
1976
+ ),
1977
+
1978
+ errorCode: `${this.ecBase}DyNTS-DS0-GFF4`,
1979
+ });
1980
+ }
1981
+
1982
+ if (Array.isArray(searchValue)) {
1983
+ const lowerCaseSearchValueArray: string[] = (searchValue as string[]).filter(
1984
+ (searchString: string): boolean => Boolean(searchString)
1985
+ ).map(
1986
+ (searchString: string): string => searchString.toLowerCase()
1987
+ );
1988
+
1989
+ return (dataProperty: string): boolean => {
1990
+ // Convert to string if not already a string (handles numbers, etc.)
1991
+ const dataPropertyAsString: string =
1992
+ dataProperty != null ? String(dataProperty) : '';
1993
+ return lowerCaseSearchValueArray.some(
1994
+ (searchString: string): boolean =>
1995
+ dataPropertyAsString?.toLowerCase().includes(searchString)
1996
+ );
1997
+ };
1998
+ } else {
1999
+ const lowerCaseSearchValue: string = (searchValue as string)?.toLowerCase();
2000
+
2001
+ if (!lowerCaseSearchValue) {
2002
+ return (dataProperty: string): boolean => !dataProperty;
2003
+ }
2004
+
2005
+ return (dataProperty: string): boolean => {
2006
+ // Convert to string if not already a string (handles numbers, etc.)
2007
+ const dataPropertyAsString: string =
2008
+ dataProperty != null ? String(dataProperty) : '';
2009
+ return dataPropertyAsString?.toLowerCase().includes(lowerCaseSearchValue);
2010
+ };
2011
+ }
2012
+
2013
+ case DyFM_BasicProperty_Type.date:
2014
+ if ((searchValue as DyFM_RangeValue).isRange) {
2015
+ if (
2016
+ (searchValue as DyFM_RangeValue).from === undefined ||
2017
+ (searchValue as DyFM_RangeValue).from === null
2018
+ ) {
2019
+ const toAsNumber = +new Date((searchValue as DyFM_RangeValue).to);
2020
+
2021
+ return (dataProperty): boolean => +new Date(dataProperty) <= toAsNumber;
2022
+ } else if (
2023
+ (searchValue as DyFM_RangeValue).to === undefined ||
2024
+ (searchValue as DyFM_RangeValue).to === null
2025
+ ) {
2026
+ const fromAsNumber = +new Date((searchValue as DyFM_RangeValue).from);
2027
+
2028
+ return (dataProperty): boolean => +new Date(dataProperty) >= fromAsNumber;
2029
+ }
2030
+
2031
+ const rangeAsNumber = new DyFM_RangeValue<number>(
2032
+ +new Date((searchValue as DyFM_RangeValue).from),
2033
+ +new Date((searchValue as DyFM_RangeValue).to)
2034
+ );
2035
+
2036
+ return (dataProperty): boolean => DyFM_RangeValue.isInRange(
2037
+ +new Date(dataProperty), rangeAsNumber
2038
+ );
2039
+ }
2040
+
2041
+ if (Array.isArray(searchValue)) {
2042
+ const searchValueAsNumberArray: number[] = (searchValue as Date[]).map(
2043
+ (date: Date): number => +new Date(date)
2044
+ );
2045
+
2046
+ return (dataProperty): boolean => searchValueAsNumberArray.includes(+new Date(dataProperty));
2047
+ }
2048
+
2049
+ const searchValueAsNumber: number = +new Date(searchValue as Date);
2050
+
2051
+ return (dataProperty): boolean => +new Date(dataProperty) === searchValueAsNumber;
2052
+
2053
+ case DyFM_BasicProperty_Type.number:
2054
+ if ((searchValue as DyFM_RangeValue).isRange) {
2055
+ if (
2056
+ (searchValue as DyFM_RangeValue).from === undefined ||
2057
+ (searchValue as DyFM_RangeValue).from === null
2058
+ ) {
2059
+ return (dataProperty): boolean => dataProperty <= (searchValue as DyFM_RangeValue).to;
2060
+ } else if (
2061
+ (searchValue as DyFM_RangeValue).to === undefined ||
2062
+ (searchValue as DyFM_RangeValue).to === null
2063
+ ) {
2064
+ return (dataProperty): boolean => dataProperty >= (searchValue as DyFM_RangeValue).from;
2065
+ }
2066
+
2067
+ return (dataProperty): boolean => DyFM_RangeValue.isInRange(
2068
+ dataProperty, searchValue as DyFM_RangeValue<number>
2069
+ );
2070
+ }
2071
+
2072
+ if (Array.isArray(searchValue)) {
2073
+ return (dataProperty): boolean => (searchValue as number[]).includes(dataProperty);
2074
+ }
2075
+
2076
+ return (dataProperty): boolean => dataProperty === searchValue;
2077
+
2078
+ case DyFM_BasicProperty_Type.boolean:
2079
+ return (dataProperty): boolean => dataProperty === searchValue;
2080
+
2081
+ case DyFM_BasicProperty_Type.object:
2082
+ if (Array.isArray(searchValue)) {
2083
+ throw new DyFM_Error({
2084
+ ...this._getDefaultErrorSettings(
2085
+ 'getFilterFunctionForKey',
2086
+ new Error(
2087
+ `getFilterFunctionForKey failed, array search not implemented for this type ` +
2088
+ `(${this.dataParams.dataName})`
2089
+ )
2090
+ ),
2091
+
2092
+ errorCode: `${this.ecBase}DyNTS-DS0-GFF5`,
2093
+ });
2094
+ }
2095
+
2096
+ return (dataProperty): boolean => {
2097
+ const searchValueKeys: string[] = Object.keys(searchValue as object);
2098
+ const stringifiedSearchValue = {};
2099
+
2100
+ try {
2101
+ searchValueKeys.forEach(
2102
+ (key: string): void => {
2103
+ stringifiedSearchValue[key] = JSON.stringify(searchValue[key]);
2104
+ }
2105
+ );
2106
+ } catch (error) {
2107
+ throw new DyFM_Error({
2108
+ ...this._getDefaultErrorSettings(
2109
+ 'getFilterFunctionForKey',
2110
+ new Error(
2111
+ `getFilterFunctionForKey failed, object search failed! ` +
2112
+ `(${this.dataParams.dataName})`
2113
+ )
2114
+ ),
2115
+
2116
+ errorCode: `${this.ecBase}DyNTS-DS0-GFF6`,
2117
+ additionalContent: {
2118
+ searchValue: searchValue,
2119
+ },
2120
+ });
2121
+ }
2122
+
2123
+ return searchValueKeys.every(
2124
+ (key: string): boolean => {
2125
+ try {
2126
+ return stringifiedSearchValue[key] === JSON.stringify(dataProperty[key]);
2127
+ } catch (error) {
2128
+ console.error(
2129
+ 'object filter returning false, bc of an error',
2130
+ 'searchValue:', searchValue,
2131
+ 'dataProperty:', dataProperty,
2132
+ 'key:', key,
2133
+ 'error:', error
2134
+ );
2135
+ return false;
2136
+ }
2137
+ }
2138
+ );
2139
+ }
2140
+
2141
+ default:
2142
+ if ((searchValue as DyFM_RangeValue).isRange) {
2143
+ throw new DyFM_Error({
2144
+ ...this._getDefaultErrorSettings(
2145
+ 'getFilterFunctionForKey',
2146
+ new Error(
2147
+ `getFilterFunctionForKey failed, range search not implemented for this type ` +
2148
+ `(${this.dataParams.dataName})`
2149
+ )
2150
+ ),
2151
+
2152
+ errorCode: `${this.ecBase}DyNTS-DS0-GFF7`,
2153
+ });
2154
+ }
2155
+
2156
+ if (Array.isArray(searchValue)) {
2157
+ return (dataProperty): boolean => (searchValue as number[]).includes(dataProperty);
2158
+ }
2159
+
2160
+ return (dataProperty): boolean => dataProperty === searchValue;
2161
+ }
2162
+ } catch (error) {
2163
+ throw new DyFM_Error({
2164
+ ...this._getDefaultErrorSettings(
2165
+ 'getFilterFunctionForKey',
2166
+ error
2167
+ ),
2168
+
2169
+ errorCode: `${this.ecBase}DyNTS-DS0-GFF0`,
2170
+ additionalContent: {
2171
+ key: key,
2172
+ searchValue: searchValue,
2173
+ propertyType: propertyType,
2174
+ },
2175
+ });
2176
+ }
2177
+ }
2178
+
2179
+ /**
2180
+ * Get the value of a nested key.
2181
+ * @param nestKeys - The nested keys to resolve.
2182
+ * @returns The value of the nested key.
2183
+ */
2184
+ private getKeyResolver(nestKeys: string[]): (data: T) => any {
2185
+ return (data: T): any => {
2186
+ let value: any = data;
2187
+
2188
+ nestKeys.forEach((nestKey: string): void => {
2189
+ if (!value) {
2190
+ return;
2191
+ }
2192
+
2193
+ if (nestKey === '*') {
2194
+ // Wildcard: ha value egy tömb, akkor minden elemet visszaadunk
2195
+ // A következő nestKey minden elemre alkalmazva lesz
2196
+ if (Array.isArray(value)) {
2197
+ // value marad tömb, a következő nestKey minden elemre alkalmazva lesz
2198
+ // Nem változtatjuk meg a value-t, hogy a következő iterációban
2199
+ // a tömb minden elemére alkalmazva legyen a következő nestKey
2200
+ } else if (value && typeof value === 'object') {
2201
+ // Ha objektum, akkor az értékeit adja vissza tömbként
2202
+ value = Object.values(value);
2203
+ } else {
2204
+ // Egyébként undefined
2205
+ value = undefined;
2206
+ }
2207
+ } else if (Array.isArray(value)) {
2208
+ // Ha value tömb, akkor minden elemre alkalmazzuk a nestKey-t
2209
+ if (!isNaN(+nestKey)) {
2210
+ value = value[+nestKey];
2211
+ } else {
2212
+ value = value.map((element: any): any => element ? element[nestKey] : element);
2213
+ }
2214
+ } else {
2215
+ value = value[nestKey];
2216
+ }
2217
+ });
2218
+
2219
+ return value;
2220
+ };
2221
+ }
2222
+
2223
+ /**
2224
+ * Get the sort function for a key.
2225
+ * @param sortSettings - The sort settings.
2226
+ * @returns The sort function for the key.
2227
+ */
2228
+ protected getSortFunctionForKey<T>(sortSettings: DyFM_DSSort): (a: T, b: T) => number {
2229
+ let keyResolver: (data: any) => any;
2230
+ let valueType: DyFM_BasicProperty_Type;
2231
+
2232
+ if (
2233
+ !this.dataParams.properties[(sortSettings as DyFM_DSPropertySort).key] &&
2234
+ !(sortSettings as DyFM_DSNestedPropertySort).isNestedPropertySort
2235
+ ) {
2236
+ throw new DyFM_Error({
2237
+ ...this._getDefaultErrorSettings(
2238
+ 'getSortFunctionForKey',
2239
+ new Error(
2240
+ `getSortFunctionForKey failed, key not found in dataParams ` +
2241
+ `(${this.dataParams.dataName})`
2242
+ )
2243
+ ),
2244
+
2245
+ errorCode: `${this.ecBase}DyNTS-DS0-GSF1`,
2246
+ });
2247
+ }
2248
+
2249
+ if ((sortSettings as DyFM_DSNestedPropertySort).isNestedPropertySort) {
2250
+ keyResolver = this.getKeyResolver((sortSettings as DyFM_DSNestedPropertySort).nestKeys);
2251
+ valueType = (sortSettings as DyFM_DSNestedPropertySort).valueType;
2252
+ } else {
2253
+ keyResolver = (data: T): any => data[(sortSettings as DyFM_DSPropertySort).key];
2254
+ valueType = this.dataParams.properties[(sortSettings as DyFM_DSPropertySort).key].type;
2255
+ }
2256
+
2257
+ if (!valueType) {
2258
+ throw new DyFM_Error({
2259
+ ...this._getDefaultErrorSettings(
2260
+ 'getSortFunctionForKey',
2261
+ new Error(
2262
+ `getSortFunctionForKey failed, valueType is missing! ` +
2263
+ `(${this.dataParams.dataName})`
2264
+ )
2265
+ ),
2266
+
2267
+ errorCode: `${this.ecBase}DyNTS-DS0-GSF1`,
2268
+ });
2269
+ }
2270
+
2271
+ const sortValue: 1 | -1 = (
2272
+ sortSettings.order === 1 ||
2273
+ sortSettings.order === 'asc' ||
2274
+ sortSettings.order === 'ascending'
2275
+ ) ? 1 : -1;
2276
+
2277
+ switch (valueType) {
2278
+ case DyFM_BasicProperty_Type.string:
2279
+ return (a: T, b: T): number => {
2280
+ a = keyResolver(a);
2281
+ b = keyResolver(b);
2282
+
2283
+ return (a as string).localeCompare(b as string) * sortValue;
2284
+ }
2285
+
2286
+ case DyFM_BasicProperty_Type.date:
2287
+ return (a: T, b: T): number => {
2288
+ const aNum = +new Date(keyResolver(a));
2289
+ const bNum = +new Date(keyResolver(b));
2290
+
2291
+ if (aNum < bNum) {
2292
+ return -sortValue;
2293
+ } else if (aNum > bNum) {
2294
+ return sortValue;
2295
+ } else {
2296
+ return 0;
2297
+ }
2298
+ };
2299
+
2300
+ case DyFM_BasicProperty_Type.number:
2301
+ return (a: T, b: T): number => {
2302
+ a = keyResolver(a);
2303
+ b = keyResolver(b);
2304
+
2305
+ if (a < b) {
2306
+ return -sortValue;
2307
+ } else if (a > b) {
2308
+ return sortValue;
2309
+ } else {
2310
+ return 0;
2311
+ }
2312
+ };
2313
+
2314
+ case DyFM_BasicProperty_Type.boolean:
2315
+ return (a: T, b: T): number => {
2316
+ a = keyResolver(a);
2317
+ b = keyResolver(b);
2318
+
2319
+ if (a === b) {
2320
+ return 0;
2321
+ }
2322
+
2323
+ if (a) {
2324
+ return sortValue;
2325
+ }
2326
+
2327
+ return -sortValue;
2328
+ };
2329
+
2330
+ default:
2331
+ throw new DyFM_Error({
2332
+ ...this._getDefaultErrorSettings(
2333
+ 'getSortFunctionForKey',
2334
+ new Error(
2335
+ `getSortFunctionForKey failed, sorting not implemented for this type ` +
2336
+ `(${this.dataParams.dataName}, ${valueType})`
2337
+ )
2338
+ ),
2339
+
2340
+ errorCode: `${this.ecBase}DyNTS-DS0-GSF2`,
2341
+ });
2342
+ }
2343
+ }
2344
+
2345
+ /* private getSortFunctionForNestedKeys<T>(
2346
+ sortSettings: DyFM_DSNestedPropertySort
2347
+ ): (a: T, b: T) => number {
2348
+ const nestedPropertyResolver: (data) => any = this.getKeyResolver(sortSettings.nestKeys);
2349
+
2350
+ const sortValue: 1 | -1 = (
2351
+ sortSettings.order === 1 ||
2352
+ sortSettings.order === 'asc' ||
2353
+ sortSettings.order === 'ascending'
2354
+ ) ? 1 : -1;
2355
+
2356
+ switch (sortSettings.valueType) {
2357
+ case DyFM_BasicProperty_Type.string:
2358
+ return (a: T, b: T): number => {
2359
+ a = nestedPropertyResolver(a);
2360
+ b = nestedPropertyResolver(b);
2361
+
2362
+ return (a as string).localeCompare(b as string) * sortValue
2363
+ };
2364
+
2365
+ case DyFM_BasicProperty_Type.date:
2366
+ return (a: T, b: T): number => {
2367
+ a = nestedPropertyResolver(a);
2368
+ b = nestedPropertyResolver(b);
2369
+
2370
+ if (+new Date(a) < +new Date(b)) {
2371
+ return -sortValue;
2372
+ } else if (+new Date(a) > +new Date(b)) {
2373
+ return sortValue;
2374
+ } else {
2375
+ return 0;
2376
+ }
2377
+ };
2378
+
2379
+ case DyFM_BasicProperty_Type.number:
2380
+ return (a: T, b: T): number => {
2381
+ a = nestedPropertyResolver(a);
2382
+ b = nestedPropertyResolver(b);
2383
+
2384
+ if (a < b) {
2385
+ return -sortValue;
2386
+ } else if (a > b) {
2387
+ return sortValue;
2388
+ } else {
2389
+ return 0;
2390
+ }
2391
+ };
2392
+
2393
+ case DyFM_BasicProperty_Type.boolean:
2394
+ return (a: T, b: T): number => {
2395
+ a = nestedPropertyResolver(a);
2396
+ b = nestedPropertyResolver(b);
2397
+
2398
+ if (a === b) {
2399
+ return 0;
2400
+ }
2401
+
2402
+ if (a) {
2403
+ return sortValue;
2404
+ }
2405
+
2406
+ return -sortValue;
2407
+ };
2408
+
2409
+ default:
2410
+ throw new DyFM_Error({
2411
+ ...this._getDefaultErrorSettings(
2412
+ 'getSortFunctionForKey',
2413
+ new Error(
2414
+ `getSortFunctionForKey failed, sorting not implemented for this type ` +
2415
+ `(${this.dataParams.dataName}, ${this.dataParams.properties[key].type})`
2416
+ )
2417
+ ),
2418
+
2419
+ errorCode: `${this.ecBase}DyNTS-DS0-GSF2`,
2420
+ });
2421
+ }
2422
+ } */
2423
+
2424
+
2425
+ /**
2426
+ * setting up dependency dataHook by DynamoNTSDataModelParams
2427
+ */
2428
+ private lookForDependencyDataSettings(): void {
2429
+ const dependencyParams: DyFM_DataProperty_Params<any, T>[] =
2430
+ Object.values(this.dataParams.properties).filter(
2431
+ (modelParams: DyFM_DataProperty_Params<any, T>): boolean =>
2432
+ Boolean(modelParams.dependencyDataName)
2433
+ );
2434
+
2435
+ this.depSettings.push(...dependencyParams.map((dependencyParams) => ({
2436
+ key: dependencyParams.key,
2437
+ dbServiceKey: dependencyParams.dependencyDataName,
2438
+ keyIsUnique: dependencyParams.unique,
2439
+ keyIsRequired: dependencyParams.required,
2440
+ })));
2441
+
2442
+ if (this.depSettings.length === 1) {
2443
+ this.depDataDBService = DyNTS_GlobalService.getDBServiceByKey(
2444
+ this.depSettings[0].dbServiceKey
2445
+ );
2446
+ }
2447
+ }
2448
+
2449
+ /**
2450
+ *
2451
+ * @returns
2452
+ */
2453
+ getDependencyDataDBService(dBServiceKey?: string): DyNTS_DBService<any> {
2454
+ if (!dBServiceKey && !this.depDataDBService) {
2455
+ throw new DyFM_Error({
2456
+ ...this._getDefaultErrorSettings(
2457
+ 'getDependencyDataDBService',
2458
+ new Error(
2459
+ `getDependencyDataDBService was unsuccessful, service key not setted up! ` +
2460
+ `(key: ${this.depSettings.map((depSetting) => depSetting.key).join(', ')}, ` +
2461
+ `(${this.dataParams.dataName})`
2462
+ )
2463
+ ),
2464
+
2465
+ status: 500,
2466
+ errorCode: `${this.ecBase}DyNTS-DS0-GDDB0`,
2467
+ });
2468
+ }
2469
+
2470
+ if (this.depDataDBService) {
2471
+ return this.depDataDBService;
2472
+ } else {
2473
+ this.depDataDBService = DyNTS_GlobalService.getDBServiceByKey(dBServiceKey);
2474
+
2475
+ return this.depDataDBService;
2476
+ }
2477
+ }
2478
+
2479
+ getProvidedData(data: T): T {
2480
+ return data;
2481
+ }
2482
+
2483
+ getProvidedDataList(dataList: T[]): T[] {
2484
+ return dataList;
2485
+ }
2486
+
2487
+ private _getDefaultErrorSettings(
2488
+ fnName: string,
2489
+ error: DyFM_AnyError
2490
+ ): DyFM_Error_Settings {
2491
+ return {
2492
+ status: (error as DyFM_Error)?.___status ?? 500,
2493
+ message: (error as Error)?.message ??
2494
+ (error as DyFM_Error)?._message ??
2495
+ `${fnName} was UNSUCCESSFUL (NTS; ${this.dataParams.dataName})`,
2496
+ addECToUserMsg: !(error as DyFM_Error)?.__userMessage,
2497
+ userMessage: (error as DyFM_Error)?.__userMessage ?? this.defaultErrorUserMsg,
2498
+ issuer: this.issuer,
2499
+ issuerService: this.serviceName + ` (${this?.constructor?.name}-DyNTS_DataService)`,
2500
+ systemVersion: DyNTS_global_settings.systemVersion,
2501
+ error: error,
2502
+ };
2503
+ }
2504
+
2505
+ protected getDefaultErrorSettings(
2506
+ fnName: string,
2507
+ error: DyFM_AnyError,
2508
+ /** @deprecated we wont support the separate user message in the future, use the message instead */
2509
+ useMessageAsUserMessage: boolean = true,
2510
+ ): DyFM_Error_Settings {
2511
+ return {
2512
+ status: (error as DyFM_Error)?.___status ?? 500,
2513
+ message: (error as Error)?.message ??
2514
+ (error as DyFM_Error)?._message ??
2515
+ `${fnName} was UNSUCCESSFUL (${DyNTS_global_settings.systemShortCodeName})`,
2516
+ addECToUserMsg: !(error as DyFM_Error)?.__userMessage,
2517
+ userMessage: (error as DyFM_Error)?.__userMessage ??
2518
+ (useMessageAsUserMessage ? (error as Error)?.message : this.defaultErrorUserMsg),
2519
+ issuer: this.issuer,
2520
+ issuerService: this.constructor?.name,
2521
+ systemVersion: DyNTS_global_settings.systemVersion,
2522
+ error: error,
2523
+ };
2524
+ }
2525
+ }