@autofleet/sadot 1.6.18-beta.2 → 1.6.18-beta.21

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 (162) hide show
  1. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.cjs +11 -0
  2. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.js +10 -0
  3. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.cjs +8 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.js +7 -0
  5. package/dist/_virtual/rolldown_runtime.cjs +43 -0
  6. package/dist/{chunk-DJulfCLt.js → _virtual/rolldown_runtime.js} +1 -2
  7. package/dist/api/index.cjs +12 -0
  8. package/dist/api/index.js +11 -0
  9. package/dist/api/v1/definition/index.cjs +113 -0
  10. package/dist/api/v1/definition/index.js +112 -0
  11. package/dist/api/v1/definition/validations.cjs +128 -0
  12. package/dist/api/v1/definition/validations.js +125 -0
  13. package/dist/api/v1/errors.cjs +14 -0
  14. package/dist/api/v1/errors.js +13 -0
  15. package/dist/api/v1/field-policies/index.cjs +104 -0
  16. package/dist/api/v1/field-policies/index.js +103 -0
  17. package/dist/api/v1/index.cjs +16 -0
  18. package/dist/api/v1/index.js +15 -0
  19. package/dist/api/v1/validator/index.cjs +132 -0
  20. package/dist/api/v1/validator/index.js +131 -0
  21. package/dist/api/v1/validator/validations.cjs +40 -0
  22. package/dist/api/v1/validator/validations.d.cts +22 -0
  23. package/dist/api/v1/validator/validations.d.ts +22 -0
  24. package/dist/api/v1/validator/validations.js +38 -0
  25. package/dist/errors/index.cjs +62 -0
  26. package/dist/errors/index.js +55 -0
  27. package/dist/events/index.cjs +52 -0
  28. package/dist/events/index.js +50 -0
  29. package/dist/hooks/enrich.cjs +175 -0
  30. package/dist/hooks/enrich.js +175 -0
  31. package/dist/hooks/find.cjs +22 -0
  32. package/dist/hooks/find.js +22 -0
  33. package/dist/hooks/hooks.cjs +310 -0
  34. package/dist/hooks/hooks.js +302 -0
  35. package/dist/hooks/index.cjs +3 -0
  36. package/dist/hooks/index.js +5 -0
  37. package/dist/hooks/utils/updateInstanceValues.cjs +27 -0
  38. package/dist/hooks/utils/updateInstanceValues.js +27 -0
  39. package/dist/index.cjs +48 -33508
  40. package/dist/index.d.cts +13 -396
  41. package/dist/index.d.ts +14 -394
  42. package/dist/index.js +21 -33435
  43. package/dist/init-state.cjs +56 -0
  44. package/dist/init-state.d.cts +35 -0
  45. package/dist/init-state.d.ts +35 -0
  46. package/dist/init-state.js +55 -0
  47. package/dist/mat-path-state.cjs +10 -0
  48. package/dist/mat-path-state.js +9 -0
  49. package/dist/migrations/001-create-core-tables.cjs +70 -0
  50. package/dist/migrations/001-create-core-tables.js +68 -0
  51. package/dist/migrations/002-create-custom-field-entries.cjs +27 -0
  52. package/dist/migrations/002-create-custom-field-entries.js +25 -0
  53. package/dist/migrations/003-create-custom-field-model-type-map.cjs +35 -0
  54. package/dist/migrations/003-create-custom-field-model-type-map.js +33 -0
  55. package/dist/migrations/004-create-field-policy-tables.cjs +29 -0
  56. package/dist/migrations/004-create-field-policy-tables.js +27 -0
  57. package/dist/migrations/005-add-entity-type-to-field-policies.cjs +20 -0
  58. package/dist/migrations/005-add-entity-type-to-field-policies.js +18 -0
  59. package/dist/migrations/index.cjs +94 -0
  60. package/dist/migrations/index.js +95 -0
  61. package/dist/models/CustomFieldDefinition.cjs +147 -0
  62. package/dist/models/CustomFieldDefinition.d.cts +40 -0
  63. package/dist/models/CustomFieldDefinition.d.ts +40 -0
  64. package/dist/models/CustomFieldDefinition.js +146 -0
  65. package/dist/models/CustomFieldEntries.cjs +71 -0
  66. package/dist/models/CustomFieldEntries.d.cts +15 -0
  67. package/dist/models/CustomFieldEntries.d.ts +15 -0
  68. package/dist/models/CustomFieldEntries.js +70 -0
  69. package/dist/models/CustomFieldModelTypeMap.cjs +62 -0
  70. package/dist/models/CustomFieldModelTypeMap.d.cts +14 -0
  71. package/dist/models/CustomFieldModelTypeMap.d.ts +14 -0
  72. package/dist/models/CustomFieldModelTypeMap.js +61 -0
  73. package/dist/models/CustomFieldValue.cjs +109 -0
  74. package/dist/models/CustomFieldValue.d.cts +20 -0
  75. package/dist/models/CustomFieldValue.d.ts +20 -0
  76. package/dist/models/CustomFieldValue.js +108 -0
  77. package/dist/models/CustomValidator.cjs +68 -0
  78. package/dist/models/CustomValidator.d.cts +18 -0
  79. package/dist/models/CustomValidator.d.ts +18 -0
  80. package/dist/models/CustomValidator.js +67 -0
  81. package/dist/models/FieldPolicy.cjs +54 -0
  82. package/dist/models/FieldPolicy.d.cts +25 -0
  83. package/dist/models/FieldPolicy.d.ts +25 -0
  84. package/dist/models/FieldPolicy.js +53 -0
  85. package/dist/models/index.cjs +163 -0
  86. package/dist/models/index.d.ts +11 -0
  87. package/dist/models/index.js +161 -0
  88. package/dist/models/tests/AssociatedTestModel.cjs +51 -0
  89. package/dist/models/tests/AssociatedTestModel.d.ts +2 -0
  90. package/dist/models/tests/AssociatedTestModel.js +50 -0
  91. package/dist/models/tests/TestModel.cjs +43 -0
  92. package/dist/models/tests/TestModel.d.ts +2 -0
  93. package/dist/models/tests/TestModel.js +42 -0
  94. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.cjs +35 -0
  95. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +2 -0
  96. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +34 -0
  97. package/dist/models/tests/contextAwareModels/ContextTestModel.cjs +33 -0
  98. package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +1 -0
  99. package/dist/models/tests/contextAwareModels/ContextTestModel.js +32 -0
  100. package/dist/repository/definition.cjs +221 -0
  101. package/dist/repository/definition.js +212 -0
  102. package/dist/repository/entries.cjs +69 -0
  103. package/dist/repository/entries.js +67 -0
  104. package/dist/repository/field-policy.cjs +69 -0
  105. package/dist/repository/field-policy.d.cts +35 -0
  106. package/dist/repository/field-policy.d.ts +35 -0
  107. package/dist/repository/field-policy.js +59 -0
  108. package/dist/repository/utils/formatValues.cjs +14 -0
  109. package/dist/repository/utils/formatValues.js +14 -0
  110. package/dist/repository/validator.cjs +85 -0
  111. package/dist/repository/validator.js +78 -0
  112. package/dist/repository/value.cjs +77 -0
  113. package/dist/repository/value.js +76 -0
  114. package/dist/scopes/filter.cjs +56 -0
  115. package/dist/scopes/filter.d.cts +22 -0
  116. package/dist/scopes/filter.d.ts +22 -0
  117. package/dist/scopes/filter.js +54 -0
  118. package/dist/scopes/helpers/filter.helpers.cjs +152 -0
  119. package/dist/scopes/helpers/filter.helpers.d.cts +16 -0
  120. package/dist/scopes/helpers/filter.helpers.d.ts +16 -0
  121. package/dist/scopes/helpers/filter.helpers.js +147 -0
  122. package/dist/scopes/index.cjs +1 -0
  123. package/dist/scopes/index.js +3 -0
  124. package/dist/types/index.d.cts +75 -0
  125. package/dist/types/index.d.ts +76 -0
  126. package/dist/utils/constants/index.cjs +32 -0
  127. package/dist/utils/constants/index.d.cts +23 -0
  128. package/dist/utils/constants/index.d.ts +23 -0
  129. package/dist/utils/constants/index.js +29 -0
  130. package/dist/utils/db/index.cjs +15 -0
  131. package/dist/utils/db/index.js +14 -0
  132. package/dist/utils/helpers/index.cjs +23 -0
  133. package/dist/utils/helpers/index.d.cts +30 -0
  134. package/dist/utils/helpers/index.d.ts +30 -0
  135. package/dist/utils/helpers/index.js +21 -0
  136. package/dist/utils/init.cjs +115 -0
  137. package/dist/utils/init.js +111 -0
  138. package/dist/utils/logger/index.cjs +15 -0
  139. package/dist/utils/logger/index.js +12 -0
  140. package/dist/utils/scopeAttributes.cjs +12 -0
  141. package/dist/utils/scopeAttributes.js +11 -0
  142. package/dist/utils/validations/index.cjs +34 -0
  143. package/dist/utils/validations/index.js +32 -0
  144. package/dist/utils/validations/schema/custom-fields.cjs +9 -0
  145. package/dist/utils/validations/schema/custom-fields.d.cts +6 -0
  146. package/dist/utils/validations/schema/custom-fields.d.ts +6 -0
  147. package/dist/utils/validations/schema/custom-fields.js +7 -0
  148. package/dist/utils/validations/schema/validator-schema.cjs +102 -0
  149. package/dist/utils/validations/schema/validator-schema.js +99 -0
  150. package/dist/utils/validations/validators/index.cjs +45 -0
  151. package/dist/utils/validations/validators/index.js +42 -0
  152. package/dist/utils/validations/validators/multi-select-dropdown.validator.cjs +7 -0
  153. package/dist/utils/validations/validators/multi-select-dropdown.validator.js +7 -0
  154. package/dist/utils/validations/validators/multi-select-status.validator.cjs +10 -0
  155. package/dist/utils/validations/validators/multi-select-status.validator.js +10 -0
  156. package/dist/utils/validations/validators/multi-select.helpers.cjs +12 -0
  157. package/dist/utils/validations/validators/multi-select.helpers.js +10 -0
  158. package/dist/utils/validations/validators/select.validator.cjs +12 -0
  159. package/dist/utils/validations/validators/select.validator.js +10 -0
  160. package/dist/utils/validations/validators/status.validator.cjs +12 -0
  161. package/dist/utils/validations/validators/status.validator.js +10 -0
  162. package/package.json +4 -4
@@ -0,0 +1,35 @@
1
+ const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
2
+ const require_decorateMetadata = require('../../../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.cjs');
3
+ const require_decorate = require('../../../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.cjs');
4
+ const require_ContextTestModel = require('./ContextTestModel.cjs');
5
+ let sequelize_typescript = require("sequelize-typescript");
6
+
7
+ //#region src/models/tests/contextAwareModels/ContextAwareTestModel.ts
8
+ var _ref;
9
+ let ContextAwareTestModel = class ContextAwareTestModel$1 extends sequelize_typescript.Model {};
10
+ require_decorate.__decorate([
11
+ sequelize_typescript.PrimaryKey,
12
+ (0, sequelize_typescript.Column)({
13
+ type: sequelize_typescript.DataType.UUID,
14
+ defaultValue: sequelize_typescript.DataType.UUIDV4,
15
+ allowNull: false
16
+ }),
17
+ require_decorateMetadata.__decorateMetadata("design:type", String)
18
+ ], ContextAwareTestModel.prototype, "id", void 0);
19
+ require_decorate.__decorate([
20
+ (0, sequelize_typescript.ForeignKey)(() => require_ContextTestModel.default),
21
+ (0, sequelize_typescript.Column)({ type: sequelize_typescript.DataType.UUID }),
22
+ require_decorateMetadata.__decorateMetadata("design:type", String)
23
+ ], ContextAwareTestModel.prototype, "contextId", void 0);
24
+ require_decorate.__decorate([(0, sequelize_typescript.Column)({ type: sequelize_typescript.DataType.BOOLEAN }), require_decorateMetadata.__decorateMetadata("design:type", Boolean)], ContextAwareTestModel.prototype, "coolAttribute", void 0);
25
+ require_decorate.__decorate([(0, sequelize_typescript.Column)({ type: sequelize_typescript.DataType.VIRTUAL }), require_decorateMetadata.__decorateMetadata("design:type", Object)], ContextAwareTestModel.prototype, "customFields", void 0);
26
+ require_decorate.__decorate([(0, sequelize_typescript.BelongsTo)(() => require_ContextTestModel.default), require_decorateMetadata.__decorateMetadata("design:type", typeof (_ref = typeof require_ContextTestModel.default !== "undefined" && require_ContextTestModel.default) === "function" ? _ref : Object)], ContextAwareTestModel.prototype, "context", void 0);
27
+ ContextAwareTestModel = require_decorate.__decorate([(0, sequelize_typescript.Table)({
28
+ createdAt: false,
29
+ updatedAt: false,
30
+ modelName: "ContextAwareTestModel"
31
+ })], ContextAwareTestModel);
32
+ var ContextAwareTestModel_default = ContextAwareTestModel;
33
+
34
+ //#endregion
35
+ exports.default = ContextAwareTestModel_default;
@@ -0,0 +1,2 @@
1
+ import "./ContextTestModel.js";
2
+ import { Model } from "sequelize-typescript";
@@ -0,0 +1,34 @@
1
+ import { __decorateMetadata } from "../../../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.js";
2
+ import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.js";
3
+ import ContextTestModel_default from "./ContextTestModel.js";
4
+ import { BelongsTo, Column, DataType, ForeignKey, Model, PrimaryKey, Table } from "sequelize-typescript";
5
+
6
+ //#region src/models/tests/contextAwareModels/ContextAwareTestModel.ts
7
+ var _ref;
8
+ let ContextAwareTestModel = class ContextAwareTestModel$1 extends Model {};
9
+ __decorate([
10
+ PrimaryKey,
11
+ Column({
12
+ type: DataType.UUID,
13
+ defaultValue: DataType.UUIDV4,
14
+ allowNull: false
15
+ }),
16
+ __decorateMetadata("design:type", String)
17
+ ], ContextAwareTestModel.prototype, "id", void 0);
18
+ __decorate([
19
+ ForeignKey(() => ContextTestModel_default),
20
+ Column({ type: DataType.UUID }),
21
+ __decorateMetadata("design:type", String)
22
+ ], ContextAwareTestModel.prototype, "contextId", void 0);
23
+ __decorate([Column({ type: DataType.BOOLEAN }), __decorateMetadata("design:type", Boolean)], ContextAwareTestModel.prototype, "coolAttribute", void 0);
24
+ __decorate([Column({ type: DataType.VIRTUAL }), __decorateMetadata("design:type", Object)], ContextAwareTestModel.prototype, "customFields", void 0);
25
+ __decorate([BelongsTo(() => ContextTestModel_default), __decorateMetadata("design:type", typeof (_ref = typeof ContextTestModel_default !== "undefined" && ContextTestModel_default) === "function" ? _ref : Object)], ContextAwareTestModel.prototype, "context", void 0);
26
+ ContextAwareTestModel = __decorate([Table({
27
+ createdAt: false,
28
+ updatedAt: false,
29
+ modelName: "ContextAwareTestModel"
30
+ })], ContextAwareTestModel);
31
+ var ContextAwareTestModel_default = ContextAwareTestModel;
32
+
33
+ //#endregion
34
+ export { ContextAwareTestModel_default as default };
@@ -0,0 +1,33 @@
1
+ const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
2
+ const require_decorateMetadata = require('../../../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.cjs');
3
+ const require_decorate = require('../../../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.cjs');
4
+ let sequelize_typescript = require("sequelize-typescript");
5
+
6
+ //#region src/models/tests/contextAwareModels/ContextTestModel.ts
7
+ var EEntityTypes = /* @__PURE__ */ function(EEntityTypes$1) {
8
+ EEntityTypes$1["BUSINESS_MODEL"] = "businessModel";
9
+ EEntityTypes$1["CONTEXT"] = "context";
10
+ EEntityTypes$1["DEMAND_SOURCE"] = "demandSource";
11
+ EEntityTypes$1["FLEET"] = "fleet";
12
+ return EEntityTypes$1;
13
+ }(EEntityTypes || {});
14
+ let ContextTestModel = class ContextTestModel$1 extends sequelize_typescript.Model {};
15
+ require_decorate.__decorate([
16
+ sequelize_typescript.PrimaryKey,
17
+ (0, sequelize_typescript.Column)({
18
+ defaultValue: sequelize_typescript.DataType.UUIDV4,
19
+ type: sequelize_typescript.DataType.UUID
20
+ }),
21
+ require_decorateMetadata.__decorateMetadata("design:type", String)
22
+ ], ContextTestModel.prototype, "id", void 0);
23
+ require_decorate.__decorate([(0, sequelize_typescript.Column)({ type: sequelize_typescript.DataType.UUID }), require_decorateMetadata.__decorateMetadata("design:type", String)], ContextTestModel.prototype, "entityId", void 0);
24
+ require_decorate.__decorate([(0, sequelize_typescript.Column)({ type: sequelize_typescript.DataType.ENUM(...Object.values(EEntityTypes)) }), require_decorateMetadata.__decorateMetadata("design:type", String)], ContextTestModel.prototype, "entityType", void 0);
25
+ ContextTestModel = require_decorate.__decorate([(0, sequelize_typescript.Table)({
26
+ createdAt: false,
27
+ updatedAt: false,
28
+ modelName: "ContextTestModel"
29
+ })], ContextTestModel);
30
+ var ContextTestModel_default = ContextTestModel;
31
+
32
+ //#endregion
33
+ exports.default = ContextTestModel_default;
@@ -0,0 +1 @@
1
+ import { Model } from "sequelize-typescript";
@@ -0,0 +1,32 @@
1
+ import { __decorateMetadata } from "../../../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.js";
2
+ import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.js";
3
+ import { Column, DataType, Model, PrimaryKey, Table } from "sequelize-typescript";
4
+
5
+ //#region src/models/tests/contextAwareModels/ContextTestModel.ts
6
+ var EEntityTypes = /* @__PURE__ */ function(EEntityTypes$1) {
7
+ EEntityTypes$1["BUSINESS_MODEL"] = "businessModel";
8
+ EEntityTypes$1["CONTEXT"] = "context";
9
+ EEntityTypes$1["DEMAND_SOURCE"] = "demandSource";
10
+ EEntityTypes$1["FLEET"] = "fleet";
11
+ return EEntityTypes$1;
12
+ }(EEntityTypes || {});
13
+ let ContextTestModel = class ContextTestModel$1 extends Model {};
14
+ __decorate([
15
+ PrimaryKey,
16
+ Column({
17
+ defaultValue: DataType.UUIDV4,
18
+ type: DataType.UUID
19
+ }),
20
+ __decorateMetadata("design:type", String)
21
+ ], ContextTestModel.prototype, "id", void 0);
22
+ __decorate([Column({ type: DataType.UUID }), __decorateMetadata("design:type", String)], ContextTestModel.prototype, "entityId", void 0);
23
+ __decorate([Column({ type: DataType.ENUM(...Object.values(EEntityTypes)) }), __decorateMetadata("design:type", String)], ContextTestModel.prototype, "entityType", void 0);
24
+ ContextTestModel = __decorate([Table({
25
+ createdAt: false,
26
+ updatedAt: false,
27
+ modelName: "ContextTestModel"
28
+ })], ContextTestModel);
29
+ var ContextTestModel_default = ContextTestModel;
30
+
31
+ //#endregion
32
+ export { ContextTestModel_default as default };
@@ -0,0 +1,221 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_index = require('../errors/index.cjs');
3
+ const require_CustomFieldDefinition = require('../models/CustomFieldDefinition.cjs');
4
+ const require_CustomFieldModelTypeMap = require('../models/CustomFieldModelTypeMap.cjs');
5
+ require('../models/index.cjs');
6
+ let sequelize = require("sequelize");
7
+
8
+ //#region src/repository/definition.ts
9
+ /**
10
+ * Check if the modelTypeMappings association exists on CustomFieldDefinition
11
+ * The association is only created when useModelTypeMapping: true is passed to initTables
12
+ */
13
+ const hasModelTypeMappingsAssociation = () => "modelTypeMappings" in (require_CustomFieldDefinition.default.associations || {});
14
+ /**
15
+ * Helper to extract modelTypeIds from the modelTypeMappings association data
16
+ * Handles both raw and non-raw Sequelize results
17
+ */
18
+ const extractModelTypeIdsFromMappings = (mappings) => {
19
+ if (!mappings || !Array.isArray(mappings)) return [];
20
+ return mappings.map((m) => m.modelTypeId || m.dataValues?.modelTypeId).filter((id) => Boolean(id));
21
+ };
22
+ /**
23
+ * Helper to extract modelTypeIds from a definition with loaded association
24
+ * Handles the common pattern of getting mappings from a definition
25
+ */
26
+ const enrichDefinitionWithModelTypeIds = (definition) => {
27
+ const defWithMappings = definition;
28
+ definition.modelTypeIds = extractModelTypeIdsFromMappings(defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings);
29
+ return definition;
30
+ };
31
+ const create = async (data) => {
32
+ const { modelTypeIds, ...definitionData } = data;
33
+ const result = await require_CustomFieldDefinition.default.sequelize.transaction(async (transaction) => {
34
+ const customFieldDefinition = await require_CustomFieldDefinition.default.create(definitionData, { transaction });
35
+ if (modelTypeIds?.length) await require_CustomFieldModelTypeMap.default.bulkCreate(modelTypeIds.map((modelTypeId) => ({
36
+ customFieldDefinitionId: customFieldDefinition.id,
37
+ modelTypeId
38
+ })), { transaction });
39
+ return customFieldDefinition;
40
+ });
41
+ result.modelTypeIds = modelTypeIds ?? [];
42
+ return result;
43
+ };
44
+ const findAll = async (where, options = { withDisabled: false }) => {
45
+ const queryModel = options.withDisabled ? require_CustomFieldDefinition.default.unscoped() : require_CustomFieldDefinition.default;
46
+ let includeToUse = options.include;
47
+ if (options.enrichWithModelTypeIds && hasModelTypeMappingsAssociation()) {
48
+ const modelTypeMappingsInclude = {
49
+ association: "modelTypeMappings",
50
+ required: false,
51
+ attributes: ["modelTypeId"]
52
+ };
53
+ if (options.include) includeToUse = Array.isArray(options.include) ? [...options.include, modelTypeMappingsInclude] : [options.include, modelTypeMappingsInclude];
54
+ else includeToUse = modelTypeMappingsInclude;
55
+ }
56
+ const definitions = await queryModel.scope("userScope").findAll({
57
+ where,
58
+ transaction: options.transaction,
59
+ raw: !options.enrichWithModelTypeIds,
60
+ include: includeToUse
61
+ });
62
+ if (options.enrichWithModelTypeIds && hasModelTypeMappingsAssociation()) return definitions.map(enrichDefinitionWithModelTypeIds);
63
+ return definitions;
64
+ };
65
+ const findByIds = (ids, options = { withDisabled: false }) => findAll({ id: { [sequelize.Op.in]: ids } }, options);
66
+ const findById = async (id, options = { withDisabled: false }) => {
67
+ const { withDisabled } = options;
68
+ const queryModel = withDisabled ? require_CustomFieldDefinition.default.unscoped().scope("userScope") : require_CustomFieldDefinition.default.scope("userScope");
69
+ const includeModelTypeMappings = hasModelTypeMappingsAssociation();
70
+ const definition = await queryModel.findByPk(id, { include: includeModelTypeMappings ? [{
71
+ association: "modelTypeMappings",
72
+ required: false,
73
+ attributes: ["modelTypeId"]
74
+ }] : void 0 });
75
+ if (!definition) return null;
76
+ if (includeModelTypeMappings) return enrichDefinitionWithModelTypeIds(definition);
77
+ return definition;
78
+ };
79
+ const findByEntityIds = async (modelType, entityIds, options = {}) => {
80
+ const { include, useEntityIdFromInclude } = options.modelOptions;
81
+ const where = {
82
+ modelType,
83
+ ...!useEntityIdFromInclude && { entityId: { [sequelize.Op.in]: entityIds } }
84
+ };
85
+ return require_CustomFieldDefinition.default.findAll({
86
+ where,
87
+ transaction: options.transaction,
88
+ include: include?.(entityIds),
89
+ raw: true
90
+ });
91
+ };
92
+ const findByWhere = (where) => require_CustomFieldDefinition.default.scope("userScope").findOne({ where });
93
+ const update = async (id, data) => {
94
+ const { modelTypeIds, ...definitionData } = data;
95
+ const result = await require_CustomFieldDefinition.default.sequelize.transaction(async (transaction) => {
96
+ const updatedDefinition = (await require_CustomFieldDefinition.default.scope("userScope").update(definitionData, {
97
+ where: { id },
98
+ returning: true,
99
+ individualHooks: true,
100
+ transaction
101
+ }))[1][0];
102
+ if (modelTypeIds) {
103
+ const existingMappings = await require_CustomFieldModelTypeMap.default.findAll({
104
+ where: { customFieldDefinitionId: id },
105
+ attributes: ["id", "modelTypeId"],
106
+ raw: true,
107
+ transaction
108
+ });
109
+ const existingTypeIds = existingMappings.map((m) => m.modelTypeId);
110
+ const toAdd = modelTypeIds.filter((typeId) => !existingTypeIds.includes(typeId));
111
+ const toRemove = existingMappings.filter((m) => !modelTypeIds.includes(m.modelTypeId));
112
+ const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;
113
+ if (toRemove.length > 0) await require_CustomFieldModelTypeMap.default.destroy({
114
+ where: { id: toRemove.map((m) => m.id) },
115
+ transaction
116
+ });
117
+ if (toAdd.length > 0) await require_CustomFieldModelTypeMap.default.bulkCreate(toAdd.map((modelTypeId) => ({
118
+ customFieldDefinitionId: id,
119
+ modelTypeId
120
+ })), { transaction });
121
+ if (hasMappingChanges) {
122
+ updatedDefinition.changed("updatedAt", true);
123
+ await updatedDefinition.save({ transaction });
124
+ }
125
+ }
126
+ return updatedDefinition;
127
+ });
128
+ result.modelTypeIds = modelTypeIds ?? [];
129
+ return result;
130
+ };
131
+ /**
132
+ * @returns A promise resolving with a dictionary of custom field definitions by name.
133
+ * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.
134
+ */
135
+ /**
136
+ * Find custom field definitions applicable to a specific model type instance
137
+ *
138
+ * Returns definitions that either:
139
+ * 1. Have NO mappings (apply to all instances of this model type)
140
+ * 2. Have a mapping to this specific model type instance
141
+ */
142
+ const findByModelTypeId = async (modelTypeId, options = {
143
+ withDisabled: false,
144
+ modelType: ""
145
+ }) => {
146
+ const { withDisabled, entityIds, modelType, modelOptions } = options;
147
+ const { include, useEntityIdFromInclude } = modelOptions ?? {};
148
+ const baseWhere = {
149
+ modelType,
150
+ ...!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [sequelize.Op.in]: entityIds } }
151
+ };
152
+ const queryModel = withDisabled ? require_CustomFieldDefinition.default.unscoped() : require_CustomFieldDefinition.default;
153
+ const includeModelTypeMappings = hasModelTypeMappingsAssociation();
154
+ const includeArray = [];
155
+ if (includeModelTypeMappings) includeArray.push({
156
+ association: "modelTypeMappings",
157
+ required: false,
158
+ attributes: ["id", "modelTypeId"]
159
+ });
160
+ if (useEntityIdFromInclude && include && entityIds) {
161
+ const contextIncludes = include(entityIds);
162
+ includeArray.push(...contextIncludes);
163
+ }
164
+ const definitions = await queryModel.scope("userScope").findAll({
165
+ where: baseWhere,
166
+ include: includeArray.length > 0 ? includeArray : void 0,
167
+ raw: false,
168
+ transaction: options.transaction
169
+ });
170
+ const filteredDefinitions = includeModelTypeMappings ? definitions.filter((def) => {
171
+ const mappings = def.modelTypeMappings ?? def.dataValues?.modelTypeMappings ?? [];
172
+ if (!Array.isArray(mappings) || mappings.length === 0) return true;
173
+ return mappings.some((mapping) => {
174
+ return (mapping.modelTypeId ?? mapping.dataValues?.modelTypeId) === modelTypeId;
175
+ });
176
+ }) : definitions;
177
+ if (includeModelTypeMappings) return filteredDefinitions.map(enrichDefinitionWithModelTypeIds);
178
+ return filteredDefinitions;
179
+ };
180
+ const getCustomFieldDefinitionsDictionary = async (instances, { findAll: _findAll, modelOptions = {}, withDisabled = false, ...options } = {
181
+ withDisabled: false,
182
+ modelOptions: {}
183
+ }) => {
184
+ const { modelType } = instances[0]?.dataValues ?? {};
185
+ const customFields = /* @__PURE__ */ new Set();
186
+ const modelIds = [];
187
+ const entityIds = /* @__PURE__ */ new Set();
188
+ instances.forEach((instance) => {
189
+ const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;
190
+ modelIds.push(modelId);
191
+ entityIds.add(entityId);
192
+ Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {
193
+ customFields.add(fieldName);
194
+ });
195
+ });
196
+ const where = {
197
+ modelType,
198
+ entityId: { [sequelize.Op.in]: Array.from(entityIds) },
199
+ name: { [sequelize.Op.in]: Array.from(customFields) }
200
+ };
201
+ const definitions = await (_findAll ?? findAll)(where, {
202
+ withDisabled,
203
+ modelOptions,
204
+ ...options
205
+ });
206
+ const matchedDefinitions = definitions.filter((def) => customFields.has(def.name));
207
+ const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map((definition) => [definition.name, definition]));
208
+ if (!definitions?.length || matchedDefinitions.length !== customFields.size) throw new require_index.MissingDefinitionError(Array.from(customFields).filter((customField) => !matchedDefinitionsByName[customField]));
209
+ return matchedDefinitionsByName;
210
+ };
211
+
212
+ //#endregion
213
+ exports.create = create;
214
+ exports.findAll = findAll;
215
+ exports.findByEntityIds = findByEntityIds;
216
+ exports.findById = findById;
217
+ exports.findByIds = findByIds;
218
+ exports.findByModelTypeId = findByModelTypeId;
219
+ exports.findByWhere = findByWhere;
220
+ exports.getCustomFieldDefinitionsDictionary = getCustomFieldDefinitionsDictionary;
221
+ exports.update = update;
@@ -0,0 +1,212 @@
1
+ import { MissingDefinitionError } from "../errors/index.js";
2
+ import CustomFieldDefinition_default from "../models/CustomFieldDefinition.js";
3
+ import CustomFieldModelTypeMap_default from "../models/CustomFieldModelTypeMap.js";
4
+ import "../models/index.js";
5
+ import { Op } from "sequelize";
6
+
7
+ //#region src/repository/definition.ts
8
+ /**
9
+ * Check if the modelTypeMappings association exists on CustomFieldDefinition
10
+ * The association is only created when useModelTypeMapping: true is passed to initTables
11
+ */
12
+ const hasModelTypeMappingsAssociation = () => "modelTypeMappings" in (CustomFieldDefinition_default.associations || {});
13
+ /**
14
+ * Helper to extract modelTypeIds from the modelTypeMappings association data
15
+ * Handles both raw and non-raw Sequelize results
16
+ */
17
+ const extractModelTypeIdsFromMappings = (mappings) => {
18
+ if (!mappings || !Array.isArray(mappings)) return [];
19
+ return mappings.map((m) => m.modelTypeId || m.dataValues?.modelTypeId).filter((id) => Boolean(id));
20
+ };
21
+ /**
22
+ * Helper to extract modelTypeIds from a definition with loaded association
23
+ * Handles the common pattern of getting mappings from a definition
24
+ */
25
+ const enrichDefinitionWithModelTypeIds = (definition) => {
26
+ const defWithMappings = definition;
27
+ definition.modelTypeIds = extractModelTypeIdsFromMappings(defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings);
28
+ return definition;
29
+ };
30
+ const create = async (data) => {
31
+ const { modelTypeIds, ...definitionData } = data;
32
+ const result = await CustomFieldDefinition_default.sequelize.transaction(async (transaction) => {
33
+ const customFieldDefinition = await CustomFieldDefinition_default.create(definitionData, { transaction });
34
+ if (modelTypeIds?.length) await CustomFieldModelTypeMap_default.bulkCreate(modelTypeIds.map((modelTypeId) => ({
35
+ customFieldDefinitionId: customFieldDefinition.id,
36
+ modelTypeId
37
+ })), { transaction });
38
+ return customFieldDefinition;
39
+ });
40
+ result.modelTypeIds = modelTypeIds ?? [];
41
+ return result;
42
+ };
43
+ const findAll = async (where, options = { withDisabled: false }) => {
44
+ const queryModel = options.withDisabled ? CustomFieldDefinition_default.unscoped() : CustomFieldDefinition_default;
45
+ let includeToUse = options.include;
46
+ if (options.enrichWithModelTypeIds && hasModelTypeMappingsAssociation()) {
47
+ const modelTypeMappingsInclude = {
48
+ association: "modelTypeMappings",
49
+ required: false,
50
+ attributes: ["modelTypeId"]
51
+ };
52
+ if (options.include) includeToUse = Array.isArray(options.include) ? [...options.include, modelTypeMappingsInclude] : [options.include, modelTypeMappingsInclude];
53
+ else includeToUse = modelTypeMappingsInclude;
54
+ }
55
+ const definitions = await queryModel.scope("userScope").findAll({
56
+ where,
57
+ transaction: options.transaction,
58
+ raw: !options.enrichWithModelTypeIds,
59
+ include: includeToUse
60
+ });
61
+ if (options.enrichWithModelTypeIds && hasModelTypeMappingsAssociation()) return definitions.map(enrichDefinitionWithModelTypeIds);
62
+ return definitions;
63
+ };
64
+ const findByIds = (ids, options = { withDisabled: false }) => findAll({ id: { [Op.in]: ids } }, options);
65
+ const findById = async (id, options = { withDisabled: false }) => {
66
+ const { withDisabled } = options;
67
+ const queryModel = withDisabled ? CustomFieldDefinition_default.unscoped().scope("userScope") : CustomFieldDefinition_default.scope("userScope");
68
+ const includeModelTypeMappings = hasModelTypeMappingsAssociation();
69
+ const definition = await queryModel.findByPk(id, { include: includeModelTypeMappings ? [{
70
+ association: "modelTypeMappings",
71
+ required: false,
72
+ attributes: ["modelTypeId"]
73
+ }] : void 0 });
74
+ if (!definition) return null;
75
+ if (includeModelTypeMappings) return enrichDefinitionWithModelTypeIds(definition);
76
+ return definition;
77
+ };
78
+ const findByEntityIds = async (modelType, entityIds, options = {}) => {
79
+ const { include, useEntityIdFromInclude } = options.modelOptions;
80
+ const where = {
81
+ modelType,
82
+ ...!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }
83
+ };
84
+ return CustomFieldDefinition_default.findAll({
85
+ where,
86
+ transaction: options.transaction,
87
+ include: include?.(entityIds),
88
+ raw: true
89
+ });
90
+ };
91
+ const findByWhere = (where) => CustomFieldDefinition_default.scope("userScope").findOne({ where });
92
+ const update = async (id, data) => {
93
+ const { modelTypeIds, ...definitionData } = data;
94
+ const result = await CustomFieldDefinition_default.sequelize.transaction(async (transaction) => {
95
+ const updatedDefinition = (await CustomFieldDefinition_default.scope("userScope").update(definitionData, {
96
+ where: { id },
97
+ returning: true,
98
+ individualHooks: true,
99
+ transaction
100
+ }))[1][0];
101
+ if (modelTypeIds) {
102
+ const existingMappings = await CustomFieldModelTypeMap_default.findAll({
103
+ where: { customFieldDefinitionId: id },
104
+ attributes: ["id", "modelTypeId"],
105
+ raw: true,
106
+ transaction
107
+ });
108
+ const existingTypeIds = existingMappings.map((m) => m.modelTypeId);
109
+ const toAdd = modelTypeIds.filter((typeId) => !existingTypeIds.includes(typeId));
110
+ const toRemove = existingMappings.filter((m) => !modelTypeIds.includes(m.modelTypeId));
111
+ const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;
112
+ if (toRemove.length > 0) await CustomFieldModelTypeMap_default.destroy({
113
+ where: { id: toRemove.map((m) => m.id) },
114
+ transaction
115
+ });
116
+ if (toAdd.length > 0) await CustomFieldModelTypeMap_default.bulkCreate(toAdd.map((modelTypeId) => ({
117
+ customFieldDefinitionId: id,
118
+ modelTypeId
119
+ })), { transaction });
120
+ if (hasMappingChanges) {
121
+ updatedDefinition.changed("updatedAt", true);
122
+ await updatedDefinition.save({ transaction });
123
+ }
124
+ }
125
+ return updatedDefinition;
126
+ });
127
+ result.modelTypeIds = modelTypeIds ?? [];
128
+ return result;
129
+ };
130
+ /**
131
+ * @returns A promise resolving with a dictionary of custom field definitions by name.
132
+ * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.
133
+ */
134
+ /**
135
+ * Find custom field definitions applicable to a specific model type instance
136
+ *
137
+ * Returns definitions that either:
138
+ * 1. Have NO mappings (apply to all instances of this model type)
139
+ * 2. Have a mapping to this specific model type instance
140
+ */
141
+ const findByModelTypeId = async (modelTypeId, options = {
142
+ withDisabled: false,
143
+ modelType: ""
144
+ }) => {
145
+ const { withDisabled, entityIds, modelType, modelOptions } = options;
146
+ const { include, useEntityIdFromInclude } = modelOptions ?? {};
147
+ const baseWhere = {
148
+ modelType,
149
+ ...!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }
150
+ };
151
+ const queryModel = withDisabled ? CustomFieldDefinition_default.unscoped() : CustomFieldDefinition_default;
152
+ const includeModelTypeMappings = hasModelTypeMappingsAssociation();
153
+ const includeArray = [];
154
+ if (includeModelTypeMappings) includeArray.push({
155
+ association: "modelTypeMappings",
156
+ required: false,
157
+ attributes: ["id", "modelTypeId"]
158
+ });
159
+ if (useEntityIdFromInclude && include && entityIds) {
160
+ const contextIncludes = include(entityIds);
161
+ includeArray.push(...contextIncludes);
162
+ }
163
+ const definitions = await queryModel.scope("userScope").findAll({
164
+ where: baseWhere,
165
+ include: includeArray.length > 0 ? includeArray : void 0,
166
+ raw: false,
167
+ transaction: options.transaction
168
+ });
169
+ const filteredDefinitions = includeModelTypeMappings ? definitions.filter((def) => {
170
+ const mappings = def.modelTypeMappings ?? def.dataValues?.modelTypeMappings ?? [];
171
+ if (!Array.isArray(mappings) || mappings.length === 0) return true;
172
+ return mappings.some((mapping) => {
173
+ return (mapping.modelTypeId ?? mapping.dataValues?.modelTypeId) === modelTypeId;
174
+ });
175
+ }) : definitions;
176
+ if (includeModelTypeMappings) return filteredDefinitions.map(enrichDefinitionWithModelTypeIds);
177
+ return filteredDefinitions;
178
+ };
179
+ const getCustomFieldDefinitionsDictionary = async (instances, { findAll: _findAll, modelOptions = {}, withDisabled = false, ...options } = {
180
+ withDisabled: false,
181
+ modelOptions: {}
182
+ }) => {
183
+ const { modelType } = instances[0]?.dataValues ?? {};
184
+ const customFields = /* @__PURE__ */ new Set();
185
+ const modelIds = [];
186
+ const entityIds = /* @__PURE__ */ new Set();
187
+ instances.forEach((instance) => {
188
+ const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;
189
+ modelIds.push(modelId);
190
+ entityIds.add(entityId);
191
+ Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {
192
+ customFields.add(fieldName);
193
+ });
194
+ });
195
+ const where = {
196
+ modelType,
197
+ entityId: { [Op.in]: Array.from(entityIds) },
198
+ name: { [Op.in]: Array.from(customFields) }
199
+ };
200
+ const definitions = await (_findAll ?? findAll)(where, {
201
+ withDisabled,
202
+ modelOptions,
203
+ ...options
204
+ });
205
+ const matchedDefinitions = definitions.filter((def) => customFields.has(def.name));
206
+ const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map((definition) => [definition.name, definition]));
207
+ if (!definitions?.length || matchedDefinitions.length !== customFields.size) throw new MissingDefinitionError(Array.from(customFields).filter((customField) => !matchedDefinitionsByName[customField]));
208
+ return matchedDefinitionsByName;
209
+ };
210
+
211
+ //#endregion
212
+ export { create, findAll, findByEntityIds, findById, findByIds, findByModelTypeId, findByWhere, getCustomFieldDefinitionsDictionary, update };
@@ -0,0 +1,69 @@
1
+ const require_index = require('../utils/logger/index.cjs');
2
+ const require_index$1 = require('../errors/index.cjs');
3
+ const require_definition = require('./definition.cjs');
4
+ const require_CustomFieldEntries = require('../models/CustomFieldEntries.cjs');
5
+ require('../models/index.cjs');
6
+ const require_formatValues = require('./utils/formatValues.cjs');
7
+
8
+ //#region src/repository/entries.ts
9
+ const findEntriesByModelId = async (modelId, options = {}) => {
10
+ const { transaction } = options;
11
+ return require_CustomFieldEntries.default.findOne({
12
+ where: { modelId },
13
+ transaction
14
+ });
15
+ };
16
+ const findEntriesByModelIds = async (modelIds, options = {}) => {
17
+ const { transaction } = options;
18
+ return require_CustomFieldEntries.default.findAll({
19
+ where: { modelId: modelIds },
20
+ transaction
21
+ });
22
+ };
23
+ const updateEntries = async (modelId, modelType, customFields, identifiers, options = {}) => {
24
+ const customFieldsNames = Object.keys(customFields);
25
+ require_index.default.debug(`custom-fields: updating entries for ${modelType} ${modelId}`, {
26
+ customFieldsNames,
27
+ optionsKeys: options ? Object.keys(options) : null,
28
+ customFields,
29
+ identifiers
30
+ });
31
+ const { modelOptions, transaction } = options;
32
+ const where = {
33
+ modelType,
34
+ name: customFieldsNames,
35
+ ...!options.modelOptions?.useEntityIdFromInclude && { entityId: identifiers }
36
+ };
37
+ const fieldDefinitions = await require_definition.findAll(where, {
38
+ withDisabled: true,
39
+ transaction,
40
+ include: modelOptions.include?.(identifiers)
41
+ }) ?? [];
42
+ const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
43
+ if (fieldDefinitions.length !== customFieldsNames.length) {
44
+ require_index.default.warn(`custom-fields: missing definitions for ${modelType} ${modelId}`, {
45
+ names: customFieldsNames,
46
+ fieldDefinitions
47
+ });
48
+ throw new require_index$1.MissingDefinitionError(customFieldsNames.filter((name) => !fieldDefinitions.some((def) => def.name === name)));
49
+ }
50
+ const disabledNames = disabledDefinitions?.map((def) => def.name) || [];
51
+ const valuesWithDisabledDefinitions = customFieldsNames.filter((name) => disabledNames.includes(name));
52
+ if (valuesWithDisabledDefinitions?.length > 0) require_index.default.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(", ")}`);
53
+ const definitionsByName = Object.fromEntries(fieldDefinitions.map((definition) => [definition.name, definition]));
54
+ Object.entries(customFields).filter(([definitionName]) => require_formatValues.formatFunctions[definitionsByName[definitionName].fieldType]).forEach(([definitionName, value]) => {
55
+ const { fieldType } = definitionsByName[definitionName];
56
+ customFields[definitionName] = require_formatValues.formatFunctions[fieldType](value);
57
+ });
58
+ return require_CustomFieldEntries.default.upsert({
59
+ modelId,
60
+ entityId: fieldDefinitions[0].entityId,
61
+ modelType,
62
+ customFields
63
+ }, options);
64
+ };
65
+
66
+ //#endregion
67
+ exports.findEntriesByModelId = findEntriesByModelId;
68
+ exports.findEntriesByModelIds = findEntriesByModelIds;
69
+ exports.updateEntries = updateEntries;