@autofleet/sadot 0.13.2-beta.0 → 0.13.2-beta.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.13.2-beta.0",
3
+ "version": "0.13.2-beta.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -253,12 +253,15 @@ const formatDates = (fieldDefinitions: CustomFieldDefinition[], instance: any) =
253
253
  if ([CustomFieldDefinitionType.DATE, CustomFieldDefinitionType.DATETIME].includes(fieldType)) {
254
254
  const value = instance.customFields?.[name];
255
255
  if (value) {
256
- const validationError = Joi.date().validate(value).error;
256
+ const { value: joiValue, error: validationError } = Joi.date().validate(value);
257
257
  if (validationError) {
258
258
  throw new InvalidValueError(value, name, validationError);
259
259
  }
260
260
  // eslint-disable-next-line no-param-reassign
261
- instance.customFields[name] = new Date(value).toISOString();
261
+ const typeofjoi = typeof joiValue;
262
+ logger.info('sadot - formatting date', { name, value, joiValue, type: typeof joiValue });
263
+ instance.customFields[name] = joiValue.toISOString();
264
+ new Date().toString
262
265
  }
263
266
  }
264
267
  });
@@ -275,72 +278,72 @@ export const beforeCreate = (
275
278
  instance,
276
279
  options,
277
280
  ): Promise<void> => {
278
- logger.debug('sadot - before create hook');
279
- const { fields } = options;
280
- const modelType = instance.constructor.name;
281
+ logger.debug('sadot - before create hook');
282
+ const { fields } = options;
283
+ const modelType = instance.constructor.name;
281
284
 
282
- const identifiers = applyScopeToInstance(instance, scopeAttributes);
285
+ const identifiers = applyScopeToInstance(instance, scopeAttributes);
283
286
 
284
- // Step 1: Handle custom fields default values and required fields
287
+ // Step 1: Handle custom fields default values and required fields
285
288
 
286
- const fieldDefinitions = await getFieldDefinitions({
287
- modelType, modelOptions, identifiers, options,
288
- });
289
+ const fieldDefinitions = await getFieldDefinitions({
290
+ modelType, modelOptions, identifiers, options,
291
+ });
289
292
 
290
- // Apply default values
291
- const fieldsWithDefaultValue = fieldDefinitions.filter((def) => ![null, undefined].includes(def.defaultValue));
292
- if (fieldsWithDefaultValue.length) {
293
- // eslint-disable-next-line no-param-reassign
294
- instance.customFields ||= {};
295
- fieldsWithDefaultValue
296
- .filter((def) => (instance.customFields?.[def.name] === undefined))
297
- .forEach(({ name, defaultValue }) => {
298
- // eslint-disable-next-line no-param-reassign
299
- instance.customFields[name] = defaultValue;
300
- });
301
- }
293
+ // Apply default values
294
+ const fieldsWithDefaultValue = fieldDefinitions.filter((def) => ![null, undefined].includes(def.defaultValue));
295
+ if (fieldsWithDefaultValue.length) {
296
+ // eslint-disable-next-line no-param-reassign
297
+ instance.customFields ||= {};
298
+ fieldsWithDefaultValue
299
+ .filter((def) => (instance.customFields?.[def.name] === undefined))
300
+ .forEach(({ name, defaultValue }) => {
301
+ // eslint-disable-next-line no-param-reassign
302
+ instance.customFields[name] = defaultValue;
303
+ });
304
+ }
302
305
 
303
- // Check for required fields
304
- const requiredFieldsNames = Array.from(
305
- new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)),
306
- );
307
- const { customFields } = instance;
308
- const fieldsNames = Object.keys(customFields ?? {});
309
- const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
310
- if (missingFields?.length) {
311
- throw new MissingRequiredCustomFieldError(missingFields);
312
- }
306
+ // Check for required fields
307
+ const requiredFieldsNames = Array.from(
308
+ new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)),
309
+ );
310
+ const { customFields } = instance;
311
+ const fieldsNames = Object.keys(customFields ?? {});
312
+ const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
313
+ if (missingFields?.length) {
314
+ throw new MissingRequiredCustomFieldError(missingFields);
315
+ }
313
316
 
314
- // Step 2: Validate the model data (including custom fields)
315
- await validateModel(instance, options, scopeAttributes, true);
317
+ // Step 2: Validate the model data (including custom fields)
318
+ await validateModel(instance, options, scopeAttributes, true);
316
319
 
317
- // format date and datetime fields
318
- formatDates(fieldDefinitions, instance);
320
+ // format date and datetime fields
321
+ formatDates(fieldDefinitions, instance);
319
322
 
320
- // Step 3: Save custom field values if they exist
321
- const customFieldsIdx = fields.indexOf('customFields');
322
- if (customFieldsIdx === -1 || !customFields || !Object.keys(customFields).length) {
323
- // No custom fields to update
324
- return;
325
- }
323
+ // Step 3: Save custom field values if they exist
324
+ const customFieldsIdx = fields.indexOf('customFields');
325
+ if (customFieldsIdx === -1 || !customFields || !Object.keys(customFields).length) {
326
+ // No custom fields to update
327
+ return;
328
+ }
326
329
 
327
- // Save custom field values
328
- await updateInstanceValues({
329
- modelId: instance.id,
330
- modelType,
331
- identifiers,
332
- customFields,
333
- options: {
334
- useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
335
- transaction: options.transaction,
336
- modelOptions,
337
- },
338
- });
330
+ // Save custom field values
331
+ await updateInstanceValues({
332
+ modelId: instance.id,
333
+ modelType,
334
+ identifiers,
335
+ customFields,
336
+ options: {
337
+ useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
338
+ transaction: options.transaction,
339
+ modelOptions,
340
+ },
341
+ });
339
342
 
340
- // Remove customFields from fields array after handling
341
- // eslint-disable-next-line no-param-reassign
342
- fields.splice(customFieldsIdx, 1);
343
- };
343
+ // Remove customFields from fields array after handling
344
+ // eslint-disable-next-line no-param-reassign
345
+ fields.splice(customFieldsIdx, 1);
346
+ };
344
347
 
345
348
  /**
346
349
  * Hook to handle validation and custom fields during update
@@ -353,48 +356,48 @@ export const beforeUpdate = (
353
356
  instance,
354
357
  options,
355
358
  ): Promise<void> => {
356
- logger.debug('sadot - before update hook');
357
- const { fields } = options;
358
- const modelType = instance.constructor.name;
359
- const identifiers = applyScopeToInstance(instance, scopeAttributes);
359
+ logger.debug('sadot - before update hook');
360
+ const { fields } = options;
361
+ const modelType = instance.constructor.name;
362
+ const identifiers = applyScopeToInstance(instance, scopeAttributes);
360
363
 
361
- const fieldDefinitions = await getFieldDefinitions({
362
- modelType, modelOptions, identifiers, options,
363
- });
364
+ const fieldDefinitions = await getFieldDefinitions({
365
+ modelType, modelOptions, identifiers, options,
366
+ });
364
367
 
365
- // Step 1: Validate the model data (including custom fields)
366
- await validateModel(instance, options, scopeAttributes, false);
368
+ // Step 1: Validate the model data (including custom fields)
369
+ await validateModel(instance, options, scopeAttributes, false);
367
370
 
368
- // format date and datetime fields
369
- formatDates(fieldDefinitions, instance);
371
+ // format date and datetime fields
372
+ formatDates(fieldDefinitions, instance);
370
373
 
371
- // Step 2: Update custom field values if they exist
372
- const customFieldsIdx = fields.indexOf('customFields');
373
- if (customFieldsIdx > -1) {
374
- const { customFields } = instance;
374
+ // Step 2: Update custom field values if they exist
375
+ const customFieldsIdx = fields.indexOf('customFields');
376
+ if (customFieldsIdx > -1) {
377
+ const { customFields } = instance;
375
378
 
376
- if (!Object.keys(customFields).length) {
377
- return;
378
- }
379
+ if (!Object.keys(customFields).length) {
380
+ return;
381
+ }
379
382
 
380
- // Save custom field values
381
- await updateInstanceValues({
382
- modelId: instance.id,
383
- modelType,
384
- identifiers,
385
- customFields,
386
- options: {
387
- useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
388
- transaction: options.transaction,
389
- modelOptions,
390
- },
391
- });
383
+ // Save custom field values
384
+ await updateInstanceValues({
385
+ modelId: instance.id,
386
+ modelType,
387
+ identifiers,
388
+ customFields,
389
+ options: {
390
+ useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
391
+ transaction: options.transaction,
392
+ modelOptions,
393
+ },
394
+ });
392
395
 
393
- // Remove customFields from fields array after handling
394
- // eslint-disable-next-line no-param-reassign
395
- fields.splice(customFieldsIdx, 1);
396
- }
397
- };
396
+ // Remove customFields from fields array after handling
397
+ // eslint-disable-next-line no-param-reassign
398
+ fields.splice(customFieldsIdx, 1);
399
+ }
400
+ };
398
401
 
399
402
  /**
400
403
  * Hook to enable individual hooks for bulk create operations
@@ -23,7 +23,7 @@ const productionModels: ProductionModel[] = [CustomFieldDefinition, CustomFieldV
23
23
  const testModels = [TestModel, AssociatedTestModel, ContextAwareTestModel, ContextTestModel];
24
24
 
25
25
  const SADOT_MIGRATION_PREFIX = 'sadot-migration';
26
- const SCHEMA_VERSION = 'fb0fa867-1241-4816-b08d-5ed9060c7ae5';
26
+ const SCHEMA_VERSION = '49c9dd1d-b1cc-445b-a911-fd349d783110';
27
27
 
28
28
  const initTables = async (
29
29
  sequelize: Sequelize,