@autofleet/sadot 0.6.1-temp-2 → 0.6.1

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 (94) hide show
  1. package/dist/api/index.d.ts +2 -0
  2. package/dist/api/index.js +11 -0
  3. package/dist/api/v1/definition/index.d.ts +2 -0
  4. package/dist/api/v1/definition/index.js +118 -0
  5. package/dist/api/v1/definition/validations.d.ts +2 -0
  6. package/dist/api/v1/definition/validations.js +36 -0
  7. package/dist/api/v1/errors.d.ts +2 -0
  8. package/dist/api/v1/errors.js +15 -0
  9. package/dist/api/v1/index.d.ts +2 -0
  10. package/dist/api/v1/index.js +10 -0
  11. package/dist/errors/index.d.ts +16 -0
  12. package/dist/errors/index.js +45 -0
  13. package/dist/events/index.d.ts +4 -0
  14. package/dist/events/index.js +47 -0
  15. package/dist/hooks/create.d.ts +10 -0
  16. package/dist/hooks/create.js +74 -0
  17. package/dist/hooks/enrich.d.ts +7 -0
  18. package/dist/hooks/enrich.js +126 -0
  19. package/dist/hooks/find.d.ts +1 -0
  20. package/dist/hooks/find.js +29 -0
  21. package/dist/hooks/index.d.ts +6 -0
  22. package/dist/hooks/index.js +18 -0
  23. package/dist/hooks/update.d.ts +10 -0
  24. package/dist/hooks/update.js +59 -0
  25. package/dist/hooks/workaround.d.ts +10 -0
  26. package/dist/hooks/workaround.js +37 -0
  27. package/dist/index.d.ts +14 -0
  28. package/dist/index.js +56 -0
  29. package/dist/models/CustomFieldDefinition.d.ts +23 -0
  30. package/dist/models/CustomFieldDefinition.js +165 -0
  31. package/dist/models/CustomFieldValue.d.ts +15 -0
  32. package/dist/models/CustomFieldValue.js +148 -0
  33. package/dist/models/index.d.ts +10 -0
  34. package/dist/models/index.js +93 -0
  35. package/dist/models/tests/AssociatedTestModel.d.ts +12 -0
  36. package/dist/models/tests/AssociatedTestModel.js +71 -0
  37. package/dist/models/tests/TestModel.d.ts +12 -0
  38. package/dist/models/tests/TestModel.js +69 -0
  39. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +10 -0
  40. package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +55 -0
  41. package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +13 -0
  42. package/dist/models/tests/contextAwareModels/ContextTestModel.js +47 -0
  43. package/dist/repository/definition.d.ts +20 -0
  44. package/dist/repository/definition.js +92 -0
  45. package/dist/repository/value.d.ts +28 -0
  46. package/dist/repository/value.js +117 -0
  47. package/dist/scopes/filter.d.ts +23 -0
  48. package/dist/scopes/filter.js +55 -0
  49. package/dist/scopes/index.d.ts +2 -0
  50. package/dist/scopes/index.js +6 -0
  51. package/dist/tests/api/test-api.d.ts +2 -0
  52. package/dist/tests/api/test-api.js +56 -0
  53. package/dist/tests/functional/searching/index.d.ts +8 -0
  54. package/dist/tests/functional/searching/index.js +44 -0
  55. package/dist/tests/helpers/database-config.d.ts +16 -0
  56. package/dist/tests/helpers/database-config.js +17 -0
  57. package/dist/tests/helpers/index.d.ts +2 -0
  58. package/dist/tests/helpers/index.js +20 -0
  59. package/dist/tests/mocks/definition.mock.d.ts +43 -0
  60. package/dist/tests/mocks/definition.mock.js +70 -0
  61. package/dist/tests/mocks/events.mock.d.ts +3 -0
  62. package/dist/tests/mocks/events.mock.js +19 -0
  63. package/dist/tests/mocks/testModel.d.ts +12 -0
  64. package/dist/tests/mocks/testModel.js +35 -0
  65. package/dist/types/definition/index.d.ts +23 -0
  66. package/dist/types/definition/index.js +2 -0
  67. package/dist/types/index.d.ts +31 -0
  68. package/dist/types/index.js +2 -0
  69. package/dist/types/value/index.d.ts +15 -0
  70. package/dist/types/value/index.js +2 -0
  71. package/dist/utils/constants/index.d.ts +5 -0
  72. package/dist/utils/constants/index.js +8 -0
  73. package/dist/utils/db/index.d.ts +4 -0
  74. package/dist/utils/db/index.js +24 -0
  75. package/dist/utils/helpers/index.d.ts +25 -0
  76. package/dist/utils/helpers/index.js +34 -0
  77. package/dist/utils/init.d.ts +5 -0
  78. package/dist/utils/init.js +106 -0
  79. package/dist/utils/logger/index.d.ts +2 -0
  80. package/dist/utils/logger/index.js +6 -0
  81. package/dist/utils/scopeAttributes.d.ts +2 -0
  82. package/dist/utils/scopeAttributes.js +11 -0
  83. package/dist/utils/validations/custom-fields.d.ts +3 -0
  84. package/dist/utils/validations/custom-fields.js +10 -0
  85. package/dist/utils/validations/custom.d.ts +15 -0
  86. package/dist/utils/validations/custom.js +42 -0
  87. package/dist/utils/validations/index.d.ts +2 -0
  88. package/dist/utils/validations/index.js +20 -0
  89. package/dist/utils/validations/type.d.ts +18 -0
  90. package/dist/utils/validations/type.js +50 -0
  91. package/dist/utils/validations/validators.d.ts +12 -0
  92. package/dist/utils/validations/validators.js +33 -0
  93. package/package.json +2 -2
  94. package/src/utils/validations/type.ts +0 -10
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.applyCustomAssociation = exports.addScopes = exports.removeHooks = exports.addHooks = void 0;
7
+ const sequelize_1 = require("sequelize");
8
+ const common_types_1 = require("@autofleet/common-types");
9
+ const models_1 = require("../models");
10
+ const hooks_1 = require("../hooks");
11
+ const scopes_1 = require("../scopes");
12
+ const logger_1 = __importDefault(require("./logger"));
13
+ const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
14
+ const addHooks = (models, getModel) => {
15
+ models.forEach(async ({ name, scopeAttributes, modelOptions, }) => {
16
+ try {
17
+ const model = getModel(name);
18
+ if (!model) {
19
+ logger_1.default.warn('sadot - tried to addHooks to a model that does not exist yet', {
20
+ name,
21
+ scopeAttributes,
22
+ });
23
+ return;
24
+ }
25
+ model.rawAttributes.customFields = {
26
+ type: sequelize_1.DataTypes.VIRTUAL,
27
+ };
28
+ model.refreshAttributes();
29
+ // TODO: Uncomment after tests are passed
30
+ // model.addHook('afterFind', workaround);
31
+ model.addHook('beforeFind', 'sadot-beforeFind', (0, hooks_1.beforeFind)(scopeAttributes));
32
+ model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', hooks_1.beforeBulkCreate);
33
+ model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', hooks_1.beforeBulkUpdate);
34
+ model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes, modelOptions));
35
+ model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes, modelOptions));
36
+ model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind', modelOptions));
37
+ model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, modelOptions));
38
+ model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, modelOptions));
39
+ }
40
+ catch (e) {
41
+ logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
42
+ }
43
+ });
44
+ };
45
+ exports.addHooks = addHooks;
46
+ const removeHooks = (models, getModel) => {
47
+ models.forEach(async ({ name }) => {
48
+ try {
49
+ const model = getModel(name);
50
+ if (!model)
51
+ return;
52
+ if (model.rawAttributes.customFields) {
53
+ delete model.rawAttributes.customFields;
54
+ model.refreshAttributes();
55
+ }
56
+ // model.removeHook('afterFind', 'sadot-workaround');
57
+ model.removeHook('beforeFind', 'sadot-beforeFind');
58
+ model.removeHook('beforeBulkCreate', 'sadot-beforeBulkCreate');
59
+ model.removeHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate');
60
+ model.removeHook('beforeCreate', 'sadot-beforeCreate');
61
+ model.removeHook('beforeUpdate', 'sadot-beforeUpdate');
62
+ model.removeHook('afterFind', 'sadot-afterFind');
63
+ model.removeHook('afterUpdate', 'sadot-afterUpdate');
64
+ }
65
+ catch (e) {
66
+ logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
67
+ }
68
+ });
69
+ };
70
+ exports.removeHooks = removeHooks;
71
+ /**
72
+ * Necessary associations for the {@link customFieldsFilterScope} scope
73
+ */
74
+ const addAssociations = (model, modelName) => {
75
+ model.hasMany(models_1.CustomFieldValue, { foreignKey: 'modelId', as: 'customFieldValue' });
76
+ // TBC: maybe can be removed
77
+ models_1.CustomFieldValue.belongsTo(model, { foreignKey: 'modelId', as: modelName });
78
+ };
79
+ const addScopes = (models, getModel) => {
80
+ models.forEach(async ({ name, scopeAttributes }) => {
81
+ try {
82
+ const model = getModel(name);
83
+ if (!model) {
84
+ logger_1.default.warn('sadot - tried to addScopes to a model that does not exist yet', {
85
+ name,
86
+ scopeAttributes,
87
+ });
88
+ return;
89
+ }
90
+ // Necessary associations for the filtering scope
91
+ addAssociations(model, name);
92
+ // Add filter scope
93
+ model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, (0, scopes_1.customFieldsFilterScope)(name));
94
+ }
95
+ catch (e) {
96
+ logger_1.default.error(`Could not add custom fields scopes to model ${name}. `, e);
97
+ }
98
+ });
99
+ };
100
+ exports.addScopes = addScopes;
101
+ const applyCustomAssociation = (models) => {
102
+ models.forEach(({ modelOptions }) => {
103
+ modelOptions?.customAssociation?.(models_1.CustomFieldDefinition);
104
+ });
105
+ };
106
+ exports.applyCustomAssociation = applyCustomAssociation;
@@ -0,0 +1,2 @@
1
+ declare const logger: any;
2
+ export default logger;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
4
+ const Logger = require('@autofleet/logger');
5
+ const logger = Logger();
6
+ exports.default = logger;
@@ -0,0 +1,2 @@
1
+ declare const applyScopeToInstance: (instance: any, scopeAttributes: string[]) => any[];
2
+ export default applyScopeToInstance;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const mapAttributeToInstance = (scopeAttributes, instance) => scopeAttributes.map((attr) => instance[attr]);
4
+ const applyScopeToInstance = (instance, scopeAttributes) => {
5
+ const uniqueAttributes = Array.from(new Set(scopeAttributes));
6
+ if (Array.isArray(instance)) {
7
+ return instance.flatMap((ins) => mapAttributeToInstance(uniqueAttributes, ins));
8
+ }
9
+ return mapAttributeToInstance(uniqueAttributes, instance);
10
+ };
11
+ exports.default = applyScopeToInstance;
@@ -0,0 +1,3 @@
1
+ import Joi from 'joi';
2
+ declare const CustomFieldsSchema: Joi.ObjectSchema<any>;
3
+ export { CustomFieldsSchema };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.CustomFieldsSchema = void 0;
7
+ /* eslint-disable import/prefer-default-export */
8
+ const joi_1 = __importDefault(require("joi"));
9
+ const CustomFieldsSchema = joi_1.default.object().pattern(joi_1.default.string(), joi_1.default.any());
10
+ exports.CustomFieldsSchema = CustomFieldsSchema;
@@ -0,0 +1,15 @@
1
+ export declare const mustHaveCustomValidation: {
2
+ select: boolean;
3
+ };
4
+ /**
5
+ * Validates the given validations object against the supported field types and their validators.
6
+ * @return true if the validation is valid, false otherwise.
7
+ */
8
+ export declare const validateValidation: (valueType: any, validation: any) => boolean;
9
+ /**
10
+ * Validates the given value against the custom validation rules for the specified field type.
11
+ * If no custom validation rules are provided, it falls back to the default validation.
12
+ * @returns true if the value is valid according to the validation rules, false otherwise.
13
+ */
14
+ declare const customValidation: (value: any, valueType: any, validation: any) => boolean;
15
+ export default customValidation;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.validateValidation = exports.mustHaveCustomValidation = void 0;
7
+ /* eslint-disable no-shadow */
8
+ const logger_1 = __importDefault(require("../logger"));
9
+ const type_1 = require("./type");
10
+ const validators_1 = __importDefault(require("./validators"));
11
+ exports.mustHaveCustomValidation = {
12
+ [type_1.CustomFieldDefinitionType.SELECT]: true,
13
+ };
14
+ /**
15
+ * Validates the given validations object against the supported field types and their validators.
16
+ * @return true if the validation is valid, false otherwise.
17
+ */
18
+ const validateValidation = (valueType, validation) => {
19
+ if (!validation) {
20
+ if (exports.mustHaveCustomValidation[valueType]) {
21
+ logger_1.default.error(`No custom validation for custom field type ${valueType} found`);
22
+ return false;
23
+ }
24
+ return true;
25
+ }
26
+ return true;
27
+ };
28
+ exports.validateValidation = validateValidation;
29
+ /**
30
+ * Validates the given value against the custom validation rules for the specified field type.
31
+ * If no custom validation rules are provided, it falls back to the default validation.
32
+ * @returns true if the value is valid according to the validation rules, false otherwise.
33
+ */
34
+ const customValidation = (value, valueType, validation) => {
35
+ const validator = validators_1.default?.[valueType];
36
+ if (!validation || !validator) {
37
+ return (0, exports.validateValidation)(valueType, validation);
38
+ }
39
+ // Always allow null values
40
+ return value === null || validator(value, validation);
41
+ };
42
+ exports.default = customValidation;
@@ -0,0 +1,2 @@
1
+ declare const validateValue: (value: any, valueType: any, customValidations: any) => boolean;
2
+ export default validateValue;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const custom_1 = __importDefault(require("./custom"));
7
+ const type_1 = __importDefault(require("./type"));
8
+ const validateValue = (value, valueType, customValidations) => {
9
+ if (!(0, type_1.default)(value, valueType)) {
10
+ return false;
11
+ }
12
+ if (customValidations) {
13
+ return (0, custom_1.default)(value, valueType, customValidations);
14
+ }
15
+ // if (validations.required && !value) {
16
+ // return false;
17
+ // }
18
+ return true;
19
+ };
20
+ exports.default = validateValue;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Supported custom field types
3
+ */
4
+ export declare enum CustomFieldDefinitionType {
5
+ NUMBER = "number",
6
+ BOOLEAN = "boolean",
7
+ DATE = "date",
8
+ DATETIME = "datetime",
9
+ TEXT = "text",
10
+ IMAGE = "image",
11
+ SELECT = "select"
12
+ }
13
+ /**
14
+ * Validate that the given value is really of type "valueType"
15
+ * TODO: verify that required field not set to null
16
+ */
17
+ declare const validateValueType: (value: unknown, valueType: CustomFieldDefinitionType) => boolean;
18
+ export default validateValueType;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.CustomFieldDefinitionType = void 0;
7
+ const joi_1 = __importDefault(require("joi"));
8
+ /**
9
+ * Supported custom field types
10
+ */
11
+ // eslint-disable-next-line no-shadow
12
+ var CustomFieldDefinitionType;
13
+ (function (CustomFieldDefinitionType) {
14
+ CustomFieldDefinitionType["NUMBER"] = "number";
15
+ CustomFieldDefinitionType["BOOLEAN"] = "boolean";
16
+ CustomFieldDefinitionType["DATE"] = "date";
17
+ CustomFieldDefinitionType["DATETIME"] = "datetime";
18
+ CustomFieldDefinitionType["TEXT"] = "text";
19
+ CustomFieldDefinitionType["IMAGE"] = "image";
20
+ CustomFieldDefinitionType["SELECT"] = "select";
21
+ })(CustomFieldDefinitionType = exports.CustomFieldDefinitionType || (exports.CustomFieldDefinitionType = {}));
22
+ /**
23
+ * Validate that the given value is really of type "valueType"
24
+ * TODO: verify that required field not set to null
25
+ */
26
+ const validateValueType = (value, valueType) => {
27
+ if (value === null) {
28
+ // Null is always allowed
29
+ return true;
30
+ }
31
+ switch (valueType) {
32
+ case CustomFieldDefinitionType.TEXT:
33
+ return typeof value === 'string';
34
+ case CustomFieldDefinitionType.NUMBER:
35
+ return typeof value === 'number';
36
+ case CustomFieldDefinitionType.BOOLEAN:
37
+ return typeof value === 'boolean';
38
+ case CustomFieldDefinitionType.DATE:
39
+ case CustomFieldDefinitionType.DATETIME:
40
+ return !joi_1.default.date().validate(value).error;
41
+ case CustomFieldDefinitionType.SELECT:
42
+ return true; // custom validation
43
+ case CustomFieldDefinitionType.IMAGE:
44
+ return !joi_1.default.array().min(1).unique().items(joi_1.default.string().uri())
45
+ .validate(value).error;
46
+ default:
47
+ return false;
48
+ }
49
+ };
50
+ exports.default = validateValueType;
@@ -0,0 +1,12 @@
1
+ export declare enum CustomValidations {
2
+ SELECT = "select",
3
+ RANGE = "between"
4
+ }
5
+ type Validator = (value: any, validation: any) => boolean;
6
+ /**
7
+ * Validators for custom fields
8
+ */
9
+ declare const validators: {
10
+ [key: string]: Validator;
11
+ };
12
+ export default validators;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomValidations = void 0;
4
+ // eslint-disable-next-line no-shadow
5
+ var CustomValidations;
6
+ (function (CustomValidations) {
7
+ CustomValidations["SELECT"] = "select";
8
+ CustomValidations["RANGE"] = "between";
9
+ })(CustomValidations = exports.CustomValidations || (exports.CustomValidations = {}));
10
+ /**
11
+ * Validate {@link CustomValidations.ENUM Enum}
12
+ */
13
+ const validateEnum = (value, enumValues) => (Array.isArray(enumValues)
14
+ && enumValues.length > 0
15
+ && enumValues.includes(value));
16
+ /**
17
+ * Validate {@link CustomValidations.RANGE Range}
18
+ */
19
+ const validateRange = (value, range) => {
20
+ const [min, max] = range;
21
+ if (min === undefined || max === undefined) {
22
+ return false;
23
+ }
24
+ return value >= range.min && value <= range.max;
25
+ };
26
+ /**
27
+ * Validators for custom fields
28
+ */
29
+ const validators = {
30
+ [CustomValidations.SELECT]: validateEnum,
31
+ [CustomValidations.RANGE]: validateRange,
32
+ };
33
+ exports.default = validators;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.6.1-temp-2",
3
+ "version": "0.6.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -59,4 +59,4 @@
59
59
  },
60
60
  "author": "Autofleet",
61
61
  "license": "ISC"
62
- }
62
+ }
@@ -11,7 +11,6 @@ export enum CustomFieldDefinitionType {
11
11
  TEXT = 'text',
12
12
  IMAGE = 'image',
13
13
  SELECT = 'select',
14
- FILE = 'file',
15
14
  }
16
15
  /**
17
16
  * Validate that the given value is really of type "valueType"
@@ -38,15 +37,6 @@ const validateValueType = (value: unknown, valueType: CustomFieldDefinitionType)
38
37
  case CustomFieldDefinitionType.IMAGE:
39
38
  return !Joi.array().min(1).unique().items(Joi.string().uri())
40
39
  .validate(value).error;
41
- case CustomFieldDefinitionType.FILE:
42
- return !Joi.array().min(1).unique().items(Joi.object({
43
- id: Joi.string().uuid().required(),
44
- name: Joi.string().required(),
45
- link: Joi.string().required(),
46
- createdAt: Joi.date(),
47
- addedBy: Joi.string().uuid(),
48
- }))
49
- .validate(value).error;
50
40
  default:
51
41
  return false;
52
42
  }