From 6a8eed73bd0ca6f8101de0e43f938f60244de29a Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:30:29 +0300 Subject: [PATCH] Refactor user service to use removeBase64Prefix utility function --- libs/common/src/helper/removeBase64Prefix.ts | 3 ++ src/users/services/user.service.ts | 35 ++++++++++++++------ 2 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 libs/common/src/helper/removeBase64Prefix.ts diff --git a/libs/common/src/helper/removeBase64Prefix.ts b/libs/common/src/helper/removeBase64Prefix.ts new file mode 100644 index 0000000..bc4ca5f --- /dev/null +++ b/libs/common/src/helper/removeBase64Prefix.ts @@ -0,0 +1,3 @@ +export function removeBase64Prefix(dataUrl: string): string { + return dataUrl.replace(/^data:image\/[a-z]+;base64,/, ''); +} diff --git a/src/users/services/user.service.ts b/src/users/services/user.service.ts index 11262bb..b035c05 100644 --- a/src/users/services/user.service.ts +++ b/src/users/services/user.service.ts @@ -13,6 +13,7 @@ import { import { UserRepository } from '@app/common/modules/user/repositories'; import { RegionRepository } from '@app/common/modules/region/repositories'; import { TimeZoneRepository } from '@app/common/modules/timezone/repositories'; +import { removeBase64Prefix } from '@app/common/helper/removeBase64Prefix'; @Injectable() export class UserService { @@ -33,12 +34,15 @@ export class UserService { throw new BadRequestException('Invalid room UUID'); } + // Use the utility function to remove the base64 prefix + const cleanedProfilePicture = removeBase64Prefix(user.profilePicture); + return { uuid: user.uuid, email: user.email, firstName: user.firstName, lastName: user.lastName, - profilePicture: user.profilePicture, + profilePicture: cleanedProfilePicture, region: user.region, timeZone: user.timezone, }; @@ -61,17 +65,19 @@ export class UserService { { ...updateProfilePictureDataDto }, ); const updatedUser = await this.getUserDetailsByUserUuid(userUuid); + // Use the utility function to remove the base64 prefix + const cleanedProfilePicture = removeBase64Prefix( + updatedUser.profilePicture, + ); return { uuid: updatedUser.uuid, firstName: updatedUser.firstName, lastName: updatedUser.lastName, - profilePicture: updatedUser.profilePicture, + profilePicture: cleanedProfilePicture, region: updatedUser.region, timeZoneUuid: updatedUser.timeZone, }; } catch (err) { - console.log('err', err); - if (err instanceof BadRequestException) { throw err; // Re-throw BadRequestException } else { @@ -115,12 +121,15 @@ export class UserService { if (!updatedUser.region) { throw new BadRequestException('Region update failed'); } - + // Use the utility function to remove the base64 prefix + const cleanedProfilePicture = removeBase64Prefix( + updatedUser.profilePicture, + ); return { uuid: updatedUser.uuid, firstName: updatedUser.firstName, lastName: updatedUser.lastName, - profilePicture: updatedUser.profilePicture, + profilePicture: cleanedProfilePicture, region: updatedUser.region, timeZoneUuid: updatedUser.timeZone, }; @@ -167,12 +176,15 @@ export class UserService { if (!updatedUser.timeZone) { throw new BadRequestException('Timezone update failed'); } - + // Use the utility function to remove the base64 prefix + const cleanedProfilePicture = removeBase64Prefix( + updatedUser.profilePicture, + ); return { uuid: updatedUser.uuid, firstName: updatedUser.firstName, lastName: updatedUser.lastName, - profilePicture: updatedUser.profilePicture, + profilePicture: cleanedProfilePicture, region: updatedUser.region, timeZoneUuid: updatedUser.timeZone, }; @@ -206,12 +218,15 @@ export class UserService { if (!updatedUser.firstName || !updatedUser.lastName) { throw new BadRequestException('First Name and Last Name update failed'); } - + // Use the utility function to remove the base64 prefix + const cleanedProfilePicture = removeBase64Prefix( + updatedUser.profilePicture, + ); return { uuid: updatedUser.uuid, firstName: updatedUser.firstName, lastName: updatedUser.lastName, - profilePicture: updatedUser.profilePicture, + profilePicture: cleanedProfilePicture, region: updatedUser.region, timeZoneUuid: updatedUser.timeZone, };