@autofleet/sadot 0.13.2-beta.3 → 0.13.2-beta.5

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,6 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.beforeBulkUpdate = exports.beforeBulkCreate = exports.beforeUpdate = exports.beforeCreate = void 0;
30
30
  const ajv_1 = __importDefault(require("ajv"));
31
+ const joi_1 = __importDefault(require("joi"));
31
32
  const ajv_formats_1 = __importDefault(require("ajv-formats"));
32
33
  const errors_1 = require("@autofleet/errors");
33
34
  const logger_1 = __importDefault(require("../utils/logger"));
@@ -196,7 +197,7 @@ const validateModel = async (instance, options, scopeAttributes, isCreate = fals
196
197
  }
197
198
  }
198
199
  };
199
- const getFieldDefinitions = async ({ modelType, modelOptions, identifiers, options }) => {
200
+ const getFieldDefinitions = async ({ modelType, modelOptions, identifiers, options, }) => {
200
201
  const { include, useEntityIdFromInclude } = modelOptions;
201
202
  const where = {
202
203
  modelType,
@@ -210,14 +211,21 @@ const getFieldDefinitions = async ({ modelType, modelOptions, identifiers, optio
210
211
  });
211
212
  return fieldDefinitions;
212
213
  };
213
- const formatDates = (fieldDefinitions = [], instance) => {
214
- fieldDefinitions.forEach((fieldDefinition) => {
214
+ const formatDates = (fieldDefinitions, instance) => {
215
+ (fieldDefinitions || []).forEach((fieldDefinition) => {
215
216
  const { fieldType, name } = fieldDefinition;
216
217
  if ([constants_1.CustomFieldDefinitionType.DATE, constants_1.CustomFieldDefinitionType.DATETIME].includes(fieldType)) {
217
218
  const value = instance.customFields?.[name];
218
219
  if (value) {
220
+ const { value: joiValue, error: validationError } = joi_1.default.date().validate(value);
221
+ if (validationError) {
222
+ throw new errors_2.InvalidValueError(value, name, validationError);
223
+ }
219
224
  // eslint-disable-next-line no-param-reassign
220
- instance.customFields[name] = new Date(value).toISOString();
225
+ const typeofjoi = typeof joiValue;
226
+ logger_1.default.info('sadot - formatting date', { name, value, joiValue, type: typeof joiValue });
227
+ instance.customFields[name] = joiValue.toISOString();
228
+ new Date().toString;
221
229
  }
222
230
  }
223
231
  });
@@ -232,7 +240,7 @@ const beforeCreate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCu
232
240
  const identifiers = (0, scopeAttributes_1.default)(instance, scopeAttributes);
233
241
  // Step 1: Handle custom fields default values and required fields
234
242
  const fieldDefinitions = await getFieldDefinitions({
235
- modelType, modelOptions, identifiers, options
243
+ modelType, modelOptions, identifiers, options,
236
244
  });
237
245
  // Apply default values
238
246
  const fieldsWithDefaultValue = fieldDefinitions.filter((def) => ![null, undefined].includes(def.defaultValue));
@@ -290,7 +298,7 @@ const beforeUpdate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCu
290
298
  const modelType = instance.constructor.name;
291
299
  const identifiers = (0, scopeAttributes_1.default)(instance, scopeAttributes);
292
300
  const fieldDefinitions = await getFieldDefinitions({
293
- modelType, modelOptions, identifiers, options
301
+ modelType, modelOptions, identifiers, options,
294
302
  });
295
303
  // Step 1: Validate the model data (including custom fields)
296
304
  await validateModel(instance, options, scopeAttributes, false);
package/dist/index.js CHANGED
@@ -54,7 +54,10 @@ const useCustomFields = async (app, getModel, options) => {
54
54
  }
55
55
  // The order is important
56
56
  (0, init_1.addHooks)(models, getModel, { useCustomFieldsEntries });
57
- await (0, models_1.initTables)(sequelize, options.getUser, { useCustomFieldsEntries });
57
+ await (0, models_1.initTables)(sequelize, options.getUser, { useCustomFieldsEntries }).catch((e) => {
58
+ logger_1.default.error('sadot - error while initializing custom fields tables', e);
59
+ throw e;
60
+ });
58
61
  (0, init_1.addScopes)(models, getModel, { useCustomFieldsEntries });
59
62
  (0, init_1.applyCustomAssociation)(models);
60
63
  logger_1.default.debug('sadot - custom fields finished initializing with models', models);
@@ -26,7 +26,7 @@ exports.CustomValidator = CustomValidator_1.default;
26
26
  const productionModels = [CustomFieldDefinition_1.default, CustomFieldValue_1.default, CustomValidator_1.default];
27
27
  const testModels = [TestModel_1.default, AssociatedTestModel_1.default, ContextAwareTestModel_1.default, ContextTestModel_1.default];
28
28
  const SADOT_MIGRATION_PREFIX = 'sadot-migration';
29
- const SCHEMA_VERSION = 'fb0fa867-1241-4816-b08d-5ed9060c7ae5';
29
+ const SCHEMA_VERSION = '49c9dd1d-b1cc-445b-a911-fd349d783110';
30
30
  const initTables = async (sequelize, getUser, { schemaPrefix, schemaVersion, useCustomFieldsEntries, } = {
31
31
  schemaPrefix: SADOT_MIGRATION_PREFIX,
32
32
  schemaVersion: SCHEMA_VERSION,
@@ -87,10 +87,13 @@ const initTables = async (sequelize, getUser, { schemaPrefix, schemaVersion, use
87
87
  timestamps: false,
88
88
  schema: 'public',
89
89
  });
90
+ logger_1.default.info('custom-fields: starting migrations');
90
91
  const migrations = await SequelizeMeta.findAll({ where: { name: { [sequelize_1.Op.like]: `${schemaPrefix}%` } }, raw: true });
91
92
  const currentSadotSchemaVersion = migrations.at(-1);
92
93
  const expectedSchemaVersionIndex = migrations.findIndex((m) => m.name === CUSTOM_FIELDS_SCHEMA_VERSION);
94
+ logger_1.default.info('custom-fields: migrations', { migrations, currentSadotSchemaVersion, expectedSchemaVersionIndex });
93
95
  if (!currentSadotSchemaVersion || currentSadotSchemaVersion.name !== CUSTOM_FIELDS_SCHEMA_VERSION) {
96
+ logger_1.default.info('custom-fields: syncing models');
94
97
  await CustomFieldDefinition_1.default.sync({ alter: true });
95
98
  await CustomFieldValue_1.default.sync({ alter: true });
96
99
  // T.Y TODO: Remove the if statement once we're ready to add the new entries table for all MS
@@ -0,0 +1,9 @@
1
+ const SADOT_MIGRATION_PREFIX = 'sadot-migration';
2
+ const SCHEMA_VERSION = '49c9dd1d-b1cc-445b-a911-fd349d783110';
3
+ const initTables = async ({ schemaPrefix = SADOT_MIGRATION_PREFIX, schemaVersion = SCHEMA_VERSION, useCustomFieldsEntries = false }) => {
4
+ console.log('custom-fields: initialize custom-fields tables', {
5
+ schemaPrefix, schemaVersion, useCustomFieldsEntries,
6
+ });
7
+ }
8
+
9
+ initTables({useCustomFieldsEntries: true});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.13.2-beta.3",
3
+ "version": "0.13.2-beta.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -29,14 +29,10 @@ const initTables = async (
29
29
  sequelize: Sequelize,
30
30
  getUser: CustomFieldOptions['getUser'],
31
31
  {
32
- schemaPrefix,
33
- schemaVersion,
34
- useCustomFieldsEntries,
35
- }: InitTablesOptions = {
36
- schemaPrefix: SADOT_MIGRATION_PREFIX,
37
- schemaVersion: SCHEMA_VERSION,
38
- useCustomFieldsEntries: false,
39
- },
32
+ schemaPrefix = SADOT_MIGRATION_PREFIX,
33
+ schemaVersion = SCHEMA_VERSION,
34
+ useCustomFieldsEntries = false,
35
+ }: InitTablesOptions,
40
36
  ): Promise<void> => {
41
37
  const CUSTOM_FIELDS_SCHEMA_VERSION = `${schemaPrefix}_${schemaVersion}${useCustomFieldsEntries ? '_withEntries' : ''}`;
42
38
  logger.info('custom-fields: initialize custom-fields tables');