From b7b277f90ea675849e865c58bf99952ebd250de8 Mon Sep 17 00:00:00 2001 From: RogerLi Date: Sat, 15 Jun 2024 01:34:02 +0800 Subject: [PATCH] fix: add isFavorite in get user favorite vo --- src/service/userService.ts | 12 ++++++++--- src/swagger/definition/user/general.ts | 1 + src/types/user.type.ts | 6 ++++-- .../aggregate/user/getFavorite.pipeline.ts | 20 +++++++++++++++++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/service/userService.ts b/src/service/userService.ts index 82becf4..46fae02 100644 --- a/src/service/userService.ts +++ b/src/service/userService.ts @@ -10,7 +10,7 @@ import log4js from '../config/log4js'; import { ResetPwdDto } from '../dto/user/resetPwdDto'; import passport from 'passport'; import { GoogleProfileDto } from '../dto/user/googleProfileDto'; -import { TGoogleUser, ThirdPartyType } from '../types/user.type'; +import { FavoriteItem, TGoogleUser, ThirdPartyType } from '../types/user.type'; import { EditFavoriteDTO } from '../dto/user/editFavoriteDto'; import { GetUserFavoriteDTO } from '../dto/user/getUserFavoriteDto'; import { ProductRepository } from '../repository/productRepository'; @@ -265,8 +265,14 @@ export class UserService { } }; - public getFavorite = async (getUserFavoriteDto: GetUserFavoriteDTO) => - await this.userRepository.findFavoriteByUserId(getUserFavoriteDto); + public getFavorite = async (getUserFavoriteDto: GetUserFavoriteDTO) => { + const result = + await this.userRepository.findFavoriteByUserId(getUserFavoriteDto); + result.favorites.forEach((favorite: FavoriteItem) => { + favorite.isFavorite = true; + }); + return result; + }; public addFavorite = async (editFavoriteDto: EditFavoriteDTO) => { const { productId } = editFavoriteDto; diff --git a/src/swagger/definition/user/general.ts b/src/swagger/definition/user/general.ts index 808457f..a929e40 100644 --- a/src/swagger/definition/user/general.ts +++ b/src/swagger/definition/user/general.ts @@ -13,6 +13,7 @@ const favorite = { photoPath: 'https://images.unsplash.com/photo-1554080353-a576cf803bda', sellStartAt: new Date().toISOString(), sellEndAt: new Date().toISOString(), + isFavorite: true, }; export const GetFavoriteSuccess = { diff --git a/src/types/user.type.ts b/src/types/user.type.ts index e846c67..18018a7 100644 --- a/src/types/user.type.ts +++ b/src/types/user.type.ts @@ -55,7 +55,7 @@ export interface IGetUserFavoriteReq extends IUserReq { query: TPaginationQuery; } -interface FavoriteItem +export interface FavoriteItem extends Pick< IProduct, | '_id' @@ -69,7 +69,9 @@ interface FavoriteItem | 'photoPath' | 'sellStartAt' | 'sellEndAt' - > {} + > { + isFavorite: boolean; +} export interface IGetFavoritePagination { _id: Types.ObjectId; diff --git a/src/utils/aggregate/user/getFavorite.pipeline.ts b/src/utils/aggregate/user/getFavorite.pipeline.ts index 006e98b..9ac68eb 100644 --- a/src/utils/aggregate/user/getFavorite.pipeline.ts +++ b/src/utils/aggregate/user/getFavorite.pipeline.ts @@ -88,10 +88,26 @@ export const createGetFavoritePipeline = ({ $cond: { if: { $gt: [{ $size: `$${Status.active}` }, 0] }, then: { $arrayElemAt: [`$${Status.active}`, 0] }, - else: { $arrayElemAt: [`$${Status.disabled}`, 0] }, + else: { + $cond: { + if: { $gt: [{ $size: `$${Status.disabled}` }, 0] }, + then: { $arrayElemAt: [`$${Status.disabled}`, 0] }, + else: { fallbackField: 'noData' }, + }, + }, + }, + }, + }, + }, + { + $replaceRoot: { + newRoot: { + $cond: { + if: { $ne: ['$result', null] }, + then: '$result', + else: { fallbackField: 'noData' }, }, }, }, }, - { $replaceRoot: { newRoot: '$result' } }, ];