From 319131237208f12ce657954c3ea55f0394a69273 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Fri, 23 Feb 2024 22:12:36 +0100 Subject: [PATCH] =?UTF-8?q?-=20Bitcoin=20-=20Circuito=20non=20aggiungere?= =?UTF-8?q?=20se=20gi=C3=A0=20esiste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/models/user.js | 17 +++++++++++------ src/server/populate/contribtypes.js | 6 ++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/server/models/user.js b/src/server/models/user.js index 482f30e..70fc370 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -2057,15 +2057,20 @@ UserSchema.statics.addCircuitToUser = async function (idapp, usernameOrig, circu } else { + // prima di aggiungerlo controlla se esiste già ! + let update = { - $push: { + $addToSet: { // Utilizziamo $addToSet invece di $push per garantire che l'elemento venga aggiunto solo se non esiste già 'profile.mycircuits': { - circuitname, - date: new Date(), - }, - }, + $each: [{ + circuitname, + date: new Date(), + }], + } + } }; - ris = await User.updateOne({ idapp, username: usernameOrig }, update); + + ris = await User.updateOne({ idapp, username: usernameOrig, 'profile.mycircuits': { $not: { $elemMatch: { circuitname } } } }, update); if (confido) { // Elimina la richiesta: diff --git a/src/server/populate/contribtypes.js b/src/server/populate/contribtypes.js index 8c3a987..eb6f8c0 100644 --- a/src/server/populate/contribtypes.js +++ b/src/server/populate/contribtypes.js @@ -173,5 +173,11 @@ module.exports = { "label" : "Euro", "__v" : 0 }, + { + "_id" : ObjectID("51bc482667de9a1f64b254ff"), + "idapp" : "13", + "label" : "Bitcoin", + "__v" : 0 + }, ] }