From 7d70a64cd6d962fa1d05985ae9004dcba26af248 Mon Sep 17 00:00:00 2001 From: Kavehhn174 Date: Wed, 19 Aug 2026 07:13:29 +0330 Subject: [PATCH] fix(property): support resolving properties by propertyCode and ObjectId in CRUD --- components/property/propertyService.js | 42 +++++++++++++++++++++----- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/components/property/propertyService.js b/components/property/propertyService.js index 9327563..c2f4e08 100644 --- a/components/property/propertyService.js +++ b/components/property/propertyService.js @@ -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, {})