Refactor user service to use removeBase64Prefix utility function

This commit is contained in:
faris Aljohari
2024-07-24 14:30:29 +03:00
parent 0bdee7c2ea
commit 6a8eed73bd
2 changed files with 28 additions and 10 deletions

View File

@ -0,0 +1,3 @@
export function removeBase64Prefix(dataUrl: string): string {
return dataUrl.replace(/^data:image\/[a-z]+;base64,/, '');
}

View File

@ -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,
};