Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: autoNumber fields duplicating #139

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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