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

View File

@@ -10,12 +10,14 @@ export interface UserPreferences {
showThumbnails: boolean;
cacheMode: "memory" | "file";
showOnlyUnread: boolean;
debug: boolean;
}
const defaultPreferences: UserPreferences = {
showThumbnails: true,
cacheMode: "memory",
showOnlyUnread: false,
debug: false,
};
export class PreferencesService {
@@ -53,7 +55,6 @@ export class PreferencesService {
static async updatePreferences(preferences: Partial<UserPreferences>): Promise<UserPreferences> {
try {
console.log("Service - Préférences reçues pour mise à jour:", preferences);
const user = await this.getCurrentUser();
const updatedPreferences = await PreferencesModel.findOneAndUpdate(
{ userId: user.id },
@@ -61,12 +62,10 @@ export class PreferencesService {
{ new: true, upsert: true }
);
console.log("Service - Document MongoDB après mise à jour:", updatedPreferences);
const result = {
...defaultPreferences,
...updatedPreferences.toObject(),
};
console.log("Service - Résultat final:", result);
return result;
} catch (error) {
console.error("Error updating preferences:", error);