flexbiz-server 12.3.45 → 12.3.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "flexbiz-server",
3
3
  "main": "./server/app.js",
4
4
  "description": "Flexible Server",
5
- "version": "12.3.45",
5
+ "version": "12.3.46",
6
6
  "author": {
7
7
  "name": "Van Truong Pham",
8
8
  "email": "invncur@gmail.com"
@@ -1,3 +1,81 @@
1
- 'use strict';const {AsyncLocalStorage}=require("node:async_hooks"),storage=new AsyncLocalStorage,SESSION_EXCLUDE_MODELS="log token otp cache notification approve email tontucthoi".split(" ");exports.runWithSession=async function($session$$,$fn$$){return storage.run({session:$session$$},$fn$$)};
2
- exports.onAfterCommit=function($cb$$){const $store$$=storage.getStore();$store$$&&Array.isArray($store$$.afterCommit)?$store$$.afterCommit.push($cb$$):Promise.resolve().then($cb$$).catch($err$$=>console.error("[onAfterCommit immediate error]",$err$$))};exports.runWithoutSession=async function($fn$$){return storage.run({session:void 0},$fn$$)};exports.getCurrentSession=function(){const $store$$=storage.getStore();return $store$$?$store$$.session:void 0};
3
- exports.isModelExcludedFromSession=function($model_name$$){$model_name$$="string"===typeof $model_name$$?$model_name$$:$model_name$$?.modelName;return!(!$model_name$$||!SESSION_EXCLUDE_MODELS.includes($model_name$$))};exports.isSessionActive=function($session$$){if(!$session$$)return!1;try{return $session$$.inTransaction()}catch{return!1}};console.log("[sessionContext] \u2705 Loaded with model exclude & safety check");
1
+ // sessionContext.js
2
+ const { AsyncLocalStorage } = require("node:async_hooks");
3
+
4
+ const storage = new AsyncLocalStorage();
5
+
6
+ /**
7
+ * Danh sách các model KHÔNG bao giờ được gắn session
8
+ * (ví dụ: các model ghi log, lịch sử, audit,...)
9
+ */
10
+ const SESSION_EXCLUDE_MODELS = ["log","token","otp","cache", "notification","approve","email",'tontucthoi','listinfo','reportinfo','labelinfo'];
11
+
12
+ /**
13
+ * Chạy một hàm trong context có session.
14
+ * Mọi câu lệnh Mongoose trong hàm này sẽ được gắn cùng session đó.
15
+ * @param {ClientSession} session - mongoose session
16
+ * @param {Function} fn - async function cần chạy
17
+ */
18
+ exports.runWithSession = async function (session, fn) {
19
+ return storage.run({ session }, fn);
20
+ };
21
+
22
+ /**
23
+ * Thêm callback để chạy sau khi commit.
24
+ * - Nếu không có session => chạy ngay lập tức.
25
+ */
26
+ exports.onAfterCommit = function (cb) {
27
+ const store = storage.getStore();
28
+ if (store && Array.isArray(store.afterCommit)) {
29
+ store.afterCommit.push(cb);
30
+ } else {
31
+ // Không có session → chạy luôn
32
+ Promise.resolve()
33
+ .then(cb)
34
+ .catch((err) => console.error("[onAfterCommit immediate error]", err));
35
+ }
36
+ };
37
+
38
+ /**
39
+ * Chạy một hàm mà KHÔNG gắn session (dù đang trong transaction).
40
+ * Dùng cho các thao tác như ghi log, gửi email, lưu lịch sử...
41
+ * @param {Function} fn - async function cần chạy
42
+ */
43
+ exports.runWithoutSession = async function (fn) {
44
+ return storage.run({ session: undefined }, fn);
45
+ };
46
+
47
+ /**
48
+ * Lấy session hiện tại trong context.
49
+ * @returns {ClientSession|undefined}
50
+ */
51
+ exports.getCurrentSession = function () {
52
+ const store = storage.getStore();
53
+ return store ? store.session : undefined;
54
+ };
55
+
56
+ /**
57
+ * Kiểm tra xem model có được loại trừ khỏi session tự động không.
58
+ * @param {String|Model} model - model name hoặc đối tượng model
59
+ * @returns {Boolean}
60
+ */
61
+ exports.isModelExcludedFromSession = function (model) {
62
+ const name = typeof model === "string" ? model : model?.modelName;
63
+ return !!(name && SESSION_EXCLUDE_MODELS.includes(name));
64
+ };
65
+
66
+ /**
67
+ * Kiểm tra session còn hợp lệ (chưa commit hoặc abort)
68
+ * @param {ClientSession} session
69
+ * @returns {Boolean}
70
+ */
71
+ exports.isSessionActive = function (session) {
72
+ if (!session) return false;
73
+ try {
74
+ // Một số driver Mongoose không expose state, nên ta kiểm tra gián tiếp:
75
+ return session.inTransaction(); // true nếu đang trong transaction
76
+ } catch {
77
+ return false;
78
+ }
79
+ };
80
+
81
+ console.log("[sessionContext] ✅ Loaded with model exclude & safety check");