Skip to content

Commit

Permalink
Merge pull request #2192 from Inist-CNRS/avoid-save-field-bug
Browse files Browse the repository at this point in the history
try to avoid save field bug
  • Loading branch information
touv authored Oct 21, 2024
2 parents 383511d + dfad396 commit 9b371e2
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions src/api/models/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b371e2

Please sign in to comment.