From d53785b768a69cb97dfd71e0d0f3b51ec36f83d0 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 22 Jul 2026 10:03:48 -0700 Subject: [PATCH] remove unused utilities, fix delete profile image issue --- server/src/api/controllers/controllerUtils.ts | 18 ------------------ server/src/api/validation/userValidation.ts | 3 ++- .../src/domain/users/user.repository.mongo.ts | 4 +--- server/src/utils/utils.ts | 17 ----------------- 4 files changed, 3 insertions(+), 39 deletions(-) delete mode 100755 server/src/utils/utils.ts diff --git a/server/src/api/controllers/controllerUtils.ts b/server/src/api/controllers/controllerUtils.ts index 45bf3fa9f..be3f46533 100755 --- a/server/src/api/controllers/controllerUtils.ts +++ b/server/src/api/controllers/controllerUtils.ts @@ -54,24 +54,6 @@ export const optionalNumber = (value: unknown, fieldName: string): number | unde throw new AppError({ message: `${fieldName} must be a number`, status: 400 }); }; -export const optionalBoolean = (value: unknown, fieldName: string): boolean | undefined => { - if (value === undefined) { - return undefined; - } - if (typeof value === "boolean") { - return value; - } - if (typeof value === "string") { - if (value === "true") { - return true; - } - if (value === "false") { - return false; - } - } - throw new AppError({ message: `${fieldName} must be a boolean`, status: 400 }); -}; - export const parseMonitorTypeFilter = (value: unknown): MonitorType | MonitorType[] | undefined => { const parseSingle = (input: unknown): MonitorType => { if (typeof input !== "string") { diff --git a/server/src/api/validation/userValidation.ts b/server/src/api/validation/userValidation.ts index 21fb55317..882c2d898 100644 --- a/server/src/api/validation/userValidation.ts +++ b/server/src/api/validation/userValidation.ts @@ -1,6 +1,6 @@ import { z } from "zod"; import { UserRoles } from "@/domain/users/user.type.js"; -import { nameValidation, lowercaseEmailValidation, passwordValidation } from "./shared.js"; +import { nameValidation, lowercaseEmailValidation, passwordValidation, booleanCoercion } from "@/api/validation/shared.js"; //**************************************** // User Validations @@ -46,6 +46,7 @@ export const editUserBodyValidation = z profilePicture: z.string().optional(), password: passwordValidation.optional(), newPassword: passwordValidation.optional(), + deleteProfileImage: booleanCoercion.optional(), }) // Changing a password requires both the current password and the new one. // The service only runs the change-password flow when both are present, so diff --git a/server/src/domain/users/user.repository.mongo.ts b/server/src/domain/users/user.repository.mongo.ts index b137c5f4c..af4b78b26 100644 --- a/server/src/domain/users/user.repository.mongo.ts +++ b/server/src/domain/users/user.repository.mongo.ts @@ -2,7 +2,6 @@ import { IUsersRepository } from "@/domain/users/user.repository.interface.js"; import { UserModel, type UserDocument } from "@/domain/users/user.model.js"; import type { User, UserProfileImage } from "@/domain/users/user.type.js"; import { GenerateAvatarImage } from "@/utils/imageProcessing.js"; -import { ParseBoolean } from "@/utils/utils.js"; import { AppError } from "@/utils/AppError.js"; import { toStringId, toDateString } from "@/utils/mongoMappers.js"; const SERVICE_NAME = "MongoUsersRepository"; @@ -87,9 +86,8 @@ class MongoUsersRepository implements IUsersRepository { const candidateUser = { ...patch }; let unsetFields: Record | undefined; - if (ParseBoolean(candidateUser.deleteProfileImage) === true) { + if (candidateUser.deleteProfileImage === true) { unsetFields = { profileImage: 1, avatarImage: 1 }; - delete candidateUser.deleteProfileImage; } else if (file) { // 1. Save the full size image candidateUser.profileImage = { diff --git a/server/src/utils/utils.ts b/server/src/utils/utils.ts deleted file mode 100755 index a82d8734f..000000000 --- a/server/src/utils/utils.ts +++ /dev/null @@ -1,17 +0,0 @@ -export const ParseBoolean = (value: boolean | string | null | undefined) => { - if (value === true || value === "true") { - return true; - } else if (value === false || value === "false" || value === null || value === undefined) { - return false; - } -}; - -export const getTokenFromHeaders = (headers: Record) => { - const authorizationHeader = headers.authorization; - if (!authorizationHeader) throw new Error("No auth headers"); - - const parts = authorizationHeader.split(" "); - if (parts.length !== 2 || parts[0] !== "Bearer") throw new Error("Invalid auth headers"); - - return parts[1]; -};