diff --git a/src/controllers/Perfume.ts b/src/controllers/Perfume.ts index c4d41811..a98c6604 100644 --- a/src/controllers/Perfume.ts +++ b/src/controllers/Perfume.ts @@ -93,11 +93,11 @@ const LikePerfume: LikePerfumeService = new LikePerfumeService(); * description: Perfume not found * x-swagger-router-controller: Perfume * */ -const getPerfume: RequestHandler = ( +const getPerfume: RequestHandler = async ( req: Request | any, res: Response, next: NextFunction -): any => { +) => { const perfumeIdx: number = req.params['perfumeIdx']; if (isNaN(perfumeIdx)) { next(); @@ -109,30 +109,27 @@ const getPerfume: RequestHandler = ( req.params )})` ); - Promise.all([ - Perfume.getPerfumeById(perfumeIdx, loginUserIdx), - SearchHistory.recordInquire( - loginUserIdx, - perfumeIdx, - '' /* 향후 경로가 다양화 되면 경로 기록 용 */ - ), - ]) - .then(([result, _]: [PerfumeIntegralDTO, void]) => { - return PerfumeDetailResponse.createByPerfumeIntegralDTO(result); - }) - .then((response: PerfumeDetailResponse) => { - LoggerHelper.logTruncated( - logger.debug, - `${LOG_TAG} getPerfume's result = ${response}` - ); - res.status(StatusCode.OK).json( - new ResponseDTO( - MSG_GET_PERFUME_DETAIL_SUCCESS, - response - ) - ); - }) - .catch((err: Error) => next(err)); + + try { + const [result, _]: [PerfumeIntegralDTO, void] = await Promise.all([ + Perfume.getPerfumeById(perfumeIdx, loginUserIdx), + SearchHistory.recordInquire( + loginUserIdx, + perfumeIdx, + '' /* 향후 경로가 다양화 되면 경로 기록 용 */ + ), + ]); + const response: PerfumeDetailResponse = + PerfumeDetailResponse.createByPerfumeIntegralDTO(result); + res.status(StatusCode.OK).json( + new ResponseDTO( + MSG_GET_PERFUME_DETAIL_SUCCESS, + response + ) + ); + } catch (err) { + next(err); + } }; /** diff --git a/src/dao/PerfumeDao.ts b/src/dao/PerfumeDao.ts index 311e4496..1e6c7021 100644 --- a/src/dao/PerfumeDao.ts +++ b/src/dao/PerfumeDao.ts @@ -188,43 +188,51 @@ class PerfumeDao { logger.debug( `${LOG_TAG} recentSearchPerfumeList(userIdx = ${userIdx}, pagingDTO = ${pagingDTO})` ); - return InquireHistory.findAndCountAll( - _.merge({}, pagingDTO.sequelizeOption(), { - attributes: { - include: [ - [ - Sequelize.fn( - 'max', - Sequelize.col('InquireHistory.created_at') - ), - 'inquireAt', - ], - ], - }, - where: { - userIdx, - }, - include: [ - { - model: Perfume, - as: 'Perfume', - required: true, + try { + const result: { + rows: InquireHistory[]; + // todo: count에 배열이 아닌 number이 오게끔 수정 + count: { perfume_idx: number; count: number }[]; + } = (await InquireHistory.findAndCountAll( + _.merge({}, pagingDTO.sequelizeOption(), { + raw: true, + nest: true, + attributes: { include: [ - { - model: Brand, - as: 'Brand', - required: true, - }, + [ + Sequelize.fn( + 'max', + Sequelize.col('InquireHistory.created_at') + ), + 'inquireAt', + ], ], - raw: true, - nest: true, }, - ], - order: [[sequelize.col('inquireAt'), 'desc']], - group: ['InquireHistory.perfume_idx'], - }) - ).then((it: any) => { - const rows: PerfumeInquireHistoryDTO[] = it.rows + where: { + userIdx, + }, + include: [ + { + model: Perfume, + as: 'Perfume', + required: true, + include: [ + { + model: Brand, + as: 'Brand', + required: true, + }, + ], + raw: true, + nest: true, + }, + ], + order: [[sequelize.col('inquireAt'), 'desc']], + group: ['InquireHistory.perfume_idx'], + }) + )) as any; + + const rows: PerfumeInquireHistoryDTO[] = result.rows .map((json: any) => { return { perfumeIdx: json.Perfume.perfumeIdx, @@ -243,8 +251,10 @@ class PerfumeDao { }; }) .map(PerfumeInquireHistoryDTO.createByJson); - return new ListAndCountDTO(it.count.length, rows); - }); + return new ListAndCountDTO(result.count.length as number, rows); + } catch (err) { + throw err; + } } /**