From 0fe0587846ce6ad86891f8926fc936718e5579e2 Mon Sep 17 00:00:00 2001 From: Kaveh Hajinajaf Date: Sat, 19 Apr 2025 23:33:11 +0330 Subject: [PATCH] FIX: Bugs --- components/belonging/belongingModel.js | 35 ++++++++++++++++-------- components/belonging/belongingRoutes.js | 2 +- components/belonging/belongingService.js | 17 ++++++++---- components/file/fileService.js | 2 +- config/.env.sample | 13 +++++++++ middlewares/multerMiddleware.js | 2 +- middlewares/permissionMiddleware.js | 4 +-- 7 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 config/.env.sample diff --git a/components/belonging/belongingModel.js b/components/belonging/belongingModel.js index cd8a6a7..c2b8179 100644 --- a/components/belonging/belongingModel.js +++ b/components/belonging/belongingModel.js @@ -5,11 +5,15 @@ const { getMultipleSignedUrls, getSignedUrl } = require('../file/fileService') const belongingSchema = new mongoose.Schema( { - belongingCode: { + name: { type: String, required: true, unique: true, }, + belongingCode: { + type: Number, + unique: true, + }, category: { type: ObjectID, ref: 'Category', @@ -20,26 +24,33 @@ const belongingSchema = new mongoose.Schema( type: { type: String, enum: ['Point'], // GeoJSON type - required: true, + required: false, }, coordinates: { type: [Number], // Array of numbers: [longitude, latitude] - required: true, + required: false, }, }, documentNames: [String], imageNames: [String], mainImageNames: [String], - numId: { - type: Number, - unique: true, - }, + mainImageUrl: String, tags: [String], isActive: { type: Boolean, default: true, select: false, }, + features: [ + { + definition: { + type: ObjectID, + ref: 'Feature', + required: true, + }, + value: mongoose.Schema.Types.Mixed, + }, + ], }, { toJSON: { virtuals: true }, @@ -48,17 +59,17 @@ const belongingSchema = new mongoose.Schema( } ) -belongingSchema.plugin(AutoIncrement, { inc_field: 'numId' }) +belongingSchema.plugin(AutoIncrement, { inc_field: 'belongingCode' }) -belongingSchema.methods.getDocumentsSignedUrls = async function () { - return getMultipleSignedUrls(this.documentNames, 'belonging-images') +belongingSchema.methods.getDocumentsSignedUrls = function () { + return getMultipleSignedUrls(this.documentNames, 'belonging-documents') } -belongingSchema.methods.getImagesSignedUrls = async function () { +belongingSchema.methods.getImagesSignedUrls = function () { return getMultipleSignedUrls(this.imageNames, 'belonging-images') } -belongingSchema.methods.getMainImagedSignedUrl = async function () { +belongingSchema.methods.getMainImagedSignedUrl = function () { return getMultipleSignedUrls(this.mainImageNames, 'belonging-images') } diff --git a/components/belonging/belongingRoutes.js b/components/belonging/belongingRoutes.js index d2d306c..584f9c9 100644 --- a/components/belonging/belongingRoutes.js +++ b/components/belonging/belongingRoutes.js @@ -15,7 +15,7 @@ router.post('/admin/index-all', perm.requires('indexbelongings', belongingContro router.get('/admin/get-one/:belongingId', perm.requires('getabelonging'), belongingController.getBelonging) -router.patch('/admin/update/:belongingId', perm.requires('updateabelonging'), belongingController.updateBelonging) +router.put('/admin/update/:belongingId', perm.requires('updateabelonging'), belongingController.updateBelonging) router.delete('/admin/delete/:belongingId', perm.requires('deleteabelonging'), belongingController.deleteBelonging) diff --git a/components/belonging/belongingService.js b/components/belonging/belongingService.js index 1deeb8c..bf889dc 100644 --- a/components/belonging/belongingService.js +++ b/components/belonging/belongingService.js @@ -4,25 +4,30 @@ const { client } = require('../../setup/typesense') const TsApiFeatures = require("../../utils/TsApiFeatures"); const getAllBelongings = async () => { - return Belonging.find({}, null, null) + return Belonging.find({}, null, null).populate('category') } const createBelonging = async (data) => { + console.log(data) return Belonging.create(data, null) } const getBelonging = async (belongingId) => { - const belonging = await Belonging.findById(belongingId, null, null).lean() + let belonging = await Belonging.findById(belongingId, null, null) if (!belonging) { throw new AppError('NoBelongingFound', 404, {}) } + const documentUrls = await belonging.getDocumentsSignedUrls() + const imageUrls = await belonging.getImagesSignedUrls() + const mainImageUrls = await belonging.getMainImagedSignedUrl() - belonging.signedDocumentUrls = belonging.getDocumentsSignedUrls() - belonging.signedImageUrls = belonging.getImagesSignedUrls() - belonging.signedMainImageUrls = belonging.getMainImagedSignedUrl() + belonging = JSON.parse(JSON.stringify(belonging)) + belonging.signedDocumentUrls = documentUrls + belonging.signedImageUrls = imageUrls + belonging.signedMainImageUrls = mainImageUrls return belonging @@ -127,4 +132,4 @@ const getTypeSenseSchema = () => { } } -module.exports = {indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging, searchBelongings} \ No newline at end of file +module.exports = {updateBelonging, indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging, searchBelongings} \ No newline at end of file diff --git a/components/file/fileService.js b/components/file/fileService.js index 9b85d5f..04079cd 100644 --- a/components/file/fileService.js +++ b/components/file/fileService.js @@ -106,7 +106,7 @@ const getSignedUrl = async (fileName, bucketName) => { const getMultipleSignedUrls = async (fileNameArray, bucketName) => { const urls = [] for (let fileName of fileNameArray) { - urls.push({ url: await getSignedUrl(fileName, bucketName)}) + urls.push({ url: await getSignedUrl(fileName, bucketName), fileName: fileName}) } return urls } diff --git a/config/.env.sample b/config/.env.sample new file mode 100644 index 0000000..afe3f52 --- /dev/null +++ b/config/.env.sample @@ -0,0 +1,13 @@ +MONGODB_URL=mongodb://admin:*135792468*@localhost:27018/marbm?authSource=admin +NODE_ENV=development +PORT=3000 +JWT_SECRET=Y@RJNS@MPL! +REDIS_URL= redis://:135792468@localhost:6379/0 +LOGIN_ATTEMPT_LIMIT=10 +SUPER_ADMIN_ROLE_ID=66a2da5b9b989a8909948848 +TYPE_SENSE_URL=localhost:8108 +TYPE_SENSE_API_KEY=vYd7pTgaoRy +AWS_ENDPOINT=localhost:8001 +AWS_ACCESS_KEY=EFYrmV0EiasdtbhJygNglaCZYtti +AWS_SECRET_KEY=ArkI5GDTIdaKFSp54lrx78gSW1 +AWS_REGION=default \ No newline at end of file diff --git a/middlewares/multerMiddleware.js b/middlewares/multerMiddleware.js index 6cea728..78a5931 100644 --- a/middlewares/multerMiddleware.js +++ b/middlewares/multerMiddleware.js @@ -1,7 +1,7 @@ const multer = require('multer') const AppError = require('../globalErrorHandler/AppError') -const types = ['png', 'jpg', 'jpeg', 'webp'] +const types = ['png', 'jpg', 'jpeg', 'webp', 'pdf', 'doc', 'docx', 'xlsx', 'dwg'] const formatsText = types.join(' - ') const fileFilter = (req, file, cb) => { diff --git a/middlewares/permissionMiddleware.js b/middlewares/permissionMiddleware.js index ac20844..0561fb4 100644 --- a/middlewares/permissionMiddleware.js +++ b/middlewares/permissionMiddleware.js @@ -19,7 +19,7 @@ const requires = (...permissions) => { if ( !checker(rolePerm, perm?._id) && !checker(userPerm, perm?._id) && - role._id.toString() !== process.env.SUPER_ADMIN_ID + role._id.toString() !== process.env.SUPER_ADMIN_ROLE_ID ) { throw new AppError('NoPermissionAccess', 403, {}) } @@ -50,7 +50,7 @@ const adminCheck = () => { if ( !checker(rolePerm, perm?._id) && !checker(userPerm, perm?._id) && - user.role?._id.toString() !== process.env.SUPER_ADMIN_ID + user.role?._id.toString() !== process.env.SUPER_ADMIN_ROLE_ID ) { throw new AppError('NoPermissionAccess', 403, {}) }