Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

유저는 기본 avatarUrl을 갖는다. #521

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/be.cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
FILESTORE_IMAGE_PREFIX: ${{ secrets.FILESTORE_IMAGE_PREFIX }}
FILESTORE_THUMBNAIL_PREFIX: ${{ secrets.FILESTORE_THUMBNAIL_PREFIX }}
GROUP_AVATAR_URLS: ${{ secrets.GROUP_AVATAR_URLS }}
USER_AVATAR_URLS: ${{ secrets.USER_AVATAR_URLS }}
EOF

- name: Install dependencies
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/be.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
FILESTORE_IMAGE_PREFIX: ${{ secrets.FILESTORE_IMAGE_PREFIX }}
FILESTORE_THUMBNAIL_PREFIX: ${{ secrets.FILESTORE_THUMBNAIL_PREFIX }}
GROUP_AVATAR_URLS: ${{ secrets.GROUP_AVATAR_URLS }}
USER_AVATAR_URLS: ${{ secrets.USER_AVATAR_URLS }}
EOF
- name: Install dependencies
run: npm ci
Expand Down
4 changes: 4 additions & 0 deletions BE/src/auth/application/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Cache } from 'cache-manager';
import { RefreshTokenNotFoundException } from '../exception/refresh-token-not-found.exception';
import { AuthTestModule } from '../../../test/auth/auth-test.module';
import { anyString, instance, mock, when } from 'ts-mockito';
import { AvatarHolder } from './avatar.holder';

describe('AuthService', () => {
let authService: AuthService;
Expand All @@ -41,6 +42,7 @@ describe('AuthService', () => {
],
providers: [
AuthService,
AvatarHolder,
OauthHandler,
OauthRequester,
JwtUtils,
Expand Down Expand Up @@ -76,6 +78,8 @@ describe('AuthService', () => {
response.user.userCode,
);
expect(response.user).toBeDefined();
expect(response.user.userCode).toBeDefined();
expect(response.user.avatarUrl).toBeDefined();
expect(response.accessToken).toBeDefined();
expect(response.refreshToken).toBeDefined();
expect(response.refreshToken).toBeDefined();
Expand Down
3 changes: 3 additions & 0 deletions BE/src/auth/application/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { RefreshAuthResponseDto } from '../dto/refresh-auth-response.dto';
import { RefreshTokenNotFoundException } from '../exception/refresh-token-not-found.exception';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { AvatarHolder } from './avatar.holder';

@Injectable()
export class AuthService {
Expand All @@ -23,6 +24,7 @@ export class AuthService {
private readonly oauthHandler: OauthHandler,
private readonly userCodeGenerator: UserCodeGenerator,
private readonly jwtUtils: JwtUtils,
private readonly avatarHolder: AvatarHolder,
) {}

@Transactional()
Expand Down Expand Up @@ -56,6 +58,7 @@ export class AuthService {
const newUser = User.from(userIdentifier);
const userCode = await this.userCodeGenerator.generate();
newUser.assignUserCode(userCode);
newUser.assignAvatar(this.avatarHolder.getUrl());
return await this.usersRepository.saveUser(newUser);
}

Expand Down
23 changes: 23 additions & 0 deletions BE/src/auth/application/avatar.holder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ConfigService } from '@nestjs/config';
import { AvatarHolder } from './avatar.holder';

describe('AvatarHolder Test', () => {
test('기본 avatarUrl을 환경변수로 부터 가져온다.', () => {
//given
//when
const configService = new ConfigService({
USER_AVATAR_URLS: 'url1,url2,url3,url4,url5',
});
const avatarHolder: AvatarHolder = new AvatarHolder(configService);

//then
expect(avatarHolder.getUrls()).toEqual([
'url1',
'url2',
'url3',
'url4',
'url5',
]);
expect(avatarHolder.getUrls()).toContain(avatarHolder.getUrl());
});
});
20 changes: 20 additions & 0 deletions BE/src/auth/application/avatar.holder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AvatarHolder {
private readonly defaultAvatarUrls: string[];
constructor(configService: ConfigService) {
const rawAvatarUrls = configService.get<string>('USER_AVATAR_URLS');
this.defaultAvatarUrls = rawAvatarUrls.split(',');
}

getUrls() {
return this.defaultAvatarUrls;
}

getUrl() {
const idx = Math.floor(Math.random() * this.defaultAvatarUrls.length);
return this.defaultAvatarUrls[idx];
}
}
2 changes: 2 additions & 0 deletions BE/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AccessTokenGuard } from './guard/access-token.guard';
import { CacheModule } from '@nestjs/cache-manager';
import { redisModuleOptions } from '../config/redis';
import type { RedisClientOptions } from 'redis';
import { AvatarHolder } from './application/avatar.holder';

@Global()
@Module({
Expand All @@ -32,6 +33,7 @@ import type { RedisClientOptions } from 'redis';
JwtUtils,
UserCodeGenerator,
AccessTokenGuard,
AvatarHolder,
],
exports: [JwtUtils, AccessTokenGuard],
})
Expand Down
3 changes: 2 additions & 1 deletion BE/src/config/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ export const configServiceModuleOptions = {
}),

GROUP_AVATAR_URLS: Joi.string().required(),
USER_AVATAR_URLS: Joi.string().required(),

REDIS_HOST: Joi.number().when('NODE_ENV', {
REDIS_HOST: Joi.string().when('NODE_ENV', {
is: 'production',
then: Joi.required(),
otherwise: Joi.optional(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ export class GroupAchievementService {
);
return new PaginateGroupAchievementResponse(
paginateGroupAchievementRequest,
achievements.map((achievement) =>
GroupAchievementResponse.from(achievement),
),
achievements,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,30 @@ describe('GroupAchievementController', () => {
{
id: 6,
title: 'test6',
userCode: 'ABCDEFG',
user: {
userCode: 'ABCDEF3',
avatarUrl: 'avatarUrl1',
},
categoryId: 1,
thumbnailUrl: 'thumbnail_url6',
},
{
id: 3,
title: 'test3',
userCode: 'ABCDEFG',
user: {
userCode: 'ABCDEF2',
avatarUrl: 'avatarUrl2',
},
categoryId: 1,
thumbnailUrl: 'thumbnail_url3',
},
{
id: 2,
title: 'test2',
userCode: 'ABCDEFG',
user: {
userCode: 'ABCDEF1',
avatarUrl: 'avatarUrl3',
},
categoryId: 1,
thumbnailUrl: 'thumbnail_url2',
},
Expand Down Expand Up @@ -394,21 +403,30 @@ describe('GroupAchievementController', () => {
id: 6,
thumbnailUrl: 'thumbnail_url6',
title: 'test6',
userCode: 'ABCDEFG',
user: {
avatarUrl: 'avatarUrl1',
userCode: 'ABCDEF3',
},
},
{
categoryId: 1,
id: 3,
thumbnailUrl: 'thumbnail_url3',
title: 'test3',
userCode: 'ABCDEFG',
user: {
avatarUrl: 'avatarUrl2',
userCode: 'ABCDEF2',
},
},
{
categoryId: 1,
id: 2,
thumbnailUrl: 'thumbnail_url2',
title: 'test2',
userCode: 'ABCDEFG',
user: {
avatarUrl: 'avatarUrl3',
userCode: 'ABCDEF1',
},
},
],
next: {
Expand Down
11 changes: 6 additions & 5 deletions BE/src/group/achievement/dto/group-achievement-response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { IGroupAchievementListDetail } from '../index';
import { IGroupAchievementListDetail, UserInfo } from '../index';

export class GroupAchievementResponse {
@ApiProperty({ description: 'id' })
Expand All @@ -10,20 +10,20 @@ export class GroupAchievementResponse {
title: string;
@ApiProperty({ description: 'categoryId' })
categoryId: number;
@ApiProperty({ description: 'userCode' })
userCode: string;

@ApiProperty({ description: 'user', type: UserInfo })
user: UserInfo;
constructor(
id: number,
thumbnailUrl: string,
title: string,
userCode: string,
avatarUrl: string,
categoryId: number | null,
) {
this.id = id;
this.thumbnailUrl = thumbnailUrl;
this.title = title;
this.userCode = userCode;
this.user = new UserInfo(userCode, avatarUrl);
this.categoryId = categoryId ? categoryId : -1;
}

Expand All @@ -33,6 +33,7 @@ export class GroupAchievementResponse {
groupAchievementListDetail.thumbnailUrl,
groupAchievementListDetail.title,
groupAchievementListDetail.userCode,
groupAchievementListDetail.avatarUrl,
groupAchievementListDetail.categoryId,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ describe('GroupRepository Test', () => {
// then
expect(findAll.length).toEqual(30);
expect(findAll[0].id).toEqual(last.id);
expect(findAll[0].userCode).toEqual(last.user.userCode);
expect(findAll[0].user.userCode).toEqual(last.user.userCode);
expect(findAll[0].user.avatarUrl).toEqual(last.user.avatarUrl);
expect(findAll[0].title).toEqual(last.title);
expect(findAll[0].categoryId).toEqual(last.groupCategory.id);
expect(findAll[0].thumbnailUrl).toEqual(last.image.thumbnailUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class GroupAchievementRepository extends TransactionalRepository<GroupAch
'user',
'group_achievement.user_id = user.id',
)
.addSelect(['user.user_code as userCode'])
.addSelect(['user.user_code as userCode', 'user.avatar_url as avatarUrl'])

.leftJoin('group_achievement.groupCategory', 'category')
.addSelect('category.id as categoryId')
Expand Down
10 changes: 10 additions & 0 deletions BE/src/group/achievement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IGroupAchievementListDetail {
title: string;
thumbnailUrl: string;
userCode: string;
avatarUrl: string;
categoryId: number;
}

Expand All @@ -22,3 +23,12 @@ export interface GroupAchievementUpdate {
imageUrl?: string;
thumbnailUrl?: string;
}

export class UserInfo {
userCode: string;
avatarUrl: string;
constructor(userCode: string, avatarUrl: string) {
this.userCode = userCode;
this.avatarUrl = avatarUrl;
}
}
4 changes: 4 additions & 0 deletions BE/src/users/domain/user.domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export class User {
assignUserCode(userCode: string) {
this.userCode = userCode;
}

assignAvatar(avatarUrl: string) {
this.avatarUrl = avatarUrl;
}
}