|
|
|
@@ -7,28 +7,96 @@ const TsApiFeatures = require('../../utils/TsApiFeatures')
|
|
|
|
|
const { moveFromTempToMain, getSignedUrl } = require('../file/fileService')
|
|
|
|
|
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 () => {
|
|
|
|
|
return Property.find({}, null, null).populate('category').populate('status')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const createProperty = async (data) => {
|
|
|
|
|
if (data.mainImageUrls && Array.isArray(data.mainImageUrls) && data.mainImageUrls.length > 0) {
|
|
|
|
|
data.mainImageUrls = await moveFromTempToMain(data.mainImageUrls, 'properties')
|
|
|
|
|
const cleanData = sanitizePropertyData(data)
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
data.documentUrls = await moveFromTempToMain(data.documentUrls, 'documents')
|
|
|
|
|
if (cleanData.documentUrls && Array.isArray(cleanData.documentUrls) && cleanData.documentUrls.length > 0) {
|
|
|
|
|
cleanData.documentUrls = await moveFromTempToMain(cleanData.documentUrls, 'documents')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.registrationDate !== undefined) {
|
|
|
|
|
data.registrationDate = normalizeToDate(data.registrationDate)
|
|
|
|
|
if (cleanData.propertyType === 'Land' || cleanData.propertyType === 'زمین') {
|
|
|
|
|
return Land.create(cleanData)
|
|
|
|
|
} else if (cleanData.propertyType === 'Building' || cleanData.propertyType === 'ساختمان') {
|
|
|
|
|
return Building.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)
|
|
|
|
|
return Property.create(cleanData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const resolvePropertyFilter = (propertyId) => {
|
|
|
|
@@ -76,19 +144,17 @@ const getProperty = async (propertyId) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updateProperty = async (propertyId, data) => {
|
|
|
|
|
if (data.mainImageUrls && Array.isArray(data.mainImageUrls) && data.mainImageUrls.length > 0) {
|
|
|
|
|
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')
|
|
|
|
|
}
|
|
|
|
|
const cleanData = sanitizePropertyData(data)
|
|
|
|
|
|
|
|
|
|
if (data.registrationDate !== undefined) {
|
|
|
|
|
data.registrationDate = normalizeToDate(data.registrationDate)
|
|
|
|
|
if (cleanData.mainImageUrls && Array.isArray(cleanData.mainImageUrls) && cleanData.mainImageUrls.length > 0) {
|
|
|
|
|
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 property = await Property.findOneAndUpdate(filter, data, { new: true })
|
|
|
|
|
const property = await Property.findOneAndUpdate(filter, cleanData, { new: true })
|
|
|
|
|
|
|
|
|
|
if (!property) {
|
|
|
|
|
throw new AppError('NoPropertyFound', 404, {})
|
|
|
|
@@ -323,30 +389,36 @@ const indexAllProperties = async () => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const indexOneProperty = async (propertyId) => {
|
|
|
|
|
await ensureCollectionExists(false)
|
|
|
|
|
const property = await getProperty(propertyId)
|
|
|
|
|
try {
|
|
|
|
|
await ensureCollectionExists(false)
|
|
|
|
|
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 = {
|
|
|
|
|
_id: property._id.toString(),
|
|
|
|
|
isActive: property.isActive,
|
|
|
|
|
name: property.name || '',
|
|
|
|
|
category: property.category?.englishName || '',
|
|
|
|
|
nativeCategory: property.category?.nativeName || '',
|
|
|
|
|
imageUrl: property.mainImageUrls?.[0] || '',
|
|
|
|
|
documents: property.documentUrls || [],
|
|
|
|
|
description: property.description || '',
|
|
|
|
|
nationalGeoCode: property.nationalGeoCode || '',
|
|
|
|
|
tags: combinedTags,
|
|
|
|
|
coordinates: property.location?.coordinates || [0, 0],
|
|
|
|
|
createdAt: Math.floor(new Date(property.createdAt || Date.now()).getTime() / 1000),
|
|
|
|
|
id: property.propertyCode ? property.propertyCode.toString() : property._id.toString(),
|
|
|
|
|
const data = {
|
|
|
|
|
_id: property._id.toString(),
|
|
|
|
|
isActive: property.isActive !== false,
|
|
|
|
|
name: property.name || '',
|
|
|
|
|
category: property.category?.englishName || (typeof property.category === 'string' ? property.category : ''),
|
|
|
|
|
nativeCategory: property.category?.nativeName || '',
|
|
|
|
|
imageUrl: property.mainImageUrls?.[0] || '',
|
|
|
|
|
documents: property.documentUrls || [],
|
|
|
|
|
description: property.description || '',
|
|
|
|
|
nationalGeoCode: property.nationalGeoCode || '',
|
|
|
|
|
tags: combinedTags,
|
|
|
|
|
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),
|
|
|
|
|
id: property.propertyCode ? property.propertyCode.toString() : property._id.toString(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await client.collections('properties').documents().upsert(data)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn(`Typesense indexing skipped for property ${propertyId}:`, err.message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await client.collections('properties').documents().upsert(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getTypeSenseSchema = () => {
|
|
|
|
|