@autofleet/sadot 0.7.0 → 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.
- package/dist/repository/value.js +23 -6
- package/dist/scopes/filter.js +4 -1
- package/package.json +1 -1
- package/src/repository/value.ts +24 -6
- package/src/scopes/filter.ts +4 -1
package/dist/repository/value.js
CHANGED
|
@@ -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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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/dist/scopes/filter.js
CHANGED
|
@@ -110,7 +110,10 @@ const customFieldsSortScope = (name) => ({ replacementsMap, scopeValue: sort })
|
|
|
110
110
|
`), randomStr,
|
|
111
111
|
]);
|
|
112
112
|
});
|
|
113
|
-
const orders = Object.entries(sort).map(([,
|
|
113
|
+
const orders = Object.entries(sort).map(([, sortObject]) => {
|
|
114
|
+
const direction = typeof sortObject === 'string' ? sortObject : Object.values(sortObject)[0];
|
|
115
|
+
return sequelize_typescript_1.Sequelize.literal(`"${randomStr}" ${direction || 'ASC'}`);
|
|
116
|
+
});
|
|
114
117
|
return {
|
|
115
118
|
attributes: {
|
|
116
119
|
include: includes,
|
package/package.json
CHANGED
package/src/repository/value.ts
CHANGED
|
@@ -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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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, {
|
package/src/scopes/filter.ts
CHANGED
|
@@ -158,7 +158,10 @@ export const customFieldsSortScope = (
|
|
|
158
158
|
]);
|
|
159
159
|
});
|
|
160
160
|
|
|
161
|
-
const orders = Object.entries(sort).map(([,
|
|
161
|
+
const orders = Object.entries(sort).map(([, sortObject]) => {
|
|
162
|
+
const direction = typeof sortObject === 'string' ? sortObject : Object.values(sortObject)[0];
|
|
163
|
+
return Sequelize.literal(`"${randomStr}" ${direction || 'ASC'}`);
|
|
164
|
+
});
|
|
162
165
|
return {
|
|
163
166
|
attributes: {
|
|
164
167
|
include: includes,
|