@open-mercato/core 0.5.1-develop.2691.d8a0934b37 → 0.5.1-develop.2699.f8b50c8046

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 (414) hide show
  1. package/dist/modules/api_keys/data/entities.js +1 -1
  2. package/dist/modules/api_keys/data/entities.js.map +1 -1
  3. package/dist/modules/api_keys/services/apiKeyService.js +5 -5
  4. package/dist/modules/api_keys/services/apiKeyService.js.map +2 -2
  5. package/dist/modules/attachments/api/library/[id]/route.js +1 -1
  6. package/dist/modules/attachments/api/library/[id]/route.js.map +2 -2
  7. package/dist/modules/attachments/api/library/route.js +7 -9
  8. package/dist/modules/attachments/api/library/route.js.map +2 -2
  9. package/dist/modules/attachments/api/partitions/route.js +3 -3
  10. package/dist/modules/attachments/api/partitions/route.js.map +2 -2
  11. package/dist/modules/attachments/api/route.js +6 -5
  12. package/dist/modules/attachments/api/route.js.map +2 -2
  13. package/dist/modules/attachments/api/transfer/route.js +1 -1
  14. package/dist/modules/attachments/api/transfer/route.js.map +2 -2
  15. package/dist/modules/attachments/data/entities.js +2 -1
  16. package/dist/modules/attachments/data/entities.js.map +2 -2
  17. package/dist/modules/attachments/lib/ocrQueue.js +1 -1
  18. package/dist/modules/attachments/lib/ocrQueue.js.map +2 -2
  19. package/dist/modules/audit_logs/api/audit-logs/actions/export/route.js.map +2 -2
  20. package/dist/modules/audit_logs/api/audit-logs/actions/route.js.map +2 -2
  21. package/dist/modules/audit_logs/data/entities.js +1 -1
  22. package/dist/modules/audit_logs/data/entities.js.map +1 -1
  23. package/dist/modules/audit_logs/services/actionLogService.js +77 -70
  24. package/dist/modules/audit_logs/services/actionLogService.js.map +2 -2
  25. package/dist/modules/auth/api/roles/acl/route.js +1 -1
  26. package/dist/modules/auth/api/roles/acl/route.js.map +2 -2
  27. package/dist/modules/auth/api/users/acl/route.js +2 -2
  28. package/dist/modules/auth/api/users/acl/route.js.map +2 -2
  29. package/dist/modules/auth/api/users/resend-invite/route.js +1 -1
  30. package/dist/modules/auth/api/users/resend-invite/route.js.map +2 -2
  31. package/dist/modules/auth/cli.js +12 -6
  32. package/dist/modules/auth/cli.js.map +2 -2
  33. package/dist/modules/auth/commands/users.js +1 -1
  34. package/dist/modules/auth/commands/users.js.map +2 -2
  35. package/dist/modules/auth/data/entities.js +1 -1
  36. package/dist/modules/auth/data/entities.js.map +2 -2
  37. package/dist/modules/auth/lib/setup-app.js +3 -3
  38. package/dist/modules/auth/lib/setup-app.js.map +2 -2
  39. package/dist/modules/auth/services/authService.js +2 -2
  40. package/dist/modules/auth/services/authService.js.map +2 -2
  41. package/dist/modules/business_rules/api/rules/route.js +3 -3
  42. package/dist/modules/business_rules/api/rules/route.js.map +2 -2
  43. package/dist/modules/business_rules/api/sets/[id]/members/route.js +7 -4
  44. package/dist/modules/business_rules/api/sets/[id]/members/route.js.map +2 -2
  45. package/dist/modules/business_rules/api/sets/route.js +3 -3
  46. package/dist/modules/business_rules/api/sets/route.js.map +2 -2
  47. package/dist/modules/business_rules/cli.js +1 -1
  48. package/dist/modules/business_rules/cli.js.map +2 -2
  49. package/dist/modules/business_rules/data/entities.js +2 -9
  50. package/dist/modules/business_rules/data/entities.js.map +2 -2
  51. package/dist/modules/business_rules/lib/rule-engine.js +1 -1
  52. package/dist/modules/business_rules/lib/rule-engine.js.map +2 -2
  53. package/dist/modules/catalog/api/option-schemas/route.js +0 -1
  54. package/dist/modules/catalog/api/option-schemas/route.js.map +2 -2
  55. package/dist/modules/catalog/data/entities.js +2 -11
  56. package/dist/modules/catalog/data/entities.js.map +2 -2
  57. package/dist/modules/configs/data/entities.js +2 -1
  58. package/dist/modules/configs/data/entities.js.map +2 -2
  59. package/dist/modules/currencies/commands/fetch-configs.js +3 -3
  60. package/dist/modules/currencies/commands/fetch-configs.js.map +2 -2
  61. package/dist/modules/currencies/data/entities.js +1 -1
  62. package/dist/modules/currencies/data/entities.js.map +2 -2
  63. package/dist/modules/customer_accounts/api/signup.js +1 -1
  64. package/dist/modules/customer_accounts/api/signup.js.map +2 -2
  65. package/dist/modules/customer_accounts/data/entities.js +1 -1
  66. package/dist/modules/customer_accounts/data/entities.js.map +2 -2
  67. package/dist/modules/customer_accounts/services/customerInvitationService.js +1 -1
  68. package/dist/modules/customer_accounts/services/customerInvitationService.js.map +2 -2
  69. package/dist/modules/customer_accounts/services/customerSessionService.js +1 -1
  70. package/dist/modules/customer_accounts/services/customerSessionService.js.map +2 -2
  71. package/dist/modules/customer_accounts/services/customerTokenService.js +12 -7
  72. package/dist/modules/customer_accounts/services/customerTokenService.js.map +2 -2
  73. package/dist/modules/customers/api/interactions/conflicts/route.js +19 -17
  74. package/dist/modules/customers/api/interactions/conflicts/route.js.map +2 -2
  75. package/dist/modules/customers/api/interactions/counts/route.js +7 -6
  76. package/dist/modules/customers/api/interactions/counts/route.js.map +2 -2
  77. package/dist/modules/customers/api/interactions/route.js +28 -42
  78. package/dist/modules/customers/api/interactions/route.js.map +2 -2
  79. package/dist/modules/customers/api/utils.js +29 -24
  80. package/dist/modules/customers/api/utils.js.map +2 -2
  81. package/dist/modules/customers/cli.js +45 -40
  82. package/dist/modules/customers/cli.js.map +2 -2
  83. package/dist/modules/customers/commands/dictionaries.js +1 -1
  84. package/dist/modules/customers/commands/dictionaries.js.map +2 -2
  85. package/dist/modules/customers/commands/tags.js +1 -1
  86. package/dist/modules/customers/commands/tags.js.map +2 -2
  87. package/dist/modules/customers/data/entities.js +2 -12
  88. package/dist/modules/customers/data/entities.js.map +2 -2
  89. package/dist/modules/customers/lib/interactionProjection.js +18 -15
  90. package/dist/modules/customers/lib/interactionProjection.js.map +2 -2
  91. package/dist/modules/customers/lib/personCompanyLinkTable.js +6 -8
  92. package/dist/modules/customers/lib/personCompanyLinkTable.js.map +2 -2
  93. package/dist/modules/dashboards/api/roles/widgets/route.js +1 -1
  94. package/dist/modules/dashboards/api/roles/widgets/route.js.map +2 -2
  95. package/dist/modules/dashboards/api/users/widgets/route.js +1 -1
  96. package/dist/modules/dashboards/api/users/widgets/route.js.map +2 -2
  97. package/dist/modules/dashboards/data/entities.js +1 -1
  98. package/dist/modules/dashboards/data/entities.js.map +1 -1
  99. package/dist/modules/data_sync/api/mappings/route.js +1 -1
  100. package/dist/modules/data_sync/api/mappings/route.js.map +2 -2
  101. package/dist/modules/data_sync/data/entities.js +2 -1
  102. package/dist/modules/data_sync/data/entities.js.map +2 -2
  103. package/dist/modules/data_sync/lib/id-mapping.js +1 -1
  104. package/dist/modules/data_sync/lib/id-mapping.js.map +2 -2
  105. package/dist/modules/data_sync/lib/sync-run-service.js +1 -1
  106. package/dist/modules/data_sync/lib/sync-run-service.js.map +2 -2
  107. package/dist/modules/dictionaries/commands/factory.js +1 -1
  108. package/dist/modules/dictionaries/commands/factory.js.map +2 -2
  109. package/dist/modules/dictionaries/data/entities.js +2 -9
  110. package/dist/modules/dictionaries/data/entities.js.map +2 -2
  111. package/dist/modules/directory/commands/organizations.js +4 -4
  112. package/dist/modules/directory/commands/organizations.js.map +2 -2
  113. package/dist/modules/directory/data/entities.js +2 -1
  114. package/dist/modules/directory/data/entities.js.map +2 -2
  115. package/dist/modules/entities/api/definitions.js +2 -2
  116. package/dist/modules/entities/api/definitions.js.map +2 -2
  117. package/dist/modules/entities/api/encryption.js +2 -2
  118. package/dist/modules/entities/api/encryption.js.map +2 -2
  119. package/dist/modules/entities/api/relations/options.js +2 -2
  120. package/dist/modules/entities/api/relations/options.js.map +2 -2
  121. package/dist/modules/entities/cli.js +4 -4
  122. package/dist/modules/entities/cli.js.map +2 -2
  123. package/dist/modules/entities/data/entities.js +1 -1
  124. package/dist/modules/entities/data/entities.js.map +2 -2
  125. package/dist/modules/entities/lib/field-definitions.js +2 -2
  126. package/dist/modules/entities/lib/field-definitions.js.map +2 -2
  127. package/dist/modules/entities/lib/register.js +1 -1
  128. package/dist/modules/entities/lib/register.js.map +2 -2
  129. package/dist/modules/feature_toggles/data/entities.js +2 -9
  130. package/dist/modules/feature_toggles/data/entities.js.map +2 -2
  131. package/dist/modules/inbox_ops/api/proposals/counts/route.js +3 -6
  132. package/dist/modules/inbox_ops/api/proposals/counts/route.js.map +2 -2
  133. package/dist/modules/inbox_ops/data/entities.js +2 -8
  134. package/dist/modules/inbox_ops/data/entities.js.map +2 -2
  135. package/dist/modules/inbox_ops/lib/messagesIntegration.js +6 -6
  136. package/dist/modules/inbox_ops/lib/messagesIntegration.js.map +2 -2
  137. package/dist/modules/integrations/data/entities.js +2 -1
  138. package/dist/modules/integrations/data/entities.js.map +2 -2
  139. package/dist/modules/integrations/lib/credentials-service.js +1 -1
  140. package/dist/modules/integrations/lib/credentials-service.js.map +2 -2
  141. package/dist/modules/integrations/lib/log-service.js +1 -1
  142. package/dist/modules/integrations/lib/log-service.js.map +2 -2
  143. package/dist/modules/integrations/lib/state-service.js +1 -1
  144. package/dist/modules/integrations/lib/state-service.js.map +2 -2
  145. package/dist/modules/messages/api/route.js +90 -93
  146. package/dist/modules/messages/api/route.js.map +2 -2
  147. package/dist/modules/messages/api/unread-count/route.js +8 -7
  148. package/dist/modules/messages/api/unread-count/route.js.map +2 -2
  149. package/dist/modules/messages/commands/confirmations.js +1 -1
  150. package/dist/modules/messages/commands/confirmations.js.map +2 -2
  151. package/dist/modules/messages/commands/messages.js +3 -3
  152. package/dist/modules/messages/commands/messages.js.map +2 -2
  153. package/dist/modules/messages/data/entities.js +2 -1
  154. package/dist/modules/messages/data/entities.js.map +2 -2
  155. package/dist/modules/messages/lib/email-sender.js +1 -1
  156. package/dist/modules/messages/lib/email-sender.js.map +2 -2
  157. package/dist/modules/messages/lib/searchLookup.js +8 -8
  158. package/dist/modules/messages/lib/searchLookup.js.map +2 -2
  159. package/dist/modules/messages/lib/tokenConsumption.js +9 -4
  160. package/dist/modules/messages/lib/tokenConsumption.js.map +2 -2
  161. package/dist/modules/notifications/data/entities.js +2 -1
  162. package/dist/modules/notifications/data/entities.js.map +2 -2
  163. package/dist/modules/notifications/lib/notificationRecipients.js +15 -5
  164. package/dist/modules/notifications/lib/notificationRecipients.js.map +2 -2
  165. package/dist/modules/notifications/lib/notificationService.js +39 -34
  166. package/dist/modules/notifications/lib/notificationService.js.map +2 -2
  167. package/dist/modules/notifications/workers/create-notification.worker.js +14 -13
  168. package/dist/modules/notifications/workers/create-notification.worker.js.map +2 -2
  169. package/dist/modules/payment_gateways/api/transactions/route.js +2 -2
  170. package/dist/modules/payment_gateways/api/transactions/route.js.map +2 -2
  171. package/dist/modules/payment_gateways/data/entities.js +2 -1
  172. package/dist/modules/payment_gateways/data/entities.js.map +2 -2
  173. package/dist/modules/payment_gateways/lib/gateway-service.js +1 -1
  174. package/dist/modules/payment_gateways/lib/gateway-service.js.map +2 -2
  175. package/dist/modules/payment_gateways/lib/webhook-utils.js +2 -2
  176. package/dist/modules/payment_gateways/lib/webhook-utils.js.map +2 -2
  177. package/dist/modules/perspectives/data/entities.js +1 -1
  178. package/dist/modules/perspectives/data/entities.js.map +2 -2
  179. package/dist/modules/planner/data/entities.js +1 -1
  180. package/dist/modules/planner/data/entities.js.map +2 -2
  181. package/dist/modules/progress/data/entities.js +2 -1
  182. package/dist/modules/progress/data/entities.js.map +2 -2
  183. package/dist/modules/progress/lib/progressServiceImpl.js +1 -1
  184. package/dist/modules/progress/lib/progressServiceImpl.js.map +2 -2
  185. package/dist/modules/query_index/api/status.js +66 -57
  186. package/dist/modules/query_index/api/status.js.map +2 -2
  187. package/dist/modules/query_index/cli.js +39 -24
  188. package/dist/modules/query_index/cli.js.map +2 -2
  189. package/dist/modules/query_index/data/entities.js +1 -1
  190. package/dist/modules/query_index/data/entities.js.map +2 -2
  191. package/dist/modules/query_index/di.js +25 -13
  192. package/dist/modules/query_index/di.js.map +2 -2
  193. package/dist/modules/query_index/lib/batch.js +31 -33
  194. package/dist/modules/query_index/lib/batch.js.map +2 -2
  195. package/dist/modules/query_index/lib/coverage.js +63 -50
  196. package/dist/modules/query_index/lib/coverage.js.map +2 -2
  197. package/dist/modules/query_index/lib/engine.js +592 -588
  198. package/dist/modules/query_index/lib/engine.js.map +2 -2
  199. package/dist/modules/query_index/lib/indexer.js +74 -47
  200. package/dist/modules/query_index/lib/indexer.js.map +2 -2
  201. package/dist/modules/query_index/lib/jobs.js +37 -24
  202. package/dist/modules/query_index/lib/jobs.js.map +2 -2
  203. package/dist/modules/query_index/lib/purge.js +19 -11
  204. package/dist/modules/query_index/lib/purge.js.map +2 -2
  205. package/dist/modules/query_index/lib/reindexer.js +47 -44
  206. package/dist/modules/query_index/lib/reindexer.js.map +2 -2
  207. package/dist/modules/query_index/lib/search-tokens.js +47 -25
  208. package/dist/modules/query_index/lib/search-tokens.js.map +2 -2
  209. package/dist/modules/query_index/lib/stale.js +14 -12
  210. package/dist/modules/query_index/lib/stale.js.map +2 -2
  211. package/dist/modules/query_index/lib/subscriber-scope.js +2 -2
  212. package/dist/modules/query_index/lib/subscriber-scope.js.map +2 -2
  213. package/dist/modules/query_index/subscribers/delete_one.js +3 -2
  214. package/dist/modules/query_index/subscribers/delete_one.js.map +2 -2
  215. package/dist/modules/resources/commands/tag-assignments.js +1 -1
  216. package/dist/modules/resources/commands/tag-assignments.js.map +2 -2
  217. package/dist/modules/resources/commands/tags.js +1 -1
  218. package/dist/modules/resources/commands/tags.js.map +2 -2
  219. package/dist/modules/resources/data/entities.js +2 -1
  220. package/dist/modules/resources/data/entities.js.map +2 -2
  221. package/dist/modules/sales/commands/documentAddresses.js +2 -2
  222. package/dist/modules/sales/commands/documentAddresses.js.map +2 -2
  223. package/dist/modules/sales/commands/notes.js.map +2 -2
  224. package/dist/modules/sales/commands/tags.js +1 -1
  225. package/dist/modules/sales/commands/tags.js.map +2 -2
  226. package/dist/modules/sales/data/enrichers.js +9 -8
  227. package/dist/modules/sales/data/enrichers.js.map +2 -2
  228. package/dist/modules/sales/data/entities.js +2 -11
  229. package/dist/modules/sales/data/entities.js.map +2 -2
  230. package/dist/modules/shipping_carriers/data/entities.js +2 -1
  231. package/dist/modules/shipping_carriers/data/entities.js.map +2 -2
  232. package/dist/modules/shipping_carriers/lib/shipping-service.js +1 -1
  233. package/dist/modules/shipping_carriers/lib/shipping-service.js.map +2 -2
  234. package/dist/modules/shipping_carriers/lib/webhook-utils.js +2 -2
  235. package/dist/modules/shipping_carriers/lib/webhook-utils.js.map +2 -2
  236. package/dist/modules/staff/data/entities.js +1 -1
  237. package/dist/modules/staff/data/entities.js.map +2 -2
  238. package/dist/modules/translations/api/[entityType]/[entityId]/route.js +3 -5
  239. package/dist/modules/translations/api/[entityType]/[entityId]/route.js.map +2 -2
  240. package/dist/modules/translations/api/context.js +2 -2
  241. package/dist/modules/translations/api/context.js.map +2 -2
  242. package/dist/modules/translations/commands/translations.js +46 -39
  243. package/dist/modules/translations/commands/translations.js.map +2 -2
  244. package/dist/modules/translations/components/TranslationManager.js +19 -10
  245. package/dist/modules/translations/components/TranslationManager.js.map +2 -2
  246. package/dist/modules/translations/data/entities.js +1 -1
  247. package/dist/modules/translations/data/entities.js.map +2 -2
  248. package/dist/modules/translations/lib/apply.js +4 -4
  249. package/dist/modules/translations/lib/apply.js.map +2 -2
  250. package/dist/modules/translations/lib/batch.js +3 -2
  251. package/dist/modules/translations/lib/batch.js.map +2 -2
  252. package/dist/modules/translations/subscribers/cleanup.js +3 -5
  253. package/dist/modules/translations/subscribers/cleanup.js.map +2 -2
  254. package/dist/modules/workflows/api/definitions/route.js +1 -1
  255. package/dist/modules/workflows/api/definitions/route.js.map +2 -2
  256. package/dist/modules/workflows/cli.js +5 -5
  257. package/dist/modules/workflows/cli.js.map +2 -2
  258. package/dist/modules/workflows/data/entities.js +2 -1
  259. package/dist/modules/workflows/data/entities.js.map +2 -2
  260. package/dist/modules/workflows/lib/event-logger.js +2 -2
  261. package/dist/modules/workflows/lib/event-logger.js.map +2 -2
  262. package/dist/modules/workflows/lib/seeds.js +16 -1
  263. package/dist/modules/workflows/lib/seeds.js.map +2 -2
  264. package/dist/modules/workflows/lib/step-handler.js +3 -3
  265. package/dist/modules/workflows/lib/step-handler.js.map +2 -2
  266. package/dist/modules/workflows/lib/task-handler.js +1 -1
  267. package/dist/modules/workflows/lib/task-handler.js.map +2 -2
  268. package/dist/modules/workflows/lib/transition-handler.js +1 -1
  269. package/dist/modules/workflows/lib/transition-handler.js.map +2 -2
  270. package/dist/modules/workflows/lib/workflow-executor.js +2 -2
  271. package/dist/modules/workflows/lib/workflow-executor.js.map +2 -2
  272. package/jest.config.cjs +4 -2
  273. package/package.json +3 -3
  274. package/src/modules/api_keys/data/entities.ts +1 -1
  275. package/src/modules/api_keys/services/apiKeyService.ts +5 -5
  276. package/src/modules/attachments/api/library/[id]/route.ts +1 -1
  277. package/src/modules/attachments/api/library/route.ts +10 -12
  278. package/src/modules/attachments/api/partitions/route.ts +3 -3
  279. package/src/modules/attachments/api/route.ts +10 -8
  280. package/src/modules/attachments/api/transfer/route.ts +1 -1
  281. package/src/modules/attachments/data/entities.ts +2 -1
  282. package/src/modules/attachments/lib/ocrQueue.ts +1 -1
  283. package/src/modules/audit_logs/api/audit-logs/actions/export/route.ts +4 -4
  284. package/src/modules/audit_logs/api/audit-logs/actions/route.ts +4 -4
  285. package/src/modules/audit_logs/data/entities.ts +1 -1
  286. package/src/modules/audit_logs/services/actionLogService.ts +96 -87
  287. package/src/modules/auth/api/roles/acl/route.ts +1 -1
  288. package/src/modules/auth/api/users/acl/route.ts +2 -2
  289. package/src/modules/auth/api/users/resend-invite/route.ts +1 -1
  290. package/src/modules/auth/cli.ts +46 -40
  291. package/src/modules/auth/commands/users.ts +1 -1
  292. package/src/modules/auth/data/entities.ts +1 -1
  293. package/src/modules/auth/lib/setup-app.ts +3 -3
  294. package/src/modules/auth/services/authService.ts +2 -2
  295. package/src/modules/business_rules/api/rules/route.ts +3 -3
  296. package/src/modules/business_rules/api/sets/[id]/members/route.ts +7 -4
  297. package/src/modules/business_rules/api/sets/route.ts +3 -3
  298. package/src/modules/business_rules/cli.ts +1 -1
  299. package/src/modules/business_rules/data/entities.ts +2 -9
  300. package/src/modules/business_rules/lib/rule-engine.ts +1 -1
  301. package/src/modules/catalog/api/option-schemas/route.ts +0 -1
  302. package/src/modules/catalog/data/entities.ts +2 -11
  303. package/src/modules/configs/data/entities.ts +2 -1
  304. package/src/modules/currencies/commands/fetch-configs.ts +3 -3
  305. package/src/modules/currencies/data/entities.ts +1 -1
  306. package/src/modules/customer_accounts/api/signup.ts +1 -1
  307. package/src/modules/customer_accounts/data/entities.ts +1 -1
  308. package/src/modules/customer_accounts/services/customerInvitationService.ts +1 -1
  309. package/src/modules/customer_accounts/services/customerSessionService.ts +1 -1
  310. package/src/modules/customer_accounts/services/customerTokenService.ts +26 -15
  311. package/src/modules/customers/api/interactions/conflicts/route.ts +26 -23
  312. package/src/modules/customers/api/interactions/counts/route.ts +13 -11
  313. package/src/modules/customers/api/interactions/route.ts +32 -44
  314. package/src/modules/customers/api/utils.ts +45 -37
  315. package/src/modules/customers/cli.ts +88 -67
  316. package/src/modules/customers/commands/dictionaries.ts +1 -1
  317. package/src/modules/customers/commands/tags.ts +1 -1
  318. package/src/modules/customers/data/entities.ts +2 -12
  319. package/src/modules/customers/lib/interactionProjection.ts +36 -25
  320. package/src/modules/customers/lib/personCompanyLinkTable.ts +13 -18
  321. package/src/modules/dashboards/api/roles/widgets/route.ts +1 -1
  322. package/src/modules/dashboards/api/users/widgets/route.ts +1 -1
  323. package/src/modules/dashboards/data/entities.ts +1 -1
  324. package/src/modules/data_sync/api/mappings/route.ts +1 -1
  325. package/src/modules/data_sync/data/entities.ts +2 -1
  326. package/src/modules/data_sync/lib/id-mapping.ts +1 -1
  327. package/src/modules/data_sync/lib/sync-run-service.ts +1 -1
  328. package/src/modules/dictionaries/commands/factory.ts +1 -1
  329. package/src/modules/dictionaries/data/entities.ts +2 -9
  330. package/src/modules/directory/commands/organizations.ts +4 -4
  331. package/src/modules/directory/data/entities.ts +2 -1
  332. package/src/modules/entities/api/definitions.ts +2 -2
  333. package/src/modules/entities/api/encryption.ts +2 -2
  334. package/src/modules/entities/api/relations/options.ts +8 -3
  335. package/src/modules/entities/cli.ts +4 -4
  336. package/src/modules/entities/data/entities.ts +1 -1
  337. package/src/modules/entities/lib/field-definitions.ts +2 -2
  338. package/src/modules/entities/lib/register.ts +1 -1
  339. package/src/modules/feature_toggles/data/entities.ts +2 -9
  340. package/src/modules/inbox_ops/api/proposals/counts/route.ts +10 -10
  341. package/src/modules/inbox_ops/data/entities.ts +2 -8
  342. package/src/modules/inbox_ops/lib/messagesIntegration.ts +12 -11
  343. package/src/modules/integrations/data/entities.ts +2 -1
  344. package/src/modules/integrations/lib/credentials-service.ts +1 -1
  345. package/src/modules/integrations/lib/log-service.ts +1 -1
  346. package/src/modules/integrations/lib/state-service.ts +1 -1
  347. package/src/modules/messages/api/route.ts +134 -123
  348. package/src/modules/messages/api/unread-count/route.ts +19 -16
  349. package/src/modules/messages/commands/confirmations.ts +1 -1
  350. package/src/modules/messages/commands/messages.ts +3 -3
  351. package/src/modules/messages/data/entities.ts +2 -1
  352. package/src/modules/messages/lib/email-sender.ts +1 -1
  353. package/src/modules/messages/lib/searchLookup.ts +16 -13
  354. package/src/modules/messages/lib/tokenConsumption.ts +16 -8
  355. package/src/modules/notifications/data/entities.ts +2 -1
  356. package/src/modules/notifications/lib/notificationRecipients.ts +42 -26
  357. package/src/modules/notifications/lib/notificationService.ts +53 -42
  358. package/src/modules/notifications/workers/create-notification.worker.ts +20 -17
  359. package/src/modules/payment_gateways/api/transactions/route.ts +2 -2
  360. package/src/modules/payment_gateways/data/entities.ts +2 -1
  361. package/src/modules/payment_gateways/lib/gateway-service.ts +1 -1
  362. package/src/modules/payment_gateways/lib/webhook-utils.ts +2 -2
  363. package/src/modules/perspectives/data/entities.ts +1 -1
  364. package/src/modules/planner/data/entities.ts +1 -1
  365. package/src/modules/progress/data/entities.ts +2 -1
  366. package/src/modules/progress/lib/progressServiceImpl.ts +1 -1
  367. package/src/modules/query_index/api/status.ts +85 -71
  368. package/src/modules/query_index/cli.ts +51 -31
  369. package/src/modules/query_index/data/entities.ts +1 -1
  370. package/src/modules/query_index/di.ts +41 -16
  371. package/src/modules/query_index/lib/batch.ts +68 -55
  372. package/src/modules/query_index/lib/coverage.ts +115 -88
  373. package/src/modules/query_index/lib/engine.ts +1036 -1096
  374. package/src/modules/query_index/lib/indexer.ts +115 -79
  375. package/src/modules/query_index/lib/jobs.ts +51 -31
  376. package/src/modules/query_index/lib/purge.ts +25 -19
  377. package/src/modules/query_index/lib/reindexer.ts +97 -84
  378. package/src/modules/query_index/lib/search-tokens.ts +67 -36
  379. package/src/modules/query_index/lib/stale.ts +14 -17
  380. package/src/modules/query_index/lib/subscriber-scope.ts +6 -5
  381. package/src/modules/query_index/subscribers/delete_one.ts +9 -6
  382. package/src/modules/resources/commands/tag-assignments.ts +1 -1
  383. package/src/modules/resources/commands/tags.ts +1 -1
  384. package/src/modules/resources/data/entities.ts +2 -1
  385. package/src/modules/sales/commands/documentAddresses.ts +2 -2
  386. package/src/modules/sales/commands/notes.ts +1 -1
  387. package/src/modules/sales/commands/tags.ts +1 -1
  388. package/src/modules/sales/data/enrichers.ts +17 -13
  389. package/src/modules/sales/data/entities.ts +2 -11
  390. package/src/modules/shipping_carriers/data/entities.ts +2 -1
  391. package/src/modules/shipping_carriers/lib/shipping-service.ts +1 -1
  392. package/src/modules/shipping_carriers/lib/webhook-utils.ts +2 -2
  393. package/src/modules/staff/data/entities.ts +1 -1
  394. package/src/modules/translations/api/[entityType]/[entityId]/route.ts +14 -11
  395. package/src/modules/translations/api/context.ts +4 -4
  396. package/src/modules/translations/commands/translations.ts +116 -81
  397. package/src/modules/translations/components/TranslationManager.tsx +23 -14
  398. package/src/modules/translations/data/entities.ts +1 -1
  399. package/src/modules/translations/i18n/de.json +1 -0
  400. package/src/modules/translations/i18n/en.json +1 -0
  401. package/src/modules/translations/i18n/es.json +1 -0
  402. package/src/modules/translations/i18n/pl.json +1 -0
  403. package/src/modules/translations/lib/apply.ts +6 -6
  404. package/src/modules/translations/lib/batch.ts +9 -7
  405. package/src/modules/translations/subscribers/cleanup.ts +10 -11
  406. package/src/modules/workflows/api/definitions/route.ts +1 -1
  407. package/src/modules/workflows/cli.ts +5 -5
  408. package/src/modules/workflows/data/entities.ts +2 -1
  409. package/src/modules/workflows/lib/event-logger.ts +2 -2
  410. package/src/modules/workflows/lib/seeds.ts +16 -1
  411. package/src/modules/workflows/lib/step-handler.ts +3 -3
  412. package/src/modules/workflows/lib/task-handler.ts +1 -1
  413. package/src/modules/workflows/lib/transition-handler.ts +1 -1
  414. package/src/modules/workflows/lib/workflow-executor.ts +2 -2
@@ -2,7 +2,7 @@ import type { EntityManager } from '@mikro-orm/postgresql'
2
2
  import { resolveEntityTableName } from '@open-mercato/shared/lib/query/engine'
3
3
  import { resolveTenantEncryptionService } from '@open-mercato/shared/lib/encryption/customFieldValues'
4
4
  import { decryptIndexDocForSearch, encryptIndexDocForStorage } from '@open-mercato/shared/lib/encryption/indexDoc'
5
- import type { Knex } from 'knex'
5
+ import { sql } from 'kysely'
6
6
  import { replaceSearchTokensForRecord, deleteSearchTokensForRecord } from './search-tokens'
7
7
  import { attachAggregateSearchField } from './document'
8
8
 
@@ -14,13 +14,15 @@ type BuildDocParams = {
14
14
  }
15
15
 
16
16
  export async function buildIndexDoc(em: EntityManager, params: BuildDocParams): Promise<Record<string, any> | null> {
17
- const knex = (em as any).getConnection().getKnex() as Knex
17
+ const db = (em as any).getKysely()
18
18
  const baseTable = resolveEntityTableName(em, params.entityType)
19
19
 
20
20
  // Fetch base row
21
- const baseRow = await knex(baseTable)
22
- .where('id', params.recordId)
23
- .first()
21
+ const baseRow = await db
22
+ .selectFrom(baseTable as any)
23
+ .selectAll()
24
+ .where('id' as any, '=', params.recordId)
25
+ .executeTakeFirst() as Record<string, any> | undefined
24
26
  if (!baseRow) return null
25
27
  const docSources: Array<Record<string, any>> = []
26
28
 
@@ -29,15 +31,18 @@ export async function buildIndexDoc(em: EntityManager, params: BuildDocParams):
29
31
  if (params.entityType === 'customers:customer_person_profile' || params.entityType === 'customers:customer_company_profile') {
30
32
  const entityId = (baseRow as any).entity_id ?? (baseRow as any).entityId
31
33
  if (entityId) {
32
- const entityRow = await knex('customer_entities')
33
- .where('id', entityId)
34
- .first()
34
+ const entityRow = await db
35
+ .selectFrom('customer_entities' as any)
36
+ .selectAll()
37
+ .where('id' as any, '=', entityId)
38
+ .executeTakeFirst() as Record<string, any> | undefined
35
39
  if (entityRow) {
36
40
  docSources.push(entityRow)
37
41
  parentEntityRow = entityRow
38
42
  }
39
43
  }
40
44
  }
45
+ void parentEntityRow
41
46
 
42
47
  // Build base document (snake_case keys as in DB)
43
48
  let doc: Record<string, any> = {}
@@ -47,15 +52,38 @@ export async function buildIndexDoc(em: EntityManager, params: BuildDocParams):
47
52
  }
48
53
 
49
54
  // Attach custom fields under flat keys 'cf:<key>'
50
- const cfRows = await knex('custom_field_values')
51
- .select(['field_key', 'value_text', 'value_multiline', 'value_int', 'value_float', 'value_bool'])
52
- .where({ entity_id: params.entityType, record_id: String(params.recordId) })
53
- .modify((qb: any) => {
54
- if (params.organizationId != null) qb.andWhere((b: any) => b.where({ organization_id: params.organizationId }).orWhereNull('organization_id'))
55
- else qb.whereNull('organization_id')
56
- if (params.tenantId != null) qb.andWhere((b: any) => b.where({ tenant_id: params.tenantId }).orWhereNull('tenant_id'))
57
- else qb.whereNull('tenant_id')
58
- })
55
+ let cfQuery = db
56
+ .selectFrom('custom_field_values' as any)
57
+ .select([
58
+ 'field_key' as any,
59
+ 'value_text' as any,
60
+ 'value_multiline' as any,
61
+ 'value_int' as any,
62
+ 'value_float' as any,
63
+ 'value_bool' as any,
64
+ ])
65
+ .where('entity_id' as any, '=', params.entityType)
66
+ .where('record_id' as any, '=', String(params.recordId))
67
+
68
+ if (params.organizationId != null) {
69
+ cfQuery = cfQuery.where((eb: any) => eb.or([
70
+ eb('organization_id' as any, '=', params.organizationId),
71
+ eb('organization_id' as any, 'is', null),
72
+ ]))
73
+ } else {
74
+ cfQuery = cfQuery.where('organization_id' as any, 'is', null)
75
+ }
76
+
77
+ if (params.tenantId != null) {
78
+ cfQuery = cfQuery.where((eb: any) => eb.or([
79
+ eb('tenant_id' as any, '=', params.tenantId),
80
+ eb('tenant_id' as any, 'is', null),
81
+ ]))
82
+ } else {
83
+ cfQuery = cfQuery.where('tenant_id' as any, 'is', null)
84
+ }
85
+
86
+ const cfRows = await cfQuery.execute() as Array<Record<string, any>>
59
87
 
60
88
  const cfMap: Record<string, any[]> = {}
61
89
  for (const r of cfRows) {
@@ -72,12 +100,14 @@ export async function buildIndexDoc(em: EntityManager, params: BuildDocParams):
72
100
 
73
101
  // Attach translations under flat keys 'l10n:{locale}:{field}'
74
102
  try {
75
- const translationRow = await knex('entity_translations')
76
- .where({ entity_type: params.entityType, entity_id: String(params.recordId) })
77
- .andWhereRaw('tenant_id is not distinct from ?', [params.tenantId ?? null])
78
- .andWhereRaw('organization_id is not distinct from ?', [params.organizationId ?? null])
79
- .select(['translations'])
80
- .first()
103
+ const translationRow = await db
104
+ .selectFrom('entity_translations' as any)
105
+ .select(['translations' as any])
106
+ .where('entity_type' as any, '=', params.entityType)
107
+ .where('entity_id' as any, '=', String(params.recordId))
108
+ .where(sql`tenant_id is not distinct from ${params.tenantId ?? null}`)
109
+ .where(sql`organization_id is not distinct from ${params.organizationId ?? null}`)
110
+ .executeTakeFirst() as { translations: Record<string, Record<string, unknown>> | null } | undefined
81
111
 
82
112
  if (translationRow?.translations && typeof translationRow.translations === 'object') {
83
113
  for (const [locale, fields] of Object.entries(translationRow.translations)) {
@@ -113,29 +143,37 @@ export type UpsertIndexResult = {
113
143
  revived: boolean
114
144
  }
115
145
 
146
+ function scopeEntityIndexes<QB extends { where: (...args: any[]) => QB }>(
147
+ q: QB,
148
+ args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null },
149
+ ): QB {
150
+ let chain = q.where('entity_type' as any, '=', args.entityType)
151
+ chain = chain.where('entity_id' as any, '=', String(args.recordId))
152
+ chain = args.organizationId == null
153
+ ? chain.where('organization_id' as any, 'is', null as any)
154
+ : chain.where('organization_id' as any, '=', args.organizationId)
155
+ chain = chain.where(sql`tenant_id is not distinct from ${args.tenantId ?? null}`)
156
+ return chain
157
+ }
158
+
116
159
  export async function upsertIndexRow(
117
160
  em: EntityManager,
118
161
  args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null }
119
162
  ): Promise<UpsertIndexResult> {
120
- const knex = (em as any).getConnection().getKnex() as Knex
121
- const baseScopeQuery = knex('entity_indexes')
122
- .select(['id', 'deleted_at'])
123
- .where({
124
- entity_type: args.entityType,
125
- entity_id: String(args.recordId),
126
- organization_id: args.organizationId ?? null,
127
- })
128
- .andWhereRaw('tenant_id is not distinct from ?', [args.tenantId ?? null])
129
- .first<{ id: string; deleted_at: Date | null } | undefined>()
163
+ const db = (em as any).getKysely()
164
+
165
+ const existing = await scopeEntityIndexes(
166
+ db.selectFrom('entity_indexes' as any).select(['id' as any, 'deleted_at' as any]),
167
+ args,
168
+ ).executeTakeFirst() as { id: string; deleted_at: Date | null } | undefined
130
169
 
131
- const existing = await baseScopeQuery
132
170
  const existed = !!existing
133
171
  const wasDeleted = !!existing && existing.deleted_at != null
134
172
 
135
173
  const doc = await buildIndexDoc(em, args)
136
174
  if (!doc) {
137
175
  try {
138
- await deleteSearchTokensForRecord(knex, {
176
+ await deleteSearchTokensForRecord(db, {
139
177
  entityType: args.entityType,
140
178
  recordId: args.recordId,
141
179
  organizationId: args.organizationId ?? null,
@@ -143,14 +181,10 @@ export async function upsertIndexRow(
143
181
  })
144
182
  } catch {}
145
183
  if (existed) {
146
- await knex('entity_indexes')
147
- .where({
148
- entity_type: args.entityType,
149
- entity_id: String(args.recordId),
150
- organization_id: args.organizationId ?? null,
151
- })
152
- .andWhereRaw('tenant_id is not distinct from ?', [args.tenantId ?? null])
153
- .del()
184
+ await scopeEntityIndexes(
185
+ db.deleteFrom('entity_indexes' as any) as any,
186
+ args,
187
+ ).execute()
154
188
  }
155
189
  return { doc: null, existed, wasDeleted, created: false, revived: false }
156
190
  }
@@ -160,29 +194,40 @@ export async function upsertIndexRow(
160
194
  entity_id: String(args.recordId),
161
195
  organization_id: args.organizationId ?? null,
162
196
  tenant_id: args.tenantId ?? null,
163
- doc,
197
+ doc: sql`${JSON.stringify(doc)}::jsonb`,
164
198
  index_version: 1,
165
- updated_at: knex.fn.now(),
199
+ updated_at: sql`now()`,
166
200
  deleted_at: null,
167
201
  }
202
+
168
203
  // Prefer modern upsert keyed by coalesced org id when available; fallback to update-then-insert
169
204
  try {
170
- const insertQ = knex('entity_indexes').insert({ ...payload, created_at: knex.fn.now() })
171
- await insertQ
172
- .onConflict(['entity_type', 'entity_id', 'organization_id_coalesced'])
173
- .merge(payload)
205
+ await db
206
+ .insertInto('entity_indexes' as any)
207
+ .values({ ...payload, created_at: sql`now()` } as any)
208
+ .onConflict((oc: any) => oc
209
+ .columns(['entity_type', 'entity_id', 'organization_id_coalesced'])
210
+ .doUpdateSet({
211
+ tenant_id: args.tenantId ?? null,
212
+ doc: sql`${JSON.stringify(doc)}::jsonb`,
213
+ index_version: 1,
214
+ updated_at: sql`now()`,
215
+ deleted_at: null,
216
+ } as any))
217
+ .execute()
174
218
  } catch {
175
219
  // Fallback for schemas without organization_id_coalesced column/index
176
- const updated = await knex('entity_indexes')
177
- .where({
178
- entity_type: args.entityType,
179
- entity_id: String(args.recordId),
180
- organization_id: args.organizationId ?? null,
181
- })
182
- .andWhereRaw('tenant_id is not distinct from ?', [args.tenantId ?? null])
183
- .update(payload)
184
- if (!updated) {
185
- try { await knex('entity_indexes').insert({ ...payload, created_at: knex.fn.now() }) } catch {}
220
+ const updated = await scopeEntityIndexes(
221
+ db.updateTable('entity_indexes' as any).set(payload as any) as any,
222
+ args,
223
+ ).executeTakeFirst() as { numUpdatedRows?: bigint | number } | undefined
224
+ if (!updated || Number(updated.numUpdatedRows ?? 0) === 0) {
225
+ try {
226
+ await db
227
+ .insertInto('entity_indexes' as any)
228
+ .values({ ...payload, created_at: sql`now()` } as any)
229
+ .execute()
230
+ } catch {}
186
231
  }
187
232
  }
188
233
 
@@ -198,7 +243,7 @@ export async function upsertIndexRow(
198
243
  encryption,
199
244
  dekKeyCache,
200
245
  )
201
- await replaceSearchTokensForRecord(knex, {
246
+ await replaceSearchTokensForRecord(db, {
202
247
  entityType: args.entityType,
203
248
  recordId: args.recordId,
204
249
  organizationId: args.organizationId ?? null,
@@ -213,36 +258,27 @@ export async function markDeleted(
213
258
  em: EntityManager,
214
259
  args: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null }
215
260
  ): Promise<{ wasActive: boolean }> {
216
- const knex = (em as any).getConnection().getKnex() as Knex
217
- const existing = await knex('entity_indexes')
218
- .select(['deleted_at'])
219
- .where({
220
- entity_type: args.entityType,
221
- entity_id: String(args.recordId),
222
- organization_id: args.organizationId ?? null,
223
- })
224
- .andWhereRaw('tenant_id is not distinct from ?', [args.tenantId ?? null])
225
- .first<{ deleted_at: Date | null } | undefined>()
261
+ const db = (em as any).getKysely()
262
+ const existing = await scopeEntityIndexes(
263
+ db.selectFrom('entity_indexes' as any).select(['deleted_at' as any]),
264
+ args,
265
+ ).executeTakeFirst() as { deleted_at: Date | null } | undefined
226
266
 
227
267
  const wasActive = !!existing && existing.deleted_at == null
228
268
 
229
269
  if (existing) {
230
270
  try {
231
- await deleteSearchTokensForRecord(knex, {
271
+ await deleteSearchTokensForRecord(db, {
232
272
  entityType: args.entityType,
233
273
  recordId: args.recordId,
234
274
  organizationId: args.organizationId ?? null,
235
275
  tenantId: args.tenantId ?? null,
236
276
  })
237
277
  } catch {}
238
- await knex('entity_indexes')
239
- .where({
240
- entity_type: args.entityType,
241
- entity_id: String(args.recordId),
242
- organization_id: args.organizationId ?? null,
243
- })
244
- .andWhereRaw('tenant_id is not distinct from ?', [args.tenantId ?? null])
245
- .del()
278
+ await scopeEntityIndexes(
279
+ db.deleteFrom('entity_indexes' as any) as any,
280
+ args,
281
+ ).execute()
246
282
  }
247
283
 
248
284
  return { wasActive }
@@ -1,4 +1,4 @@
1
- import type { Knex } from 'knex'
1
+ import { type Kysely, sql } from 'kysely'
2
2
 
3
3
  export type JobScope = {
4
4
  entityType: string
@@ -8,65 +8,85 @@ export type JobScope = {
8
8
  partitionCount?: number | null
9
9
  }
10
10
 
11
- function scopeQuery(knex: Knex, scope: JobScope) {
12
- let query = knex('entity_index_jobs').where('entity_type', scope.entityType)
13
- query = query.andWhereRaw('organization_id is not distinct from ?', [scope.organizationId ?? null])
14
- query = query.andWhereRaw('tenant_id is not distinct from ?', [scope.tenantId ?? null])
15
- query = query.andWhereRaw('partition_index is not distinct from ?', [scope.partitionIndex ?? null])
16
- query = query.andWhereRaw('partition_count is not distinct from ?', [scope.partitionCount ?? null])
17
- return query
11
+ function applyScopeWhere<QB extends { where: (...args: any[]) => QB }>(
12
+ builder: QB,
13
+ scope: JobScope,
14
+ ): QB {
15
+ let q = builder.where('entity_type' as any, '=', scope.entityType)
16
+ q = q.where(sql`organization_id is not distinct from ${scope.organizationId ?? null}`)
17
+ q = q.where(sql`tenant_id is not distinct from ${scope.tenantId ?? null}`)
18
+ q = q.where(sql`partition_index is not distinct from ${scope.partitionIndex ?? null}`)
19
+ q = q.where(sql`partition_count is not distinct from ${scope.partitionCount ?? null}`)
20
+ return q
18
21
  }
19
22
 
20
23
  export async function prepareJob(
21
- knex: Knex,
24
+ db: Kysely<any>,
22
25
  scope: JobScope,
23
26
  status: 'reindexing' | 'purging',
24
27
  options: { totalCount?: number | null } = {},
25
- ) {
28
+ ): Promise<string | null> {
26
29
  const base = {
27
30
  organization_id: scope.organizationId ?? null,
28
31
  tenant_id: scope.tenantId ?? null,
29
32
  partition_index: scope.partitionIndex ?? null,
30
33
  partition_count: scope.partitionCount ?? null,
31
34
  status,
32
- started_at: knex.fn.now(),
35
+ started_at: sql`now()`,
33
36
  finished_at: null,
34
- heartbeat_at: knex.fn.now(),
37
+ heartbeat_at: sql`now()`,
35
38
  processed_count: 0,
36
39
  total_count: options.totalCount ?? null,
37
40
  }
38
- const existing = await scopeQuery(knex, scope).first<{ id: string }>()
41
+
42
+ const existing = await applyScopeWhere(
43
+ db.selectFrom('entity_index_jobs' as any).select(['id' as any]),
44
+ scope,
45
+ ).executeTakeFirst() as { id: string } | undefined
46
+
39
47
  if (existing) {
40
- await scopeQuery(knex, scope).update(base)
48
+ await applyScopeWhere(
49
+ db.updateTable('entity_index_jobs' as any).set(base as any) as any,
50
+ scope,
51
+ ).execute()
41
52
  return existing.id
42
53
  }
43
- const inserted = await knex('entity_index_jobs')
44
- .insert({
54
+
55
+ const inserted = await db
56
+ .insertInto('entity_index_jobs' as any)
57
+ .values({
45
58
  entity_type: scope.entityType,
46
59
  ...base,
47
- })
48
- .returning<{ id: string }[]>('id')
60
+ } as any)
61
+ .returning(['id' as any])
62
+ .execute() as Array<{ id: string }>
63
+
49
64
  return inserted?.[0]?.id ?? null
50
65
  }
51
66
 
52
67
  export async function updateJobProgress(
53
- knex: Knex,
68
+ db: Kysely<any>,
54
69
  scope: JobScope,
55
70
  processedDelta: number,
56
- ) {
57
- await scopeQuery(knex, scope).update({
58
- processed_count: knex.raw('coalesce(processed_count, 0) + ?', [Math.max(0, processedDelta)]),
59
- heartbeat_at: knex.fn.now(),
60
- })
71
+ ): Promise<void> {
72
+ await applyScopeWhere(
73
+ db.updateTable('entity_index_jobs' as any).set({
74
+ processed_count: sql`coalesce(processed_count, 0) + ${Math.max(0, processedDelta)}`,
75
+ heartbeat_at: sql`now()`,
76
+ } as any) as any,
77
+ scope,
78
+ ).execute()
61
79
  }
62
80
 
63
81
  export async function finalizeJob(
64
- knex: Knex,
82
+ db: Kysely<any>,
65
83
  scope: JobScope,
66
- ) {
67
- await scopeQuery(knex, scope).update({
68
- finished_at: knex.fn.now(),
69
- heartbeat_at: knex.fn.now(),
70
- })
84
+ ): Promise<void> {
85
+ await applyScopeWhere(
86
+ db.updateTable('entity_index_jobs' as any).set({
87
+ finished_at: sql`now()`,
88
+ heartbeat_at: sql`now()`,
89
+ } as any) as any,
90
+ scope,
91
+ ).execute()
71
92
  }
72
-
@@ -1,5 +1,5 @@
1
1
  import type { EntityManager } from '@mikro-orm/postgresql'
2
- import type { Knex } from 'knex'
2
+ import { sql } from 'kysely'
3
3
  import { prepareJob, updateJobProgress, finalizeJob, type JobScope } from './jobs'
4
4
 
5
5
  export type PurgeOptions = {
@@ -12,7 +12,7 @@ export async function purgeIndexScope(
12
12
  em: EntityManager,
13
13
  options: PurgeOptions,
14
14
  ): Promise<void> {
15
- const knex = (em as any).getConnection().getKnex() as Knex
15
+ const db = em.getKysely<any>()
16
16
  const scope: JobScope = {
17
17
  entityType: options.entityType,
18
18
  organizationId: options.organizationId ?? null,
@@ -21,28 +21,34 @@ export async function purgeIndexScope(
21
21
  partitionCount: null,
22
22
  }
23
23
 
24
- const countQuery = knex('entity_indexes')
25
- .where({ entity_type: options.entityType })
26
- .modify((qb) => {
27
- if (options.organizationId !== undefined) {
28
- qb.andWhereRaw('organization_id is not distinct from ?', [options.organizationId ?? null])
29
- }
30
- if (options.tenantId !== undefined) {
31
- qb.andWhereRaw('tenant_id is not distinct from ?', [options.tenantId ?? null])
32
- }
33
- })
34
-
35
- const totalRow = await countQuery.clone().count<{ count: unknown }>({ count: '*' }).first()
24
+ const applyScope = <QB extends { where: (...args: any[]) => QB }>(q: QB): QB => {
25
+ let chain = q.where('entity_type' as any, '=', options.entityType)
26
+ if (options.organizationId !== undefined) {
27
+ chain = chain.where(sql`organization_id is not distinct from ${options.organizationId ?? null}`)
28
+ }
29
+ if (options.tenantId !== undefined) {
30
+ chain = chain.where(sql`tenant_id is not distinct from ${options.tenantId ?? null}`)
31
+ }
32
+ return chain
33
+ }
34
+
35
+ const totalRow = await applyScope(
36
+ db.selectFrom('entity_indexes' as any).select(sql`count(*)`.as('count')),
37
+ ).executeTakeFirst() as { count: unknown } | undefined
38
+
36
39
  const total = totalRow ? Number(totalRow.count) || 0 : 0
37
40
 
38
- await prepareJob(knex, scope, 'purging', { totalCount: total })
41
+ await prepareJob(db, scope, 'purging', { totalCount: total })
39
42
 
40
43
  if (total > 0) {
41
- const removed = await countQuery.clone().del()
42
- await updateJobProgress(knex, scope, typeof removed === 'number' ? removed : total)
44
+ const result = await applyScope(
45
+ db.deleteFrom('entity_indexes' as any) as any,
46
+ ).executeTakeFirst() as { numDeletedRows?: bigint | number } | undefined
47
+ const removed = Number(result?.numDeletedRows ?? 0)
48
+ await updateJobProgress(db, scope, removed || total)
43
49
  } else {
44
- await updateJobProgress(knex, scope, 0)
50
+ await updateJobProgress(db, scope, 0)
45
51
  }
46
52
 
47
- await finalizeJob(knex, scope)
53
+ await finalizeJob(db, scope)
48
54
  }