@autofleet/sadot 0.7.3-beta-4c642ed9.2 → 0.7.3

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.
@@ -63,7 +63,7 @@ const beforeCreate = (scopeAttributes, modelOptions = {}) => async (instance, op
63
63
  await ValueRepo.updateValues(modelType, instance.id, identifiers, customFields, {
64
64
  transaction: options.transaction,
65
65
  modelOptions,
66
- });
66
+ }, true);
67
67
  // eslint-disable-next-line no-param-reassign
68
68
  fields.splice(customFieldsIdx, 1);
69
69
  }
@@ -24,5 +24,5 @@ export declare const findValuesByModelIds: (modelIds: string[], options?: any) =
24
24
  */
25
25
  export declare const updateValues: (modelType: string, modelId: string, identifiers: string[], valuesToUpdate: ValuesToUpdate, options?: FindOptions & {
26
26
  modelOptions?: ModelOptions;
27
- }) => Promise<CustomFieldValue[]>;
27
+ }, defineAllDefaults?: boolean) => Promise<CustomFieldValue[]>;
28
28
  export declare const deleteValue: (id: string, options?: any) => Promise<any>;
@@ -84,7 +84,7 @@ const formatFunctions = {
84
84
  * Create new value record if not exists, but fails if value's definition not exist.
85
85
  * Return the updated values
86
86
  */
87
- const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}) => {
87
+ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}, defineAllDefaults = false) => {
88
88
  const names = Object.keys(valuesToUpdate);
89
89
  logger_1.default.debug(`custom-fields: updating values for ${modelType} ${modelId}`, {
90
90
  names,
@@ -112,8 +112,10 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
112
112
  if (valuesWithDisabledDefinitions?.length > 0) {
113
113
  logger_1.default.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
114
114
  }
115
+ const visitedFields = new Set();
115
116
  const values = names.map((name) => {
116
117
  const fieldDefinition = fieldDefinitions.find((def) => def.name === name);
118
+ visitedFields.add(fieldDefinition);
117
119
  const formatFunction = formatFunctions[fieldDefinition.fieldType];
118
120
  return {
119
121
  modelId,
@@ -122,6 +124,16 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
122
124
  customFieldDefinitionId: fieldDefinition.id,
123
125
  };
124
126
  });
127
+ if (defineAllDefaults) {
128
+ fieldDefinitions.filter((def) => !visitedFields.has(def) && ![null, undefined].includes(def.defaultValue)).forEach(({ id, defaultValue }) => {
129
+ values.push({
130
+ modelId,
131
+ value: defaultValue,
132
+ updatedAt: new Date(),
133
+ customFieldDefinitionId: id,
134
+ });
135
+ });
136
+ }
125
137
  return Promise.all(values.map(async (value) => {
126
138
  const [cfv] = await models_1.CustomFieldValue.upsert(value, {
127
139
  transaction: options.transaction,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.7.3-beta-4c642ed9.2",
3
+ "version": "0.7.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -49,6 +49,7 @@ export const beforeCreate = (scopeAttributes: string[], modelOptions: ModelOptio
49
49
  transaction: options.transaction,
50
50
  modelOptions,
51
51
  },
52
+ true,
52
53
  );
53
54
 
54
55
  // eslint-disable-next-line no-param-reassign
@@ -67,6 +67,7 @@ export const updateValues = async (
67
67
  identifiers: string[],
68
68
  valuesToUpdate: ValuesToUpdate,
69
69
  options: FindOptions & { modelOptions?: ModelOptions } = {},
70
+ defineAllDefaults = false,
70
71
  ): Promise<CustomFieldValue[]> => {
71
72
  const names = Object.keys(valuesToUpdate);
72
73
  logger.debug(`custom-fields: updating values for ${modelType} ${modelId}`, {
@@ -100,8 +101,11 @@ export const updateValues = async (
100
101
  logger.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
101
102
  }
102
103
 
104
+ const visitedFields = new Set<CustomFieldDefinition>();
105
+
103
106
  const values: CreateCustomFieldValue[] = names.map((name) => {
104
107
  const fieldDefinition = fieldDefinitions.find((def) => def.name === name);
108
+ visitedFields.add(fieldDefinition);
105
109
  const formatFunction = formatFunctions[fieldDefinition.fieldType];
106
110
  return {
107
111
  modelId,
@@ -111,6 +115,17 @@ export const updateValues = async (
111
115
  };
112
116
  });
113
117
 
118
+ if (defineAllDefaults) {
119
+ fieldDefinitions.filter((def) => !visitedFields.has(def) && ![null, undefined].includes(def.defaultValue)).forEach(({ id, defaultValue }) => {
120
+ values.push({
121
+ modelId,
122
+ value: defaultValue,
123
+ updatedAt: new Date(),
124
+ customFieldDefinitionId: id,
125
+ });
126
+ });
127
+ }
128
+
114
129
  return Promise.all(values.map(async (value) => {
115
130
  const [cfv] = await CustomFieldValue.upsert(value, {
116
131
  transaction: options.transaction,