@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 +1 -1
- package/src/hooks/hooks.ts +96 -93
- package/src/models/index.ts +1 -1
package/package.json
CHANGED
package/src/hooks/hooks.ts
CHANGED
|
@@ -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)
|
|
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
|
-
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
+
logger.debug('sadot - before create hook');
|
|
282
|
+
const { fields } = options;
|
|
283
|
+
const modelType = instance.constructor.name;
|
|
281
284
|
|
|
282
|
-
|
|
285
|
+
const identifiers = applyScopeToInstance(instance, scopeAttributes);
|
|
283
286
|
|
|
284
|
-
|
|
287
|
+
// Step 1: Handle custom fields default values and required fields
|
|
285
288
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
+
const fieldDefinitions = await getFieldDefinitions({
|
|
290
|
+
modelType, modelOptions, identifiers, options,
|
|
291
|
+
});
|
|
289
292
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
315
|
-
|
|
317
|
+
// Step 2: Validate the model data (including custom fields)
|
|
318
|
+
await validateModel(instance, options, scopeAttributes, true);
|
|
316
319
|
|
|
317
|
-
|
|
318
|
-
|
|
320
|
+
// format date and datetime fields
|
|
321
|
+
formatDates(fieldDefinitions, instance);
|
|
319
322
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
-
|
|
341
|
-
|
|
342
|
-
|
|
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
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
+
const fieldDefinitions = await getFieldDefinitions({
|
|
365
|
+
modelType, modelOptions, identifiers, options,
|
|
366
|
+
});
|
|
364
367
|
|
|
365
|
-
|
|
366
|
-
|
|
368
|
+
// Step 1: Validate the model data (including custom fields)
|
|
369
|
+
await validateModel(instance, options, scopeAttributes, false);
|
|
367
370
|
|
|
368
|
-
|
|
369
|
-
|
|
371
|
+
// format date and datetime fields
|
|
372
|
+
formatDates(fieldDefinitions, instance);
|
|
370
373
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
+
if (!Object.keys(customFields).length) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
379
382
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
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
|
-
|
|
394
|
-
|
|
395
|
-
|
|
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
|
package/src/models/index.ts
CHANGED
|
@@ -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 = '
|
|
26
|
+
const SCHEMA_VERSION = '49c9dd1d-b1cc-445b-a911-fd349d783110';
|
|
27
27
|
|
|
28
28
|
const initTables = async (
|
|
29
29
|
sequelize: Sequelize,
|