From e1e0e4543498b12b320dada8bc3fc22c2cbb6a59 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 19 Sep 2020 17:33:29 +0200 Subject: [PATCH] UsersService: Add `toUserDto()` converter This conversion function makes sure that a photo URL exists. Signed-off-by: David Mehren --- src/users/users.service.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/users/users.service.ts b/src/users/users.service.ts index c558775ac..9e8cb79fc 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -1,5 +1,6 @@ import { Injectable, Logger } from '@nestjs/common'; import { UserInfoDto } from './user-info.dto'; +import { User } from './user.entity'; @Injectable() export class UsersService { @@ -15,4 +16,26 @@ export class UsersService { photo: '', }; } + + getPhotoUrl(user: User) { + if (user.photo) { + return user.photo; + } else { + // TODO: Create new photo, see old code + return ''; + } + } + + toUserDto(user: User): UserInfoDto { + if (user === undefined) { + this.logger.warn('toUserDto recieved undefined argument!'); + return null; + } + return { + userName: user.userName, + displayName: user.displayName, + photo: this.getPhotoUrl(user), + email: user.email, + }; + } }