ADD: Server urls

ADD: SearchBelongings function
REMOVE: API from docker compose (run it manually)
ADD: Required packages
REMOVE: Redundant import from TsApiFeatures
This commit is contained in:
2025-01-02 16:45:16 +03:30
parent 25a6978346
commit 1021774c89
7 changed files with 162 additions and 21 deletions
@@ -1,6 +1,15 @@
const catchAsync = require('../../globalErrorHandler/catchAsync')
const belongingService = require('./belongingService')
exports.searchBelongings = catchAsync(async (req, res, next) => {
const {facets, belongings, totalCount, pagesCount} = await belongingService.searchBelongings()
res.status(200).json({
status: 'success',
facets, belongings, totalCount, pagesCount
})
})
exports.indexAllBelongings = catchAsync(async (req, res, next) => {
const { indexedCount } = await belongingService.indexAllBelongings()
+12 -3
View File
@@ -1,6 +1,7 @@
const Belonging = require('./belongingModel')
const AppError = require('../../globalErrorHandler/AppError')
const { client } = require('../../setup/typesense')
const TsApiFeatures = require("../../utils/TsApiFeatures");
const getAllBelongings = async () => {
return Belonging.find({}, null, null)
@@ -53,15 +54,23 @@ const ensureCollectionExists = async () => {
await client.collections().create(schema)
console.log('Collection "belongings" created successfully')
}
const searchBelongings = async (inputQuery) => {
const tsFeatures = new TsApiFeatures(client, 'belongings', inputQuery).filter().facet().sort().limitFields().paginate().search()
let {facets, items, totalCount, pagesCount} = await tsFeatures.execute()
return {facets, belongings: items, totalCount, pagesCount}
}
const indexAllBelongings = async () => {
const batchSize = 20
const count = await Product.countDocuments({isTrash: false, isActive: true, isHidden: false}, null)
const count = await Belonging.countDocuments({}, null)
const rounds = Math.ceil(count / batchSize)
await ensureCollectionExists()
for (let i = 1; i <= rounds; i++) {
console.log(`Indexing ${batchSize} products, Round ${i}`)
console.log(`Indexing ${batchSize} belongings, Round ${i}`)
const belongings = await Belonging.find({}, null, null)
.populate('category')
@@ -111,4 +120,4 @@ const getTypeSenseSchema = () => {
}
}
module.exports = {indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging}
module.exports = {indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging, searchBelongings}