@autofleet/sadot 0.7.2 → 0.7.3-beta-4c642ed9.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.
@@ -28,10 +28,16 @@ const ValidationSchema = joi_1.default.when('fieldType', {
28
28
  then: statusValidationObjectSchema,
29
29
  otherwise: joi_1.default.any(),
30
30
  });
31
+ const DefaultValueSchema = joi_1.default.when('fieldType', {
32
+ is: constants_1.CustomFieldDefinitionType.BOOLEAN,
33
+ then: joi_1.default.boolean(),
34
+ otherwise: joi_1.default.any(),
35
+ });
31
36
  const CustomFieldDefinitionCreationSchema = joi_1.default.object({
32
37
  name: joi_1.default.string().required(),
33
38
  displayName: joi_1.default.string().required(),
34
39
  validation: ValidationSchema,
40
+ defaultValue: DefaultValueSchema,
35
41
  fieldType: joi_1.default.string().valid(...Object.values(constants_1.CustomFieldDefinitionType)).required(),
36
42
  entityId: joi_1.default.string().guid().required(),
37
43
  entityType: joi_1.default.string().required(),
@@ -42,6 +48,7 @@ const CustomFieldDefinitionCreationSchema = joi_1.default.object({
42
48
  const CustomFieldDefinitionUpdateSchema = joi_1.default.object({
43
49
  displayName: joi_1.default.string(),
44
50
  validation: ValidationSchema,
51
+ defaultValue: DefaultValueSchema,
45
52
  fieldType: joi_1.default.string().valid(...Object.values(constants_1.CustomFieldDefinitionType)),
46
53
  description: joi_1.default.string().allow(null),
47
54
  required: joi_1.default.boolean(),
@@ -13,6 +13,7 @@ declare class CustomFieldDefinition extends Model {
13
13
  description?: string;
14
14
  required?: boolean;
15
15
  disabled?: boolean;
16
+ defaultValue?: any;
16
17
  createdAt?: Date;
17
18
  updatedAt?: Date;
18
19
  deletedAt?: Date;
@@ -19,12 +19,19 @@ const _1 = require(".");
19
19
  const events_1 = require("../events");
20
20
  const errors_1 = require("../errors");
21
21
  const logger_1 = __importDefault(require("../utils/logger"));
22
+ const validations_1 = require("../utils/validations");
22
23
  let CustomFieldDefinition = class CustomFieldDefinition extends sequelize_typescript_1.Model {
23
24
  static displayNameDefaultValue(instance) {
24
25
  if (!instance?.displayName) {
25
26
  // eslint-disable-next-line no-param-reassign
26
27
  instance.displayName = instance.name;
27
28
  }
29
+ if (![null, undefined].includes(instance.defaultValue)) {
30
+ const isValid = (0, validations_1.validateValue)(instance.defaultValue, instance.fieldType, instance.validation);
31
+ if (!isValid) {
32
+ throw new errors_1.InvalidValueError(instance.defaultValue, instance.fieldType);
33
+ }
34
+ }
28
35
  }
29
36
  static afterSaveHandler(instance, options) {
30
37
  if (options.transaction) {
@@ -116,6 +123,13 @@ __decorate([
116
123
  }),
117
124
  __metadata("design:type", Boolean)
118
125
  ], CustomFieldDefinition.prototype, "disabled", void 0);
126
+ __decorate([
127
+ (0, sequelize_typescript_1.Column)({
128
+ type: sequelize_typescript_1.DataType.JSONB,
129
+ allowNull: true,
130
+ }),
131
+ __metadata("design:type", Object)
132
+ ], CustomFieldDefinition.prototype, "defaultValue", void 0);
119
133
  __decorate([
120
134
  sequelize_typescript_1.Column,
121
135
  __metadata("design:type", Date)
@@ -22,7 +22,7 @@ exports.AssociatedTestModel = AssociatedTestModel_1.default;
22
22
  const productionModels = [CustomFieldDefinition_1.default, CustomFieldValue_1.default];
23
23
  const testModels = [TestModel_1.default, AssociatedTestModel_1.default, ContextAwareTestModel_1.default, ContextTestModel_1.default];
24
24
  const SADOT_MIGRATION_PREFIX = 'sadot-migration';
25
- const SCHEMA_VERSION = 1;
25
+ const SCHEMA_VERSION = 2;
26
26
  const CUSTOM_FIELDS_SCHEMA_VERSION = `${SADOT_MIGRATION_PREFIX}_${SCHEMA_VERSION}`;
27
27
  const initTables = async (sequelize, getUser) => {
28
28
  logger_1.default.info('custom-fields: initialize custom-fields tables');
@@ -117,7 +117,7 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
117
117
  const formatFunction = formatFunctions[fieldDefinition.fieldType];
118
118
  return {
119
119
  modelId,
120
- value: formatFunction ? formatFunction(valuesToUpdate[name]) : valuesToUpdate[name],
120
+ value: (formatFunction ? formatFunction(valuesToUpdate[name]) : valuesToUpdate[name]) ?? fieldDefinition.defaultValue,
121
121
  updatedAt: new Date(),
122
122
  customFieldDefinitionId: fieldDefinition.id,
123
123
  };
@@ -14,6 +14,7 @@ export declare const coolFieldDefinition2: {
14
14
  createdAt?: Date;
15
15
  updatedAt?: Date;
16
16
  deletedAt?: Date;
17
+ defaultValue?: any;
17
18
  displayName?: string;
18
19
  validation?: any;
19
20
  fieldType: string;
@@ -29,6 +30,7 @@ export declare const coolFieldDefinition3: {
29
30
  createdAt?: Date;
30
31
  updatedAt?: Date;
31
32
  deletedAt?: Date;
33
+ defaultValue?: any;
32
34
  displayName?: string;
33
35
  validation?: any;
34
36
  fieldType: string;
@@ -56,6 +56,7 @@ const createDefinition = (defaults) => ({
56
56
  fieldType: defaults?.fieldType || 'boolean',
57
57
  entityId: defaults?.entityId || (0, uuid_1.v4)(),
58
58
  entityType: defaults?.entityType || 'fleetId',
59
+ ...(defaults?.defaultValue && { defaultValue: defaults.defaultValue }),
59
60
  });
60
61
  exports.createDefinition = createDefinition;
61
62
  const createDefinitions = (defaults, length = 1) => (Array(length).fill({}).map((_) => ({
@@ -3,6 +3,7 @@ export interface CustomFieldDefinitionDTO {
3
3
  name: string;
4
4
  displayName?: string;
5
5
  validation?: any;
6
+ defaultValue?: any;
6
7
  fieldType: string;
7
8
  entityId: string;
8
9
  entityType: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.7.2",
3
+ "version": "0.7.3-beta-4c642ed9.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -26,10 +26,17 @@ const ValidationSchema = Joi.when('fieldType', {
26
26
  otherwise: Joi.any(),
27
27
  });
28
28
 
29
+ const DefaultValueSchema = Joi.when('fieldType', {
30
+ is: CustomFieldDefinitionType.BOOLEAN,
31
+ then: Joi.boolean(),
32
+ otherwise: Joi.any(),
33
+ });
34
+
29
35
  const CustomFieldDefinitionCreationSchema = Joi.object({
30
36
  name: Joi.string().required(),
31
37
  displayName: Joi.string().required(),
32
38
  validation: ValidationSchema,
39
+ defaultValue: DefaultValueSchema,
33
40
  fieldType: Joi.string().valid(...Object.values(CustomFieldDefinitionType)).required(),
34
41
  entityId: Joi.string().guid().required(),
35
42
  entityType: Joi.string().required(),
@@ -41,6 +48,7 @@ const CustomFieldDefinitionCreationSchema = Joi.object({
41
48
  const CustomFieldDefinitionUpdateSchema = Joi.object({
42
49
  displayName: Joi.string(),
43
50
  validation: ValidationSchema,
51
+ defaultValue: DefaultValueSchema,
44
52
  fieldType: Joi.string().valid(...Object.values(CustomFieldDefinitionType)),
45
53
  description: Joi.string().allow(null),
46
54
  required: Joi.boolean(),
@@ -14,8 +14,9 @@ import { CustomFieldDefinitionType } from '../utils/constants';
14
14
  import { CustomValidationTypes } from '../utils/validations/validators';
15
15
  import { CustomFieldValue } from '.';
16
16
  import { sendDimEvent } from '../events';
17
- import { UnsupportedCustomFieldTypeError, UnsupportedCustomValidationError } from '../errors';
17
+ import { InvalidValueError, UnsupportedCustomFieldTypeError, UnsupportedCustomValidationError } from '../errors';
18
18
  import logger from '../utils/logger';
19
+ import { validateValue } from '../utils/validations';
19
20
 
20
21
  @DefaultScope(() => ({ where: { disabled: false } }))
21
22
  @Table({
@@ -109,6 +110,12 @@ class CustomFieldDefinition extends Model {
109
110
  })
110
111
  disabled?: boolean;
111
112
 
113
+ @Column({
114
+ type: DataType.JSONB,
115
+ allowNull: true,
116
+ })
117
+ defaultValue?: any;
118
+
112
119
  @Column
113
120
  createdAt?: Date;
114
121
 
@@ -127,6 +134,12 @@ class CustomFieldDefinition extends Model {
127
134
  // eslint-disable-next-line no-param-reassign
128
135
  instance.displayName = instance.name;
129
136
  }
137
+ if (![null, undefined].includes(instance.defaultValue)) {
138
+ const isValid = validateValue(instance.defaultValue, instance.fieldType, instance.validation);
139
+ if (!isValid) {
140
+ throw new InvalidValueError(instance.defaultValue, instance.fieldType);
141
+ }
142
+ }
130
143
  }
131
144
 
132
145
  @AfterSave
@@ -13,7 +13,7 @@ const productionModels = [CustomFieldDefinition, CustomFieldValue];
13
13
  const testModels = [TestModel, AssociatedTestModel, ContextAwareTestModel, ContextTestModel];
14
14
 
15
15
  const SADOT_MIGRATION_PREFIX = 'sadot-migration';
16
- const SCHEMA_VERSION = 1;
16
+ const SCHEMA_VERSION = 2;
17
17
  const CUSTOM_FIELDS_SCHEMA_VERSION = `${SADOT_MIGRATION_PREFIX}_${SCHEMA_VERSION}`;
18
18
 
19
19
  const initTables = async (sequelize: Sequelize, getUser): Promise<void> => {
@@ -105,7 +105,7 @@ export const updateValues = async (
105
105
  const formatFunction = formatFunctions[fieldDefinition.fieldType];
106
106
  return {
107
107
  modelId,
108
- value: formatFunction ? formatFunction(valuesToUpdate[name]) : valuesToUpdate[name],
108
+ value: (formatFunction ? formatFunction(valuesToUpdate[name]) : valuesToUpdate[name]) ?? fieldDefinition.defaultValue,
109
109
  updatedAt: new Date(),
110
110
  customFieldDefinitionId: fieldDefinition.id,
111
111
  };
@@ -59,6 +59,7 @@ export const createDefinition = (defaults: Partial<CustomFieldDefinitionDTO>): C
59
59
  fieldType: defaults?.fieldType || 'boolean',
60
60
  entityId: defaults?.entityId || uuidv4(),
61
61
  entityType: defaults?.entityType || 'fleetId',
62
+ ...(defaults?.defaultValue && { defaultValue: defaults.defaultValue }),
62
63
  });
63
64
 
64
65
  export const createDefinitions = (
@@ -3,6 +3,7 @@ export interface CustomFieldDefinitionDTO {
3
3
  name: string;
4
4
  displayName?: string;
5
5
  validation?: any;
6
+ defaultValue?: any;
6
7
  fieldType: string;
7
8
  entityId: string;
8
9
  entityType: string;