From dfad3965c0938754fe9edc9b439258588dd50326 Mon Sep 17 00:00:00 2001 From: Nicolas Thouvenin Date: Fri, 18 Oct 2024 16:38:06 +0200 Subject: [PATCH] try to avoid save field bug --- src/api/models/field.js | 54 ++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/api/models/field.js b/src/api/models/field.js index 1baa8d796..922997046 100644 --- a/src/api/models/field.js +++ b/src/api/models/field.js @@ -157,34 +157,38 @@ export default async (db) => { const objectId = new ObjectID(id); const previousFieldVersion = await collection.findOneById(id); - if (previousFieldVersion.position > field.position) { - await collection.updateMany( - { - _id: { $ne: objectId }, - position: { - $gte: field.position, - $lt: previousFieldVersion.position, + if (previousFieldVersion) { + if (previousFieldVersion.position > field.position) { + await collection.updateMany( + { + _id: { $ne: objectId }, + position: { + $gte: field.position, + $lt: previousFieldVersion.position, + }, }, - }, - { - $inc: { position: 1 }, - }, - ); - } + { + $inc: { position: 1 }, + }, + ); + } - if (previousFieldVersion.position < field.position) { - await collection.updateMany( - { - _id: { $ne: objectId }, - position: { - $gt: previousFieldVersion.position, - $lte: field.position, + if (previousFieldVersion.position < field.position) { + await collection.updateMany( + { + _id: { $ne: objectId }, + position: { + $gt: previousFieldVersion.position, + $lte: field.position, + }, }, - }, - { - $inc: { position: -1 }, - }, - ); + { + $inc: { position: -1 }, + }, + ); + } + } else { + console.warn(`field #${id} is not found`); } return collection