fix(property): support resolving properties by propertyCode and ObjectId in CRUD
deploy-chabokan / deploy (push) Canceled after 0s
deploy-chabokan / deploy (push) Canceled after 0s
This commit is contained in:
@@ -31,20 +31,46 @@ const createProperty = async (data) => {
|
||||
return Property.create(data)
|
||||
}
|
||||
|
||||
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) => {
|
||||
let property = await Property.findById(propertyId, null, null).populate('category').populate('status')
|
||||
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, {})
|
||||
}
|
||||
|
||||
const documentUrls = await property.getDocumentsSignedUrls()
|
||||
const mainImageUrls = await property.getMainImagedSignedUrl()
|
||||
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
|
||||
property.signedDocumentUrls = documentUrls || []
|
||||
property.signedMainImageUrls = mainImageUrls || []
|
||||
|
||||
return property
|
||||
}
|
||||
@@ -61,7 +87,8 @@ const updateProperty = async (propertyId, data) => {
|
||||
data.registrationDate = normalizeToDate(data.registrationDate)
|
||||
}
|
||||
|
||||
const property = await Property.findOneAndUpdate({ _id: propertyId }, data, { new: true })
|
||||
const filter = resolvePropertyFilter(propertyId)
|
||||
const property = await Property.findOneAndUpdate(filter, data, { new: true })
|
||||
|
||||
if (!property) {
|
||||
throw new AppError('NoPropertyFound', 404, {})
|
||||
@@ -71,7 +98,8 @@ const updateProperty = async (propertyId, data) => {
|
||||
}
|
||||
|
||||
const deleteProperty = async (propertyId) => {
|
||||
const property = await Property.findByIdAndUpdate(propertyId, { isActive: false }, { new: true })
|
||||
const filter = resolvePropertyFilter(propertyId)
|
||||
const property = await Property.findOneAndUpdate(filter, { isActive: false }, { new: true })
|
||||
|
||||
if (!property) {
|
||||
throw new AppError('NoPropertyFound', 404, {})
|
||||
|
||||
Reference in New Issue
Block a user