Skip to content

Commit

Permalink
Merge pull request #139 from konecty/fix/autonumber
Browse files Browse the repository at this point in the history
Fix: autoNumber fields duplicating
  • Loading branch information
CaduGomes authored Jun 17, 2024
2 parents 08e9f7b + a11dfa3 commit 580051a
Showing 1 changed file with 6 additions and 38 deletions.
44 changes: 6 additions & 38 deletions src/imports/data/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,12 @@ export async function create({ authTokenId, document, data, contextUser, upsert,
tracingSpan?.addEvent('Validate&processValueFor all fields');
const validateAllFieldsResult = await BluebirdPromise.mapSeries(Object.keys(metaObject.fields), async key => {
const value = cleanedData[key];
const field = metaObject.fields[key];

if (field.type === 'autoNumber' && (ignoreAutoNumber || value != null)) {
return successReturn();
}

const result = await validateAndProcessValueFor({
meta: metaObject,
fieldName: key,
Expand Down Expand Up @@ -704,44 +710,6 @@ export async function create({ authTokenId, document, data, contextUser, upsert,
}
}

tracingSpan?.addEvent('Processing autoNumber');
const autoNumberResult = await BluebirdPromise.mapSeries(Object.keys(metaObject.fields), async key => {
const field = metaObject.fields[key];
if (field.type === 'autoNumber') {
const value = get(data, key);

if (ignoreAutoNumber !== true || value == null) {
const autoNumberResult = await validateAndProcessValueFor({
meta: metaObject,
fieldName: key,
value,
actionType: 'insert',
objectOriginalValues: data,
objectNewValues: cleanedData,
});

if (autoNumberResult.success === false) {
return autoNumberResult;
}
if (autoNumberResult.data != null) {
cleanedData[key] = autoNumberResult.data;
}
} else {
cleanedData[key] = value;
}
}
return successReturn();
});

if (autoNumberResult.some(result => result.success === false)) {
return errorReturn(
autoNumberResult
.filter(result => result.success === false)
.map(result => result.errors)
.flat(),
);
}

if (Object.keys(cleanedData).length > 0) {
const insertedQuery = {};

Expand Down

0 comments on commit 580051a

Please sign in to comment.