Thank you for your reply. The corresponding code blocks are as follows.
[parent code]
const axios = require('axios');
const { searchFilterFunc } = require('../../common/commonFunc');
const mediaPostSearch = async (req, res) => {
const { PORTAL_ID, PRIVATE_KEY } = process.env;
try {
const searchText = req.query.searchText;
const pageSize = req.query.pageSize ? parseInt(req.query.pageSize) : 20;
const offset = req.query.offset ? parseInt(req.query.offset) : 1;
const startDate = req.query.startDate;
const endDate = req.query.endDate;
const optionType = req.query.optionType ? req.query.optionType : 'N';
const sorting = req.query.sorting ? req.query.sorting : [];
const productSort = req.query.productSort ? req.query.productSort : [];
let params = {};
if (searchText.length > 1) {
params = {
limit: pageSize,
offset: (offset - 1) * pageSize,
state: 'PUBLISHED',
content_group_id: 140364672876,
portalId: PORTAL_ID,
};
} else {
params = {
type: 'BLOG_POST',
content_group_id: 140364672876,
portalId: PORTAL_ID,
};
}
const headers = {
Authorization: `Bearer ${PRIVATE_KEY}`,
'Content-Type': 'application/json',
};
const response = await axios.get(
`https://api.hubapi.com/content/api/v2/blog-posts`,
{ params, headers }
);
const mediaData = response.data.objects;
const tagResponse = await axios.get(
`https://api.hubapi.com/cms/v3/blogs/tags?limit=20000`,
{
headers: {
Authorization: `Bearer ${PRIVATE_KEY}`,
'Content-Type': 'application/json',
},
}
);
const tagList = tagResponse.data.results.reduce(
(acc, cur) =>
acc.concat({
id: cur.id,
name: cur.name,
}),
[]
);
const blogTagList = mediaData.map(el => el['tag_ids']);
const tagArr2 = blogTagList.map(el => {
const arr = [];
for (let i = 0; i < el.length; i++) {
const tagFilter = tagList.filter(item => parseInt(item.id) === el[i]);
arr.push(tagFilter[0]);
}
const newArr = arr.filter(Boolean);
return newArr.map(el => el.name);
});
const mediaArr = mediaData.reduce(
(acc, cur, index) =>
acc.concat({
id: cur.id,
custom_module: cur.widgets['module_1697612084455100']
? cur.widgets['module_1697612084455100'].body['media_group']
: null,
url: cur.root_url,
thumbNailImgUrl: cur.featured_image,
title: cur.title,
description: cur.meta_description,
authorFullName: cur.authorFullName,
tags: tagArr2[index],
imageAlt: cur.widgets['module_1697612084455100']
? cur.widgets['module_1697612084455100'].body['media_group'][0]
.image.alt
: '',
featured_image_alt_text: cur.featured_image_alt_text,
publishedAt: cur.created,
sort: cur.widgets['module_1697612084455100']
? cur.widgets['module_1697612084455100'].body['media_group'][0].sort
: '',
productSort: cur.widgets['module_1697612084455100']
? cur.widgets['module_1697612084455100'].body['media_group'][0]
.media_type
: '',
imageTitle: cur.widgets['module_1697612084455100']
? cur.widgets['module_1697612084455100'].body['media_group'][0]
.media_description
: '',
postbody: cur['post_body'] ? cur['post_body'] : '',
}),
[]
);
const responseMediaArr = searchFilterFunc(
mediaArr,
searchText,
startDate,
endDate,
optionType,
sorting,
productSort
);
const responseData = {
status: 200,
paging: {
offset,
pageSize,
total: responseMediaArr.length,
},
result: mediaArr,
data: responseMediaArr,
};
res.json(responseData);
} catch (e) {
console.log(e);
return [];
}
};
module.exports = {
mediaPostSearch,
};
[searchFilterFunc call code]
const moment = require('moment');
const searchFilterFunc = (
arr,
searchText,
startDate = '',
endDate = '',
optionType,
sorting,
productSort
) => {
const highlight = [];
const noHighlight = [];
if (optionType === 'N') {
if (searchText) {
const searchIncludesTitle = arr.filter(el => {
const title = el.title.toUpperCase().match(searchText.toUpperCase());
const mediaTitle = el?.imageTitle
?.toUpperCase()
.match(searchText.toUpperCase())?.input;
const featured_image_alt_text = el?.featured_image_alt_text
?.toUpperCase()
.match(searchText.toUpperCase());
const imageAlt = el?.imageAlt
?.toUpperCase()
.match(searchText.toUpperCase())?.input;
const postBody = el?.postbody
?.toUpperCase()
.match(searchText.toUpperCase())?.input;
const descriptionTitle = el?.description
?.toUpperCase()
.match(searchText.toUpperCase())?.input;
return (
title ||
mediaTitle ||
featured_image_alt_text ||
imageAlt ||
descriptionTitle ||
postBody
);
});
return searchIncludesTitle;
} else {
return arr;
}
} else if (optionType === 'Y') {
const querySort = sorting.split(',');
const queryProduct = productSort.split(',');
if (!searchText && sorting.length > 1 && productSort.length > 1) {
const matchingObjects = arr.filter(item => {
// 날짜가 있을 떄 로직
if (startDate && endDate) {
// const dataPicker =
// moment(moment(item.publishedAt).format('YYYY-MM-DD') >= startDate &&
// moment(item.publishedAt).format('YYYY-MM-DD') <= endDate;
const dataPicker = moment(
moment(item.publishedAt).format('YYYY-MM-DD')
).isBetween(startDate, endDate, undefined, '[]');
const hasQ = querySort.some(qItem => item.sort.includes(qItem));
const hasX = queryProduct.some(xItem =>
item.productSort.includes(xItem)
);
return hasQ && hasX && dataPicker;
}
// 날짜가 없을 떄 로직
const hasQ = querySort.some(qItem => item.sort.includes(qItem));
const hasX = queryProduct.some(xItem =>
item.productSort.includes(xItem)
);
return hasQ && hasX;
});
return matchingObjects;
} else if (searchText && sorting.length > 1 && productSort.length > 1) {
// const matchingObjects = arr.filter(
// item =>
// JSON.stringify(item.sort) === JSON.stringify(querySort) &&
// item.productSort.includes(queryProduct[0]) &&
// item.title.toUpperCase().match(searchText.toUpperCase())
// );
// return matchingObjects;
// 필터조건 => 검색어와 옵션이 모두 있는 경우
const result = arr.filter(item => {
// 검색어 & 옵션 & 날짜가 모두 있는 경우
const hasQ = querySort.some(qItem => item.sort.includes(qItem));
const hasX = queryProduct.some(xItem =>
item.productSort.includes(xItem)
);
const text = item.title.toUpperCase().match(searchText.toUpperCase());
const mediaTitle = item?.imageTitle
?.toUpperCase()
.match(searchText.toUpperCase())?.input;
const featured_image_alt_text = item?.featured_image_alt_text
?.toUpperCase()
.match(searchText.toUpperCase());
const imageAlt = item?.imageAlt
?.toUpperCase()
.match(searchText.toUpperCase())?.input;
const descriptionTitle = item?.description
?.toUpperCase()
.match(searchText?.toUpperCase())?.input;
const postBody = item?.postbody
?.toUpperCase()
.match(searchText.toUpperCase())?.input;
// const dataPicker =
// startDate && endDate
// ? moment(item.publishedAt).format('YYYY-MM-DD') >= startDate &&
// moment(item.publishedAt).format('YYYY-MM-DD') <= endDate
// : '';
const dataPicker = moment(
moment(item.publishedAt).format('YYYY-MM-DD')
).isBetween(startDate, endDate, undefined, '[]');
return (
(mediaTitle ||
text ||
featured_image_alt_text ||
imageAlt ||
descriptionTitle ||
postBody) &&
dataPicker &&
hasQ &&
hasX
);
});
return result;
}
}
if (dateType === 'all') {
if (searchText) {
const searchIncludesTitle = arr.filter(el =>
el.title.toUpperCase().match(searchText.toUpperCase())
);
return searchIncludesTitle;
}
} else if (dateType === 'select') {
arr.forEach(el => {
if (!el.description) {
noHighlight.push(el);
}
if (
getKorean(el.title).includes(searchText) ||
getKorean(el.description).includes(searchText)
) {
highlight.push(el);
}
if (
!getKorean(el.title).includes(searchText) &&
!getKorean(el.description).includes(searchText)
) {
noHighlight.push(el);
}
});
const totalArrA = highlight
.filter(
el =>
moment(el.publishedDate).format('YYYY-MM-DD') >= startDate &&
moment(el.publishedDate).format('YYYY-MM-DD') <= endDate
)
.sort((a, b) => b.publishedDate - a.publishedDate);
const totalArrB = noHighlight
.filter(
el =>
moment(el.publishedDate).format('YYYY-MM-DD') >= startDate &&
moment(el.publishedDate).format('YYYY-MM-DD') <= endDate
)
.sort((a, b) => b.publishedDate - a.publishedDate);
const totalArrC = [...totalArrA, ...totalArrB].reduce(
(unique, item) =>
JSON.stringify(unique).includes(JSON.stringify(item))
? unique
: [...unique, item],
[]
);
return totalArrC;
}
};