@autofleet/sadot 0.7.1 → 0.7.2

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.
@@ -31,6 +31,7 @@ const models_1 = require("../models");
31
31
  const DefinitionRepo = __importStar(require("./definition"));
32
32
  const logger_1 = __importDefault(require("../utils/logger"));
33
33
  const errors_1 = require("../errors");
34
+ const constants_1 = require("../utils/constants");
34
35
  const findByModelIdAndDefinition = async (modelId, customFieldDefinitionId) => models_1.CustomFieldValue.findAll({ where: { modelId, customFieldDefinitionId }, include: [models_1.CustomFieldDefinition] });
35
36
  exports.findByModelIdAndDefinition = findByModelIdAndDefinition;
36
37
  const create = async (data, withAssociations = false) => {
@@ -66,6 +67,18 @@ const findValuesByModelIds = async (modelIds, options) => {
66
67
  });
67
68
  };
68
69
  exports.findValuesByModelIds = findValuesByModelIds;
70
+ const formatFunctions = {
71
+ [constants_1.CustomFieldDefinitionType.DATE]: (value) => {
72
+ if (value) {
73
+ const date = new Date(value);
74
+ if (date.toString() === 'Invalid Date') {
75
+ throw new Error(`Invalid date value: ${value}`);
76
+ }
77
+ return date.toISOString();
78
+ }
79
+ return null;
80
+ },
81
+ };
69
82
  /**
70
83
  * Try to update custom field values for a model instance.
71
84
  * Create new value record if not exists, but fails if value's definition not exist.
@@ -99,12 +112,16 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
99
112
  if (valuesWithDisabledDefinitions?.length > 0) {
100
113
  logger_1.default.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
101
114
  }
102
- const values = names.map((name) => ({
103
- modelId,
104
- value: valuesToUpdate[name],
105
- updatedAt: new Date(),
106
- customFieldDefinitionId: fieldDefinitions.find((def) => def.name === name).id,
107
- }));
115
+ const values = names.map((name) => {
116
+ const fieldDefinition = fieldDefinitions.find((def) => def.name === name);
117
+ const formatFunction = formatFunctions[fieldDefinition.fieldType];
118
+ return {
119
+ modelId,
120
+ value: formatFunction ? formatFunction(valuesToUpdate[name]) : valuesToUpdate[name],
121
+ updatedAt: new Date(),
122
+ customFieldDefinitionId: fieldDefinition.id,
123
+ };
124
+ });
108
125
  return Promise.all(values.map(async (value) => {
109
126
  const [cfv] = await models_1.CustomFieldValue.upsert(value, {
110
127
  transaction: options.transaction,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -6,6 +6,7 @@ import type { CreateCustomFieldValue, ValuesToUpdate } from '../types/value';
6
6
  import logger from '../utils/logger';
7
7
  import { MissingDefinitionError } from '../errors';
8
8
  import type { ModelOptions } from '../types';
9
+ import { CustomFieldDefinitionType } from '../utils/constants';
9
10
 
10
11
  export const findByModelIdAndDefinition = async (modelId: string, customFieldDefinitionId: string) =>
11
12
  CustomFieldValue.findAll({ where: { modelId, customFieldDefinitionId }, include: [CustomFieldDefinition] });
@@ -42,6 +43,19 @@ export const findValuesByModelIds = async (modelIds: string[], options?): Promis
42
43
  });
43
44
  };
44
45
 
46
+ const formatFunctions = {
47
+ [CustomFieldDefinitionType.DATE]: (value) => {
48
+ if (value) {
49
+ const date = new Date(value);
50
+ if (date.toString() === 'Invalid Date') {
51
+ throw new Error(`Invalid date value: ${value}`);
52
+ }
53
+ return date.toISOString();
54
+ }
55
+ return null;
56
+ },
57
+ };
58
+
45
59
  /**
46
60
  * Try to update custom field values for a model instance.
47
61
  * Create new value record if not exists, but fails if value's definition not exist.
@@ -86,12 +100,16 @@ export const updateValues = async (
86
100
  logger.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
87
101
  }
88
102
 
89
- const values: CreateCustomFieldValue[] = names.map((name) => ({
90
- modelId,
91
- value: valuesToUpdate[name],
92
- updatedAt: new Date(),
93
- customFieldDefinitionId: fieldDefinitions.find((def) => def.name === name).id,
94
- }));
103
+ const values: CreateCustomFieldValue[] = names.map((name) => {
104
+ const fieldDefinition = fieldDefinitions.find((def) => def.name === name);
105
+ const formatFunction = formatFunctions[fieldDefinition.fieldType];
106
+ return {
107
+ modelId,
108
+ value: formatFunction ? formatFunction(valuesToUpdate[name]) : valuesToUpdate[name],
109
+ updatedAt: new Date(),
110
+ customFieldDefinitionId: fieldDefinition.id,
111
+ };
112
+ });
95
113
 
96
114
  return Promise.all(values.map(async (value) => {
97
115
  const [cfv] = await CustomFieldValue.upsert(value, {