From c0d5566417208d2e031c89edad1a4ec807b029d3 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Wed, 26 Mar 2025 19:37:52 +0100 Subject: [PATCH] Make sure no userhistory exists before initializing it (#15415) --- website/server/models/user/hooks.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/website/server/models/user/hooks.js b/website/server/models/user/hooks.js index 3fd58fd492..1ffec99762 100644 --- a/website/server/models/user/hooks.js +++ b/website/server/models/user/hooks.js @@ -365,9 +365,13 @@ schema.pre('save', true, async function preSaveUser (next, done) { } if (!this.flags.initializedUserHistory) { this.flags.initializedUserHistory = true; - const history = UserHistory(); - history.userId = this._id; - await history.save(); + // Ensure that it does not try to create a new history if it already exists + const existingHistory = await UserHistory.findOne({ userId: this._id }); + if (!existingHistory) { + const history = UserHistory(); + history.userId = this._id; + await history.save(); + } } }