From 95c0532374b3b82f86334e894967207242a3704c Mon Sep 17 00:00:00 2001 From: Kavehhn174 Date: Wed, 19 Aug 2026 07:08:36 +0330 Subject: [PATCH] fix(auth/property): resolve auth token resolution, add property seeder --- components/property/propertySeeder.js | 148 ++++++++++++++++++++++++++ components/user/userController.js | 10 ++ middlewares/loginCheck.js | 11 +- seeder/seederRunner.js | 2 + 4 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 components/property/propertySeeder.js diff --git a/components/property/propertySeeder.js b/components/property/propertySeeder.js new file mode 100644 index 0000000..df2e7ef --- /dev/null +++ b/components/property/propertySeeder.js @@ -0,0 +1,148 @@ +const Property = require('./propertyModel') +const Land = require('./landModel') +const Building = require('./buildingModel') +const Category = require('../category/categoryModel') +const PropertyStatus = require('../propertyStatus/propertyStatusModel') +const chalk = require('chalk') + +const runSeeder = async () => { + const buildingCategory = await Category.findOne({ englishName: 'Building' }) + const lotCategory = await Category.findOne({ englishName: 'Lot' }) + const availableStatus = await PropertyStatus.findOne({ englishName: 'available' }) + const inReviewStatus = await PropertyStatus.findOne({ englishName: 'in_review' }) + const reservedStatus = await PropertyStatus.findOne({ englishName: 'reserved' }) + + const sampleProperties = [ + { + name: 'مجتمع اداری تجاری نگین الهیه', + propertyType: 'Building', + category: buildingCategory?._id, + status: availableStatus?._id, + nationalGeoCode: '537123395123439', + description: 'مجتمع اداری ۱۰ طبقه با دسترسی عالی به بزرگراه صدر و مترو', + dong: 6, + ownerName: 'شرکت سرمایه‌گذاری پارس', + registrationPlate: '۱۲۳۴/۵۶', + uniqueId: '140201123456789012', + serialNumber: 'A/987654', + zoningType: 'تجاری - اداری', + address: 'تهران، الهیه، خیابان فرشته، پلاک ۱۲', + postalCode: '1965834211', + location: { + type: 'Point', + coordinates: [51.4235, 35.7952], // [lng, lat] + }, + tags: ['اداری', 'الهیه', 'مدرن', 'سند تک‌برگ'], + isActive: true, + }, + { + name: 'زمین ویلایی شهرک غرب فاز ۴', + propertyType: 'Land', + category: lotCategory?._id, + status: availableStatus?._id, + nationalGeoCode: '536987394852139', + description: 'زمین دو کله با بر ۲۵ متر و دستور نقشه ۵ طبقه مسکونی', + dong: 6, + ownerName: 'محسن رضایی', + registrationPlate: '۴۵۶۷/۸۹', + uniqueId: '140202234567890123', + serialNumber: 'B/123456', + zoningType: 'مسکونی', + address: 'تهران، شهرک غرب، فاز ۴، خیابان فلامک شمالی', + postalCode: '1467893214', + location: { + type: 'Point', + coordinates: [51.3712, 35.7725], + }, + tags: ['زمین', 'شهرک غرب', 'دو کله'], + isActive: true, + }, + { + name: 'برج مسکونی یاس نیاوران', + propertyType: 'Building', + category: buildingCategory?._id, + status: reservedStatus?._id || availableStatus?._id, + nationalGeoCode: '539145395412039', + description: 'واحد ۳۰۰ متری طبقه ۸ با ویوی ابدی کاخ نیاوران و ۳ پارکینگ سندی', + dong: 6, + ownerName: 'سارا صادقی', + registrationPlate: '۷۸۹۰/۱۲', + uniqueId: '140203345678901234', + serialNumber: 'C/654321', + zoningType: 'مسکونی', + address: 'تهران، نیاوران، خیابان باهنر، کوچه یاس', + postalCode: '1978945612', + location: { + type: 'Point', + coordinates: [51.4678, 35.8156], + }, + tags: ['لوکس', 'نیاوران', 'پنت‌هاوس'], + isActive: true, + }, + { + name: 'باغ ویلای قصرالدشت شیراز', + propertyType: 'Land', + category: lotCategory?._id, + status: availableStatus?._id, + nationalGeoCode: '645123328456139', + description: 'باغ ۲۰۰۰ متری با درختان کهنسال و عمارت دوبلکس ۴۰۰ متری', + dong: 6, + ownerName: 'علیرضا زندی', + registrationPlate: '۳۲۱۴/۵۶', + uniqueId: '140204456789012345', + serialNumber: 'D/789123', + zoningType: 'باغ ویلا', + address: 'شیراز، قصرالدشت، خیابان وکلا، کوچه ۱۲', + postalCode: '7198765432', + location: { + type: 'Point', + coordinates: [52.4823, 29.6412], + }, + tags: ['شیراز', 'باغ', 'قصرالدشت'], + isActive: true, + }, + { + name: 'ملک تجاری چهارباغ عباسی اصفهان', + propertyType: 'Building', + category: buildingCategory?._id, + status: inReviewStatus?._id || availableStatus?._id, + nationalGeoCode: '584123361456239', + description: 'مغازه تجاری دو دهنه بر خیابان اصلی چهارباغ با سند شش دانگ ملکیت', + dong: 6, + ownerName: 'محمد مهدی اصفهانی', + registrationPlate: '۹۶۳۲/۱۴', + uniqueId: '140205567890123456', + serialNumber: 'E/456789', + zoningType: 'تجاری', + address: 'اصفهان، خیابان چهارباغ عباسی، جنب سینما سپاهان', + postalCode: '8145678901', + location: { + type: 'Point', + coordinates: [51.6685, 32.6546], + }, + tags: ['اصفهان', 'تجاری', 'چهارباغ'], + isActive: true, + }, + ] + + await Property.deleteMany({}) + console.log(chalk.red('Deleted all property records.')) + + for (const propData of sampleProperties) { + if (propData.propertyType === 'Building') { + await Building.create(propData) + } else if (propData.propertyType === 'Land') { + await Land.create(propData) + } else { + await Property.create(propData) + } + } + + console.log( + chalk.green.bold( + `PROPERTIES: Created ${sampleProperties.length} sample property(ies)` + ) + ) +} + +module.exports = { runSeeder } diff --git a/components/user/userController.js b/components/user/userController.js index aaa3e42..3641772 100644 --- a/components/user/userController.js +++ b/components/user/userController.js @@ -26,6 +26,11 @@ exports.loginUser = catchAsync(async (req, res, next) => { maxAge: maxAge * 1000, path: '/', }) + res.cookie('bm-token', token, { + httpOnly: false, + maxAge: maxAge * 1000, + path: '/', + }) res.status(200).json({ status: 'success', @@ -39,6 +44,11 @@ exports.logoutUser = catchAsync(async (req, res, next) => { maxAge: 0, path: '/', }) + res.cookie('bm-token', '', { + httpOnly: false, + maxAge: 0, + path: '/', + }) res.status(200).json({ status: 'success', diff --git a/middlewares/loginCheck.js b/middlewares/loginCheck.js index 2b4a5d4..4949aeb 100644 --- a/middlewares/loginCheck.js +++ b/middlewares/loginCheck.js @@ -8,10 +8,15 @@ exports.loginProtect = async (req, res, next) => { let token - token = req.cookies['bm-token'] - if (req.headers.authorization && req.headers.authorization.startsWith('Bearer')) { - token = req.headers.authorization.split(' ')[1] + const headerToken = req.headers.authorization.split(' ')[1] + if (headerToken && headerToken !== 'undefined' && headerToken !== 'null' && headerToken.trim() !== '') { + token = headerToken.trim() + } + } + + if (!token && req.cookies) { + token = req.cookies['bm-token'] || req.cookies['belonging_token'] || req.cookies['jwt'] } if (!token) { diff --git a/seeder/seederRunner.js b/seeder/seederRunner.js index 7c11a5e..f7f6405 100644 --- a/seeder/seederRunner.js +++ b/seeder/seederRunner.js @@ -3,6 +3,7 @@ const categorySeeder = require('../components/category/categorySeeder') const roleSeeder = require('../components/role/roleSeeder') const featureSeeder = require('../components/feature/featureSeeder') const propertyStatusSeeder = require('../components/propertyStatus/propertyStatusSeeder') +const propertySeeder = require('../components/property/propertySeeder') const propertyService = require('../components/property/propertyService') const transactionService = require('../components/transaction/transactionService') @@ -12,6 +13,7 @@ module.exports.run = async () => { await roleSeeder.runSeeder() await featureSeeder.runSeeder() await propertyStatusSeeder.runSeeder() + await propertySeeder.runSeeder() try { await propertyService.indexAllProperties()