ADD: Events

ADD: Index One Belonging
ADD: Belonging Code
This commit is contained in:
2025-06-04 16:07:45 +03:30
parent 98044a18f2
commit de60d4f163
9 changed files with 142 additions and 7 deletions
+34 -5
View File
@@ -85,7 +85,7 @@ const updateInputQuery = (inputQuery, extraQueries, features) => {
const indexAllBelongings = async () => {
const batchSize = 20
const count = await Belonging.countDocuments({}, null)
const count = await Belonging.countDocuments({isActive: true}, null)
const rounds = Math.ceil(count / batchSize)
await ensureCollectionExists()
@@ -93,7 +93,7 @@ const indexAllBelongings = async () => {
for (let i = 1; i <= rounds; i++) {
console.log(`Indexing ${batchSize} belongings, Round ${i}`)
const belongings = await Belonging.find({}, null, null)
const belongings = await Belonging.find({isActive: true}, null, null)
.populate('category')
.skip((i - 1) * batchSize)
.limit(batchSize)
@@ -109,8 +109,8 @@ const indexAllBelongings = async () => {
isActive: belonging.isActive,
// belongingCode: belonging.belongingCode,
name: belonging.name,
category: belonging.category.englishName || '',
nativeCategory: belonging.category.nativeName || '',
category: belonging.category?.englishName || '',
nativeCategory: belonging.category?.nativeName || '',
imageUrl: belonging.mainImageUrls[0] || '',
documents: belonging.documentUrls || [] ,
description: belonging.description || '',
@@ -118,6 +118,7 @@ const indexAllBelongings = async () => {
tags: combinedTags || '',
coordinates: belonging.location.coordinates || [0, 0],
createdAt: Math.floor(new Date(belonging.createdAt).getTime() / 1000), // Store as timestamp in seconds
id: belonging.belongingCode.toString(),
}
documents.push(data)
@@ -129,6 +130,34 @@ const indexAllBelongings = async () => {
return { totalCount: count }
}
const indexOneBelonging = async (belongingId) => {
const belonging = await getBelonging(belongingId)
console.log(`Belonging ${belonging._id} indexing`)
const combinedTags = belonging.tags.join(' ');
const data = {
_id: belonging._id,
isActive: belonging.isActive,
// belongingCode: belonging.belongingCode,
name: belonging.name,
category: belonging.category?.englishName || '',
nativeCategory: belonging.category?.nativeName || '',
imageUrl: belonging.mainImageUrls[0] || '',
documents: belonging.documentUrls || [] ,
description: belonging.description || '',
unit: belonging.unit || '',
tags: combinedTags || '',
coordinates: belonging.location.coordinates || [0, 0],
createdAt: Math.floor(new Date(belonging.createdAt).getTime() / 1000), // Store as timestamp in seconds
id: belonging.belongingCode.toString(),
}
await client.collections('belongings').documents().upsert(data)
}
const getTypeSenseSchema = () => {
return {
name: 'belongings',
@@ -150,4 +179,4 @@ const getTypeSenseSchema = () => {
}
}
module.exports = { updateBelonging, indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging, searchBelongings }
module.exports = { updateBelonging, indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging, searchBelongings, indexOneBelonging }