fix(property): optimize save performance, fix 2dsphere index error and make search indexing non-blocking
deploy-chabokan / deploy (push) Canceled after 0s

This commit is contained in:
2026-08-19 07:46:08 +03:30
parent 30ea3aed84
commit e63ac1ddf4
7 changed files with 193 additions and 82 deletions
+17 -15
View File
@@ -174,17 +174,15 @@ const moveFromTempToMain = async (files, targetBucket) => {
return [] return []
} }
const { ensureBucketsExist, minioClient } = require('../../utils/upload/bucket') const { minioClient } = require('../../utils/upload/bucket')
await ensureBucketsExist([tempBucket, targetBucket]).catch(() => {})
const updatedUrls = [] const updatedUrls = await Promise.all(
files.map(async (item) => {
for (const item of files) {
const rawStr = typeof item === 'object' && item !== null ? (item.url || item.fileName || item.name || '') : String(item || '') const rawStr = typeof item === 'object' && item !== null ? (item.url || item.fileName || item.name || '') : String(item || '')
const fileName = getLastUrlSegment(rawStr) const fileName = getLastUrlSegment(rawStr)
if (!fileName) { if (!fileName) {
continue return null
} }
try { try {
@@ -192,9 +190,13 @@ const moveFromTempToMain = async (files, targetBucket) => {
const existsInTemp = await minioClient.statObject(tempBucket, fileName).then(() => true).catch(() => false) const existsInTemp = await minioClient.statObject(tempBucket, fileName).then(() => true).catch(() => false)
if (existsInTemp) { if (existsInTemp) {
// Copy object from temp to targetBucket // Copy object from temp to targetBucket
await copyObject(targetBucket, fileName, `/${tempBucket}/${fileName}`) await copyObject(targetBucket, fileName, `/${tempBucket}/${fileName}`).catch((err) => {
console.error(`Failed to copy object "${fileName}" to "${targetBucket}":`, err.message)
})
// Remove from temp bucket // Remove from temp bucket
await removeObject(tempBucket, fileName) await removeObject(tempBucket, fileName).catch((err) => {
console.error(`Failed to remove object "${fileName}" from "${tempBucket}":`, err.message)
})
// Update File record in database if exists // Update File record in database if exists
await File.findOneAndUpdate({ fileName }, { bucketName: targetBucket }, null).catch(() => {}) await File.findOneAndUpdate({ fileName }, { bucketName: targetBucket }, null).catch(() => {})
} }
@@ -206,18 +208,18 @@ const moveFromTempToMain = async (files, targetBucket) => {
const fullHost = hostPort.startsWith('http://') || hostPort.startsWith('https://') ? hostPort : `${protocol}${hostPort}` const fullHost = hostPort.startsWith('http://') || hostPort.startsWith('https://') ? hostPort : `${protocol}${hostPort}`
const newUrl = `${fullHost.replace(/\/$/, '')}/${targetBucket}/${fileName}` const newUrl = `${fullHost.replace(/\/$/, '')}/${targetBucket}/${fileName}`
updatedUrls.push(newUrl) return newUrl
} catch (err) { } catch (err) {
console.error(`Failed to move file "${fileName}" from ${tempBucket} to ${targetBucket}:`, err) console.error(`Failed to move file "${fileName}" from ${tempBucket} to ${targetBucket}:`, err.message)
if (rawStr.includes(`/${tempBucket}/`)) { if (rawStr.includes(`/${tempBucket}/`)) {
updatedUrls.push(rawStr.replace(`/${tempBucket}/`, `/${targetBucket}/`)) return rawStr.replace(`/${tempBucket}/`, `/${targetBucket}/`)
} else {
updatedUrls.push(rawStr)
}
} }
return rawStr
} }
})
)
return updatedUrls return updatedUrls.filter(Boolean)
} }
const getImage = async (bucketName, fileName) => { const getImage = async (bucketName, fileName) => {
@@ -1,6 +1,12 @@
const propertyService = require('../propertyService') const propertyService = require('../propertyService')
module.exports = (eventEmitter) => { module.exports = (eventEmitter) => {
eventEmitter.on('createProperty', async (propertyData) => { eventEmitter.on('createProperty', async (propertyData) => {
try {
if (propertyData && propertyData._id) {
await propertyService.indexOneProperty(propertyData._id) await propertyService.indexOneProperty(propertyData._id)
}
} catch (err) {
console.warn('Error in createProperty event handler:', err.message)
}
}) })
} }
@@ -0,0 +1,12 @@
const propertyService = require('../propertyService')
module.exports = (eventEmitter) => {
eventEmitter.on('updateProperty', async (propertyData) => {
try {
if (propertyData && propertyData._id) {
await propertyService.indexOneProperty(propertyData._id)
}
} catch (err) {
console.warn('Error in updateProperty event handler:', err.message)
}
})
}
+1 -1
View File
@@ -77,7 +77,7 @@ exports.getProperty = catchAsync(async (req, res, next) => {
exports.updateProperty = catchAsync(async (req, res, next) => { exports.updateProperty = catchAsync(async (req, res, next) => {
const property = await propertyService.updateProperty(req.params.propertyId, req.body) const property = await propertyService.updateProperty(req.params.propertyId, req.body)
await propertyService.indexAllProperties() eventEmitter.emit('updateProperty', property)
res.status(200).json({ res.status(200).json({
status: 'success', status: 'success',
+18 -1
View File
@@ -73,6 +73,7 @@ const propertySchema = new mongoose.Schema(
ownershipDocs: String, ownershipDocs: String,
// Property Specifications (مشخصات ملک) // Property Specifications (مشخصات ملک)
area: Number, // مساحت عرصه (متر مربع)
registrationPlate: String, registrationPlate: String,
mainNumber: String, mainNumber: String,
subNumber: String, subNumber: String,
@@ -115,6 +116,22 @@ const propertySchema = new mongoose.Schema(
propertySchema.plugin(AutoIncrement, { inc_field: 'propertyCode' }) propertySchema.plugin(AutoIncrement, { inc_field: 'propertyCode' })
// Pre-validate hook: sanitize location for 2dsphere index
propertySchema.pre('validate', function (next) {
if (
!this.location ||
!Array.isArray(this.location.coordinates) ||
this.location.coordinates.length !== 2 ||
isNaN(this.location.coordinates[0]) ||
isNaN(this.location.coordinates[1])
) {
this.location = undefined
} else {
this.location.type = 'Point'
}
next()
})
propertySchema.methods.getDocumentsSignedUrls = function () { propertySchema.methods.getDocumentsSignedUrls = function () {
return getMultipleSignedUrls(this.documentUrls, 'documents') return getMultipleSignedUrls(this.documentUrls, 'documents')
} }
@@ -123,7 +140,7 @@ propertySchema.methods.getMainImagedSignedUrl = function () {
return getMultipleSignedUrls(this.mainImageUrls, 'properties') return getMultipleSignedUrls(this.mainImageUrls, 'properties')
} }
propertySchema.index({ location: '2dsphere' }) propertySchema.index({ location: '2dsphere' }, { sparse: true })
const Property = mongoose.model('Property', propertySchema) const Property = mongoose.model('Property', propertySchema)
+98 -26
View File
@@ -7,28 +7,96 @@ const TsApiFeatures = require('../../utils/TsApiFeatures')
const { moveFromTempToMain, getSignedUrl } = require('../file/fileService') const { moveFromTempToMain, getSignedUrl } = require('../file/fileService')
const { normalizeToDate } = require('../../utils/jalali') const { normalizeToDate } = require('../../utils/jalali')
const sanitizePropertyData = (data) => {
const cleanData = { ...data }
// Remove immutable / system fields
delete cleanData._id
delete cleanData.createdAt
delete cleanData.updatedAt
delete cleanData.__v
// Sanitize category & status ObjectIds
if (cleanData.category && !mongoose.Types.ObjectId.isValid(cleanData.category)) {
cleanData.category = null
} else if (cleanData.category === '') {
cleanData.category = null
}
if (cleanData.status && !mongoose.Types.ObjectId.isValid(cleanData.status)) {
cleanData.status = null
} else if (cleanData.status === '') {
cleanData.status = null
}
// Sanitize registrationDate
if (cleanData.registrationDate !== undefined) {
cleanData.registrationDate = normalizeToDate(cleanData.registrationDate)
}
// Sanitize Location (GeoJSON Point for 2dsphere)
if (cleanData.location) {
const coords = cleanData.location.coordinates
if (Array.isArray(coords) && coords.length === 2 && coords[0] !== null && coords[1] !== null && !isNaN(coords[0]) && !isNaN(coords[1])) {
cleanData.location = {
type: 'Point',
coordinates: [Number(coords[0]), Number(coords[1])]
}
} else {
cleanData.location = undefined
}
}
// Sanitize Features
if (cleanData.features && Array.isArray(cleanData.features)) {
cleanData.features = cleanData.features.filter(
(f) => f && f.definition && mongoose.Types.ObjectId.isValid(f.definition)
)
}
// Sanitize dong
if (cleanData.dong !== undefined && cleanData.dong !== null && cleanData.dong !== '') {
const dongNum = Number(cleanData.dong)
cleanData.dong = isNaN(dongNum) ? 6 : Math.max(1, Math.min(6, dongNum))
}
// Sanitize numeric fields
const numericFields = [
'area', 'landArea', 'builtArea', 'floorsCount', 'unitsCount',
'constructionYear', 'streetWidth', 'facadeCount', 'landLength', 'landWidth'
]
numericFields.forEach((field) => {
if (cleanData[field] === '' || cleanData[field] === undefined) {
cleanData[field] = null
} else if (cleanData[field] !== null) {
const num = Number(cleanData[field])
cleanData[field] = isNaN(num) ? null : num
}
})
return cleanData
}
const getAllProperties = async () => { const getAllProperties = async () => {
return Property.find({}, null, null).populate('category').populate('status') return Property.find({}, null, null).populate('category').populate('status')
} }
const createProperty = async (data) => { const createProperty = async (data) => {
if (data.mainImageUrls && Array.isArray(data.mainImageUrls) && data.mainImageUrls.length > 0) { const cleanData = sanitizePropertyData(data)
data.mainImageUrls = await moveFromTempToMain(data.mainImageUrls, 'properties')
if (cleanData.mainImageUrls && Array.isArray(cleanData.mainImageUrls) && cleanData.mainImageUrls.length > 0) {
cleanData.mainImageUrls = await moveFromTempToMain(cleanData.mainImageUrls, 'properties')
} }
if (data.documentUrls && Array.isArray(data.documentUrls) && data.documentUrls.length > 0) { if (cleanData.documentUrls && Array.isArray(cleanData.documentUrls) && cleanData.documentUrls.length > 0) {
data.documentUrls = await moveFromTempToMain(data.documentUrls, 'documents') cleanData.documentUrls = await moveFromTempToMain(cleanData.documentUrls, 'documents')
} }
if (data.registrationDate !== undefined) { if (cleanData.propertyType === 'Land' || cleanData.propertyType === 'زمین') {
data.registrationDate = normalizeToDate(data.registrationDate) return Land.create(cleanData)
} else if (cleanData.propertyType === 'Building' || cleanData.propertyType === 'ساختمان') {
return Building.create(cleanData)
} }
return Property.create(cleanData)
if (data.propertyType === 'Land' || data.propertyType === 'زمین') {
return Land.create(data)
} else if (data.propertyType === 'Building' || data.propertyType === 'ساختمان') {
return Building.create(data)
}
return Property.create(data)
} }
const resolvePropertyFilter = (propertyId) => { const resolvePropertyFilter = (propertyId) => {
@@ -76,19 +144,17 @@ const getProperty = async (propertyId) => {
} }
const updateProperty = async (propertyId, data) => { const updateProperty = async (propertyId, data) => {
if (data.mainImageUrls && Array.isArray(data.mainImageUrls) && data.mainImageUrls.length > 0) { const cleanData = sanitizePropertyData(data)
data.mainImageUrls = await moveFromTempToMain(data.mainImageUrls, 'properties')
}
if (data.documentUrls && Array.isArray(data.documentUrls) && data.documentUrls.length > 0) {
data.documentUrls = await moveFromTempToMain(data.documentUrls, 'documents')
}
if (data.registrationDate !== undefined) { if (cleanData.mainImageUrls && Array.isArray(cleanData.mainImageUrls) && cleanData.mainImageUrls.length > 0) {
data.registrationDate = normalizeToDate(data.registrationDate) cleanData.mainImageUrls = await moveFromTempToMain(cleanData.mainImageUrls, 'properties')
}
if (cleanData.documentUrls && Array.isArray(cleanData.documentUrls) && cleanData.documentUrls.length > 0) {
cleanData.documentUrls = await moveFromTempToMain(cleanData.documentUrls, 'documents')
} }
const filter = resolvePropertyFilter(propertyId) const filter = resolvePropertyFilter(propertyId)
const property = await Property.findOneAndUpdate(filter, data, { new: true }) const property = await Property.findOneAndUpdate(filter, cleanData, { new: true })
if (!property) { if (!property) {
throw new AppError('NoPropertyFound', 404, {}) throw new AppError('NoPropertyFound', 404, {})
@@ -323,30 +389,36 @@ const indexAllProperties = async () => {
} }
const indexOneProperty = async (propertyId) => { const indexOneProperty = async (propertyId) => {
try {
await ensureCollectionExists(false) await ensureCollectionExists(false)
const property = await getProperty(propertyId) const property = await getProperty(propertyId)
console.log(`Property ${property._id} indexing`) if (!property) return
const combinedTags = Array.isArray(property.tags) ? property.tags.join(' ') : (property.tags || '') const combinedTags = Array.isArray(property.tags) ? property.tags.join(' ') : (property.tags || '')
const data = { const data = {
_id: property._id.toString(), _id: property._id.toString(),
isActive: property.isActive, isActive: property.isActive !== false,
name: property.name || '', name: property.name || '',
category: property.category?.englishName || '', category: property.category?.englishName || (typeof property.category === 'string' ? property.category : ''),
nativeCategory: property.category?.nativeName || '', nativeCategory: property.category?.nativeName || '',
imageUrl: property.mainImageUrls?.[0] || '', imageUrl: property.mainImageUrls?.[0] || '',
documents: property.documentUrls || [], documents: property.documentUrls || [],
description: property.description || '', description: property.description || '',
nationalGeoCode: property.nationalGeoCode || '', nationalGeoCode: property.nationalGeoCode || '',
tags: combinedTags, tags: combinedTags,
coordinates: property.location?.coordinates || [0, 0], coordinates: (property.location?.coordinates && property.location.coordinates.length === 2 && !isNaN(property.location.coordinates[0]) && !isNaN(property.location.coordinates[1]))
? [property.location.coordinates[1], property.location.coordinates[0]] // Typesense geopoint is [lat, lng]
: [0, 0],
createdAt: Math.floor(new Date(property.createdAt || Date.now()).getTime() / 1000), createdAt: Math.floor(new Date(property.createdAt || Date.now()).getTime() / 1000),
id: property.propertyCode ? property.propertyCode.toString() : property._id.toString(), id: property.propertyCode ? property.propertyCode.toString() : property._id.toString(),
} }
await client.collections('properties').documents().upsert(data) await client.collections('properties').documents().upsert(data)
} catch (err) {
console.warn(`Typesense indexing skipped for property ${propertyId}:`, err.message)
}
} }
const getTypeSenseSchema = () => { const getTypeSenseSchema = () => {
+2
View File
@@ -1,10 +1,12 @@
// events/index.js // events/index.js
const { eventEmitter } = require('./eventEmitter') const { eventEmitter } = require('./eventEmitter')
const createProperty = require('../components/property/events/createProperty') const createProperty = require('../components/property/events/createProperty')
const updateProperty = require('../components/property/events/updateProperty')
const deleteProperty = require('../components/property/events/deleteProperty') const deleteProperty = require('../components/property/events/deleteProperty')
const registerEventHandlers = () => { const registerEventHandlers = () => {
// Register all event handlers // Register all event handlers
createProperty(eventEmitter) createProperty(eventEmitter)
updateProperty(eventEmitter)
deleteProperty(eventEmitter) deleteProperty(eventEmitter)
// Add additional error monitoring if needed // Add additional error monitoring if needed
eventEmitter.on('error', (error) => { eventEmitter.on('error', (error) => {