diff --git a/server/src/cartItems/application/cartItem-list.service.test.ts b/server/src/cartItems/application/cartItem-list.service.test.ts index 3aee876..06a478d 100644 --- a/server/src/cartItems/application/cartItem-list.service.test.ts +++ b/server/src/cartItems/application/cartItem-list.service.test.ts @@ -1,4 +1,4 @@ -import { existingCartItem, existingCartItems, nonExistingCartItem } from 'src/fixture/cartItem.fixture'; +import { existingCartItems, nonExistingCartItem } from 'src/fixture/cartItem.fixture'; import { when } from 'jest-when'; @@ -6,24 +6,30 @@ 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([ { diff --git a/server/src/cartItems/application/cartItem-list.service.ts b/server/src/cartItems/application/cartItem-list.service.ts index ddbce10..fa1816d 100644 --- a/server/src/cartItems/application/cartItem-list.service.ts +++ b/server/src/cartItems/application/cartItem-list.service.ts @@ -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); diff --git a/server/src/cartItems/application/cartItem-remove.service.ts b/server/src/cartItems/application/cartItem-remove.service.ts index d147b8e..abbb221 100644 --- a/server/src/cartItems/application/cartItem-remove.service.ts +++ b/server/src/cartItems/application/cartItem-remove.service.ts @@ -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); } diff --git a/server/src/cartItems/web/cartItem-list.controller.ts b/server/src/cartItems/web/cartItem-list.controller.ts index d142283..9ddcb61 100644 --- a/server/src/cartItems/web/cartItem-list.controller.ts +++ b/server/src/cartItems/web/cartItem-list.controller.ts @@ -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;