Skip to content

Commit

Permalink
장바구니 조회 및 목록 서비스, 테스트 코드 업데이트
Browse files Browse the repository at this point in the history
- 장바구니 조회 및 목록 서비스 (server/src/cartItems/application/cartItem-list.service.ts)를 업데이트했습니다.
  - 사용자 인증을 위해 JWT 토큰 검증 로직(src/users/jwt/jwt.provider.ts)을 추가했습니다.
  - 서비스 함수 시그니처를 getCartItems(userId: number, selectedId: number[])에서 getCartItems(accessToken: any, selectedId: number[])으로 변경했습니다.
- 테스트 코드 (server/src/cartItems/application/cartItem-list.service.test.ts)를 업데이트하여 변경된 서비스 로직을 검증합니다.
  - 모의 JWT 토큰을 테스트 케이스에 추가했습니다.
- 장바구니 조회 컨트롤러 (server/src/cartItems/web/cartItem-list.controller.ts)를 업데이트했습니다.
  - 요청 헤더에서 JWT 토큰을 추출하여 서비스 함수에 전달합니다.
  • Loading branch information
jihwooon committed Mar 16, 2024
1 parent 2c902bb commit 7d84846
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
16 changes: 11 additions & 5 deletions server/src/cartItems/application/cartItem-list.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { existingCartItem, existingCartItems, nonExistingCartItem } from 'src/fixture/cartItem.fixture';
import { existingCartItems, nonExistingCartItem } from 'src/fixture/cartItem.fixture';

import { when } from 'jest-when';

import HttpException from 'src/utils/httpException';

import { StatusCodes } from 'http-status-codes';

import { existingLike } from 'src/fixture/likes.fixture';

import { validateToken } from 'src/users/jwt/jwt.provider';

import { ACCESS_TOKEN } from 'src/fixture/jwt.fixture';

import { findCartItemWithBook } from '../domain/cartItem.repository';
import { getCartItems } from './cartItem-list.service';

jest.mock('../domain/cartItem.repository.ts');
jest.mock('../../users/jwt/jwt.provider.ts');

describe('cartItem Service', () => {
const SelectedCartItem = [1, 4];
const notSelectedCartItem = [9999, 9999];

beforeEach(() => {
when(findCartItemWithBook as jest.Mock)
.calledWith(existingCartItem.userId, SelectedCartItem)
.mockResolvedValue(existingCartItems);
when(validateToken as jest.Mock).mockResolvedValue({ userId: existingLike.userId });
when(findCartItemWithBook as jest.Mock).mockResolvedValue(existingCartItems);
});

context('사용자 정보 id와 장바구니 상품을 선택하면', () => {
it('장바구니 도서 목록을 반환한다.', async () => {
const cartItems = await getCartItems(existingCartItem.userId, SelectedCartItem);
const cartItems = await getCartItems(ACCESS_TOKEN, SelectedCartItem);

expect(cartItems).toStrictEqual([
{
Expand Down
5 changes: 4 additions & 1 deletion server/src/cartItems/application/cartItem-list.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import HttpException from 'src/utils/httpException';

import { StatusCodes } from 'http-status-codes';

import { validateToken } from 'src/users/jwt/jwt.provider';

import type CartItem from '../domain/cartItem';
import { findCartItemWithBook } from '../domain/cartItem.repository';

export const getCartItems = async (userId: number, selectedId: number[]) => {
export const getCartItems = async (accessToken: any, selectedId: number[]) => {
const { userId } = validateToken(accessToken);
const cartItems = await findCartItemWithBook(userId, selectedId);
if (cartItems.length === 0) {
throw new HttpException('장바구니가 내 도서 정보가 존재하지 않습니다.', StatusCodes.NOT_FOUND);
Expand Down
4 changes: 2 additions & 2 deletions server/src/cartItems/application/cartItem-remove.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import HttpException from 'src/utils/httpException';

import { deleteById } from '../domain/cartItem.repository';

export const removeToCart = async (id: number) => {
const removedCart = await deleteById(id);
export const removeToCart = async (cartItemId: number) => {
const removedCart = await deleteById(cartItemId);
if (!removedCart) {
throw new HttpException('장바구니 제거에 실패했습니다.', StatusCodes.BAD_REQUEST);
}
Expand Down
5 changes: 3 additions & 2 deletions server/src/cartItems/web/cartItem-list.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { ResponseHandler } from 'src/utils/responseHandler';

import { getCartItems } from '../application/cartItem-list.service';

const getCartHandler = async ({ body: { userId, selectedId } }: Request, res: Response) => {
ResponseHandler(() => getCartItems(Number(userId), selectedId), StatusCodes.OK, res);
const getCartHandler = async ({ body: { selectedId }, headers }: Request, res: Response) => {
const accessToken = headers.authorization;
ResponseHandler(() => getCartItems(accessToken, selectedId), StatusCodes.OK, res);
};

export default getCartHandler;

0 comments on commit 7d84846

Please sign in to comment.