Files
mbmanager-api/components/belonging/belongingService.js
T
kavehhn 25a6978346 ADD: Description to belongingModel
ADD: Typesense Index All function
2025-01-02 16:00:38 +03:30

114 lines
3.6 KiB
JavaScript

const Belonging = require('./belongingModel')
const AppError = require('../../globalErrorHandler/AppError')
const { client } = require('../../setup/typesense')
const getAllBelongings = async () => {
return Belonging.find({}, null, null)
}
const createBelonging = async (data) => {
return Belonging.create(data, null)
}
const getBelonging = async (belongingId) => {
const belonging = await Belonging.findById(belongingId, null, null)
if (!belonging) {
throw new AppError('NoBelongingFound', 404, {})
}
return belonging
}
const updateBelonging = async (belongingId, data) => {
const belonging = await Belonging.findOneAndUpdate({ _id: belongingId }, data, { new: true })
if (!belonging) {
throw new AppError('NoBelongingFound', 404, {})
}
return belonging
}
const deleteBelonging = async (belongingId) => {
const belonging = await Belonging.findByIdAndUpdate(belongingId, { isActive: false }, { new: true })
if (!belonging) {
throw new AppError('NoBelongingFound', 404, {})
}
return null
}
const ensureCollectionExists = async () => {
const collections = await client.collections().retrieve()
const collectionExists = collections.some((collection) => collection.name === 'belongings')
if (collectionExists) {
await client.collections('belongings').delete()
}
const schema = getTypeSenseSchema()
await client.collections().create(schema)
console.log('Collection "belongings" created successfully')
}
const indexAllBelongings = async () => {
const batchSize = 20
const count = await Product.countDocuments({isTrash: false, isActive: true, isHidden: false}, null)
const rounds = Math.ceil(count / batchSize)
await ensureCollectionExists()
for (let i = 1; i <= rounds; i++) {
console.log(`Indexing ${batchSize} products, Round ${i}`)
const belongings = await Belonging.find({}, null, null)
.populate('category')
.skip((i - 1) * batchSize)
.limit(batchSize)
let documents = []
for (const belonging of belongings) {
const data = {
_id: belonging._id,
isActive: belonging.isActive,
belongingCode: belonging.belongingCode,
category: belonging.category.map( x=> x.englishName),
nativeCategory: belonging.category.map( x=> x.nativeName),
documents: belonging.documents,
description: belonging.description,
coordinates: belonging.location.coordinates
}
documents.push(data)
}
await client.collections('belonging').documents().import(documents, {action: 'emplace'})
}
return {totalCount: count}
}
const getTypeSenseSchema = () => {
return {
name: 'belongings',
enable_nested_fields: true,
fields: [
{ name: '_id', type: 'string', facet: false, infix: false },
{ name: 'isActive', type: 'bool', facet: false, infix: true },
{ name: 'belongingCode', type: 'string', facet: true },
{ name: 'category', type: 'string[]', facet: true },
{ name: 'nativeCategory', type: 'string[]', facet: false },
{ name: 'documents', type: 'string[]', facet: false },
{ name: 'description', type: 'string', facet: false },
{ name: 'coordinates', type: 'geopoint' },
],
default_sorting_field: 'createdAt',
}
}
module.exports = {indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging}