Files
mbmanager-api/components/property/propertyService.js
T

456 lines
17 KiB
JavaScript

const Property = require('./propertyModel')
const Land = require('./landModel')
const Building = require('./buildingModel')
const AppError = require('../../globalErrorHandler/AppError')
const { client } = require('../../setup/typesense')
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) => {
const cleanData = sanitizePropertyData(data)
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')
}
if (cleanData.propertyType === 'Land' || cleanData.propertyType === 'زمین') {
return Land.create(cleanData)
} else if (cleanData.propertyType === 'Building' || cleanData.propertyType === 'ساختمان') {
return Building.create(cleanData)
}
return Property.create(cleanData)
}
const resolvePropertyFilter = (propertyId) => {
if (!propertyId) return { _id: null }
if (mongoose.Types.ObjectId.isValid(propertyId)) {
return { _id: propertyId }
}
if (!isNaN(propertyId) && String(propertyId).trim() !== '') {
return { propertyCode: Number(propertyId) }
}
return { _id: propertyId }
}
const getProperty = async (propertyId) => {
const filter = resolvePropertyFilter(propertyId)
let property = await Property.findOne(filter, null, null).populate('category').populate('status')
if (!property && !filter._id && mongoose.Types.ObjectId.isValid(propertyId)) {
property = await Property.findById(propertyId, null, null).populate('category').populate('status')
}
if (!property) {
throw new AppError('NoPropertyFound', 404, {})
}
let documentUrls = []
let mainImageUrls = []
try {
if (typeof property.getDocumentsSignedUrls === 'function') {
documentUrls = await property.getDocumentsSignedUrls()
}
if (typeof property.getMainImagedSignedUrl === 'function') {
mainImageUrls = await property.getMainImagedSignedUrl()
}
} catch (e) {
console.warn('Error fetching signed URLs for property:', e.message)
}
property = JSON.parse(JSON.stringify(property))
property.signedDocumentUrls = documentUrls || []
property.signedMainImageUrls = mainImageUrls || []
return property
}
const updateProperty = async (propertyId, data) => {
const cleanData = sanitizePropertyData(data)
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, cleanData, { new: true })
if (!property) {
throw new AppError('NoPropertyFound', 404, {})
}
return property
}
const deleteProperty = async (propertyId) => {
const filter = resolvePropertyFilter(propertyId)
const property = await Property.findOneAndUpdate(filter, { isActive: false }, { new: true })
if (!property) {
throw new AppError('NoPropertyFound', 404, {})
}
return null
}
const ensureCollectionExists = async (forceRecreate = false) => {
try {
const collections = await client.collections().retrieve()
const collectionExists = collections.some((collection) => collection.name === 'properties')
if (collectionExists && forceRecreate) {
await client.collections('properties').delete()
}
if (!collectionExists || forceRecreate) {
const schema = getTypeSenseSchema()
await client.collections().create(schema)
console.log('Collection "properties" created successfully')
}
} catch (err) {
console.error('Error ensuring properties collection exists:', err)
}
}
const settingService = require('../setting/settingService')
const mongoose = require('mongoose')
const searchPropertiesMongo = async (inputQuery = {}) => {
const page = parseInt(inputQuery.page) || 1
const per_page = parseInt(inputQuery.per_page) || parseInt(inputQuery.limit) || 20
const mongoQuery = { isActive: { $ne: false } }
const q = (inputQuery.query || inputQuery.search || inputQuery.q || '').trim()
if (q && q !== '*') {
const searchRegex = { $regex: q, $options: 'i' }
const orConditions = [
{ name: searchRegex },
{ nationalGeoCode: searchRegex },
{ uniqueId: searchRegex },
{ serialNumber: searchRegex },
{ ownerName: searchRegex },
{ registrationPlate: searchRegex },
{ description: searchRegex },
{ address: searchRegex },
{ tags: searchRegex },
{ relatedProjects: searchRegex },
]
if (!isNaN(q) && q !== '') {
orConditions.push({ propertyCode: Number(q) })
}
if (mongoose.Types.ObjectId.isValid(q)) {
orConditions.push({ _id: q })
}
mongoQuery.$or = orConditions
}
if (inputQuery.category && mongoose.Types.ObjectId.isValid(inputQuery.category)) {
mongoQuery.category = inputQuery.category
}
if (inputQuery.status && mongoose.Types.ObjectId.isValid(inputQuery.status)) {
mongoQuery.status = inputQuery.status
}
let sortObj = { createdAt: -1 }
if (inputQuery.sort) {
if (typeof inputQuery.sort === 'string') {
const parts = inputQuery.sort.split(':')
const field = parts[0]
const order = parts[1] === 'asc' ? 1 : -1
sortObj = { [field]: order }
} else if (typeof inputQuery.sort === 'object') {
sortObj = inputQuery.sort
}
}
const totalCount = await Property.countDocuments(mongoQuery)
const properties = await Property.find(mongoQuery)
.populate('category')
.populate('status')
.sort(sortObj)
.skip((page - 1) * per_page)
.limit(per_page)
const items = await Promise.all(properties.map(async (prop) => {
const combinedTags = Array.isArray(prop.tags) ? prop.tags.join(' ') : (prop.tags || '')
let signedThumb = null
if (prop.mainImageUrls && prop.mainImageUrls.length > 0) {
signedThumb = await getSignedUrl(prop.mainImageUrls[0], 'properties').catch(() => null)
}
return {
_id: prop._id.toString(),
id: prop.propertyCode ? prop.propertyCode.toString() : prop._id.toString(),
propertyCode: prop.propertyCode,
name: prop.name || '',
category: prop.category?.englishName || (typeof prop.category === 'string' ? prop.category : ''),
nativeCategory: prop.category?.nativeName || '',
categoryObj: prop.category,
status: prop.status,
imageUrl: signedThumb || 'https://cloud.mbmanager.ir/images/item-no-image.png',
mainImageUrls: prop.mainImageUrls || [],
documents: prop.documentUrls || [],
documentUrls: prop.documentUrls || [],
description: prop.description || '',
nationalGeoCode: prop.nationalGeoCode || '',
tags: combinedTags,
dong: prop.dong,
ownerName: prop.ownerName || '',
registrationPlate: prop.registrationPlate || '',
uniqueId: prop.uniqueId || '',
serialNumber: prop.serialNumber || '',
registrationDate: prop.registrationDate,
coordinates: prop.location?.coordinates || [0, 0],
location: prop.location,
createdAt: Math.floor(new Date(prop.createdAt || Date.now()).getTime() / 1000),
updatedAt: prop.updatedAt,
isActive: prop.isActive !== false,
features: prop.features || [],
}
}))
const pagesCount = Math.ceil(totalCount / per_page) || 1
return {
facets: [],
belongings: items,
properties: items,
totalCount,
pagesCount,
searchEngine: 'mongo',
}
}
const searchProperties = async (inputQuery) => {
try {
const setting = await settingService.getSearchSettingByPage('properties')
if (setting && setting.searchMethod === 'mongo') {
return searchPropertiesMongo(inputQuery)
}
} catch (e) {
console.warn('Failed to fetch search setting for properties, attempting typesense:', e.message)
}
try {
await ensureCollectionExists(false)
const updatedQuery = updateInputQuery({ ...inputQuery }, {})
const tsFeatures = new TsApiFeatures(client, 'properties', updatedQuery).filter().facet().sort().limitFields().paginate().search()
let { facets, items, totalCount, pagesCount } = await tsFeatures.execute()
return { facets, belongings: items, properties: items, totalCount, pagesCount, searchEngine: 'typesense' }
} catch (err) {
console.warn('Typesense search failed for properties, falling back to MongoDB:', err.message)
return searchPropertiesMongo(inputQuery)
}
}
const updateInputQuery = (inputQuery, extraQueries) => {
for (const [key, value] of Object.entries(extraQueries)) {
inputQuery[key] = value
}
inputQuery.query_by = 'name,nationalGeoCode,tags,description,_id'
return inputQuery
}
const indexAllProperties = async () => {
const batchSize = 20
const count = await Property.countDocuments({ isActive: true }, null)
const rounds = Math.max(1, Math.ceil(count / batchSize))
await ensureCollectionExists(true)
for (let i = 1; i <= rounds; i++) {
const properties = await Property.find({ isActive: true }, null, null)
.populate('category')
.skip((i - 1) * batchSize)
.limit(batchSize)
if (properties.length === 0) break
let documents = []
for (const property of properties) {
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] || 'https://cloud.mbmanager.ir/images/item-no-image.png',
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(),
}
documents.push(data)
}
if (documents.length > 0) {
await client.collections('properties').documents().import(documents, { action: 'emplace' })
}
}
return { totalCount: count }
}
const indexOneProperty = async (propertyId) => {
try {
await ensureCollectionExists(false)
const property = await getProperty(propertyId)
if (!property) return
const combinedTags = Array.isArray(property.tags) ? property.tags.join(' ') : (property.tags || '')
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)
}
}
const getTypeSenseSchema = () => {
return {
name: 'properties',
enable_nested_fields: true,
fields: [
{ name: '_id', type: 'string', facet: false, infix: false },
{ name: 'name', type: 'string', facet: false, infix: true },
{ name: 'category', type: 'string', facet: true, infix: true },
{ name: 'nativeCategory', type: 'string', facet: false, infix: true },
{ name: 'description', type: 'string', facet: false },
{ name: 'nationalGeoCode', type: 'string', facet: false, infix: true },
{ name: 'tags', type: 'string', facet: false, infix: true },
{ name: 'coordinates', type: 'geopoint' },
{ name: 'createdAt', type: 'int64', facet: false },
{ name: 'imageUrl', type: 'string', facet: false, infix: true },
],
default_sorting_field: 'createdAt',
}
}
module.exports = {
updateProperty,
indexAllProperties,
getProperty,
getAllProperties,
createProperty,
deleteProperty,
searchProperties,
searchPropertiesMongo,
indexOneProperty,
}