Skip to content

Commit

Permalink
fix: add product title and photoPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger13579 committed Jun 14, 2024
1 parent bdf1e3c commit da0b19e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/dto/comment/getCommentsDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ export class GetCommentsDTO {
rating: 1,
content: 1,
createdAt: 1,
productId: 1,
status: this._isAdmin ? 1 : -1, // admin 才能看到 status
user: {
...(this._isAdmin && { _id: '$user._id' }),
account: '$user.account',
avatarPath: '$user.avatarPath',
name: '$user.name',
},
productId: 1,
product: {
title: '$product.title',
photoPath: '$product.photoPath',
},
};
}

Expand Down
5 changes: 5 additions & 0 deletions src/swagger/definition/comment/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ export const GetCommentsSuccess = {
$comments: [
{
...Comment,
$productId: '123141353413',
$user: {
$account: 'yjadmin0001',
$avatarPath: '',
},
$product: {
$title: '好看的電影',
$photoPath: '',
},
},
],
...PaginationSuccess,
Expand Down
28 changes: 14 additions & 14 deletions src/utils/aggregate/comment/getComment.pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,34 @@ export const createGetCommentPipeline = ({

const accountsPipeline = accounts
? [
{
$unwind: {
path: '$user',
preserveNullAndEmptyArrays: true,
},
},
{
$match: {
'user.account': { $in: accounts },
},
},
]
: [];

const unwindAccount = {
$unwind: {
path: '$user',
preserveNullAndEmptyArrays: true,
},
};
const productNamePipeline = productNameRegex
? [
{
$unwind: {
path: '$product',
preserveNullAndEmptyArrays: true,
},
},
{
$match: {
'product.title': { $regex: productNameRegex, $options: 'i' },
},
},
]
: [];

const unwindProduct = {
$unwind: {
path: '$product',
preserveNullAndEmptyArrays: true,
},
};
const basePipeline = [
{
$match: {
Expand Down Expand Up @@ -92,8 +90,10 @@ export const createGetCommentPipeline = ({
return [
...basePipeline,
lookupUser,
unwindAccount,
...accountsPipeline,
lookupProduct,
unwindProduct,
...productNamePipeline,
facetStage,
addFieldsStage,
Expand Down

0 comments on commit da0b19e

Please sign in to comment.