Skip to content

Commit

Permalink
Merge pull request #29 from f-lab-edu/feature/17-get-member-update-de…
Browse files Browse the repository at this point in the history
…lete

[#17] 멤버 정보 가져오기, 수정, 삭제 기능
  • Loading branch information
yanggwangseong authored Dec 6, 2024
2 parents f2f4e3c + 95ecdac commit 7a37c64
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ export class AuthController {
@IsPublicDecorator(IsPublicEnum.PUBLIC)
@UseGuards(BasicTokenGuard)
@Post("sign-in")
async signIn(@Headers("authorization") rawToken: string) {
signIn(@Headers("authorization") rawToken: string) {
const token = this.authService.extractTokenFromHeader(rawToken, false);

const decoded = this.authService.decodeBasicToken(token);

return await this.authService.signInByEmail(decoded);
return this.authService.signInByEmail(decoded);
}

@IsPublicDecorator(IsPublicEnum.PUBLIC)
@Post("sign-up")
async signUp(@Body() dto: RegisterMemberDto) {
return await this.authService.registerByEmail(dto);
signUp(@Body() dto: RegisterMemberDto) {
return this.authService.registerByEmail(dto);
}

@Post("sign-out")
signOut(@CurrentMemberDecorator("id") memberId: number) {
void this.authService.updateRefreshToken(memberId);
return this.authService.updateRefreshToken(memberId);
}

@IsPublicDecorator(IsPublicEnum.PUBLIC)
@Post("verify-email")
async verifyEmail(@Body() dto: VerifyEmailDto) {
return await this.authService.verifyEmail(dto);
verifyEmail(@Body() dto: VerifyEmailDto) {
return this.authService.verifyEmail(dto);
}

@IsPublicDecorator(IsPublicEnum.REFRESH)
Expand Down
70 changes: 70 additions & 0 deletions src/controllers/members.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
Body,
Controller,
Delete,
ForbiddenException,
Get,
NotFoundException,
Param,
ParseIntPipe,
Patch,
} from "@nestjs/common";
import * as bcrypt from "bcrypt";

import { CurrentMemberDecorator } from "@APP/common/decorators/current-member.decorator";
import { UpdateMemberDto } from "@APP/dtos/update-member.dto";
import { MembersService } from "@APP/services/members.service";

@Controller("members")
export class MembersController {
constructor(private readonly membersService: MembersService) {}

@Get(":memberId")
getMember(@Param("memberId", new ParseIntPipe()) memberId: number) {
return this.membersService.findById(memberId);
}

@Delete(":memberId")
async deleteMember(
@CurrentMemberDecorator("id") currentMemberId: number,
@Param("memberId", new ParseIntPipe()) memberId: number,
) {
const member = await this.membersService.findById(memberId);

if (!member) {
throw new NotFoundException("존재하지 않는 회원입니다.");
}

if (currentMemberId !== memberId) {
throw new ForbiddenException("권한이 없습니다.");
}

await this.membersService.deleteById(memberId);
}

@Patch(":memberId")
async patchMember(
@CurrentMemberDecorator("id") currentMemberId: number,
@Param("memberId", new ParseIntPipe()) memberId: number,
@Body() dto: UpdateMemberDto,
) {
const member = await this.membersService.findById(memberId);

if (!member) {
throw new NotFoundException("존재하지 않는 회원입니다.");
}

if (currentMemberId !== memberId) {
throw new ForbiddenException("권한이 없습니다.");
}

if (dto.password) {
const hashedPassword = await bcrypt.hash(dto.password, 10);
dto.password = hashedPassword;
}

await this.membersService.updateById(memberId, dto);

return this.membersService.findById(memberId);
}
}
7 changes: 7 additions & 0 deletions src/dtos/update-member.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { OmitType, PartialType } from "@nestjs/swagger";

import { RegisterMemberDto } from "./register-member.dto";

export class UpdateMemberDto extends PartialType(
OmitType(RegisterMemberDto, ["email"]),
) {}
7 changes: 6 additions & 1 deletion src/entities/member.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IsEmail, IsNotEmpty, IsString } from "class-validator";
import { Exclude } from "class-transformer";
import { IsEmail, IsNotEmpty, IsString, Length } from "class-validator";
import {
Column,
CreateDateColumn,
Expand Down Expand Up @@ -31,6 +32,10 @@ export class MemberEntity {

@IsNotEmpty()
@IsString()
@Exclude({
toPlainOnly: true,
})
@Length(4, 20)
@Column({ type: "varchar", length: PASSWORD_HASH_LENGTH, nullable: false })
password!: string; // 비밀번호

Expand Down
2 changes: 1 addition & 1 deletion src/modules/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import { MembersModule } from "./members.module";
imports: [JwtModule.register({}), MembersModule],
controllers: [AuthController],
providers: [AuthService, MailsService],
exports: [],
exports: [AuthService],
})
export class AuthModule {}
3 changes: 2 additions & 1 deletion src/modules/members.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";

import { MembersController } from "@APP/controllers/members.controller";
import { MemberEntity } from "@APP/entities/member.entity";
import { RefreshTokenEntity } from "@APP/entities/refresh-token.entity";
import { VerificationCodeEntity } from "@APP/entities/verification-code.entity";
Expand All @@ -17,7 +18,7 @@ import { MembersService } from "@APP/services/members.service";
VerificationCodeEntity,
]),
],
controllers: [],
controllers: [MembersController],
providers: [
MembersService,
MembersRepository,
Expand Down
2 changes: 1 addition & 1 deletion src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class AuthService {
verificationCode,
);

void this.mailsService.sendVerificationEmail(
await this.mailsService.sendVerificationEmail(
newMember.email,
verificationCode,
);
Expand Down
24 changes: 24 additions & 0 deletions src/services/members.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Injectable } from "@nestjs/common";
import { BusinessErrorException } from "@APP/common/exception/business-error.exception";
import { MemberErrorCode } from "@APP/common/exception/error-code";
import { RegisterMemberDto } from "@APP/dtos/register-member.dto";
import { UpdateMemberDto } from "@APP/dtos/update-member.dto";
import { VerifyEmailDto } from "@APP/dtos/verify-email.dto";
import { MembersRepository } from "@APP/repositories/members.repository";
import { RefreshTokenRepository } from "@APP/repositories/refresh-token.repository";
Expand All @@ -24,6 +25,29 @@ export class MembersService {
});
}

findById(memberId: number) {
return this.membersRepository.findOne({
where: {
id: memberId,
},
});
}

updateById(memberId: number, dto: UpdateMemberDto) {
return this.membersRepository.update(
{
id: memberId,
},
dto,
);
}

deleteById(memberId: number) {
return this.membersRepository.delete({
id: memberId,
});
}

async verifyEmail(dto: VerifyEmailDto) {
const member = await this.findByEmail(dto.email);

Expand Down

0 comments on commit 7a37c64

Please sign in to comment.