fix(posts): get post paginate should parse meta as json

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2023-04-05 19:05:00 +08:00
parent f426816a95
commit f7b1d0d280

View File

@@ -44,77 +44,87 @@ export class PostController {
async getPaginate(@Query() query: PagerDto) {
const { size, select, page, year, sortBy, sortOrder } = query
return this.postService.model.aggregatePaginate(
this.postService.model.aggregate(
[
{
$match: {
...addYearCondition(year),
return this.postService.model
.aggregatePaginate(
this.postService.model.aggregate(
[
{
$match: {
...addYearCondition(year),
},
},
},
// @see https://stackoverflow.com/questions/54810712/mongodb-sort-by-field-a-if-field-b-null-otherwise-sort-by-field-c
{
$addFields: {
sortField: {
// create a new field called "sortField"
$cond: {
// and assign a value that depends on
if: { $ne: ['$pin', null] }, // whether "b" is not null
then: '$pinOrder', // in which case our field shall hold the value of "a"
else: '$$REMOVE',
// @see https://stackoverflow.com/questions/54810712/mongodb-sort-by-field-a-if-field-b-null-otherwise-sort-by-field-c
{
$addFields: {
sortField: {
// create a new field called "sortField"
$cond: {
// and assign a value that depends on
if: { $ne: ['$pin', null] }, // whether "b" is not null
then: '$pinOrder', // in which case our field shall hold the value of "a"
else: '$$REMOVE',
},
},
},
},
},
{
$sort: sortBy
? {
[sortBy]: sortOrder as any,
}
: {
sortField: -1, // sort by our computed field
pin: -1,
created: -1, // and then by the "created" field
},
},
{
$project: {
sortField: 0, // remove "sort" field if needed
{
$sort: sortBy
? {
[sortBy]: sortOrder as any,
}
: {
sortField: -1, // sort by our computed field
pin: -1,
created: -1, // and then by the "created" field
},
},
},
select && {
$project: {
...(select?.split(' ').reduce(
(acc, cur) => {
const field = cur.trim()
acc[field] = 1
return acc
},
Object.keys(new PostModel()).map((k) => ({ [k]: 0 })),
) as any),
{
$project: {
sortField: 0, // remove "sort" field if needed
},
},
},
{
$lookup: {
from: 'categories',
localField: 'categoryId',
foreignField: '_id',
as: 'category',
select && {
$project: {
...(select?.split(' ').reduce(
(acc, cur) => {
const field = cur.trim()
acc[field] = 1
return acc
},
Object.keys(new PostModel()).map((k) => ({ [k]: 0 })),
) as any),
},
},
},
{
$unwind: {
path: '$category',
preserveNullAndEmptyArrays: true,
{
$lookup: {
from: 'categories',
localField: 'categoryId',
foreignField: '_id',
as: 'category',
},
},
},
].filter(Boolean) as PipelineStage[],
),
{
limit: size,
page,
},
)
{
$unwind: {
path: '$category',
preserveNullAndEmptyArrays: true,
},
},
].filter(Boolean) as PipelineStage[],
),
{
limit: size,
page,
},
)
.then((res) => {
res.docs = res.docs.map((doc: PostModel) => {
if (doc.meta && typeof doc.meta === 'string') {
doc.meta = JSON.safeParse(doc.meta as string) || doc.meta
}
return doc
})
return res
})
}
@Get('/:id')