Skip to content

Commit

Permalink
Merge pull request #177 from jihwooon/issue-103
Browse files Browse the repository at this point in the history
장바구니 조회 서비스 및 JWT 토큰 검증 개선
  • Loading branch information
jihwooon authored Mar 18, 2024
2 parents cf616e6 + ac68071 commit 7d1b8aa
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
22 changes: 22 additions & 0 deletions server/src/cartItems/domain/cartItem.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RowDataPacket[]>(
`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<boolean> => {
const [{ affectedRows }] = await doQuery((connection) =>
connection.execute<ResultSetHeader>(
Expand Down
4 changes: 2 additions & 2 deletions server/src/cartItems/web/cartItem-list.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
9 changes: 7 additions & 2 deletions server/src/cartItems/web/cartItem-list.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 8 additions & 2 deletions server/src/users/jwt/jwt.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

0 comments on commit 7d1b8aa

Please sign in to comment.