@autofleet/sadot 1.6.18-beta.13 → 1.6.18-beta.16

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.
@@ -1,12 +1,48 @@
1
1
  const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
2
2
  const require_index = require('../../../utils/logger/index.cjs');
3
3
  const require_errors = require('../errors.cjs');
4
+ const require_mat_path_state = require('../../../mat-path-state.cjs');
5
+ const require_FieldPolicy = require('../../../models/FieldPolicy.cjs');
4
6
  const require_field_policy = require('../../../repository/field-policy.cjs');
5
7
  let sequelize = require("sequelize");
6
8
  let _autofleet_node_common = require("@autofleet/node-common");
7
9
 
8
10
  //#region src/api/v1/field-policies/index.ts
9
- const router = (0, _autofleet_node_common.Router)({ logger: require_index.default });
11
+ const router = (0, _autofleet_node_common.Router)({
12
+ logger: require_index.default,
13
+ mergeParams: true
14
+ });
15
+ const tableName = require_FieldPolicy.default.getTableName();
16
+ router.get("/effective", async (req, res) => {
17
+ const EFFECTIVE_POLICY_QUERY = `
18
+ SELECT fp.*
19
+ FROM "${tableName}" fp
20
+ LEFT JOIN context_entity_path cep ON cep.deleted_at IS NULL
21
+ AND cep.entity_id = fp.context_id
22
+ AND cep.entity_type = 'context'
23
+ AND cep.path @> ANY(current_setting('mat_path.paths', true)::ltree[])
24
+ WHERE fp.entity_type = $1
25
+ ORDER BY COALESCE(nlevel(cep.path), 0) DESC
26
+ LIMIT 1
27
+ `;
28
+ const { withMatPath } = require_mat_path_state.matPathState;
29
+ if (!withMatPath) return res.status(501).json({ error: "Effective field policy resolution is not configured" });
30
+ if (!req.headers["x-request-paths"]) return res.status(400).json({ error: "x-request-paths header is required" });
31
+ const entityType = req.params.entityType;
32
+ try {
33
+ const [policy] = await withMatPath(tableName, (transaction) => require_FieldPolicy.default.sequelize.query(EFFECTIVE_POLICY_QUERY, {
34
+ bind: [entityType],
35
+ type: sequelize.QueryTypes.SELECT,
36
+ transaction
37
+ }));
38
+ return res.status(200).json(policy ?? null);
39
+ } catch (err) {
40
+ return require_errors.default(err, res, {
41
+ logger: require_index.default,
42
+ message: "Error resolving effective field policy"
43
+ });
44
+ }
45
+ });
10
46
  router.get("/", async (_req, res) => {
11
47
  try {
12
48
  const policies = await require_field_policy.findAll();
@@ -1,11 +1,47 @@
1
1
  import logger_default from "../../../utils/logger/index.js";
2
2
  import errors_default from "../errors.js";
3
+ import { matPathState } from "../../../mat-path-state.js";
4
+ import FieldPolicy_default from "../../../models/FieldPolicy.js";
3
5
  import { create, findAll, remove, update } from "../../../repository/field-policy.js";
4
- import { UniqueConstraintError } from "sequelize";
6
+ import { QueryTypes, UniqueConstraintError } from "sequelize";
5
7
  import { Router } from "@autofleet/node-common";
6
8
 
7
9
  //#region src/api/v1/field-policies/index.ts
8
- const router = Router({ logger: logger_default });
10
+ const router = Router({
11
+ logger: logger_default,
12
+ mergeParams: true
13
+ });
14
+ const tableName = FieldPolicy_default.getTableName();
15
+ router.get("/effective", async (req, res) => {
16
+ const EFFECTIVE_POLICY_QUERY = `
17
+ SELECT fp.*
18
+ FROM "${tableName}" fp
19
+ LEFT JOIN context_entity_path cep ON cep.deleted_at IS NULL
20
+ AND cep.entity_id = fp.context_id
21
+ AND cep.entity_type = 'context'
22
+ AND cep.path @> ANY(current_setting('mat_path.paths', true)::ltree[])
23
+ WHERE fp.entity_type = $1
24
+ ORDER BY COALESCE(nlevel(cep.path), 0) DESC
25
+ LIMIT 1
26
+ `;
27
+ const { withMatPath } = matPathState;
28
+ if (!withMatPath) return res.status(501).json({ error: "Effective field policy resolution is not configured" });
29
+ if (!req.headers["x-request-paths"]) return res.status(400).json({ error: "x-request-paths header is required" });
30
+ const entityType = req.params.entityType;
31
+ try {
32
+ const [policy] = await withMatPath(tableName, (transaction) => FieldPolicy_default.sequelize.query(EFFECTIVE_POLICY_QUERY, {
33
+ bind: [entityType],
34
+ type: QueryTypes.SELECT,
35
+ transaction
36
+ }));
37
+ return res.status(200).json(policy ?? null);
38
+ } catch (err) {
39
+ return errors_default(err, res, {
40
+ logger: logger_default,
41
+ message: "Error resolving effective field policy"
42
+ });
43
+ }
44
+ });
9
45
  router.get("/", async (_req, res) => {
10
46
  try {
11
47
  const policies = await findAll();
@@ -9,7 +9,7 @@ let _autofleet_node_common = require("@autofleet/node-common");
9
9
  const router = (0, _autofleet_node_common.Router)({ logger: require_index.default });
10
10
  router.use("/custom-field-definitions/:modelName", require_index$1.default);
11
11
  router.use("/custom-validators/:modelName", require_index$2.default);
12
- router.use("/field-policies/:modelName", require_index$3.default);
12
+ router.use("/field-policies/:entityType", require_index$3.default);
13
13
  var v1_default = router;
14
14
 
15
15
  //#endregion
@@ -8,7 +8,7 @@ import { Router } from "@autofleet/node-common";
8
8
  const router = Router({ logger: logger_default });
9
9
  router.use("/custom-field-definitions/:modelName", definition_default);
10
10
  router.use("/custom-validators/:modelName", validator_default);
11
- router.use("/field-policies/:modelName", field_policies_default);
11
+ router.use("/field-policies/:entityType", field_policies_default);
12
12
  var v1_default = router;
13
13
 
14
14
  //#endregion
package/dist/index.cjs CHANGED
@@ -26,8 +26,9 @@ const sadotInitState = new require_init_state.SadotInitializationState();
26
26
  */
27
27
  const internalUseCustomFields = async (app, getModel, options) => {
28
28
  require_index.tryAddingTraceIdMiddleware();
29
- const { models, useCustomFieldsEntries, useModelTypeMapping, isMatPathEnabled, runLegacyMigrations } = options;
29
+ const { models, useCustomFieldsEntries, useModelTypeMapping, isMatPathEnabled, runLegacyMigrations, withMatPath } = options;
30
30
  require_mat_path_state.matPathState.isMatPathEnabled = isMatPathEnabled;
31
+ require_mat_path_state.matPathState.withMatPath = withMatPath;
31
32
  if (app) app.use("/api", require_index$4.default);
32
33
  const sequelize = options.sequelize ?? require_index$5.default(options.databaseConfig);
33
34
  if (process.env.NODE_ENV === "test") await require_index$3.initTestModels(sequelize);
package/dist/index.js CHANGED
@@ -25,8 +25,9 @@ const sadotInitState = new SadotInitializationState();
25
25
  */
26
26
  const internalUseCustomFields = async (app, getModel, options) => {
27
27
  tryAddingTraceIdMiddleware();
28
- const { models, useCustomFieldsEntries, useModelTypeMapping, isMatPathEnabled, runLegacyMigrations } = options;
28
+ const { models, useCustomFieldsEntries, useModelTypeMapping, isMatPathEnabled, runLegacyMigrations, withMatPath } = options;
29
29
  matPathState.isMatPathEnabled = isMatPathEnabled;
30
+ matPathState.withMatPath = withMatPath;
30
31
  if (app) app.use("/api", api_default);
31
32
  const sequelize = options.sequelize ?? db_default(options.databaseConfig);
32
33
  if (process.env.NODE_ENV === "test") await initTestModels(sequelize);
@@ -64,6 +64,12 @@ interface CustomFieldOptions {
64
64
  * Pass `matPathManager.isScopingRequest` if using `@autofleet/mat-path-mvp`.
65
65
  */
66
66
  isMatPathEnabled?: () => boolean;
67
+ /**
68
+ * Optional hook for the effective field-policy route. When provided, sadot mounts
69
+ * GET /field-policies/:entityType/effective and delegates scoped queries through this callback.
70
+ * Typically: (table, cb) => { matPathManager.setScopingMode({ [table]: SCOPING_MODE.ANCESTORS }); return matPathManager.withPaths(cb); }
71
+ */
72
+ withMatPath?: (tableName: string, callback: (transaction: any) => Promise<any>) => Promise<any>;
67
73
  }
68
74
  //#endregion
69
75
  export { CustomFieldOptions, ModelFetcher, Models };
@@ -65,6 +65,12 @@ interface CustomFieldOptions {
65
65
  * Pass `matPathManager.isScopingRequest` if using `@autofleet/mat-path-mvp`.
66
66
  */
67
67
  isMatPathEnabled?: () => boolean;
68
+ /**
69
+ * Optional hook for the effective field-policy route. When provided, sadot mounts
70
+ * GET /field-policies/:entityType/effective and delegates scoped queries through this callback.
71
+ * Typically: (table, cb) => { matPathManager.setScopingMode({ [table]: SCOPING_MODE.ANCESTORS }); return matPathManager.withPaths(cb); }
72
+ */
73
+ withMatPath?: (tableName: string, callback: (transaction: any) => Promise<any>) => Promise<any>;
68
74
  }
69
75
  //#endregion
70
76
  export { CustomFieldOptions, ModelFetcher, Models };
@@ -1,4 +1,4 @@
1
- import "sequelize";
1
+ import { QueryTypes } from "sequelize";
2
2
  import { Sequelize } from "sequelize-typescript";
3
3
 
4
4
  //#region src/utils/db/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "1.6.18-beta.13",
3
+ "version": "1.6.18-beta.16",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -53,10 +53,10 @@
53
53
  "express": "^4.21.2",
54
54
  "npm-watch": "^0.11.0",
55
55
  "supertest": "^7.0.0",
56
- "@autofleet/node-common": "^4.3.22",
57
- "@autofleet/logger": "^4.7.0",
58
56
  "@autofleet/errors": "^3.3.7",
59
- "@autofleet/zehut": "^4.13.4"
57
+ "@autofleet/zehut": "^4.13.4",
58
+ "@autofleet/node-common": "^4.3.22",
59
+ "@autofleet/logger": "^4.7.0"
60
60
  },
61
61
  "peerDependencies": {
62
62
  "@autofleet/errors": "^3",