@autofleet/sadot 1.6.18-beta.20 → 1.6.18-beta.22

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,7 +1,6 @@
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
4
  const require_field_policy = require('../../../repository/field-policy.cjs');
6
5
  let sequelize = require("sequelize");
7
6
  let _autofleet_node_common = require("@autofleet/node-common");
@@ -12,18 +11,7 @@ const router = (0, _autofleet_node_common.Router)({
12
11
  mergeParams: true
13
12
  });
14
13
  router.get("/effective", async (req, res) => {
15
- const { withMatPath } = require_mat_path_state.matPathState;
16
- if (!withMatPath) return res.status(501).json({ error: "Effective field policy resolution is not configured" });
17
- const entityType = req.params.entityType;
18
- try {
19
- const [policy] = await require_field_policy.findEffectivePolicy(entityType);
20
- return res.status(200).json(policy ?? null);
21
- } catch (err) {
22
- return require_errors.default(err, res, {
23
- logger: require_index.default,
24
- message: "Error resolving effective field policy"
25
- });
26
- }
14
+ return res.status(200);
27
15
  });
28
16
  router.get("/", async (_req, res) => {
29
17
  try {
@@ -1,7 +1,6 @@
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 { create, findAll, findEffectivePolicy, remove, update } from "../../../repository/field-policy.js";
3
+ import { create, findAll, remove, update } from "../../../repository/field-policy.js";
5
4
  import { UniqueConstraintError } from "sequelize";
6
5
  import { Router } from "@autofleet/node-common";
7
6
 
@@ -11,18 +10,7 @@ const router = Router({
11
10
  mergeParams: true
12
11
  });
13
12
  router.get("/effective", async (req, res) => {
14
- const { withMatPath } = matPathState;
15
- if (!withMatPath) return res.status(501).json({ error: "Effective field policy resolution is not configured" });
16
- const entityType = req.params.entityType;
17
- try {
18
- const [policy] = await findEffectivePolicy(entityType);
19
- return res.status(200).json(policy ?? null);
20
- } catch (err) {
21
- return errors_default(err, res, {
22
- logger: logger_default,
23
- message: "Error resolving effective field policy"
24
- });
25
- }
13
+ return res.status(200);
26
14
  });
27
15
  router.get("/", async (_req, res) => {
28
16
  try {
@@ -57,7 +57,20 @@ router.get("/", async (req, res) => {
57
57
  /**
58
58
  * Effective
59
59
  */
60
- router.get("/effective", (_req, res) => res.status(http_status_codes.StatusCodes.OK).json({ ok: true }));
60
+ router.get("/effective", async (req, res) => {
61
+ const { withMatPath } = require_mat_path_state.matPathState;
62
+ if (!withMatPath) return res.status(http_status_codes.StatusCodes.NOT_IMPLEMENTED).json({ error: "Effective validator resolution is not configured" });
63
+ try {
64
+ const { modelName } = req.params;
65
+ const [validator] = await require_validator.findEffective(modelName, "AF.1");
66
+ return res.status(http_status_codes.StatusCodes.OK).json(validator ?? null);
67
+ } catch (err) {
68
+ return require_errors.default(err, res, {
69
+ logger: require_index.default,
70
+ message: `Error resolving effective ${ENTITY}`
71
+ });
72
+ }
73
+ });
61
74
  /**
62
75
  * Get by id
63
76
  */
@@ -1,7 +1,7 @@
1
1
  import logger_default from "../../../utils/logger/index.js";
2
2
  import errors_default from "../errors.js";
3
3
  import { matPathState } from "../../../mat-path-state.js";
4
- import { create, disable, findAll, update } from "../../../repository/validator.js";
4
+ import { create, disable, findAll, findEffective, update } from "../../../repository/validator.js";
5
5
  import validations_default from "./validations.js";
6
6
  import { validateValidatorSchema } from "../../../utils/validations/schema/validator-schema.js";
7
7
  import { ResourceNotFoundError } from "@autofleet/errors";
@@ -56,7 +56,20 @@ router.get("/", async (req, res) => {
56
56
  /**
57
57
  * Effective
58
58
  */
59
- router.get("/effective", (_req, res) => res.status(StatusCodes.OK).json({ ok: true }));
59
+ router.get("/effective", async (req, res) => {
60
+ const { withMatPath } = matPathState;
61
+ if (!withMatPath) return res.status(StatusCodes.NOT_IMPLEMENTED).json({ error: "Effective validator resolution is not configured" });
62
+ try {
63
+ const { modelName } = req.params;
64
+ const [validator] = await findEffective(modelName, "AF.1");
65
+ return res.status(StatusCodes.OK).json(validator ?? null);
66
+ } catch (err) {
67
+ return errors_default(err, res, {
68
+ logger: logger_default,
69
+ message: `Error resolving effective ${ENTITY}`
70
+ });
71
+ }
72
+ });
60
73
  /**
61
74
  * Get by id
62
75
  */
@@ -1,7 +1,5 @@
1
1
  const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
- const require_mat_path_state = require('../mat-path-state.cjs');
3
2
  const require_FieldPolicy = require('../models/FieldPolicy.cjs');
4
- let sequelize = require("sequelize");
5
3
 
6
4
  //#region src/repository/field-policy.ts
7
5
  var field_policy_exports = /* @__PURE__ */ require_rolldown_runtime.__export({
@@ -11,7 +9,6 @@ var field_policy_exports = /* @__PURE__ */ require_rolldown_runtime.__export({
11
9
  findAllByContextIds: () => findAllByContextIds,
12
10
  findByContextAndName: () => findByContextAndName,
13
11
  findById: () => findById,
14
- findEffectivePolicy: () => findEffectivePolicy,
15
12
  remove: () => remove,
16
13
  update: () => update
17
14
  });
@@ -36,24 +33,6 @@ const update = async (id, data) => {
36
33
  return policy.update(data);
37
34
  };
38
35
  const remove = (id) => require_FieldPolicy.default.destroy({ where: { id } });
39
- const findEffectivePolicy = (entityType) => {
40
- const tableName = require_FieldPolicy.default.getTableName();
41
- const EFFECTIVE_POLICY_QUERY = `
42
- SELECT fp.*
43
- FROM "${tableName}" fp
44
- LEFT JOIN context_entity_path cep ON cep.deleted_at IS NULL
45
- AND cep.entity_id = fp.context_id
46
- AND cep.entity_type = 'context'
47
- AND cep.path @> ANY(current_setting('mat_path.paths', true)::ltree[])
48
- WHERE fp.entity_type = $1
49
- ORDER BY COALESCE(nlevel(cep.path), 0) DESC
50
- LIMIT 1`;
51
- return require_mat_path_state.matPathState.withMatPath(tableName, (transaction) => require_FieldPolicy.default.sequelize.query(EFFECTIVE_POLICY_QUERY, {
52
- bind: [entityType],
53
- type: sequelize.QueryTypes.SELECT,
54
- transaction
55
- }));
56
- };
57
36
 
58
37
  //#endregion
59
38
  exports.create = create;
@@ -64,6 +43,5 @@ Object.defineProperty(exports, 'field_policy_exports', {
64
43
  }
65
44
  });
66
45
  exports.findAll = findAll;
67
- exports.findEffectivePolicy = findEffectivePolicy;
68
46
  exports.remove = remove;
69
47
  exports.update = update;
@@ -2,7 +2,7 @@ import { EntityType, FieldPolicy, ModelInclusion } from "../models/FieldPolicy.c
2
2
 
3
3
  //#region src/repository/field-policy.d.ts
4
4
  declare namespace field_policy_d_exports {
5
- export { CreateFieldPolicyData, UpdateFieldPolicyData, create, findAll, findAllByContext, findAllByContextIds, findByContextAndName, findById, findEffectivePolicy, remove, update };
5
+ export { CreateFieldPolicyData, UpdateFieldPolicyData, create, findAll, findAllByContext, findAllByContextIds, findByContextAndName, findById, remove, update };
6
6
  }
7
7
  interface CreateFieldPolicyData {
8
8
  contextId: string;
@@ -30,6 +30,5 @@ declare const create: ({
30
30
  }: CreateFieldPolicyData) => Promise<FieldPolicy>;
31
31
  declare const update: (id: string, data: UpdateFieldPolicyData) => Promise<FieldPolicy | null>;
32
32
  declare const remove: (id: string) => Promise<number>;
33
- declare const findEffectivePolicy: (entityType: string) => Promise<Record<string, unknown>[]>;
34
33
  //#endregion
35
34
  export { field_policy_d_exports };
@@ -2,7 +2,7 @@ import { EntityType, FieldPolicy, ModelInclusion } from "../models/FieldPolicy.j
2
2
 
3
3
  //#region src/repository/field-policy.d.ts
4
4
  declare namespace field_policy_d_exports {
5
- export { CreateFieldPolicyData, UpdateFieldPolicyData, create, findAll, findAllByContext, findAllByContextIds, findByContextAndName, findById, findEffectivePolicy, remove, update };
5
+ export { CreateFieldPolicyData, UpdateFieldPolicyData, create, findAll, findAllByContext, findAllByContextIds, findByContextAndName, findById, remove, update };
6
6
  }
7
7
  interface CreateFieldPolicyData {
8
8
  contextId: string;
@@ -30,6 +30,5 @@ declare const create: ({
30
30
  }: CreateFieldPolicyData) => Promise<FieldPolicy>;
31
31
  declare const update: (id: string, data: UpdateFieldPolicyData) => Promise<FieldPolicy | null>;
32
32
  declare const remove: (id: string) => Promise<number>;
33
- declare const findEffectivePolicy: (entityType: string) => Promise<Record<string, unknown>[]>;
34
33
  //#endregion
35
34
  export { field_policy_d_exports };
@@ -1,7 +1,5 @@
1
1
  import { __export } from "../_virtual/rolldown_runtime.js";
2
- import { matPathState } from "../mat-path-state.js";
3
2
  import FieldPolicy_default from "../models/FieldPolicy.js";
4
- import { QueryTypes } from "sequelize";
5
3
 
6
4
  //#region src/repository/field-policy.ts
7
5
  var field_policy_exports = /* @__PURE__ */ __export({
@@ -11,7 +9,6 @@ var field_policy_exports = /* @__PURE__ */ __export({
11
9
  findAllByContextIds: () => findAllByContextIds,
12
10
  findByContextAndName: () => findByContextAndName,
13
11
  findById: () => findById,
14
- findEffectivePolicy: () => findEffectivePolicy,
15
12
  remove: () => remove,
16
13
  update: () => update
17
14
  });
@@ -36,24 +33,6 @@ const update = async (id, data) => {
36
33
  return policy.update(data);
37
34
  };
38
35
  const remove = (id) => FieldPolicy_default.destroy({ where: { id } });
39
- const findEffectivePolicy = (entityType) => {
40
- const tableName = FieldPolicy_default.getTableName();
41
- const EFFECTIVE_POLICY_QUERY = `
42
- SELECT fp.*
43
- FROM "${tableName}" fp
44
- LEFT JOIN context_entity_path cep ON cep.deleted_at IS NULL
45
- AND cep.entity_id = fp.context_id
46
- AND cep.entity_type = 'context'
47
- AND cep.path @> ANY(current_setting('mat_path.paths', true)::ltree[])
48
- WHERE fp.entity_type = $1
49
- ORDER BY COALESCE(nlevel(cep.path), 0) DESC
50
- LIMIT 1`;
51
- return matPathState.withMatPath(tableName, (transaction) => FieldPolicy_default.sequelize.query(EFFECTIVE_POLICY_QUERY, {
52
- bind: [entityType],
53
- type: QueryTypes.SELECT,
54
- transaction
55
- }));
56
- };
57
36
 
58
37
  //#endregion
59
- export { create, field_policy_exports, findAll, findEffectivePolicy, remove, update };
38
+ export { create, field_policy_exports, findAll, remove, update };
@@ -1,6 +1,10 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
1
2
  const require_index = require('../utils/logger/index.cjs');
2
3
  const require_CustomValidator = require('../models/CustomValidator.cjs');
3
4
  require('../models/index.cjs');
5
+ let sequelize = require("sequelize");
6
+ let sequelize_lib_sequelize = require("sequelize/lib/sequelize");
7
+ sequelize_lib_sequelize = require_rolldown_runtime.__toESM(sequelize_lib_sequelize);
4
8
 
5
9
  //#region src/repository/validator.ts
6
10
  const create = async (validatorAttributes, options = {}) => {
@@ -49,10 +53,34 @@ const disable = async (id, options) => {
49
53
  require_index.default.debug("custom-validator - disable validator");
50
54
  return update(id, { disabled: true }, options);
51
55
  };
56
+ const findEffective = async (entityType = "Vehicle", requestPath) => {
57
+ const QUERY = `
58
+ SELECT DISTINCT ON (cv.model_type) cv.*, cep.path
59
+ FROM custom_validators cv
60
+ LEFT JOIN context_entity_path cep ON cep.deleted_at IS NULL
61
+ AND cep.entity_id = cv.entity_id
62
+ AND cep.entity_type = cv.entity_type
63
+ AND cep.path @> ANY(ARRAY['${requestPath}']::ltree[])
64
+ WHERE cv.model_type IN ($1) AND cv.disabled = false
65
+ ORDER BY cv.model_type, COALESCE(nlevel(cep.path), 0) DESC;`;
66
+ try {
67
+ await sequelize_lib_sequelize.default.query(QUERY, {
68
+ bind: [entityType],
69
+ type: sequelize.QueryTypes.SELECT
70
+ });
71
+ return {
72
+ ...result.schema.properties.after,
73
+ path: result.path
74
+ };
75
+ } catch (err) {
76
+ throw err;
77
+ }
78
+ };
52
79
 
53
80
  //#endregion
54
81
  exports.create = create;
55
82
  exports.disable = disable;
56
83
  exports.findAll = findAll;
57
84
  exports.findAllByModelType = findAllByModelType;
85
+ exports.findEffective = findEffective;
58
86
  exports.update = update;
@@ -1,6 +1,8 @@
1
1
  import logger_default from "../utils/logger/index.js";
2
2
  import CustomValidator_default from "../models/CustomValidator.js";
3
3
  import "../models/index.js";
4
+ import { QueryTypes } from "sequelize";
5
+ import sequelize from "sequelize/lib/sequelize";
4
6
 
5
7
  //#region src/repository/validator.ts
6
8
  const create = async (validatorAttributes, options = {}) => {
@@ -49,6 +51,29 @@ const disable = async (id, options) => {
49
51
  logger_default.debug("custom-validator - disable validator");
50
52
  return update(id, { disabled: true }, options);
51
53
  };
54
+ const findEffective = async (entityType = "Vehicle", requestPath) => {
55
+ const QUERY = `
56
+ SELECT DISTINCT ON (cv.model_type) cv.*, cep.path
57
+ FROM custom_validators cv
58
+ LEFT JOIN context_entity_path cep ON cep.deleted_at IS NULL
59
+ AND cep.entity_id = cv.entity_id
60
+ AND cep.entity_type = cv.entity_type
61
+ AND cep.path @> ANY(ARRAY['${requestPath}']::ltree[])
62
+ WHERE cv.model_type IN ($1) AND cv.disabled = false
63
+ ORDER BY cv.model_type, COALESCE(nlevel(cep.path), 0) DESC;`;
64
+ try {
65
+ await sequelize.query(QUERY, {
66
+ bind: [entityType],
67
+ type: QueryTypes.SELECT
68
+ });
69
+ return {
70
+ ...result.schema.properties.after,
71
+ path: result.path
72
+ };
73
+ } catch (err) {
74
+ throw err;
75
+ }
76
+ };
52
77
 
53
78
  //#endregion
54
- export { create, disable, findAll, findAllByModelType, update };
79
+ export { create, disable, findAll, findAllByModelType, findEffective, update };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "1.6.18-beta.20",
3
+ "version": "1.6.18-beta.22",
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/errors": "^3.3.7",
56
+ "@autofleet/logger": "^4.7.0",
58
57
  "@autofleet/zehut": "^4.13.4",
59
- "@autofleet/logger": "^4.7.0"
58
+ "@autofleet/errors": "^3.3.7",
59
+ "@autofleet/node-common": "^4.3.22"
60
60
  },
61
61
  "peerDependencies": {
62
62
  "@autofleet/errors": "^3",