diff --git a/server/src/cartItems/application/cartItem-list.service.test.ts b/server/src/cartItems/application/cartItem-list-existed-select.service.test.ts similarity index 97% rename from server/src/cartItems/application/cartItem-list.service.test.ts rename to server/src/cartItems/application/cartItem-list-existed-select.service.test.ts index 06a478d..6998368 100644 --- a/server/src/cartItems/application/cartItem-list.service.test.ts +++ b/server/src/cartItems/application/cartItem-list-existed-select.service.test.ts @@ -13,7 +13,7 @@ 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'; +import { getCartItems } from './cartItem-list-existed-select.service'; jest.mock('../domain/cartItem.repository.ts'); jest.mock('../../users/jwt/jwt.provider.ts'); diff --git a/server/src/cartItems/application/cartItem-list.service.ts b/server/src/cartItems/application/cartItem-list-existed-select.service.ts similarity index 99% rename from server/src/cartItems/application/cartItem-list.service.ts rename to server/src/cartItems/application/cartItem-list-existed-select.service.ts index fa1816d..bffe53a 100644 --- a/server/src/cartItems/application/cartItem-list.service.ts +++ b/server/src/cartItems/application/cartItem-list-existed-select.service.ts @@ -10,6 +10,7 @@ import { findCartItemWithBook } from '../domain/cartItem.repository'; 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-list-non-existed-select.service.ts b/server/src/cartItems/application/cartItem-list-non-existed-select.service.ts new file mode 100644 index 0000000..eae208e --- /dev/null +++ b/server/src/cartItems/application/cartItem-list-non-existed-select.service.ts @@ -0,0 +1,12 @@ +/* eslint-disable no-use-before-define */ +import { validateToken } from 'src/users/jwt/jwt.provider'; + +import { findCartItemAndBook } from '../domain/cartItem.repository'; + +export const getCartItem = async (accessToken: any) => { + const { userId } = validateToken(accessToken); + + const cartItems = await findCartItemAndBook(userId); + + return cartItems; +}; diff --git a/server/src/cartItems/domain/cartItem.repository.ts b/server/src/cartItems/domain/cartItem.repository.ts index 3613be9..126ea1a 100644 --- a/server/src/cartItems/domain/cartItem.repository.ts +++ b/server/src/cartItems/domain/cartItem.repository.ts @@ -69,6 +69,28 @@ export const findCartItemWithBook = async (userId: number, selectedId: number[]) ); }; +export const findCartItemAndBook = async (userId: number) => { + const [rows] = await doQuery((connection) => + connection.execute( + `SELECT ci.id, ci.book_id, b.title, b.summary, b.price, ci.count + FROM cartItems ci + LEFT JOIN books b + ON b.id = ci.book_id + WHERE ci.user_id = ?`, + [userId], + ), + ); + + return (rows ?? []).map((row) => ({ + id: row.id, + bookId: row.book_id, + count: row.count, + title: row.title, + summary: row.summary, + price: row.price, + })); +}; + export const deleteById = async (id: number): Promise => { const [{ affectedRows }] = await doQuery((connection) => connection.execute( diff --git a/server/src/cartItems/web/cartItem-list.controller.test.ts b/server/src/cartItems/web/cartItem-list.controller.test.ts index b607588..16510dc 100644 --- a/server/src/cartItems/web/cartItem-list.controller.test.ts +++ b/server/src/cartItems/web/cartItem-list.controller.test.ts @@ -6,9 +6,9 @@ import request from 'supertest'; import { StatusCodes } from 'http-status-codes'; import HttpException from 'src/utils/httpException'; -import { getCartItems } from '../application/cartItem-list.service'; +import { getCartItems } from '../application/cartItem-list-existed-select.service'; -jest.mock('../application/cartItem-list.service.ts'); +jest.mock('../application/cartItem-list-existed-select.service.ts'); describe('cartItemList Controller', () => { beforeEach(() => { diff --git a/server/src/cartItems/web/cartItem-list.controller.ts b/server/src/cartItems/web/cartItem-list.controller.ts index 9ddcb61..853ad03 100644 --- a/server/src/cartItems/web/cartItem-list.controller.ts +++ b/server/src/cartItems/web/cartItem-list.controller.ts @@ -3,11 +3,16 @@ import { type Request, type Response } from 'express'; import { StatusCodes } from 'http-status-codes'; import { ResponseHandler } from 'src/utils/responseHandler'; -import { getCartItems } from '../application/cartItem-list.service'; +import { getCartItems } from '../application/cartItem-list-existed-select.service'; +import { getCartItem } from '../application/cartItem-list-non-existed-select.service'; const getCartHandler = async ({ body: { selectedId }, headers }: Request, res: Response) => { const accessToken = headers.authorization; - ResponseHandler(() => getCartItems(accessToken, selectedId), StatusCodes.OK, res); + if (!selectedId) { + ResponseHandler(() => getCartItem(accessToken), StatusCodes.OK, res); + } else { + ResponseHandler(() => getCartItems(accessToken, selectedId), StatusCodes.OK, res); + } }; export default getCartHandler; diff --git a/server/src/users/jwt/jwt.provider.ts b/server/src/users/jwt/jwt.provider.ts index c2fd566..69a4a1b 100644 --- a/server/src/users/jwt/jwt.provider.ts +++ b/server/src/users/jwt/jwt.provider.ts @@ -27,7 +27,13 @@ export const validateToken = (token: string) => { return { userId, }; - } catch (e) { - throw new HttpException('인증 할 수 없는 token 입니다', StatusCodes.UNAUTHORIZED); + } catch (err: any) { + if (err instanceof jwt.TokenExpiredError) { + throw new HttpException('로그인 세션이 만료되었습니다.', StatusCodes.UNAUTHORIZED); + } else if (err instanceof jwt.JsonWebTokenError) { + throw new HttpException('인증 할 수 없는 token 입니다', StatusCodes.BAD_REQUEST); + } + + return err; } };