@intlayer/backend 5.1.4 → 5.1.6

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 (252) hide show
  1. package/dist/cjs/controllers/ai.controller.cjs +61 -43
  2. package/dist/cjs/controllers/ai.controller.cjs.map +1 -1
  3. package/dist/cjs/controllers/dictionary.controller.cjs +8 -12
  4. package/dist/cjs/controllers/dictionary.controller.cjs.map +1 -1
  5. package/dist/cjs/controllers/organization.controller.cjs +17 -1
  6. package/dist/cjs/controllers/organization.controller.cjs.map +1 -1
  7. package/dist/cjs/controllers/project.controller.cjs +1 -0
  8. package/dist/cjs/controllers/project.controller.cjs.map +1 -1
  9. package/dist/cjs/controllers/sessionAuth.controller.cjs +6 -12
  10. package/dist/cjs/controllers/sessionAuth.controller.cjs.map +1 -1
  11. package/dist/cjs/controllers/stripe.controller.cjs +31 -10
  12. package/dist/cjs/controllers/stripe.controller.cjs.map +1 -1
  13. package/dist/cjs/controllers/tag.controller.cjs +0 -7
  14. package/dist/cjs/controllers/tag.controller.cjs.map +1 -1
  15. package/dist/cjs/controllers/user.controller.cjs +1 -1
  16. package/dist/cjs/controllers/user.controller.cjs.map +1 -1
  17. package/dist/cjs/emails/InviteUserEmail.cjs +3 -3
  18. package/dist/cjs/emails/InviteUserEmail.cjs.map +1 -1
  19. package/dist/cjs/emails/PasswordChangeConfirmation.cjs +3 -3
  20. package/dist/cjs/emails/PasswordChangeConfirmation.cjs.map +1 -1
  21. package/dist/cjs/emails/ResetUserPassword.cjs +3 -3
  22. package/dist/cjs/emails/ResetUserPassword.cjs.map +1 -1
  23. package/dist/cjs/emails/SubscriptionPaymentCancellation.cjs +19 -3
  24. package/dist/cjs/emails/SubscriptionPaymentCancellation.cjs.map +1 -1
  25. package/dist/cjs/emails/SubscriptionPaymentError.cjs +19 -3
  26. package/dist/cjs/emails/SubscriptionPaymentError.cjs.map +1 -1
  27. package/dist/cjs/emails/SubscriptionPaymentSuccess.cjs +19 -3
  28. package/dist/cjs/emails/SubscriptionPaymentSuccess.cjs.map +1 -1
  29. package/dist/cjs/emails/ValidateUserEmail.cjs +3 -3
  30. package/dist/cjs/emails/ValidateUserEmail.cjs.map +1 -1
  31. package/dist/cjs/emails/Welcome.cjs +3 -3
  32. package/dist/cjs/emails/Welcome.cjs.map +1 -1
  33. package/dist/cjs/export.cjs +10 -10
  34. package/dist/cjs/export.cjs.map +1 -1
  35. package/dist/cjs/routes/ai.routes.cjs +21 -15
  36. package/dist/cjs/routes/ai.routes.cjs.map +1 -1
  37. package/dist/cjs/routes/dictionary.routes.cjs +15 -12
  38. package/dist/cjs/routes/dictionary.routes.cjs.map +1 -1
  39. package/dist/cjs/routes/event-listener.routes.cjs +4 -4
  40. package/dist/cjs/routes/event-listener.routes.cjs.map +1 -1
  41. package/dist/cjs/routes/organization.routes.cjs +15 -15
  42. package/dist/cjs/routes/organization.routes.cjs.map +1 -1
  43. package/dist/cjs/routes/project.routes.cjs +30 -18
  44. package/dist/cjs/routes/project.routes.cjs.map +1 -1
  45. package/dist/cjs/routes/sessionAuth.routes.cjs +38 -35
  46. package/dist/cjs/routes/sessionAuth.routes.cjs.map +1 -1
  47. package/dist/cjs/routes/stripe.routes.cjs +23 -11
  48. package/dist/cjs/routes/stripe.routes.cjs.map +1 -1
  49. package/dist/cjs/routes/tags.routes.cjs +11 -11
  50. package/dist/cjs/routes/tags.routes.cjs.map +1 -1
  51. package/dist/cjs/routes/user.routes.cjs +13 -13
  52. package/dist/cjs/routes/user.routes.cjs.map +1 -1
  53. package/dist/cjs/schemas/dictionary.schema.cjs +0 -4
  54. package/dist/cjs/schemas/dictionary.schema.cjs.map +1 -1
  55. package/dist/cjs/schemas/project.schema.cjs +2 -2
  56. package/dist/cjs/schemas/project.schema.cjs.map +1 -1
  57. package/dist/cjs/services/dictionary.service.cjs.map +1 -1
  58. package/dist/cjs/services/email.service.cjs +1 -1
  59. package/dist/cjs/services/email.service.cjs.map +1 -1
  60. package/dist/cjs/services/subscription.service.cjs +81 -2
  61. package/dist/cjs/services/subscription.service.cjs.map +1 -1
  62. package/dist/cjs/types/dictionary.types.cjs.map +1 -1
  63. package/dist/cjs/utils/AI/{askDocQuestion.cjs → askDocQuestion/askDocQuestion.cjs} +1 -1
  64. package/dist/cjs/utils/AI/askDocQuestion/askDocQuestion.cjs.map +1 -0
  65. package/dist/{esm/utils/AI → cjs/utils/AI/askDocQuestion}/embeddings.json +4288 -4288
  66. package/dist/cjs/utils/{auditDictionary → AI/auditDictionary}/index.cjs +5 -3
  67. package/dist/cjs/utils/AI/auditDictionary/index.cjs.map +1 -0
  68. package/dist/cjs/utils/{auditDictionaryField → AI/auditDictionaryField}/PROMPT.md +17 -7
  69. package/dist/cjs/utils/{auditDictionaryField → AI/auditDictionaryField}/index.cjs +5 -3
  70. package/dist/cjs/utils/AI/auditDictionaryField/index.cjs.map +1 -0
  71. package/dist/cjs/utils/{auditDictionaryMetadata → AI/auditDictionaryMetadata}/index.cjs +5 -3
  72. package/dist/cjs/utils/AI/auditDictionaryMetadata/index.cjs.map +1 -0
  73. package/dist/cjs/utils/AI/autocomplete/PROMPT.md +13 -0
  74. package/dist/cjs/utils/AI/autocomplete/index.cjs +73 -0
  75. package/dist/cjs/utils/AI/autocomplete/index.cjs.map +1 -0
  76. package/dist/cjs/utils/auditTag/index.cjs +4 -2
  77. package/dist/cjs/utils/auditTag/index.cjs.map +1 -1
  78. package/dist/cjs/utils/errors/errorCodes.cjs +26 -0
  79. package/dist/cjs/utils/errors/errorCodes.cjs.map +1 -1
  80. package/dist/cjs/utils/mongoDB/connectDB.cjs +10 -0
  81. package/dist/cjs/utils/mongoDB/connectDB.cjs.map +1 -1
  82. package/dist/cjs/webhooks/stripe.webhook.cjs +42 -0
  83. package/dist/cjs/webhooks/stripe.webhook.cjs.map +1 -1
  84. package/dist/esm/controllers/ai.controller.mjs +59 -42
  85. package/dist/esm/controllers/ai.controller.mjs.map +1 -1
  86. package/dist/esm/controllers/dictionary.controller.mjs +8 -12
  87. package/dist/esm/controllers/dictionary.controller.mjs.map +1 -1
  88. package/dist/esm/controllers/organization.controller.mjs +18 -2
  89. package/dist/esm/controllers/organization.controller.mjs.map +1 -1
  90. package/dist/esm/controllers/project.controller.mjs +1 -0
  91. package/dist/esm/controllers/project.controller.mjs.map +1 -1
  92. package/dist/esm/controllers/sessionAuth.controller.mjs +6 -12
  93. package/dist/esm/controllers/sessionAuth.controller.mjs.map +1 -1
  94. package/dist/esm/controllers/stripe.controller.mjs +30 -10
  95. package/dist/esm/controllers/stripe.controller.mjs.map +1 -1
  96. package/dist/esm/controllers/tag.controller.mjs +0 -7
  97. package/dist/esm/controllers/tag.controller.mjs.map +1 -1
  98. package/dist/esm/controllers/user.controller.mjs +2 -2
  99. package/dist/esm/controllers/user.controller.mjs.map +1 -1
  100. package/dist/esm/emails/InviteUserEmail.mjs +3 -3
  101. package/dist/esm/emails/InviteUserEmail.mjs.map +1 -1
  102. package/dist/esm/emails/PasswordChangeConfirmation.mjs +3 -3
  103. package/dist/esm/emails/PasswordChangeConfirmation.mjs.map +1 -1
  104. package/dist/esm/emails/ResetUserPassword.mjs +3 -3
  105. package/dist/esm/emails/ResetUserPassword.mjs.map +1 -1
  106. package/dist/esm/emails/SubscriptionPaymentCancellation.mjs +19 -3
  107. package/dist/esm/emails/SubscriptionPaymentCancellation.mjs.map +1 -1
  108. package/dist/esm/emails/SubscriptionPaymentError.mjs +19 -3
  109. package/dist/esm/emails/SubscriptionPaymentError.mjs.map +1 -1
  110. package/dist/esm/emails/SubscriptionPaymentSuccess.mjs +19 -3
  111. package/dist/esm/emails/SubscriptionPaymentSuccess.mjs.map +1 -1
  112. package/dist/esm/emails/ValidateUserEmail.mjs +3 -3
  113. package/dist/esm/emails/ValidateUserEmail.mjs.map +1 -1
  114. package/dist/esm/emails/Welcome.mjs +3 -3
  115. package/dist/esm/emails/Welcome.mjs.map +1 -1
  116. package/dist/esm/export.mjs +10 -10
  117. package/dist/esm/export.mjs.map +1 -1
  118. package/dist/esm/routes/ai.routes.mjs +22 -15
  119. package/dist/esm/routes/ai.routes.mjs.map +1 -1
  120. package/dist/esm/routes/dictionary.routes.mjs +14 -11
  121. package/dist/esm/routes/dictionary.routes.mjs.map +1 -1
  122. package/dist/esm/routes/event-listener.routes.mjs +4 -4
  123. package/dist/esm/routes/event-listener.routes.mjs.map +1 -1
  124. package/dist/esm/routes/organization.routes.mjs +13 -13
  125. package/dist/esm/routes/organization.routes.mjs.map +1 -1
  126. package/dist/esm/routes/project.routes.mjs +28 -16
  127. package/dist/esm/routes/project.routes.mjs.map +1 -1
  128. package/dist/esm/routes/sessionAuth.routes.mjs +37 -34
  129. package/dist/esm/routes/sessionAuth.routes.mjs.map +1 -1
  130. package/dist/esm/routes/stripe.routes.mjs +22 -9
  131. package/dist/esm/routes/stripe.routes.mjs.map +1 -1
  132. package/dist/esm/routes/tags.routes.mjs +9 -9
  133. package/dist/esm/routes/tags.routes.mjs.map +1 -1
  134. package/dist/esm/routes/user.routes.mjs +11 -11
  135. package/dist/esm/routes/user.routes.mjs.map +1 -1
  136. package/dist/esm/schemas/dictionary.schema.mjs +0 -4
  137. package/dist/esm/schemas/dictionary.schema.mjs.map +1 -1
  138. package/dist/esm/schemas/project.schema.mjs +2 -2
  139. package/dist/esm/schemas/project.schema.mjs.map +1 -1
  140. package/dist/esm/services/dictionary.service.mjs.map +1 -1
  141. package/dist/esm/services/email.service.mjs +1 -1
  142. package/dist/esm/services/email.service.mjs.map +1 -1
  143. package/dist/esm/services/subscription.service.mjs +78 -1
  144. package/dist/esm/services/subscription.service.mjs.map +1 -1
  145. package/dist/esm/utils/AI/{askDocQuestion.mjs → askDocQuestion/askDocQuestion.mjs} +1 -1
  146. package/dist/esm/utils/AI/askDocQuestion/askDocQuestion.mjs.map +1 -0
  147. package/dist/{cjs/utils/AI → esm/utils/AI/askDocQuestion}/embeddings.json +4288 -4288
  148. package/dist/esm/utils/{auditDictionary → AI/auditDictionary}/index.mjs +5 -3
  149. package/dist/esm/utils/AI/auditDictionary/index.mjs.map +1 -0
  150. package/dist/esm/utils/{auditDictionaryField → AI/auditDictionaryField}/PROMPT.md +17 -7
  151. package/dist/esm/utils/{auditDictionaryField → AI/auditDictionaryField}/index.mjs +5 -3
  152. package/dist/esm/utils/AI/auditDictionaryField/index.mjs.map +1 -0
  153. package/dist/esm/utils/{auditDictionaryMetadata → AI/auditDictionaryMetadata}/index.mjs +5 -3
  154. package/dist/esm/utils/AI/auditDictionaryMetadata/index.mjs.map +1 -0
  155. package/dist/esm/utils/AI/autocomplete/PROMPT.md +13 -0
  156. package/dist/esm/utils/AI/autocomplete/index.mjs +48 -0
  157. package/dist/esm/utils/AI/autocomplete/index.mjs.map +1 -0
  158. package/dist/esm/utils/auditTag/index.mjs +4 -2
  159. package/dist/esm/utils/auditTag/index.mjs.map +1 -1
  160. package/dist/esm/utils/errors/errorCodes.mjs +26 -0
  161. package/dist/esm/utils/errors/errorCodes.mjs.map +1 -1
  162. package/dist/esm/utils/mongoDB/connectDB.mjs +10 -0
  163. package/dist/esm/utils/mongoDB/connectDB.mjs.map +1 -1
  164. package/dist/esm/webhooks/stripe.webhook.mjs +32 -0
  165. package/dist/esm/webhooks/stripe.webhook.mjs.map +1 -1
  166. package/dist/types/controllers/ai.controller.d.ts +17 -2
  167. package/dist/types/controllers/ai.controller.d.ts.map +1 -1
  168. package/dist/types/controllers/dictionary.controller.d.ts.map +1 -1
  169. package/dist/types/controllers/organization.controller.d.ts.map +1 -1
  170. package/dist/types/controllers/project.controller.d.ts.map +1 -1
  171. package/dist/types/controllers/sessionAuth.controller.d.ts +4 -3
  172. package/dist/types/controllers/sessionAuth.controller.d.ts.map +1 -1
  173. package/dist/types/controllers/stripe.controller.d.ts +15 -7
  174. package/dist/types/controllers/stripe.controller.d.ts.map +1 -1
  175. package/dist/types/controllers/tag.controller.d.ts.map +1 -1
  176. package/dist/types/emails/SubscriptionPaymentCancellation.d.ts +4 -3
  177. package/dist/types/emails/SubscriptionPaymentCancellation.d.ts.map +1 -1
  178. package/dist/types/emails/SubscriptionPaymentError.d.ts +4 -3
  179. package/dist/types/emails/SubscriptionPaymentError.d.ts.map +1 -1
  180. package/dist/types/emails/SubscriptionPaymentSuccess.d.ts +4 -3
  181. package/dist/types/emails/SubscriptionPaymentSuccess.d.ts.map +1 -1
  182. package/dist/types/export.d.ts +5 -5
  183. package/dist/types/export.d.ts.map +1 -1
  184. package/dist/types/routes/ai.routes.d.ts +6 -1
  185. package/dist/types/routes/ai.routes.d.ts.map +1 -1
  186. package/dist/types/routes/dictionary.routes.d.ts +4 -4
  187. package/dist/types/routes/dictionary.routes.d.ts.map +1 -1
  188. package/dist/types/routes/event-listener.routes.d.ts +1 -1
  189. package/dist/types/routes/event-listener.routes.d.ts.map +1 -1
  190. package/dist/types/routes/organization.routes.d.ts +5 -5
  191. package/dist/types/routes/organization.routes.d.ts.map +1 -1
  192. package/dist/types/routes/project.routes.d.ts +5 -5
  193. package/dist/types/routes/project.routes.d.ts.map +1 -1
  194. package/dist/types/routes/sessionAuth.routes.d.ts +4 -7
  195. package/dist/types/routes/sessionAuth.routes.d.ts.map +1 -1
  196. package/dist/types/routes/stripe.routes.d.ts +6 -1
  197. package/dist/types/routes/stripe.routes.d.ts.map +1 -1
  198. package/dist/types/routes/tags.routes.d.ts +3 -3
  199. package/dist/types/routes/tags.routes.d.ts.map +1 -1
  200. package/dist/types/routes/user.routes.d.ts +4 -4
  201. package/dist/types/routes/user.routes.d.ts.map +1 -1
  202. package/dist/types/schemas/dictionary.schema.d.ts.map +1 -1
  203. package/dist/types/services/dictionary.service.d.ts.map +1 -1
  204. package/dist/types/services/subscription.service.d.ts +9 -0
  205. package/dist/types/services/subscription.service.d.ts.map +1 -1
  206. package/dist/types/types/dictionary.types.d.ts +0 -1
  207. package/dist/types/types/dictionary.types.d.ts.map +1 -1
  208. package/dist/types/utils/AI/askDocQuestion/askDocQuestion.d.ts.map +1 -0
  209. package/dist/types/utils/{auditDictionary → AI/auditDictionary}/index.d.ts +8 -5
  210. package/dist/types/utils/AI/auditDictionary/index.d.ts.map +1 -0
  211. package/dist/types/utils/{auditDictionaryField → AI/auditDictionaryField}/index.d.ts +8 -5
  212. package/dist/types/utils/AI/auditDictionaryField/index.d.ts.map +1 -0
  213. package/dist/types/utils/{auditDictionaryMetadata → AI/auditDictionaryMetadata}/index.d.ts +8 -5
  214. package/dist/types/utils/AI/auditDictionaryMetadata/index.d.ts.map +1 -0
  215. package/dist/types/utils/AI/autocomplete/index.d.ts +22 -0
  216. package/dist/types/utils/AI/autocomplete/index.d.ts.map +1 -0
  217. package/dist/types/utils/auditTag/index.d.ts +7 -4
  218. package/dist/types/utils/auditTag/index.d.ts.map +1 -1
  219. package/dist/types/utils/errors/errorCodes.d.ts +26 -0
  220. package/dist/types/utils/errors/errorCodes.d.ts.map +1 -1
  221. package/dist/types/utils/mongoDB/connectDB.d.ts.map +1 -1
  222. package/dist/types/webhooks/stripe.webhook.d.ts.map +1 -1
  223. package/package.json +11 -10
  224. package/dist/cjs/utils/AI/askDocQuestion.cjs.map +0 -1
  225. package/dist/cjs/utils/auditDictionary/index.cjs.map +0 -1
  226. package/dist/cjs/utils/auditDictionaryField/index.cjs.map +0 -1
  227. package/dist/cjs/utils/auditDictionaryMetadata/index.cjs.map +0 -1
  228. package/dist/esm/utils/AI/askDocQuestion.mjs.map +0 -1
  229. package/dist/esm/utils/auditDictionary/index.mjs.map +0 -1
  230. package/dist/esm/utils/auditDictionaryField/index.mjs.map +0 -1
  231. package/dist/esm/utils/auditDictionaryMetadata/index.mjs.map +0 -1
  232. package/dist/types/utils/AI/askDocQuestion.d.ts.map +0 -1
  233. package/dist/types/utils/auditDictionary/index.d.ts.map +0 -1
  234. package/dist/types/utils/auditDictionaryField/index.d.ts.map +0 -1
  235. package/dist/types/utils/auditDictionaryMetadata/index.d.ts.map +0 -1
  236. /package/dist/cjs/utils/{auditDictionary → AI/auditDictionary}/CJS_FORMAT.md +0 -0
  237. /package/dist/cjs/utils/{auditDictionary → AI/auditDictionary}/JSON_FORMAT.md +0 -0
  238. /package/dist/cjs/utils/{auditDictionary → AI/auditDictionary}/JSX_FORMAT.md +0 -0
  239. /package/dist/cjs/utils/{auditDictionary → AI/auditDictionary}/MJS_FORMAT.md +0 -0
  240. /package/dist/cjs/utils/{auditDictionary → AI/auditDictionary}/PROMPT.md +0 -0
  241. /package/dist/cjs/utils/{auditDictionary → AI/auditDictionary}/TSX_FORMAT.md +0 -0
  242. /package/dist/cjs/utils/{auditDictionary → AI/auditDictionary}/TS_FORMAT.md +0 -0
  243. /package/dist/cjs/utils/{auditDictionaryMetadata → AI/auditDictionaryMetadata}/PROMPT.md +0 -0
  244. /package/dist/esm/utils/{auditDictionary → AI/auditDictionary}/CJS_FORMAT.md +0 -0
  245. /package/dist/esm/utils/{auditDictionary → AI/auditDictionary}/JSON_FORMAT.md +0 -0
  246. /package/dist/esm/utils/{auditDictionary → AI/auditDictionary}/JSX_FORMAT.md +0 -0
  247. /package/dist/esm/utils/{auditDictionary → AI/auditDictionary}/MJS_FORMAT.md +0 -0
  248. /package/dist/esm/utils/{auditDictionary → AI/auditDictionary}/PROMPT.md +0 -0
  249. /package/dist/esm/utils/{auditDictionary → AI/auditDictionary}/TSX_FORMAT.md +0 -0
  250. /package/dist/esm/utils/{auditDictionary → AI/auditDictionary}/TS_FORMAT.md +0 -0
  251. /package/dist/esm/utils/{auditDictionaryMetadata → AI/auditDictionaryMetadata}/PROMPT.md +0 -0
  252. /package/dist/types/utils/AI/{askDocQuestion.d.ts → askDocQuestion/askDocQuestion.d.ts} +0 -0
@@ -32,38 +32,37 @@ __export(ai_controller_exports, {
32
32
  auditContentDeclaration: () => auditContentDeclaration,
33
33
  auditContentDeclarationField: () => auditContentDeclarationField,
34
34
  auditContentDeclarationMetadata: () => auditContentDeclarationMetadata,
35
- auditTag: () => auditTag
35
+ auditTag: () => auditTag,
36
+ autocomplete: () => autocomplete
36
37
  });
37
38
  module.exports = __toCommonJS(ai_controller_exports);
38
39
  var import_dictionary = require('./../services/dictionary.service.cjs');
39
40
  var import_tag = require('./../services/tag.service.cjs');
40
41
  var tagService = __toESM(require('./../services/tag.service.cjs'), 1);
41
- var askDocQuestionUtil = __toESM(require('./../utils/AI/askDocQuestion.cjs'), 1);
42
- var auditContentDeclarationUtil = __toESM(require('./../utils/auditDictionary/index.cjs'), 1);
43
- var auditContentDeclarationFieldUtil = __toESM(require('./../utils/auditDictionaryField/index.cjs'), 1);
44
- var auditContentDeclarationMetadataUtil = __toESM(require('./../utils/auditDictionaryMetadata/index.cjs'), 1);
42
+ var askDocQuestionUtil = __toESM(require('./../utils/AI/askDocQuestion/askDocQuestion.cjs'), 1);
43
+ var auditContentDeclarationUtil = __toESM(require('./../utils/AI/auditDictionary/index.cjs'), 1);
44
+ var auditContentDeclarationFieldUtil = __toESM(require('./../utils/AI/auditDictionaryField/index.cjs'), 1);
45
+ var auditContentDeclarationMetadataUtil = __toESM(require('./../utils/AI/auditDictionaryMetadata/index.cjs'), 1);
46
+ var autocompleteUtil = __toESM(require('./../utils/AI/autocomplete/index.cjs'), 1);
45
47
  var auditTagUtil = __toESM(require('./../utils/auditTag/index.cjs'), 1);
46
48
  var import_errors = require('./../utils/errors/index.cjs');
47
49
  var import_responseData = require('./../utils/responseData.cjs');
48
50
  const auditContentDeclaration = async (req, res, _next) => {
49
- const { user, project } = res.locals;
51
+ const { user, project, organization } = res.locals;
50
52
  const {
51
53
  fileContent,
52
54
  filePath,
53
55
  openAiApiKey,
54
56
  customPrompt,
55
57
  model,
58
+ temperature,
56
59
  locales,
57
60
  defaultLocale,
58
61
  tagsKeys
59
62
  } = req.body;
60
63
  if (!openAiApiKey) {
61
- if (!user) {
62
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
63
- return;
64
- }
65
- if (!project) {
66
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_NOT_DEFINED");
64
+ if (!user || !project || !organization) {
65
+ import_errors.ErrorHandler.handleGenericErrorResponse(res, "AI_ACCESS_DENIED");
67
66
  return;
68
67
  }
69
68
  }
@@ -76,7 +75,8 @@ const auditContentDeclaration = async (req, res, _next) => {
76
75
  fileContent,
77
76
  filePath,
78
77
  model,
79
- openAiApiKey: openAiApiKey ?? process.env.OPENAI_API_KEY,
78
+ temperature,
79
+ openAiApiKey,
80
80
  customPrompt,
81
81
  locales,
82
82
  defaultLocale,
@@ -97,23 +97,20 @@ const auditContentDeclaration = async (req, res, _next) => {
97
97
  }
98
98
  };
99
99
  const auditContentDeclarationField = async (req, res, _next) => {
100
- const { user, project } = res.locals;
100
+ const { user, project, organization } = res.locals;
101
101
  const {
102
102
  fileContent,
103
103
  openAiApiKey,
104
104
  customPrompt,
105
105
  model,
106
+ temperature,
106
107
  locales,
107
108
  tagsKeys,
108
109
  keyPath
109
110
  } = req.body;
110
111
  if (!openAiApiKey) {
111
- if (!user) {
112
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
113
- return;
114
- }
115
- if (!project) {
116
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_NOT_DEFINED");
112
+ if (!user || !project || !organization) {
113
+ import_errors.ErrorHandler.handleGenericErrorResponse(res, "AI_ACCESS_DENIED");
117
114
  return;
118
115
  }
119
116
  }
@@ -125,7 +122,8 @@ const auditContentDeclarationField = async (req, res, _next) => {
125
122
  const auditResponse = await auditContentDeclarationFieldUtil.auditDictionaryField({
126
123
  fileContent,
127
124
  model,
128
- openAiApiKey: openAiApiKey ?? process.env.OPENAI_API_KEY,
125
+ temperature,
126
+ openAiApiKey,
129
127
  customPrompt,
130
128
  locales,
131
129
  tags,
@@ -147,18 +145,10 @@ const auditContentDeclarationField = async (req, res, _next) => {
147
145
  };
148
146
  const auditContentDeclarationMetadata = async (req, res, _next) => {
149
147
  const { user, organization, project } = res.locals;
150
- const { fileContent, openAiApiKey, customPrompt, model } = req.body;
148
+ const { fileContent, openAiApiKey, customPrompt, model, temperature } = req.body;
151
149
  if (!openAiApiKey) {
152
- if (!user) {
153
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
154
- return;
155
- }
156
- if (!organization) {
157
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "ORGANIZATION_NOT_DEFINED");
158
- return;
159
- }
160
- if (!project) {
161
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_NOT_DEFINED");
150
+ if (!user || !project || !organization) {
151
+ import_errors.ErrorHandler.handleGenericErrorResponse(res, "AI_ACCESS_DENIED");
162
152
  return;
163
153
  }
164
154
  }
@@ -173,7 +163,8 @@ const auditContentDeclarationMetadata = async (req, res, _next) => {
173
163
  const auditResponse = await auditContentDeclarationMetadataUtil.auditDictionaryMetadata({
174
164
  fileContent,
175
165
  model,
176
- openAiApiKey: openAiApiKey ?? process.env.OPENAI_API_KEY,
166
+ openAiApiKey,
167
+ temperature,
177
168
  customPrompt,
178
169
  tags
179
170
  });
@@ -192,15 +183,11 @@ const auditContentDeclarationMetadata = async (req, res, _next) => {
192
183
  }
193
184
  };
194
185
  const auditTag = async (req, res, _next) => {
195
- const { user, project } = res.locals;
196
- const { openAiApiKey, customPrompt, model, tag } = req.body;
186
+ const { user, project, organization } = res.locals;
187
+ const { openAiApiKey, customPrompt, model, temperature, tag } = req.body;
197
188
  if (!openAiApiKey) {
198
- if (!user) {
199
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
200
- return;
201
- }
202
- if (!project) {
203
- import_errors.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_NOT_DEFINED");
189
+ if (!user || !project || !organization) {
190
+ import_errors.ErrorHandler.handleGenericErrorResponse(res, "AI_ACCESS_DENIED");
204
191
  return;
205
192
  }
206
193
  }
@@ -211,7 +198,8 @@ const auditTag = async (req, res, _next) => {
211
198
  }
212
199
  const auditResponse = await auditTagUtil.auditTag({
213
200
  model,
214
- openAiApiKey: openAiApiKey ?? process.env.OPENAI_API_KEY,
201
+ openAiApiKey,
202
+ temperature,
215
203
  customPrompt,
216
204
  dictionaries,
217
205
  tag
@@ -243,12 +231,42 @@ const askDocQuestion = async (req, res) => {
243
231
  return;
244
232
  }
245
233
  };
234
+ const autocomplete = async (req, res) => {
235
+ try {
236
+ const { text, model, openAiApiKey, customPrompt, temperature } = req.body;
237
+ const { user, project, organization } = res.locals;
238
+ if (!openAiApiKey) {
239
+ if (!user || !project || !organization) {
240
+ import_errors.ErrorHandler.handleGenericErrorResponse(res, "AI_ACCESS_DENIED");
241
+ return;
242
+ }
243
+ }
244
+ const response = await autocompleteUtil.autocomplete({
245
+ text,
246
+ model,
247
+ openAiApiKey,
248
+ temperature,
249
+ customPrompt
250
+ }) ?? {
251
+ autocompletion: "",
252
+ tokenUsed: 0
253
+ };
254
+ const responseData = (0, import_responseData.formatResponse)({
255
+ data: response
256
+ });
257
+ res.json(responseData);
258
+ } catch (error) {
259
+ import_errors.ErrorHandler.handleAppErrorResponse(res, error);
260
+ return;
261
+ }
262
+ };
246
263
  // Annotate the CommonJS export names for ESM import in node:
247
264
  0 && (module.exports = {
248
265
  askDocQuestion,
249
266
  auditContentDeclaration,
250
267
  auditContentDeclarationField,
251
268
  auditContentDeclarationMetadata,
252
- auditTag
269
+ auditTag,
270
+ autocomplete
253
271
  });
254
272
  //# sourceMappingURL=ai.controller.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/controllers/ai.controller.ts"],"sourcesContent":["import { type KeyPath } from '@intlayer/core';\nimport type { ResponseWithInformation } from '@middlewares/sessionAuth.middleware';\nimport { getDictionariesByTags } from '@services/dictionary.service';\nimport { getTagsByKeys } from '@services/tag.service';\nimport * as tagService from '@services/tag.service';\nimport * as askDocQuestionUtil from '@utils/AI/askDocQuestion';\nimport * as auditContentDeclarationUtil from '@utils/auditDictionary';\nimport * as auditContentDeclarationFieldUtil from '@utils/auditDictionaryField';\nimport * as auditContentDeclarationMetadataUtil from '@utils/auditDictionaryMetadata';\nimport * as auditTagUtil from '@utils/auditTag';\nimport { type AppError, ErrorHandler } from '@utils/errors';\nimport { formatResponse, type ResponseData } from '@utils/responseData';\nimport type { NextFunction, Request } from 'express';\nimport type { Locales } from 'intlayer';\nimport type { Dictionary } from '@/types/dictionary.types';\nimport type { Tag } from '@/types/tag.types';\n\nexport type AuditContentDeclarationBody = {\n openAiApiKey?: string;\n customPrompt?: string;\n locales: Locales[];\n defaultLocale: Locales;\n fileContent: string;\n filePath?: string;\n model?: string;\n tagsKeys?: string[];\n};\nexport type AuditContentDeclarationResult =\n ResponseData<auditContentDeclarationUtil.AuditFileResultData>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const auditContentDeclaration = async (\n req: Request<AuditContentDeclarationBody>,\n res: ResponseWithInformation<AuditContentDeclarationResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, project } = res.locals;\n const {\n fileContent,\n filePath,\n openAiApiKey,\n customPrompt,\n model,\n locales,\n defaultLocale,\n tagsKeys,\n } = req.body;\n\n if (!openAiApiKey) {\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n }\n\n try {\n let tags: Tag[] = [];\n\n if (project?.organizationId) {\n tags = await getTagsByKeys(tagsKeys, project.organizationId);\n }\n\n const auditResponse = await auditContentDeclarationUtil.auditDictionary({\n fileContent,\n filePath,\n model,\n openAiApiKey: openAiApiKey ?? process.env.OPENAI_API_KEY,\n customPrompt,\n locales,\n defaultLocale,\n tags,\n });\n\n if (!auditResponse) {\n ErrorHandler.handleGenericErrorResponse(res, 'AUDIT_FAILED');\n return;\n }\n\n const responseData =\n formatResponse<auditContentDeclarationUtil.AuditFileResultData>({\n data: auditResponse,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AuditContentDeclarationFieldBody = {\n openAiApiKey?: string;\n customPrompt?: string;\n locales: Locales[];\n fileContent: string;\n filePath?: string;\n model?: string;\n tagsKeys?: string[];\n keyPath: KeyPath[];\n};\nexport type AuditContentDeclarationFieldResult =\n ResponseData<auditContentDeclarationUtil.AuditFileResultData>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const auditContentDeclarationField = async (\n req: Request<AuditContentDeclarationFieldBody>,\n res: ResponseWithInformation<AuditContentDeclarationFieldResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, project } = res.locals;\n const {\n fileContent,\n openAiApiKey,\n customPrompt,\n model,\n locales,\n tagsKeys,\n keyPath,\n } = req.body;\n\n if (!openAiApiKey) {\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n }\n\n try {\n let tags: Tag[] = [];\n\n if (project?.organizationId) {\n tags = await getTagsByKeys(tagsKeys, project.organizationId);\n }\n\n const auditResponse =\n await auditContentDeclarationFieldUtil.auditDictionaryField({\n fileContent,\n model,\n openAiApiKey: openAiApiKey ?? process.env.OPENAI_API_KEY,\n customPrompt,\n locales,\n tags,\n keyPath,\n });\n\n if (!auditResponse) {\n ErrorHandler.handleGenericErrorResponse(res, 'AUDIT_FAILED');\n return;\n }\n\n const responseData =\n formatResponse<auditContentDeclarationUtil.AuditFileResultData>({\n data: auditResponse,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AuditContentDeclarationMetadataBody = {\n openAiApiKey?: string;\n customPrompt?: string;\n fileContent: string;\n model?: string;\n};\nexport type AuditContentDeclarationMetadataResult =\n ResponseData<auditContentDeclarationUtil.AuditFileResultData>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const auditContentDeclarationMetadata = async (\n req: Request<AuditContentDeclarationMetadataBody>,\n res: ResponseWithInformation<AuditContentDeclarationMetadataResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, organization, project } = res.locals;\n const { fileContent, openAiApiKey, customPrompt, model } = req.body;\n\n if (!openAiApiKey) {\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n if (!organization) {\n ErrorHandler.handleGenericErrorResponse(res, 'ORGANIZATION_NOT_DEFINED');\n return;\n }\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n }\n\n try {\n const tags: Tag[] = await tagService.findTags(\n {\n organizationId: organization?._id,\n },\n 0,\n 1000\n );\n\n const auditResponse =\n await auditContentDeclarationMetadataUtil.auditDictionaryMetadata({\n fileContent,\n model,\n openAiApiKey: openAiApiKey ?? process.env.OPENAI_API_KEY,\n customPrompt,\n tags,\n });\n\n if (!auditResponse) {\n ErrorHandler.handleGenericErrorResponse(res, 'AUDIT_FAILED');\n return;\n }\n\n const responseData =\n formatResponse<auditContentDeclarationUtil.AuditFileResultData>({\n data: auditResponse,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AuditTagBody = {\n openAiApiKey?: string;\n customPrompt?: string;\n filePath?: string;\n model?: string;\n tag: Tag;\n};\nexport type AuditTagResult =\n ResponseData<auditContentDeclarationUtil.AuditFileResultData>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const auditTag = async (\n req: Request<undefined, undefined, AuditTagBody>,\n res: ResponseWithInformation<AuditTagResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, project } = res.locals;\n const { openAiApiKey, customPrompt, model, tag } = req.body;\n\n if (!openAiApiKey) {\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n }\n\n try {\n let dictionaries: Dictionary[] = [];\n if (project?.organizationId) {\n dictionaries = await getDictionariesByTags([tag.key], project._id);\n }\n\n const auditResponse = await auditTagUtil.auditTag({\n model,\n openAiApiKey: openAiApiKey ?? process.env.OPENAI_API_KEY!,\n customPrompt,\n dictionaries,\n tag,\n });\n\n if (!auditResponse) {\n ErrorHandler.handleGenericErrorResponse(res, 'AUDIT_FAILED');\n return;\n }\n\n const responseData =\n formatResponse<auditContentDeclarationUtil.AuditFileResultData>({\n data: auditResponse,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AskDocQuestionBody = {\n messages: askDocQuestionUtil.ChatCompletionRequestMessage[];\n};\nexport type AskDocQuestionResult =\n ResponseData<askDocQuestionUtil.AskDocQuestionResult>;\n\nexport const askDocQuestion = async (\n req: Request<undefined, undefined, AskDocQuestionBody>,\n res: ResponseWithInformation<AskDocQuestionResult>\n) => {\n const { messages } = req.body;\n try {\n const response = await askDocQuestionUtil.askDocQuestion(messages);\n\n const responseData =\n formatResponse<askDocQuestionUtil.AskDocQuestionResult>({\n data: response,\n });\n\n res.json(responseData);\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,wBAAsC;AACtC,iBAA8B;AAC9B,iBAA4B;AAC5B,yBAAoC;AACpC,kCAA6C;AAC7C,uCAAkD;AAClD,0CAAqD;AACrD,mBAA8B;AAC9B,oBAA4C;AAC5C,0BAAkD;AAsB3C,MAAM,0BAA0B,OACrC,KACA,KACA,UACkB;AAClB,QAAM,EAAE,MAAM,QAAQ,IAAI,IAAI;AAC9B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IAC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;AAAA,EACvB,SAAS,OAAO;AACd,+BAAa,uBAAuB,KAAK,KAAiB;AAC1D;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/controllers/ai.controller.ts"],"sourcesContent":["import { type KeyPath } from '@intlayer/core';\nimport type { ResponseWithInformation } from '@middlewares/sessionAuth.middleware';\nimport { getDictionariesByTags } from '@services/dictionary.service';\nimport { getTagsByKeys } from '@services/tag.service';\nimport * as tagService from '@services/tag.service';\nimport * as askDocQuestionUtil from '@utils/AI/askDocQuestion/askDocQuestion';\nimport * as auditContentDeclarationUtil from '@utils/AI/auditDictionary';\nimport * as auditContentDeclarationFieldUtil from '@utils/AI/auditDictionaryField';\nimport * as auditContentDeclarationMetadataUtil from '@utils/AI/auditDictionaryMetadata';\nimport * as autocompleteUtil from '@utils/AI/autocomplete';\nimport * as auditTagUtil from '@utils/auditTag';\nimport { type AppError, ErrorHandler } from '@utils/errors';\nimport { formatResponse, type ResponseData } from '@utils/responseData';\nimport type { NextFunction, Request } from 'express';\nimport type { Locales } from 'intlayer';\nimport type { Dictionary } from '@/types/dictionary.types';\nimport type { Tag } from '@/types/tag.types';\n\nexport type AuditContentDeclarationBody = {\n openAiApiKey?: string;\n customPrompt?: string;\n locales: Locales[];\n defaultLocale: Locales;\n fileContent: string;\n filePath?: string;\n model?: string;\n temperature?: number;\n tagsKeys?: string[];\n};\nexport type AuditContentDeclarationResult =\n ResponseData<auditContentDeclarationUtil.AuditFileResultData>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const auditContentDeclaration = async (\n req: Request<AuditContentDeclarationBody>,\n res: ResponseWithInformation<AuditContentDeclarationResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, project, organization } = res.locals;\n const {\n fileContent,\n filePath,\n openAiApiKey,\n customPrompt,\n model,\n temperature,\n locales,\n defaultLocale,\n tagsKeys,\n } = req.body;\n\n if (!openAiApiKey) {\n if (!user || !project || !organization) {\n ErrorHandler.handleGenericErrorResponse(res, 'AI_ACCESS_DENIED');\n return;\n }\n }\n\n try {\n let tags: Tag[] = [];\n\n if (project?.organizationId) {\n tags = await getTagsByKeys(tagsKeys, project.organizationId);\n }\n\n const auditResponse = await auditContentDeclarationUtil.auditDictionary({\n fileContent,\n filePath,\n model,\n temperature,\n openAiApiKey,\n customPrompt,\n locales,\n defaultLocale,\n tags,\n });\n\n if (!auditResponse) {\n ErrorHandler.handleGenericErrorResponse(res, 'AUDIT_FAILED');\n return;\n }\n\n const responseData =\n formatResponse<auditContentDeclarationUtil.AuditFileResultData>({\n data: auditResponse,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AuditContentDeclarationFieldBody = {\n openAiApiKey?: string;\n customPrompt?: string;\n locales: Locales[];\n fileContent: string;\n filePath?: string;\n model?: string;\n temperature?: number;\n tagsKeys?: string[];\n keyPath: KeyPath[];\n};\nexport type AuditContentDeclarationFieldResult =\n ResponseData<auditContentDeclarationUtil.AuditFileResultData>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const auditContentDeclarationField = async (\n req: Request<AuditContentDeclarationFieldBody>,\n res: ResponseWithInformation<AuditContentDeclarationFieldResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, project, organization } = res.locals;\n const {\n fileContent,\n openAiApiKey,\n customPrompt,\n model,\n temperature,\n locales,\n tagsKeys,\n keyPath,\n } = req.body;\n\n if (!openAiApiKey) {\n if (!user || !project || !organization) {\n ErrorHandler.handleGenericErrorResponse(res, 'AI_ACCESS_DENIED');\n return;\n }\n }\n\n try {\n let tags: Tag[] = [];\n\n if (project?.organizationId) {\n tags = await getTagsByKeys(tagsKeys, project.organizationId);\n }\n\n const auditResponse =\n await auditContentDeclarationFieldUtil.auditDictionaryField({\n fileContent,\n model,\n temperature,\n openAiApiKey,\n customPrompt,\n locales,\n tags,\n keyPath,\n });\n\n if (!auditResponse) {\n ErrorHandler.handleGenericErrorResponse(res, 'AUDIT_FAILED');\n return;\n }\n\n const responseData =\n formatResponse<auditContentDeclarationUtil.AuditFileResultData>({\n data: auditResponse,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AuditContentDeclarationMetadataBody = {\n openAiApiKey?: string;\n customPrompt?: string;\n fileContent: string;\n model?: string;\n temperature?: number;\n};\nexport type AuditContentDeclarationMetadataResult =\n ResponseData<auditContentDeclarationUtil.AuditFileResultData>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const auditContentDeclarationMetadata = async (\n req: Request<AuditContentDeclarationMetadataBody>,\n res: ResponseWithInformation<AuditContentDeclarationMetadataResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, organization, project } = res.locals;\n const { fileContent, openAiApiKey, customPrompt, model, temperature } =\n req.body;\n\n if (!openAiApiKey) {\n if (!user || !project || !organization) {\n ErrorHandler.handleGenericErrorResponse(res, 'AI_ACCESS_DENIED');\n return;\n }\n }\n\n try {\n const tags: Tag[] = await tagService.findTags(\n {\n organizationId: organization?._id,\n },\n 0,\n 1000\n );\n\n const auditResponse =\n await auditContentDeclarationMetadataUtil.auditDictionaryMetadata({\n fileContent,\n model,\n openAiApiKey,\n temperature,\n customPrompt,\n tags,\n });\n\n if (!auditResponse) {\n ErrorHandler.handleGenericErrorResponse(res, 'AUDIT_FAILED');\n return;\n }\n\n const responseData =\n formatResponse<auditContentDeclarationUtil.AuditFileResultData>({\n data: auditResponse,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AuditTagBody = {\n openAiApiKey?: string;\n customPrompt?: string;\n filePath?: string;\n model?: string;\n temperature?: number;\n tag: Tag;\n};\nexport type AuditTagResult =\n ResponseData<auditContentDeclarationUtil.AuditFileResultData>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const auditTag = async (\n req: Request<undefined, undefined, AuditTagBody>,\n res: ResponseWithInformation<AuditTagResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, project, organization } = res.locals;\n const { openAiApiKey, customPrompt, model, temperature, tag } = req.body;\n\n if (!openAiApiKey) {\n if (!user || !project || !organization) {\n ErrorHandler.handleGenericErrorResponse(res, 'AI_ACCESS_DENIED');\n return;\n }\n }\n\n try {\n let dictionaries: Dictionary[] = [];\n if (project?.organizationId) {\n dictionaries = await getDictionariesByTags([tag.key], project._id);\n }\n\n const auditResponse = await auditTagUtil.auditTag({\n model,\n openAiApiKey,\n temperature,\n customPrompt,\n dictionaries,\n tag,\n });\n\n if (!auditResponse) {\n ErrorHandler.handleGenericErrorResponse(res, 'AUDIT_FAILED');\n return;\n }\n\n const responseData =\n formatResponse<auditContentDeclarationUtil.AuditFileResultData>({\n data: auditResponse,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AskDocQuestionBody = {\n messages: askDocQuestionUtil.ChatCompletionRequestMessage[];\n};\nexport type AskDocQuestionResult =\n ResponseData<askDocQuestionUtil.AskDocQuestionResult>;\n\nexport const askDocQuestion = async (\n req: Request<undefined, undefined, AskDocQuestionBody>,\n res: ResponseWithInformation<AskDocQuestionResult>\n) => {\n const { messages } = req.body;\n try {\n const response = await askDocQuestionUtil.askDocQuestion(messages);\n\n const responseData =\n formatResponse<askDocQuestionUtil.AskDocQuestionResult>({\n data: response,\n });\n\n res.json(responseData);\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AutocompleteBody = {\n text: string;\n model?: string;\n temperature?: number;\n openAiApiKey?: string;\n customPrompt?: string;\n};\n\nexport type AutocompleteResponse = ResponseData<{\n autocompletion: string;\n}>;\n\nexport const autocomplete = async (\n req: Request<undefined, undefined, AutocompleteBody>,\n res: ResponseWithInformation<AutocompleteResponse>\n) => {\n try {\n const { text, model, openAiApiKey, customPrompt, temperature } = req.body;\n const { user, project, organization } = res.locals;\n\n if (!openAiApiKey) {\n if (!user || !project || !organization) {\n ErrorHandler.handleGenericErrorResponse(res, 'AI_ACCESS_DENIED');\n return;\n }\n }\n\n const response = (await autocompleteUtil.autocomplete({\n text,\n model,\n openAiApiKey,\n temperature,\n customPrompt,\n })) ?? {\n autocompletion: '',\n tokenUsed: 0,\n };\n\n const responseData =\n formatResponse<autocompleteUtil.AutocompleteFileResultData>({\n data: response,\n });\n\n res.json(responseData);\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,wBAAsC;AACtC,iBAA8B;AAC9B,iBAA4B;AAC5B,yBAAoC;AACpC,kCAA6C;AAC7C,uCAAkD;AAClD,0CAAqD;AACrD,uBAAkC;AAClC,mBAA8B;AAC9B,oBAA4C;AAC5C,0BAAkD;AAuB3C,MAAM,0BAA0B,OACrC,KACA,KACA,UACkB;AAClB,QAAM,EAAE,MAAM,SAAS,aAAa,IAAI,IAAI;AAC5C,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,IAAI;AAER,MAAI,CAAC,cAAc;AACjB,QAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,cAAc;AACtC,iCAAa,2BAA2B,KAAK,kBAAkB;AAC/D;AAAA,IACF;AAAA,EACF;AAEA,MAAI;AACF,QAAI,OAAc,CAAC;AAEnB,QAAI,SAAS,gBAAgB;AAC3B,aAAO,UAAM,0BAAc,UAAU,QAAQ,cAAc;AAAA,IAC7D;AAEA,UAAM,gBAAgB,MAAM,4BAA4B,gBAAgB;AAAA,MACtE;AAAA,MACA;AAAA,MACA;AAAA,MACA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ACF;AACF;","names":[]}
@@ -39,6 +39,7 @@ __export(dictionary_controller_exports, {
39
39
  module.exports = __toCommonJS(dictionary_controller_exports);
40
40
  var import_logger = require('./../logger/index.cjs');
41
41
  var dictionaryService = __toESM(require('./../services/dictionary.service.cjs'), 1);
42
+ var import_ensureMongoDocumentToObject = require('./../utils/ensureMongoDocumentToObject.cjs');
42
43
  var import_errors = require('./../utils/errors/index.cjs');
43
44
  var import_getDictionaryFiltersAndPagination = require('./../utils/filtersAndPagination/getDictionaryFiltersAndPagination.cjs');
44
45
  var import_dictionary = require('./../utils/mapper/dictionary.cjs');
@@ -68,7 +69,7 @@ const getDictionaries = async (req, res, _next) => {
68
69
  );
69
70
  const totalItems = await dictionaryService.countDictionaries(filters);
70
71
  const dictionariesAPI = dictionaries.map(
71
- (el) => (0, import_dictionary.mapDictionaryToAPI)(el, project._id, el.publishedVersion ?? void 0)
72
+ (el) => (0, import_dictionary.mapDictionaryToAPI)(el, project._id)
72
73
  );
73
74
  const responseData = (0, import_responseData.formatPaginatedResponse)({
74
75
  data: dictionariesAPI,
@@ -162,7 +163,7 @@ const addDictionary = async (req, res, _next) => {
162
163
  import_errors.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
163
164
  return;
164
165
  }
165
- if (!dictionaryData.projectIds.includes(String(project._id))) {
166
+ if (!dictionaryData.projectIds?.includes(String(project._id))) {
166
167
  import_errors.ErrorHandler.handleGenericErrorResponse(res, "DICTIONARY_PROJECT_MISMATCH");
167
168
  return;
168
169
  }
@@ -254,7 +255,6 @@ const pushDictionaries = async (req, res, _next) => {
254
255
  const updatedDictionariesResult = [];
255
256
  const errorResult = [];
256
257
  for (const dictionaryDataEl of newDictionaries) {
257
- const publishedVersion = dictionaryDataEl.version ? dictionaryDataEl.version === "-1" ? null : dictionaryDataEl.version : null;
258
258
  const dictionary = {
259
259
  title: dictionaryDataEl.title,
260
260
  description: dictionaryDataEl.description,
@@ -266,8 +266,7 @@ const pushDictionaries = async (req, res, _next) => {
266
266
  filePath: {
267
267
  [String(project._id)]: dictionaryDataEl.filePath ?? ""
268
268
  },
269
- key: dictionaryDataEl.key,
270
- publishedVersion
269
+ key: dictionaryDataEl.key
271
270
  };
272
271
  try {
273
272
  const newDictionary = await dictionaryService.createDictionary(dictionary);
@@ -288,10 +287,9 @@ const pushDictionaries = async (req, res, _next) => {
288
287
  const existingDictionaryDB = existingDictionariesDB.find(
289
288
  (dictionaryDB) => dictionaryDB.key === dictionaryDataEl.key
290
289
  );
291
- const existingContentArray = Object.values(
292
- existingDictionaryDB.content
293
- );
294
- const lastContent = existingContentArray[existingContentArray.length - 1].content;
290
+ const versionList = [...existingDictionaryDB.content.keys() ?? []];
291
+ const lastVersion = versionList[versionList.length - 1];
292
+ const lastContent = existingDictionaryDB.content.get(lastVersion)?.content ?? null;
295
293
  const isSameContent = JSON.stringify(lastContent) === JSON.stringify(dictionaryDataEl.content);
296
294
  let newContent = existingDictionaryDB.content;
297
295
  if (!isSameContent) {
@@ -303,9 +301,8 @@ const pushDictionaries = async (req, res, _next) => {
303
301
  }
304
302
  };
305
303
  }
306
- const publishedVersion = dictionaryDataEl.version ? dictionaryDataEl.version === "-1" ? null : dictionaryDataEl.version : null;
307
304
  const dictionary = {
308
- ...existingDictionaryDB,
305
+ ...(0, import_ensureMongoDocumentToObject.ensureMongoDocumentToObject)(existingDictionaryDB),
309
306
  ...dictionaryDataEl,
310
307
  content: newContent,
311
308
  projectIds: [String(project._id)],
@@ -313,7 +310,6 @@ const pushDictionaries = async (req, res, _next) => {
313
310
  filePath: {
314
311
  [String(project._id)]: dictionaryDataEl.filePath ?? ""
315
312
  },
316
- publishedVersion,
317
313
  key: dictionaryDataEl.key
318
314
  };
319
315
  try {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/controllers/dictionary.controller.ts"],"sourcesContent":["import type {\n ContentNode,\n Dictionary as LocalDictionary,\n} from '@intlayer/core';\nimport { logger } from '@logger';\nimport type { ResponseWithInformation } from '@middlewares/sessionAuth.middleware';\nimport * as dictionaryService from '@services/dictionary.service';\nimport { type AppError, ErrorHandler } from '@utils/errors';\nimport {\n type DictionaryFiltersParams,\n getDictionaryFiltersAndPagination,\n} from '@utils/filtersAndPagination/getDictionaryFiltersAndPagination';\nimport type { FiltersAndPagination } from '@utils/filtersAndPagination/getFiltersAndPaginationFromBody';\nimport { mapDictionaryToAPI } from '@utils/mapper/dictionary';\nimport {\n formatPaginatedResponse,\n type ResponseData,\n type PaginatedResponse,\n formatResponse,\n} from '@utils/responseData';\nimport type { NextFunction, Request } from 'express';\nimport { t } from 'express-intlayer';\nimport * as eventListener from '@/controllers/event-listener';\nimport type {\n Dictionary,\n DictionaryAPI,\n DictionaryCreationData,\n DictionaryData,\n VersionedContent,\n} from '@/types/dictionary.types';\n\nexport type GetDictionariesParams =\n FiltersAndPagination<DictionaryFiltersParams>;\nexport type GetDictionariesResult = PaginatedResponse<DictionaryAPI>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const getDictionaries = async (\n req: Request<GetDictionariesParams>,\n res: ResponseWithInformation<GetDictionariesResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, project, dictionaryRights } = res.locals;\n const { filters, pageSize, skip, page, getNumberOfPages } =\n getDictionaryFiltersAndPagination(req);\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n if (!dictionaryRights?.read) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_READ');\n return;\n }\n\n try {\n const dictionaries = await dictionaryService.findDictionaries(\n filters,\n skip,\n pageSize\n );\n const totalItems = await dictionaryService.countDictionaries(filters);\n\n const dictionariesAPI = dictionaries.map((el) =>\n mapDictionaryToAPI(el, project._id, el.publishedVersion ?? undefined)\n );\n\n const responseData = formatPaginatedResponse<DictionaryAPI>({\n data: dictionariesAPI,\n page,\n pageSize,\n totalPages: getNumberOfPages(totalItems),\n totalItems,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type GetDictionariesKeysResult = ResponseData<string[]>;\n\n/**\n * Retrieves a list of dictionaries keys based on filters and pagination.\n */\nexport const getDictionariesKeys = async (\n _req: Request,\n res: ResponseWithInformation<GetDictionariesKeysResult>,\n _next: NextFunction\n) => {\n const { project, dictionaryRights } = res.locals;\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n if (!dictionaryRights?.read) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_READ');\n return;\n }\n\n try {\n const dictionariesKeys = await dictionaryService.getDictionariesKeys(\n project._id\n );\n\n const responseData = formatResponse<string[]>({\n data: dictionariesKeys,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type GetDictionaryParams = { dictionaryKey: string };\nexport type GetDictionaryQuery = { version?: string };\nexport type GetDictionaryResult = ResponseData<DictionaryAPI>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const getDictionaryByKey = async (\n req: Request<GetDictionaryParams, any, any, GetDictionaryQuery>,\n res: ResponseWithInformation<GetDictionaryResult>,\n _next: NextFunction\n): Promise<void> => {\n const { project, user, dictionaryRights } = res.locals;\n const { dictionaryKey } = req.params;\n const version = req.query.version;\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n if (!dictionaryRights?.read) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_READ');\n return;\n }\n\n try {\n const dictionary = await dictionaryService.getDictionaryByKey(\n dictionaryKey,\n project._id\n );\n\n if (!dictionary.projectIds.map(String).includes(String(project._id))) {\n ErrorHandler.handleGenericErrorResponse(\n res,\n 'DICTIONARY_PROJECT_MISMATCH'\n );\n return;\n }\n\n const apiResult = mapDictionaryToAPI(dictionary, project._id, version);\n\n const responseData = formatResponse<DictionaryAPI>({\n data: apiResult,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AddDictionaryBody = { dictionary: DictionaryCreationData };\nexport type AddDictionaryResult = ResponseData<DictionaryAPI>;\n\n/**\n * Adds a new dictionary to the database.\n */\nexport const addDictionary = async (\n req: Request<any, any, AddDictionaryBody>,\n res: ResponseWithInformation<AddDictionaryResult>,\n _next: NextFunction\n): Promise<void> => {\n const { project, user, dictionaryRights } = res.locals;\n const dictionaryData = req.body.dictionary;\n\n if (!dictionaryData) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_DATA_NOT_FOUND');\n return;\n }\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n\n if (!dictionaryData.projectIds.includes(String(project._id))) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_PROJECT_MISMATCH');\n return;\n }\n\n if (!dictionaryRights?.admin) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_ADMIN');\n return;\n }\n\n const dictionary: DictionaryData = {\n key: dictionaryData.key,\n title: dictionaryData.title,\n description: dictionaryData.description,\n content: new Map([\n ['v1', { content: dictionaryData.content ?? ({} as ContentNode) }],\n ]),\n creatorId: user._id,\n filePath: {\n [String(project._id)]: dictionaryData.filePath ?? '',\n },\n projectIds: dictionaryData.projectIds ?? [String(project._id)],\n };\n\n try {\n const newDictionary = await dictionaryService.createDictionary(dictionary);\n\n const apiResult = mapDictionaryToAPI(newDictionary, project._id);\n\n const responseData = formatResponse<DictionaryAPI>({\n message: t({\n en: 'Dictionary created successfully',\n fr: 'Dictionnaire créé avec succès',\n es: 'Diccionario creado con éxito',\n }),\n description: t({\n en: 'Your dictionary has been created successfully',\n fr: 'Votre dictionnaire a été créé avec succès',\n es: 'Su diccionario ha sido creado con éxito',\n }),\n data: apiResult,\n });\n\n res.json(responseData);\n\n eventListener.sendDictionaryUpdate([\n {\n dictionary: mapDictionaryToAPI(newDictionary, project._id),\n status: 'ADDED',\n },\n ]);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type PushDictionariesBody = {\n dictionaries: LocalDictionary[];\n};\ntype PushDictionariesResultData = {\n newDictionaries: string[];\n updatedDictionaries: string[];\n error: { dictionaryId: string; message: string }[];\n};\nexport type PushDictionariesResult = ResponseData<PushDictionariesResultData>;\n\n/**\n * Check each dictionaries, add the new ones and update the existing ones.\n * @param req - Express request object.\n * @param res - Express response object.\n * @returns Response containing the created dictionary.\n */\nexport const pushDictionaries = async (\n req: Request<any, any, PushDictionariesBody>,\n res: ResponseWithInformation<PushDictionariesResult>,\n _next: NextFunction\n): Promise<void> => {\n const { project, user, dictionaryRights } = res.locals;\n const dictionaryData = req.body.dictionaries;\n const dictionariesKeys = dictionaryData.map((dictionary) => dictionary.key);\n\n if (\n typeof dictionaryData === 'object' &&\n Array.isArray(dictionaryData) &&\n dictionaryData.length === 0\n ) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARIES_NOT_PROVIDED');\n return;\n } else if (!dictionaryData) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_DATA_NOT_FOUND');\n return;\n }\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n\n if (!dictionaryRights?.write) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_WRITE');\n return;\n }\n\n if (!dictionaryRights?.admin) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_ADMIN');\n return;\n }\n\n try {\n const { existingDictionariesKey, newDictionariesKey } =\n await dictionaryService.getExistingDictionaryKey(\n dictionariesKeys,\n project._id\n );\n\n const existingDictionaries = dictionaryData.filter((dictionary) =>\n existingDictionariesKey.includes(dictionary.key)\n );\n const newDictionaries = dictionaryData.filter((dictionary) =>\n newDictionariesKey.includes(dictionary.key)\n );\n\n const newDictionariesResult: DictionaryAPI[] = [];\n const updatedDictionariesResult: DictionaryAPI[] = [];\n const errorResult: PushDictionariesResultData['error'] = [];\n\n for (const dictionaryDataEl of newDictionaries) {\n const publishedVersion = dictionaryDataEl.version\n ? dictionaryDataEl.version === '-1'\n ? null\n : dictionaryDataEl.version\n : null;\n\n const dictionary: DictionaryData = {\n title: dictionaryDataEl.title,\n description: dictionaryDataEl.description,\n projectIds: [String(project._id)],\n creatorId: user._id,\n content: new Map([\n ['v1', { content: dictionaryDataEl.content ?? ({} as ContentNode) }],\n ]),\n filePath: {\n [String(project._id)]: dictionaryDataEl.filePath ?? '',\n },\n key: dictionaryDataEl.key,\n publishedVersion,\n };\n\n try {\n const newDictionary =\n await dictionaryService.createDictionary(dictionary);\n newDictionariesResult.push(\n mapDictionaryToAPI(newDictionary, project._id)\n );\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n }\n\n if (existingDictionariesKey.length >= 0) {\n const existingDictionariesDB =\n await dictionaryService.getDictionariesByKeys(\n existingDictionariesKey,\n project._id\n );\n\n for (const dictionaryDataEl of existingDictionaries) {\n const existingDictionaryDB = existingDictionariesDB.find(\n (dictionaryDB) => dictionaryDB.key === dictionaryDataEl.key\n )!;\n\n const existingContentArray = Object.values(\n existingDictionaryDB.content\n );\n\n const lastContent =\n existingContentArray[existingContentArray.length - 1].content;\n\n const isSameContent =\n JSON.stringify(lastContent) ===\n JSON.stringify(dictionaryDataEl.content);\n\n let newContent: VersionedContent = existingDictionaryDB.content;\n\n if (!isSameContent) {\n const newContentVersion =\n dictionaryService.incrementVersion(existingDictionaryDB);\n\n newContent = {\n ...newContent,\n [newContentVersion]: {\n content: dictionaryDataEl.content,\n },\n };\n }\n\n const publishedVersion = dictionaryDataEl.version\n ? dictionaryDataEl.version === '-1'\n ? null\n : dictionaryDataEl.version\n : null;\n\n const dictionary: DictionaryData = {\n ...existingDictionaryDB,\n ...dictionaryDataEl,\n content: newContent,\n projectIds: [String(project._id)],\n creatorId: user._id,\n filePath: {\n [String(project._id)]: dictionaryDataEl.filePath ?? '',\n },\n publishedVersion,\n key: dictionaryDataEl.key,\n };\n\n try {\n const updatedDictionary =\n await dictionaryService.updateDictionaryByKey(\n dictionaryDataEl.key,\n dictionary,\n project._id\n );\n updatedDictionariesResult.push(\n mapDictionaryToAPI(updatedDictionary, project._id)\n );\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n }\n }\n\n const result: PushDictionariesResultData = {\n newDictionaries: newDictionariesResult.map(\n (dictionary) => dictionary.key\n ),\n updatedDictionaries: updatedDictionariesResult.map(\n (dictionary) => dictionary.key\n ),\n error: errorResult,\n };\n\n const responseData = formatResponse<PushDictionariesResultData>({\n message: t({\n en: 'Dictionaries updated successfully',\n fr: 'Dictionnaires mis à jour avec succès',\n es: 'Diccionarios actualizados con éxito',\n }),\n description: t({\n en: 'Your dictionaries have been updated successfully',\n fr: 'Vos dictionnaires ont été mis à jour avec succès',\n es: 'Sus diccionarios han sido actualizados con éxito',\n }),\n data: result,\n });\n\n eventListener.sendDictionaryUpdate([\n ...newDictionariesResult.map(\n (dictionary) =>\n ({\n dictionary,\n status: 'ADDED',\n }) as eventListener.SendDictionaryUpdateArg\n ),\n ...updatedDictionariesResult.map(\n (dictionary) =>\n ({\n dictionary,\n status: 'UPDATED',\n }) as eventListener.SendDictionaryUpdateArg\n ),\n ]);\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type UpdateDictionaryParam = { dictionaryId: string };\nexport type UpdateDictionaryBody = Partial<Dictionary>;\nexport type UpdateDictionaryResult = ResponseData<DictionaryAPI>;\n\n/**\n * Updates an existing dictionary in the database.\n */\nexport const updateDictionary = async (\n req: Request<UpdateDictionaryParam, any, UpdateDictionaryBody>,\n res: ResponseWithInformation<UpdateDictionaryResult>,\n _next: NextFunction\n): Promise<void> => {\n const { dictionaryId } = req.params;\n const { project, dictionaryRights } = res.locals;\n const dictionaryData = req.body;\n\n if (!dictionaryData) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_DATA_NOT_FOUND');\n return;\n }\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n\n if (!dictionaryData.projectIds?.includes(String(project._id))) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_PROJECT_MISMATCH');\n return;\n }\n\n if (typeof dictionaryId === 'undefined') {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_ID_NOT_FOUND');\n return;\n }\n\n if (!dictionaryRights?.write) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_WRITE');\n return;\n }\n\n try {\n const updatedDictionary = await dictionaryService.updateDictionaryById(\n dictionaryId,\n dictionaryData\n );\n\n const apiResult = mapDictionaryToAPI(updatedDictionary, project._id);\n\n const responseData = formatResponse<DictionaryAPI>({\n message: t({\n en: 'Dictionary updated successfully',\n fr: 'Dictionnaire mis à jour avec succès',\n es: 'Diccionario actualizado con éxito',\n }),\n description: t({\n en: 'Your dictionary has been updated successfully',\n fr: 'Votre dictionnaire a été mis à jour avec succès',\n es: 'Su diccionario ha sido actualizado con éxito',\n }),\n data: apiResult,\n });\n\n eventListener.sendDictionaryUpdate([\n {\n dictionary: apiResult,\n status: 'UPDATED',\n },\n ]);\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type DeleteDictionaryParam = { dictionaryId: string };\nexport type DeleteDictionaryResult = ResponseData<DictionaryAPI>;\n\n/**\n * Deletes a dictionary from the database by its ID.\n */\nexport const deleteDictionary = async (\n req: Request<DeleteDictionaryParam>,\n res: ResponseWithInformation<DeleteDictionaryResult>,\n _next: NextFunction\n): Promise<void> => {\n const { project, dictionaryRights } = res.locals;\n const { dictionaryId } = req.params as Partial<DeleteDictionaryParam>;\n\n if (!dictionaryId) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_ID_NOT_FOUND');\n return;\n }\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n\n if (!dictionaryRights?.admin) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_ADMIN');\n return;\n }\n\n try {\n const dictionaryToDelete =\n await dictionaryService.getDictionaryById(dictionaryId);\n\n if (!dictionaryToDelete.projectIds.includes(project._id)) {\n ErrorHandler.handleGenericErrorResponse(\n res,\n 'DICTIONARY_PROJECT_MISMATCH'\n );\n return;\n }\n\n const deletedDictionary =\n await dictionaryService.deleteDictionaryById(dictionaryId);\n\n if (!deletedDictionary) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_NOT_FOUND', {\n dictionaryId,\n });\n return;\n }\n\n logger.info(`Dictionary deleted: ${String(deletedDictionary._id)}`);\n\n const apiResult = mapDictionaryToAPI(deletedDictionary, project._id);\n\n const responseData = formatResponse<DictionaryAPI>({\n message: t({\n en: 'Dictionary deleted successfully',\n fr: 'Dictionnaire supprimé avec succès',\n es: 'Diccionario eliminado con éxito',\n }),\n description: t({\n en: 'Your dictionary has been deleted successfully',\n fr: 'Votre dictionnaire a été supprimé avec succès',\n es: 'Su diccionario ha sido eliminado con éxito',\n }),\n data: apiResult,\n });\n\n res.json(responseData);\n\n eventListener.sendDictionaryUpdate([\n {\n dictionary: apiResult,\n status: 'DELETED',\n },\n ]);\n\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAAuB;AAEvB,wBAAmC;AACnC,oBAA4C;AAC5C,+CAGO;AAEP,wBAAmC;AACnC,0BAKO;AAEP,8BAAkB;AAClB,oBAA+B;AAgBxB,MAAM,kBAAkB,OAC7B,KACA,KACA,UACkB;AAClB,QAAM,EAAE,MAAM,SAAS,iBAAiB,IAAI,IAAI;AAChD,QAAM,EAAE,SAAS,UAAU,MAAM,MAAM,iBAAiB,QACtD,4EAAkC,GAAG;AAEvC,MAAI,CAAC,SAAS;AACZ,+BAAa,2BAA2B,KAAK,qBAAqB;AAClE;AAAA,EACF;AACA,MAAI,CAAC,MAAM;AACT,+BAAa,2BAA2B,KAAK,kBAAkB;AAC/D;AAAA,EACF;AACA,MAAI,CAAC,kBAAkB,MAAM;AAC3B,+BAAa,2BAA2B,KAAK,4BAA4B;AACzE;AAAA,EACF;AAEA,MAAI;AACF,UAAM,eAAe,MAAM,kBAAkB;AAAA,MAC3C;AAAA,MACA;AAAA,MACA;AAAA,IACF;AACA,UAAM,aAAa,MAAM,kBAAkB,kBAAkB,OAAO;AAEpE,UAAM,kBAAkB,aAAa;AAAA,MAAI,CAAC,WACxC,sCAAmB,IAAI,QAAQ,KAAK,GAAG,oBAAoB,MAAS;AAAA,IACtE;AAEA,UAAM,mBAAe,6CAAuC;AAAA,MAC1D,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA,YAAY,iBAAiB,UAAU;AAAA,MACvC;AAAA,IACF,CAAC;AAED,QAAI,KAAK,YAAY;AACrB;AAAA,EACF,SAAS,OAAO;AACd,+BAAa,uBAAu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AAmB;AACtB,iCAAa,2BAA2B,KAAK,wBAAwB;AAAA,QACnE;AAAA,MACF,CAAC;AACD;AAAA,IACF;AAEA,yBAAO,KAAK,uBAAuB,OAAO,kBAAkB,GAAG,CAAC,EAAE;AAElE,UAAM,gBAAY,sCAAmB,mBAAmB,QAAQ,GAAG;AAEnE,UAAM,mBAAe,oCAA8B;AAAA,MACjD,aAAS,2BAAE;AAAA,QACT,IAAI;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,MACN,CAAC;AAAA,MACD,iBAAa,2BAAE;AAAA,QACb,IAAI;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,MACN,CAAC;AAAA,MACD,MAAM;AAAA,IACR,CAAC;AAED,QAAI,KAAK,YAAY;AAErB,kBAAc,qBAAqB;AAAA,MACjC;AAAA,QACE,YAAY;AAAA,QACZ,QAAQ;AAAA,MACV;AAAA,IACF,CAAC;AAED;AAAA,EACF,SAAS,OAAO;AACd,+BAAa,uBAAuB,KAAK,KAAiB;AAC1D;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/controllers/dictionary.controller.ts"],"sourcesContent":["import type {\n ContentNode,\n Dictionary as LocalDictionary,\n} from '@intlayer/core';\nimport { logger } from '@logger';\nimport type { ResponseWithInformation } from '@middlewares/sessionAuth.middleware';\nimport * as dictionaryService from '@services/dictionary.service';\nimport { ensureMongoDocumentToObject } from '@utils/ensureMongoDocumentToObject';\nimport { type AppError, ErrorHandler } from '@utils/errors';\nimport {\n type DictionaryFiltersParams,\n getDictionaryFiltersAndPagination,\n} from '@utils/filtersAndPagination/getDictionaryFiltersAndPagination';\nimport type { FiltersAndPagination } from '@utils/filtersAndPagination/getFiltersAndPaginationFromBody';\nimport { mapDictionaryToAPI } from '@utils/mapper/dictionary';\nimport {\n formatPaginatedResponse,\n type ResponseData,\n type PaginatedResponse,\n formatResponse,\n} from '@utils/responseData';\nimport type { NextFunction, Request } from 'express';\nimport { t } from 'express-intlayer';\nimport * as eventListener from '@/controllers/event-listener';\nimport type {\n Dictionary,\n DictionaryAPI,\n DictionaryCreationData,\n DictionaryData,\n VersionedContent,\n} from '@/types/dictionary.types';\n\nexport type GetDictionariesParams =\n FiltersAndPagination<DictionaryFiltersParams>;\nexport type GetDictionariesResult = PaginatedResponse<DictionaryAPI>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const getDictionaries = async (\n req: Request<GetDictionariesParams>,\n res: ResponseWithInformation<GetDictionariesResult>,\n _next: NextFunction\n): Promise<void> => {\n const { user, project, dictionaryRights } = res.locals;\n const { filters, pageSize, skip, page, getNumberOfPages } =\n getDictionaryFiltersAndPagination(req);\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n if (!dictionaryRights?.read) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_READ');\n return;\n }\n\n try {\n const dictionaries = await dictionaryService.findDictionaries(\n filters,\n skip,\n pageSize\n );\n const totalItems = await dictionaryService.countDictionaries(filters);\n\n const dictionariesAPI = dictionaries.map((el) =>\n mapDictionaryToAPI(el, project._id)\n );\n\n const responseData = formatPaginatedResponse<DictionaryAPI>({\n data: dictionariesAPI,\n page,\n pageSize,\n totalPages: getNumberOfPages(totalItems),\n totalItems,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type GetDictionariesKeysResult = ResponseData<string[]>;\n\n/**\n * Retrieves a list of dictionaries keys based on filters and pagination.\n */\nexport const getDictionariesKeys = async (\n _req: Request,\n res: ResponseWithInformation<GetDictionariesKeysResult>,\n _next: NextFunction\n) => {\n const { project, dictionaryRights } = res.locals;\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n if (!dictionaryRights?.read) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_READ');\n return;\n }\n\n try {\n const dictionariesKeys = await dictionaryService.getDictionariesKeys(\n project._id\n );\n\n const responseData = formatResponse<string[]>({\n data: dictionariesKeys,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type GetDictionaryParams = { dictionaryKey: string };\nexport type GetDictionaryQuery = { version?: string };\nexport type GetDictionaryResult = ResponseData<DictionaryAPI>;\n\n/**\n * Retrieves a list of dictionaries based on filters and pagination.\n */\nexport const getDictionaryByKey = async (\n req: Request<GetDictionaryParams, any, any, GetDictionaryQuery>,\n res: ResponseWithInformation<GetDictionaryResult>,\n _next: NextFunction\n): Promise<void> => {\n const { project, user, dictionaryRights } = res.locals;\n const { dictionaryKey } = req.params;\n const version = req.query.version;\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n if (!dictionaryRights?.read) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_READ');\n return;\n }\n\n try {\n const dictionary = await dictionaryService.getDictionaryByKey(\n dictionaryKey,\n project._id\n );\n\n if (!dictionary.projectIds.map(String).includes(String(project._id))) {\n ErrorHandler.handleGenericErrorResponse(\n res,\n 'DICTIONARY_PROJECT_MISMATCH'\n );\n return;\n }\n\n const apiResult = mapDictionaryToAPI(dictionary, project._id, version);\n\n const responseData = formatResponse<DictionaryAPI>({\n data: apiResult,\n });\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type AddDictionaryBody = { dictionary: DictionaryCreationData };\nexport type AddDictionaryResult = ResponseData<DictionaryAPI>;\n\n/**\n * Adds a new dictionary to the database.\n */\nexport const addDictionary = async (\n req: Request<any, any, AddDictionaryBody>,\n res: ResponseWithInformation<AddDictionaryResult>,\n _next: NextFunction\n): Promise<void> => {\n const { project, user, dictionaryRights } = res.locals;\n const dictionaryData = req.body.dictionary;\n\n if (!dictionaryData) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_DATA_NOT_FOUND');\n return;\n }\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n\n if (!dictionaryData.projectIds?.includes(String(project._id))) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_PROJECT_MISMATCH');\n return;\n }\n\n if (!dictionaryRights?.admin) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_ADMIN');\n return;\n }\n\n const dictionary: DictionaryData = {\n key: dictionaryData.key,\n title: dictionaryData.title,\n description: dictionaryData.description,\n content: new Map([\n ['v1', { content: dictionaryData.content ?? ({} as ContentNode) }],\n ]),\n creatorId: user._id,\n filePath: {\n [String(project._id)]: dictionaryData.filePath ?? '',\n },\n projectIds: dictionaryData.projectIds ?? [String(project._id)],\n };\n\n try {\n const newDictionary = await dictionaryService.createDictionary(dictionary);\n\n const apiResult = mapDictionaryToAPI(newDictionary, project._id);\n\n const responseData = formatResponse<DictionaryAPI>({\n message: t({\n en: 'Dictionary created successfully',\n fr: 'Dictionnaire créé avec succès',\n es: 'Diccionario creado con éxito',\n }),\n description: t({\n en: 'Your dictionary has been created successfully',\n fr: 'Votre dictionnaire a été créé avec succès',\n es: 'Su diccionario ha sido creado con éxito',\n }),\n data: apiResult,\n });\n\n res.json(responseData);\n\n eventListener.sendDictionaryUpdate([\n {\n dictionary: mapDictionaryToAPI(newDictionary, project._id),\n status: 'ADDED',\n },\n ]);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type PushDictionariesBody = {\n dictionaries: LocalDictionary[];\n};\ntype PushDictionariesResultData = {\n newDictionaries: string[];\n updatedDictionaries: string[];\n error: { dictionaryId: string; message: string }[];\n};\nexport type PushDictionariesResult = ResponseData<PushDictionariesResultData>;\n\n/**\n * Check each dictionaries, add the new ones and update the existing ones.\n * @param req - Express request object.\n * @param res - Express response object.\n * @returns Response containing the created dictionary.\n */\nexport const pushDictionaries = async (\n req: Request<any, any, PushDictionariesBody>,\n res: ResponseWithInformation<PushDictionariesResult>,\n _next: NextFunction\n): Promise<void> => {\n const { project, user, dictionaryRights } = res.locals;\n const dictionaryData = req.body.dictionaries;\n const dictionariesKeys = dictionaryData.map((dictionary) => dictionary.key);\n\n if (\n typeof dictionaryData === 'object' &&\n Array.isArray(dictionaryData) &&\n dictionaryData.length === 0\n ) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARIES_NOT_PROVIDED');\n return;\n } else if (!dictionaryData) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_DATA_NOT_FOUND');\n return;\n }\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n\n if (!user) {\n ErrorHandler.handleGenericErrorResponse(res, 'USER_NOT_DEFINED');\n return;\n }\n\n if (!dictionaryRights?.write) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_WRITE');\n return;\n }\n\n if (!dictionaryRights?.admin) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_ADMIN');\n return;\n }\n\n try {\n const { existingDictionariesKey, newDictionariesKey } =\n await dictionaryService.getExistingDictionaryKey(\n dictionariesKeys,\n project._id\n );\n\n const existingDictionaries = dictionaryData.filter((dictionary) =>\n existingDictionariesKey.includes(dictionary.key)\n );\n const newDictionaries = dictionaryData.filter((dictionary) =>\n newDictionariesKey.includes(dictionary.key)\n );\n\n const newDictionariesResult: DictionaryAPI[] = [];\n const updatedDictionariesResult: DictionaryAPI[] = [];\n const errorResult: PushDictionariesResultData['error'] = [];\n\n for (const dictionaryDataEl of newDictionaries) {\n const dictionary: DictionaryData = {\n title: dictionaryDataEl.title,\n description: dictionaryDataEl.description,\n projectIds: [String(project._id)],\n creatorId: user._id,\n content: new Map([\n ['v1', { content: dictionaryDataEl.content ?? ({} as ContentNode) }],\n ]),\n filePath: {\n [String(project._id)]: dictionaryDataEl.filePath ?? '',\n },\n key: dictionaryDataEl.key,\n };\n\n try {\n const newDictionary =\n await dictionaryService.createDictionary(dictionary);\n newDictionariesResult.push(\n mapDictionaryToAPI(newDictionary, project._id)\n );\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n }\n\n if (existingDictionariesKey.length >= 0) {\n const existingDictionariesDB =\n await dictionaryService.getDictionariesByKeys(\n existingDictionariesKey,\n project._id\n );\n\n for (const dictionaryDataEl of existingDictionaries) {\n const existingDictionaryDB = existingDictionariesDB.find(\n (dictionaryDB) => dictionaryDB.key === dictionaryDataEl.key\n )!;\n\n const versionList = [...(existingDictionaryDB.content.keys() ?? [])];\n const lastVersion = versionList[versionList.length - 1];\n\n const lastContent =\n (existingDictionaryDB.content.get(lastVersion)\n ?.content as DictionaryAPI['content']) ?? null;\n\n const isSameContent =\n JSON.stringify(lastContent) ===\n JSON.stringify(dictionaryDataEl.content);\n\n let newContent: VersionedContent = existingDictionaryDB.content;\n\n if (!isSameContent) {\n const newContentVersion =\n dictionaryService.incrementVersion(existingDictionaryDB);\n\n newContent = {\n ...newContent,\n [newContentVersion]: {\n content: dictionaryDataEl.content,\n },\n };\n }\n\n const dictionary: DictionaryData = {\n ...ensureMongoDocumentToObject(existingDictionaryDB),\n ...dictionaryDataEl,\n content: newContent,\n projectIds: [String(project._id)],\n creatorId: user._id,\n filePath: {\n [String(project._id)]: dictionaryDataEl.filePath ?? '',\n },\n key: dictionaryDataEl.key,\n };\n\n try {\n const updatedDictionary =\n await dictionaryService.updateDictionaryByKey(\n dictionaryDataEl.key,\n dictionary,\n project._id\n );\n updatedDictionariesResult.push(\n mapDictionaryToAPI(updatedDictionary, project._id)\n );\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n }\n }\n\n const result: PushDictionariesResultData = {\n newDictionaries: newDictionariesResult.map(\n (dictionary) => dictionary.key\n ),\n updatedDictionaries: updatedDictionariesResult.map(\n (dictionary) => dictionary.key\n ),\n error: errorResult,\n };\n\n const responseData = formatResponse<PushDictionariesResultData>({\n message: t({\n en: 'Dictionaries updated successfully',\n fr: 'Dictionnaires mis à jour avec succès',\n es: 'Diccionarios actualizados con éxito',\n }),\n description: t({\n en: 'Your dictionaries have been updated successfully',\n fr: 'Vos dictionnaires ont été mis à jour avec succès',\n es: 'Sus diccionarios han sido actualizados con éxito',\n }),\n data: result,\n });\n\n eventListener.sendDictionaryUpdate([\n ...newDictionariesResult.map(\n (dictionary) =>\n ({\n dictionary,\n status: 'ADDED',\n }) as eventListener.SendDictionaryUpdateArg\n ),\n ...updatedDictionariesResult.map(\n (dictionary) =>\n ({\n dictionary,\n status: 'UPDATED',\n }) as eventListener.SendDictionaryUpdateArg\n ),\n ]);\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type UpdateDictionaryParam = { dictionaryId: string };\nexport type UpdateDictionaryBody = Partial<Dictionary>;\nexport type UpdateDictionaryResult = ResponseData<DictionaryAPI>;\n\n/**\n * Updates an existing dictionary in the database.\n */\nexport const updateDictionary = async (\n req: Request<UpdateDictionaryParam, any, UpdateDictionaryBody>,\n res: ResponseWithInformation<UpdateDictionaryResult>,\n _next: NextFunction\n): Promise<void> => {\n const { dictionaryId } = req.params;\n const { project, dictionaryRights } = res.locals;\n const dictionaryData = req.body;\n\n if (!dictionaryData) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_DATA_NOT_FOUND');\n return;\n }\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n\n if (!dictionaryData.projectIds?.includes(String(project._id))) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_PROJECT_MISMATCH');\n return;\n }\n\n if (typeof dictionaryId === 'undefined') {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_ID_NOT_FOUND');\n return;\n }\n\n if (!dictionaryRights?.write) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_WRITE');\n return;\n }\n\n try {\n const updatedDictionary = await dictionaryService.updateDictionaryById(\n dictionaryId,\n dictionaryData\n );\n\n const apiResult = mapDictionaryToAPI(updatedDictionary, project._id);\n\n const responseData = formatResponse<DictionaryAPI>({\n message: t({\n en: 'Dictionary updated successfully',\n fr: 'Dictionnaire mis à jour avec succès',\n es: 'Diccionario actualizado con éxito',\n }),\n description: t({\n en: 'Your dictionary has been updated successfully',\n fr: 'Votre dictionnaire a été mis à jour avec succès',\n es: 'Su diccionario ha sido actualizado con éxito',\n }),\n data: apiResult,\n });\n\n eventListener.sendDictionaryUpdate([\n {\n dictionary: apiResult,\n status: 'UPDATED',\n },\n ]);\n\n res.json(responseData);\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n\nexport type DeleteDictionaryParam = { dictionaryId: string };\nexport type DeleteDictionaryResult = ResponseData<DictionaryAPI>;\n\n/**\n * Deletes a dictionary from the database by its ID.\n */\nexport const deleteDictionary = async (\n req: Request<DeleteDictionaryParam>,\n res: ResponseWithInformation<DeleteDictionaryResult>,\n _next: NextFunction\n): Promise<void> => {\n const { project, dictionaryRights } = res.locals;\n const { dictionaryId } = req.params as Partial<DeleteDictionaryParam>;\n\n if (!dictionaryId) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_ID_NOT_FOUND');\n return;\n }\n\n if (!project) {\n ErrorHandler.handleGenericErrorResponse(res, 'PROJECT_NOT_DEFINED');\n return;\n }\n\n if (!dictionaryRights?.admin) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_RIGHTS_NOT_ADMIN');\n return;\n }\n\n try {\n const dictionaryToDelete =\n await dictionaryService.getDictionaryById(dictionaryId);\n\n if (!dictionaryToDelete.projectIds.includes(project._id)) {\n ErrorHandler.handleGenericErrorResponse(\n res,\n 'DICTIONARY_PROJECT_MISMATCH'\n );\n return;\n }\n\n const deletedDictionary =\n await dictionaryService.deleteDictionaryById(dictionaryId);\n\n if (!deletedDictionary) {\n ErrorHandler.handleGenericErrorResponse(res, 'DICTIONARY_NOT_FOUND', {\n dictionaryId,\n });\n return;\n }\n\n logger.info(`Dictionary deleted: ${String(deletedDictionary._id)}`);\n\n const apiResult = mapDictionaryToAPI(deletedDictionary, project._id);\n\n const responseData = formatResponse<DictionaryAPI>({\n message: t({\n en: 'Dictionary deleted successfully',\n fr: 'Dictionnaire supprimé avec succès',\n es: 'Diccionario eliminado con éxito',\n }),\n description: t({\n en: 'Your dictionary has been deleted successfully',\n fr: 'Votre dictionnaire a été supprimé avec succès',\n es: 'Su diccionario ha sido eliminado con éxito',\n }),\n data: apiResult,\n });\n\n res.json(responseData);\n\n eventListener.sendDictionaryUpdate([\n {\n dictionary: apiResult,\n status: 'DELETED',\n },\n ]);\n\n return;\n } catch (error) {\n ErrorHandler.handleAppErrorResponse(res, error as AppError);\n return;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAAuB;AAEvB,wBAAmC;AACnC,yCAA4C;AAC5C,oBAA4C;AAC5C,+CAGO;AAEP,wBAAmC;AACnC,0BAKO;AAEP,8BAAkB;AAClB,oBAA+B;AAgBxB,MAAM,kBAAkB,OAC7B,KACA,KACA,UACkB;AAClB,QAAM,EAAE,MAAM,SAAS,iBAAiB,IAAI,IAAI;AAChD,QAA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AAa,uBAAuB,KAAK,KAAiB;AAC1D;AAAA,EACF;AACF;","names":[]}
@@ -42,6 +42,7 @@ module.exports = __toCommonJS(organization_controller_exports);
42
42
  var import_logger = require('./../logger/index.cjs');
43
43
  var import_sessionAuth = require('./../routes/sessionAuth.routes.cjs');
44
44
  var import_email = require('./../services/email.service.cjs');
45
+ var projectService = __toESM(require('./../services/project.service.cjs'), 1);
45
46
  var sessionAuthService = __toESM(require('./../services/sessionAuth.service.cjs'), 1);
46
47
  var userService = __toESM(require('./../services/user.service.cjs'), 1);
47
48
  var import_errors = require('./../utils/errors/index.cjs');
@@ -49,6 +50,7 @@ var import_getOrganizationFiltersAndPagination = require('./../utils/filtersAndP
49
50
  var import_plan = require('./../utils/plan.cjs');
50
51
  var import_responseData = require('./../utils/responseData.cjs');
51
52
  var import_express_intlayer = require("express-intlayer");
53
+ var import_stripe = require("stripe");
52
54
  var organizationService = __toESM(require('./../services/organization.service.cjs'), 1);
53
55
  const getOrganizations = async (req, res, _next) => {
54
56
  const { user, organizationRights } = res.locals;
@@ -255,7 +257,7 @@ const addOrganizationMember = async (req, res, _next) => {
255
257
  invitedByUsername: user.name,
256
258
  invitedByEmail: user.email,
257
259
  organizationName: organization.name,
258
- inviteLink: import_sessionAuth.sessionAuthRoutes.loginEmailPassword.url,
260
+ inviteLink: (0, import_sessionAuth.getSessionAuthRoutes)().loginEmailPassword.url,
259
261
  inviteFromIp: req.ip ?? "",
260
262
  inviteFromLocation: req.hostname
261
263
  });
@@ -366,6 +368,7 @@ const updateOrganizationMembers = async (req, res, _next) => {
366
368
  }
367
369
  };
368
370
  const deleteOrganization = async (_req, res, _next) => {
371
+ const stripe = new import_stripe.Stripe(process.env.STRIPE_SECRET_KEY);
369
372
  const { isOrganizationAdmin, organization, organizationRights } = res.locals;
370
373
  if (!organization) {
371
374
  import_errors.ErrorHandler.handleGenericErrorResponse(res, "ORGANIZATION_NOT_DEFINED");
@@ -385,7 +388,19 @@ const deleteOrganization = async (_req, res, _next) => {
385
388
  );
386
389
  return;
387
390
  }
391
+ const projects = await projectService.findProjects({
392
+ organizationId: organization._id
393
+ });
394
+ if (projects.length > 0) {
395
+ import_errors.ErrorHandler.handleGenericErrorResponse(res, "PROJECTS_EXIST", {
396
+ organizationId: organization._id
397
+ });
398
+ return;
399
+ }
388
400
  try {
401
+ if (organization.plan?.subscriptionId) {
402
+ await stripe.subscriptions.cancel(organization.plan.subscriptionId);
403
+ }
389
404
  const deletedOrganization = await organizationService.deleteOrganizationById(organization._id);
390
405
  if (!deletedOrganization) {
391
406
  import_errors.ErrorHandler.handleGenericErrorResponse(res, "ORGANIZATION_NOT_FOUND", {
@@ -407,6 +422,7 @@ const deleteOrganization = async (_req, res, _next) => {
407
422
  }),
408
423
  data: deletedOrganization
409
424
  });
425
+ sessionAuthService.clearOrganizationAuth(res);
410
426
  res.json(responseData);
411
427
  return;
412
428
  } catch (error) {