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:
@@ -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()
|
||||
|
||||
|
||||
@@ -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}
|
||||
Reference in New Issue
Block a user