const mongoose = require('mongoose') const ObjectID = mongoose.Schema.Types.ObjectId const AutoIncrement = require('mongoose-sequence')(mongoose) const { getMultipleSignedUrls } = require('../file/fileService') const propertySchemaOptions = { discriminatorKey: 'propertyType', toJSON: { virtuals: true }, toObject: { virtuals: true }, timestamps: true, } const propertySchema = new mongoose.Schema( { name: { type: String, required: false, // Auto-generated: ownerName + mainNumber/subNumber }, category: { type: ObjectID, ref: 'Category', }, description: String, relatedProjects: [String], location: { type: { type: String, enum: ['Point'], required: false, }, coordinates: { type: [Number], // [longitude, latitude] required: false, }, }, documentUrls: { type: [String], default: [], }, mainImageUrls: [String], nationalGeoCode: { type: String, required: true, // شناسه ملی جغرافیایی الزامی است }, tags: [String], status: { type: ObjectID, ref: 'PropertyStatus', }, isActive: { type: Boolean, default: true, select: false, }, // Title Deed General & Administrative (شناسه و اداره ثبت) uniqueId: String, // شناسه یکتا سند مالکیت (۱۸ رقمی) serialNumber: String, // شماره سریال چاپی سند provinceRegistry: String, localRegistryOffice: String, registrationDate: Date, // تاریخ ثبت سند (MongoDB Date object) // Owner Specifications (مشخصات مالک) ownerName: String, fatherName: String, idCardNumber: String, nationalId: String, issuePlace: String, birthDate: String, nationality: { type: String, default: 'ایران', }, ownershipDocs: String, // Property Specifications (مشخصات ملک) area: Number, // مساحت عرصه (متر مربع) registrationPlate: String, mainNumber: String, subNumber: String, separatedFrom: String, plotNumber: String, registrationSection: String, district: String, blockNumber: String, sideDirection: String, floor: String, zoningType: String, specialStatus: String, documentType: String, address: String, postalCode: String, propertyBoundaries: String, restrictions: String, // دانگ: سهم ملک از ۶ دانگ (۱ تا ۶) dong: { type: Number, min: 1, max: 6, default: 6, }, features: [ { definition: { type: ObjectID, ref: 'Feature', required: true, }, value: mongoose.Schema.Types.Mixed, }, ], }, propertySchemaOptions ) 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 () { return getMultipleSignedUrls(this.documentUrls, 'documents') } propertySchema.methods.getMainImagedSignedUrl = function () { return getMultipleSignedUrls(this.mainImageUrls, 'properties') } propertySchema.index({ location: '2dsphere' }, { sparse: true }) const Property = mongoose.model('Property', propertySchema) module.exports = Property