Skip to content

Commit

Permalink
Merge pull request #247 from boostcampwm2023/BE-RegistrationToken-#239
Browse files Browse the repository at this point in the history
[BE/#239] registration token 등록 API 구현
  • Loading branch information
namewhat99 authored Nov 30, 2023
2 parents 4412478 + 9989422 commit 1b38d0b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { PostEntity } from '../entities/post.entity';
import { In, Repository } from 'typeorm';
import { Repository } from 'typeorm';
import { ChatRoomEntity } from '../entities/chatRoom.entity';
import { ChatEntity } from 'src/entities/chat.entity';
import { ChatDto } from './dto/chat.dto';
Expand Down
1 change: 0 additions & 1 deletion BE/src/entities/chat.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CreateBucketCommand } from '@aws-sdk/client-s3';
import {
Column,
CreateDateColumn,
Expand Down
4 changes: 0 additions & 4 deletions BE/src/entities/chatRoom.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import {
Entity,
OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import { PostEntity } from './post.entity';
import { BlockUserEntity } from './blockUser.entity';
import { BlockPostEntity } from './blockPost.entity';
import { ChatEntity } from './chat.entity';

@Entity('chat_room')
Expand Down
1 change: 0 additions & 1 deletion BE/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { dailyOption, winstonOptions } from './config/winston.config';
import * as winstonDaily from 'winston-daily-rotate-file';
import { ValidationPipe } from '@nestjs/common';
import { HttpLoggerInterceptor } from './utils/httpLogger.interceptor';
import { AuthGuard } from './utils/auth.guard';
import { WsAdapter } from '@nestjs/platform-ws';

async function bootstrap() {
Expand Down
10 changes: 10 additions & 0 deletions BE/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import {
UploadedFile,
HttpException,
UseGuards,
Body,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './createUser.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { MultiPartBody } from 'src/utils/multiPartBody.decorator';
import { UpdateUsersDto } from './usersUpdate.dto';
import { AuthGuard } from 'src/utils/auth.guard';
import { UserHash } from '../utils/auth.decorator';

@Controller('users')
@UseGuards(AuthGuard)
Expand Down Expand Up @@ -65,4 +67,12 @@ export class UsersController {
) {
await this.usersService.updateUserById(userId, body, file);
}

@Post('registration-token')
async registrationTokenSave(
@Body('registration_token') registrationToken: string,
@UserHash() userId: string,
) {
await this.usersService.registerToken(userId, registrationToken);
}
}
2 changes: 2 additions & 0 deletions BE/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PostImageEntity } from '../entities/postImage.entity';
import { BlockUserEntity } from '../entities/blockUser.entity';
import { BlockPostEntity } from '../entities/blockPost.entity';
import { AuthGuard } from 'src/utils/auth.guard';
import { RegistrationTokenEntity } from '../entities/registrationToken.entity';

@Module({
imports: [
Expand All @@ -18,6 +19,7 @@ import { AuthGuard } from 'src/utils/auth.guard';
PostImageEntity,
BlockUserEntity,
BlockPostEntity,
RegistrationTokenEntity,
]),
],
controllers: [UsersController],
Expand Down
29 changes: 29 additions & 0 deletions BE/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PostEntity } from '../entities/post.entity';
import { PostImageEntity } from '../entities/postImage.entity';
import { BlockUserEntity } from '../entities/blockUser.entity';
import { BlockPostEntity } from '../entities/blockPost.entity';
import { RegistrationTokenEntity } from '../entities/registrationToken.entity';

@Injectable()
export class UsersService {
Expand All @@ -24,6 +25,8 @@ export class UsersService {
private blockUserRepository: Repository<BlockUserEntity>,
@InjectRepository(BlockPostEntity)
private blockPostRepository: Repository<BlockPostEntity>,
@InjectRepository(RegistrationTokenEntity)
private registrationTokenRepository: Repository<RegistrationTokenEntity>,
private s3Handler: S3Handler,
) {}

Expand Down Expand Up @@ -138,4 +141,30 @@ export class UsersService {
const fileLocation = await this.s3Handler.uploadFile(file);
return fileLocation;
}

async registerToken(userId, registrationToken) {
const registrationTokenEntity =
await this.registrationTokenRepository.findOne({
where: { user_hash: userId },
});
if (registrationTokenEntity === null) {
await this.registrationTokenRepository.save({
user_hash: userId,
registration_token: registrationToken,
});
} else {
await this.updateRegistrationToken(userId, registrationToken);
}
}

async updateRegistrationToken(userId, registrationToken) {
const registrationTokenEntity = new RegistrationTokenEntity();
registrationTokenEntity.registration_token = registrationToken;
await this.registrationTokenRepository.update(
{
user_hash: userId,
},
registrationTokenEntity,
);
}
}
2 changes: 1 addition & 1 deletion BE/src/utils/auth.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export const UserHash = createParamDecorator(
Buffer.from(jwtPayload, 'base64').toString(),
);

return jwtPayloadJson.user_hash;
return jwtPayloadJson.userId;
},
);

0 comments on commit 1b38d0b

Please sign in to comment.