Skip to content

Commit

Permalink
fix: add isFavorite in get user favorite vo
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger13579 committed Jun 14, 2024
1 parent ccb96ff commit b7b277f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/service/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/swagger/definition/user/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 4 additions & 2 deletions src/types/user.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface IGetUserFavoriteReq extends IUserReq {
query: TPaginationQuery;
}

interface FavoriteItem
export interface FavoriteItem
extends Pick<
IProduct,
| '_id'
Expand All @@ -69,7 +69,9 @@ interface FavoriteItem
| 'photoPath'
| 'sellStartAt'
| 'sellEndAt'
> {}
> {
isFavorite: boolean;
}

export interface IGetFavoritePagination {
_id: Types.ObjectId;
Expand Down
20 changes: 18 additions & 2 deletions src/utils/aggregate/user/getFavorite.pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } },
];

0 comments on commit b7b277f

Please sign in to comment.