Make sure no userhistory exists before initializing it (#15415)

This commit is contained in:
Phillip Thelen 2025-03-26 19:37:52 +01:00 committed by GitHub
parent ded71b46c5
commit c0d5566417
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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();
}
}
}