Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

장바구니 조회 서비스 및 JWT 토큰 검증 개선 #177

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};