Browse Source

feat: random avatar color on user creation

martabal 1 year ago
parent
commit
4f77d06592

+ 6 - 0
server/src/domain/user/response-dto/user-response.dto.ts

@@ -13,6 +13,12 @@ export enum UserAvatarColor {
   AMBER = 'amber',
   AMBER = 'amber',
 }
 }
 
 
+export const getRandomAvatarColor = (): UserAvatarColor => {
+  const values = Object.values(UserAvatarColor);
+  const randomIndex = Math.floor(Math.random() * values.length);
+  return values[randomIndex] as UserAvatarColor;
+}
+
 export class UserDto {
 export class UserDto {
   id!: string;
   id!: string;
   firstName!: string;
   firstName!: string;

+ 2 - 1
server/src/domain/user/user.core.ts

@@ -4,6 +4,7 @@ import path from 'path';
 import sanitize from 'sanitize-filename';
 import sanitize from 'sanitize-filename';
 import { AuthUserDto } from '../auth';
 import { AuthUserDto } from '../auth';
 import { ICryptoRepository, ILibraryRepository, IUserRepository } from '../repositories';
 import { ICryptoRepository, ILibraryRepository, IUserRepository } from '../repositories';
+import { getRandomAvatarColor } from './response-dto/user-response.dto';
 
 
 const SALT_ROUNDS = 10;
 const SALT_ROUNDS = 10;
 
 
@@ -104,7 +105,7 @@ export class UserCore {
       if (payload.storageLabel) {
       if (payload.storageLabel) {
         payload.storageLabel = sanitize(payload.storageLabel);
         payload.storageLabel = sanitize(payload.storageLabel);
       }
       }
-
+      payload.avatarColor = getRandomAvatarColor()
       const userEntity = await this.userRepository.create(payload);
       const userEntity = await this.userRepository.create(payload);
       await this.libraryRepository.create({
       await this.libraryRepository.create({
         owner: { id: userEntity.id } as UserEntity,
         owner: { id: userEntity.id } as UserEntity,