feat: new pref for debug

This commit is contained in:
Julien Froidefond
2025-02-23 13:05:16 +01:00
parent 0bbb5e1aaa
commit 1cffe6913a
5 changed files with 65 additions and 18 deletions

View File

@@ -21,39 +21,48 @@ const preferencesSchema = new mongoose.Schema(
default: false,
required: false,
},
debug: {
type: Boolean,
default: false,
required: false,
},
},
{
timestamps: true,
strict: true,
toObject: {
transform: function (doc, ret) {
// Assurez-vous que showOnlyUnread est toujours un booléen
ret.showOnlyUnread = ret.showOnlyUnread === true;
// Force la conversion en booléen
ret.showOnlyUnread = Boolean(ret.showOnlyUnread);
ret.debug = Boolean(ret.debug);
return ret;
},
},
}
);
// Middleware pour s'assurer que showOnlyUnread est toujours un booléen
// Middleware pour s'assurer que les booléens sont toujours des booléens
preferencesSchema.pre("save", function (next) {
if (this.showOnlyUnread === undefined) {
this.showOnlyUnread = false;
}
this.showOnlyUnread = this.showOnlyUnread === true;
if (this.debug === undefined) {
this.debug = false;
}
this.showOnlyUnread = Boolean(this.showOnlyUnread);
this.debug = Boolean(this.debug);
next();
});
preferencesSchema.pre("findOneAndUpdate", function (next) {
const update = this.getUpdate() as mongoose.UpdateQuery<any>;
if (
update &&
"$set" in update &&
update.$set &&
typeof update.$set === "object" &&
"showOnlyUnread" in update.$set
) {
update.$set.showOnlyUnread = update.$set.showOnlyUnread === true;
if (update && "$set" in update && update.$set && typeof update.$set === "object") {
if ("showOnlyUnread" in update.$set) {
update.$set.showOnlyUnread = Boolean(update.$set.showOnlyUnread);
}
if ("debug" in update.$set) {
update.$set.debug = Boolean(update.$set.debug);
}
}
next();
});