From 11a86929e7ce147ca793b815c1ec6857181b0e7d Mon Sep 17 00:00:00 2001 From: Kaveh Hajinajaf Date: Mon, 21 Apr 2025 00:36:35 +0330 Subject: [PATCH 01/15] ADD: Required to role field in user --- components/user/userModel.js | 1 + components/user/userRoutes.js | 2 ++ 2 files changed, 3 insertions(+) diff --git a/components/user/userModel.js b/components/user/userModel.js index 1fb5820..494280b 100644 --- a/components/user/userModel.js +++ b/components/user/userModel.js @@ -34,6 +34,7 @@ const userSchema = new mongoose.Schema( role: { type: ObjectID, ref: 'Role', + required: true }, isAdmin: { type: Boolean, diff --git a/components/user/userRoutes.js b/components/user/userRoutes.js index e611078..a626bcb 100644 --- a/components/user/userRoutes.js +++ b/components/user/userRoutes.js @@ -6,7 +6,9 @@ const loginCheck = require('../../middlewares/loginCheck') const router = new express.Router() router.post('/signup-email', userController.signup) + router.post('/login-email', userController.loginUser) + router.post('/logout', userController.logoutUser) router.use(loginCheck.loginProtect) From 9faaeef9cea5c903208f07cea80ff0d4a991fd48 Mon Sep 17 00:00:00 2001 From: Kaveh Hajinajaf Date: Mon, 21 Apr 2025 23:52:32 +0330 Subject: [PATCH 02/15] FIX: Transaction Route names FIX: Bug where belonging transactions wouldnt select properly --- components/transaction/transactionRoutes.js | 4 ++-- components/transaction/transactionService.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/components/transaction/transactionRoutes.js b/components/transaction/transactionRoutes.js index 5281f83..dc8dd13 100644 --- a/components/transaction/transactionRoutes.js +++ b/components/transaction/transactionRoutes.js @@ -20,13 +20,13 @@ router.post( ) router.get( - '/admin/belonging/:belongingId/inventory', + '/admin/get-stock-by-belonging/:belongingId', // perm.requires('getallbelongings'), transactionController.getBelongingInventory ) router.get( - '/admin/belonging/:belongingId', + '/admin/get-all-by-belonging/:belongingId', // perm.requires('getallbelongings'), transactionController.getTransactionByBelongingId ) diff --git a/components/transaction/transactionService.js b/components/transaction/transactionService.js index 864ba5a..1c53927 100644 --- a/components/transaction/transactionService.js +++ b/components/transaction/transactionService.js @@ -69,7 +69,7 @@ const getBelongingInventory = async (belongingId) => { const result = await Transaction.aggregate([ { $match: { - belongingId: ObjectID, + belongingId: belonging._id, $or: [{ status: 'finished' }, { status: 'pending' }], }, }, From 2b84501e754e6a05209fe396b17a936be61016c1 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 12:55:09 +0330 Subject: [PATCH 03/15] UPDATE: multer and mongoose packages --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 75d0888..c3c48ff 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,9 @@ "express": "^4.21.2", "jsonwebtoken": "^9.0.2", "minio": "^8.0.3", - "mongoose": "^8.9.0", + "mongoose": "^8.9.0", "mongoose-sequence": "^6.0.1", - "multer": "^1.4.5-lts.1", + "multer": "^1.4.5-lts.1", "redis": "^4.7.0", "typesense": "^1.8.2", "validator": "^13.12.0" From bed56013c7b596908455d663ceaff79df19834f4 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 12:55:47 +0330 Subject: [PATCH 04/15] UPDATE: TYPE_API_KEY --- config/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/.env b/config/.env index ca229a9..7edbfce 100644 --- a/config/.env +++ b/config/.env @@ -6,7 +6,7 @@ REDIS_URL= redis://:GFis6zU4DXW9Q7NQ@services.irn12.chabokan.net:19794/0 LOGIN_ATTEMPT_LIMIT=900 SUPER_ADMIN_ROLE_ID=66a2da5b9b989a8909948848 TYPE_SENSE_URL=services.irn9.chabokan.net -TYPE_SENSE_API_KEY=zQzlsVKmSX5M0N09 +TYPE_SENSE_API_KEY=rHV4vZePdfBYFC9q AWS_ENDPOINT=bm-minio.chbk.app AWS_ACCESS_KEY=EFYrmV0EiH4qwGERtbhJygNglaCZYtti AWS_SECRET_KEY=ArkI5GDTId6GsQ0WvKFSp54lrx78gSW1 From 1ae70dda18c0b8546411025ed9ec8c2207b13cf1 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 12:56:15 +0330 Subject: [PATCH 05/15] UPDATE: protocole to HTTPS --- setup/typesense.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/typesense.js b/setup/typesense.js index ebe7e24..bc6ef8f 100644 --- a/setup/typesense.js +++ b/setup/typesense.js @@ -5,7 +5,7 @@ let client = new TypeSense.Client({ { host: process.env.TYPE_SENSE_URL, port: '443', - protocol: 'httpS', + protocol: 'https', }, ], apiKey: process.env.TYPE_SENSE_API_KEY, From 5ce201c4e4409e0282d61074ba15dae93f57a387 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 12:58:25 +0330 Subject: [PATCH 06/15] UPDATE: line in indexAllBelongings, collection name to belongings --- components/belonging/belongingService.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/components/belonging/belongingService.js b/components/belonging/belongingService.js index bf889dc..055f94b 100644 --- a/components/belonging/belongingService.js +++ b/components/belonging/belongingService.js @@ -1,7 +1,7 @@ const Belonging = require('./belongingModel') const AppError = require('../../globalErrorHandler/AppError') const { client } = require('../../setup/typesense') -const TsApiFeatures = require("../../utils/TsApiFeatures"); +const TsApiFeatures = require('../../utils/TsApiFeatures') const getAllBelongings = async () => { return Belonging.find({}, null, null).populate('category') @@ -29,7 +29,6 @@ const getBelonging = async (belongingId) => { belonging.signedImageUrls = imageUrls belonging.signedMainImageUrls = mainImageUrls - return belonging } @@ -70,9 +69,9 @@ const ensureCollectionExists = async () => { const searchBelongings = async (inputQuery) => { const tsFeatures = new TsApiFeatures(client, 'belongings', inputQuery).filter().facet().sort().limitFields().paginate().search() - let {facets, items, totalCount, pagesCount} = await tsFeatures.execute() + let { facets, items, totalCount, pagesCount } = await tsFeatures.execute() - return {facets, belongings: items, totalCount, pagesCount} + return { facets, belongings: items, totalCount, pagesCount } } const indexAllBelongings = async () => { const batchSize = 20 @@ -92,28 +91,26 @@ const indexAllBelongings = async () => { let documents = [] for (const belonging of belongings) { - const data = { _id: belonging._id, isActive: belonging.isActive, belongingCode: belonging.belongingCode, - category: belonging.category.map( x=> x.englishName), - nativeCategory: belonging.category.map( x=> x.nativeName), + category: belonging.category.map((x) => x.englishName), + nativeCategory: belonging.category.map((x) => x.nativeName), documents: belonging.documents, description: belonging.description, - coordinates: belonging.location.coordinates + coordinates: belonging.location.coordinates, } documents.push(data) } - await client.collections('belonging').documents().import(documents, {action: 'emplace'}) + await client.collections('belongings').documents().import(documents, { action: 'emplace' }) } - return {totalCount: count} + return { totalCount: count } } - const getTypeSenseSchema = () => { return { name: 'belongings', @@ -132,4 +129,4 @@ const getTypeSenseSchema = () => { } } -module.exports = {updateBelonging, indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging, searchBelongings} \ No newline at end of file +module.exports = { updateBelonging, indexAllBelongings, getBelonging, getAllBelongings, createBelonging, deleteBelonging, searchBelongings } From 46e3c4858d62d5015239112aa16e37dcb180a49e Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 12:59:02 +0330 Subject: [PATCH 07/15] BUG FIX: in searchBelongings req.query passed to the service --- components/belonging/belongingController.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/components/belonging/belongingController.js b/components/belonging/belongingController.js index 5c57c31..562e345 100644 --- a/components/belonging/belongingController.js +++ b/components/belonging/belongingController.js @@ -2,11 +2,14 @@ const catchAsync = require('../../globalErrorHandler/catchAsync') const belongingService = require('./belongingService') exports.searchBelongings = catchAsync(async (req, res, next) => { - const {facets, belongings, totalCount, pagesCount} = await belongingService.searchBelongings() + const { facets, belongings, totalCount, pagesCount } = await belongingService.searchBelongings(req.query) res.status(200).json({ status: 'success', - facets, belongings, totalCount, pagesCount + facets, + belongings, + totalCount, + pagesCount, }) }) @@ -15,11 +18,10 @@ exports.indexAllBelongings = catchAsync(async (req, res, next) => { res.status(200).json({ status: 'success', - indexedCount + indexedCount, }) }) - exports.getAllBelongings = catchAsync(async (req, res, next) => { const belongings = await belongingService.getAllBelongings() From 40bd335d30c1289073a3c309a110aca815597764 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 13:00:09 +0330 Subject: [PATCH 08/15] BUG FIX: in index-all route You put the parentheses wrong --- components/belonging/belongingRoutes.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/belonging/belongingRoutes.js b/components/belonging/belongingRoutes.js index 584f9c9..37b6346 100644 --- a/components/belonging/belongingRoutes.js +++ b/components/belonging/belongingRoutes.js @@ -11,7 +11,9 @@ router.get('/admin/get-all', perm.requires('getallbelongings'), belongingControl router.post('/admin/create', perm.requires('createabelonging'), belongingController.createBelonging) -router.post('/admin/index-all', perm.requires('indexbelongings', belongingController.indexAllBelongings)) +router.post('/admin/index-all', perm.requires('indexbelongings'), belongingController.indexAllBelongings) + +router.get('/admin/search', perm.requires('searchbelongings'), belongingController.searchBelongings) router.get('/admin/get-one/:belongingId', perm.requires('getabelonging'), belongingController.getBelonging) From 44d241813b74d38a6be3e2245cc17e0130e3b269 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 13:01:22 +0330 Subject: [PATCH 09/15] ADD: indexAllTransactions and searchTransactions services --- components/transaction/transactionService.js | 75 ++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/components/transaction/transactionService.js b/components/transaction/transactionService.js index 864ba5a..88cbc4b 100644 --- a/components/transaction/transactionService.js +++ b/components/transaction/transactionService.js @@ -151,6 +151,79 @@ const deleteTransaction = async (transactionId) => { return null } +const ensureCollectionExists = async () => { + const collections = await client.collections().retrieve() + const collectionExists = collections.some((collection) => collection.name === 'transactions') + + if (collectionExists) { + await client.collections('transactions').delete() + } + + const schema = getTypeSenseSchema() + + await client.collections().create(schema) + console.log('Collection "transactions" created successfully') +} + +const searchTransactions = async (inputQuery) => { + const tsFeatures = new TsApiFeatures(client, 'transactions', inputQuery).filter().facet().sort().limitFields().paginate().search() + + let { facets, items, totalCount, pagesCount } = await tsFeatures.execute() + + return { facets, transactions: items, totalCount, pagesCount } +} + +const indexAllTransactions = async () => { + const batchSize = 20 + const count = await Transaction.countDocuments({}, null) + const rounds = Math.ceil(count / batchSize) + + await ensureCollectionExists() + + for (let i = 1; i <= rounds; i++) { + console.log(`Indexing ${batchSize} transactions, Round ${i}`) + + const transactions = await Transaction.find({}, null, null) + .skip((i - 1) * batchSize) + .limit(batchSize) + + let documents = [] + + for (const transaction of transactions) { + const data = { + _id: transaction._id, + type: transaction.type, + status: transaction.status, + source: transaction.source, + value: transaction.value, + description: transaction.description, + } + + documents.push(data) + } + + await client.collections('transactions').documents().import(documents, { action: 'emplace' }) + } + + return { totalCount: count } +} + +const getTypeSenseSchema = () => { + return { + name: 'transactions', + enable_nested_fields: true, + fields: [ + { name: '_id', type: 'string', facet: false, infix: false }, + { name: 'type', type: 'string', facet: false }, + { name: 'status', type: 'string', facet: false }, + { name: 'source', type: 'string', facet: false }, + { name: 'value', type: 'int64', facet: false }, + { name: 'description', type: 'string', facet: false, infix: true }, + ], + default_sorting_field: 'createdAt', + } +} + module.exports = { createTransaction, getUserTransaction, @@ -158,4 +231,6 @@ module.exports = { updateTransaction, deleteTransaction, getTransactionByBelongingId, + indexAllTransactions, + searchTransactions, } From f7cfe625fda93a9174d07273fb925d17f9bf64c2 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 13:01:40 +0330 Subject: [PATCH 10/15] ADD: indexAllTransactions and searchTransactions handlers --- .../transaction/transactionController.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/components/transaction/transactionController.js b/components/transaction/transactionController.js index d5e6f49..a4199f6 100644 --- a/components/transaction/transactionController.js +++ b/components/transaction/transactionController.js @@ -1,6 +1,27 @@ const catchAsync = require('../../globalErrorHandler/catchAsync') const transactionService = require('./transactionService') +exports.searchTransactions = catchAsync(async (req, res, next) => { + const { facets, transactions, totalCount, pagesCount } = await transactionService.searchTransactions(req.query) + + res.status(200).json({ + status: 'success', + facets, + transactions, + totalCount, + pagesCount, + }) +}) + +exports.indexAllTransactions = catchAsync(async (req, res, next) => { + const { indexedCount } = await transactionService.indexAllTransactions() + + res.status(200).json({ + status: 'success', + indexedCount, + }) +}) + exports.createTransaction = catchAsync(async (req, res, next) => { const transactions = await transactionService.createTransaction(req.body) From 4e616ba466f75f46a9bfccc6af308f3eeb304aef Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 13:02:02 +0330 Subject: [PATCH 11/15] ADD: indexAllTransactions and searchTransactions POST and GET endpoints --- components/transaction/transactionRoutes.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/components/transaction/transactionRoutes.js b/components/transaction/transactionRoutes.js index 5281f83..3692420 100644 --- a/components/transaction/transactionRoutes.js +++ b/components/transaction/transactionRoutes.js @@ -19,6 +19,18 @@ router.post( transactionController.createTransaction ) +router.post( + '/admin/index-all', + // perm.requires('getallbelongings'), + transactionController.indexAllTransactions +) + +router.get( + '/admin/search', + // perm.requires('getallbelongings'), + transactionController.searchTransactions +) + router.get( '/admin/belonging/:belongingId/inventory', // perm.requires('getallbelongings'), From 718752589b8a422877f3d0abc78d7628aa1aa397 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 13:44:01 +0330 Subject: [PATCH 12/15] BUG FIX: wrong URL for typsense --- config/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/.env b/config/.env index 7edbfce..ef01c2e 100644 --- a/config/.env +++ b/config/.env @@ -5,7 +5,7 @@ JWT_SECRET=Y@RJ$WT!@K0NS@MPL! REDIS_URL= redis://:GFis6zU4DXW9Q7NQ@services.irn12.chabokan.net:19794/0 LOGIN_ATTEMPT_LIMIT=900 SUPER_ADMIN_ROLE_ID=66a2da5b9b989a8909948848 -TYPE_SENSE_URL=services.irn9.chabokan.net +TYPE_SENSE_URL=bm-typesense.chbk.app TYPE_SENSE_API_KEY=rHV4vZePdfBYFC9q AWS_ENDPOINT=bm-minio.chbk.app AWS_ACCESS_KEY=EFYrmV0EiH4qwGERtbhJygNglaCZYtti From 5e74bf174be8977173705ffcb9e0b1a86a32b4f1 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 13:48:14 +0330 Subject: [PATCH 13/15] BUG FIX: typesense schema and data --- components/belonging/belongingService.js | 25 +++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/components/belonging/belongingService.js b/components/belonging/belongingService.js index 055f94b..8bb3656 100644 --- a/components/belonging/belongingService.js +++ b/components/belonging/belongingService.js @@ -91,15 +91,20 @@ const indexAllBelongings = async () => { let documents = [] for (const belonging of belongings) { + const categoryArray = Array.isArray(belonging.category) ? belonging.category : belonging.category ? [belonging.category] : [] + const tags = belonging.tags.join(', ') + const data = { _id: belonging._id, - isActive: belonging.isActive, + name: belonging.name, belongingCode: belonging.belongingCode, - category: belonging.category.map((x) => x.englishName), - nativeCategory: belonging.category.map((x) => x.nativeName), - documents: belonging.documents, + category: categoryArray.map((x) => x.englishName), + nativeCategory: categoryArray.map((x) => x.nativeName), description: belonging.description, + unit: belonging.unit, + tags: tags, coordinates: belonging.location.coordinates, + createdAt: Math.floor(new Date(belonging.createdAt).getTime() / 1000), // Store as timestamp in seconds } documents.push(data) @@ -117,13 +122,15 @@ const getTypeSenseSchema = () => { enable_nested_fields: true, fields: [ { name: '_id', type: 'string', facet: false, infix: false }, - { name: 'isActive', type: 'bool', facet: false, infix: true }, - { name: 'belongingCode', type: 'string', facet: true }, - { name: 'category', type: 'string[]', facet: true }, - { name: 'nativeCategory', type: 'string[]', facet: false }, - { name: 'documents', type: 'string[]', facet: false }, + { name: 'name', type: 'string', facet: false, infix: true }, + { name: 'belongingCode', type: 'int64', facet: false }, + { name: 'category', type: 'string[]', facet: true, infix: true }, + { name: 'nativeCategory', type: 'string[]', facet: false, infix: true }, { name: 'description', type: 'string', facet: false }, + { name: 'unit', type: 'string', facet: false, infix: true }, + { name: 'tags', type: 'string', facet: false, infix: true }, { name: 'coordinates', type: 'geopoint' }, + { name: 'createdAt', type: 'int64', facet: false }, ], default_sorting_field: 'createdAt', } From 8c79d2cc76ff9eee633feb09afb08030bce05073 Mon Sep 17 00:00:00 2001 From: MohammadHMazarei Date: Tue, 22 Apr 2025 13:48:55 +0330 Subject: [PATCH 14/15] ADD: CreatedAt to the transactions typesense schema --- components/transaction/transactionService.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/transaction/transactionService.js b/components/transaction/transactionService.js index 88cbc4b..49d4f5f 100644 --- a/components/transaction/transactionService.js +++ b/components/transaction/transactionService.js @@ -197,6 +197,7 @@ const indexAllTransactions = async () => { source: transaction.source, value: transaction.value, description: transaction.description, + createdAt: Math.floor(new Date(transaction.createdAt).getTime() / 1000), // Store as timestamp in seconds } documents.push(data) @@ -219,6 +220,7 @@ const getTypeSenseSchema = () => { { name: 'source', type: 'string', facet: false }, { name: 'value', type: 'int64', facet: false }, { name: 'description', type: 'string', facet: false, infix: true }, + { name: 'createdAt', type: 'int64', facet: false }, ], default_sorting_field: 'createdAt', } From 4fc323cd160105f95375a85d4bb6e5aedafbe8b9 Mon Sep 17 00:00:00 2001 From: Kaveh Hajinajaf Date: Sun, 27 Apr 2025 16:17:17 +0330 Subject: [PATCH 15/15] ADD: API_URL to env ADD: DocumentUrls and mainImageUrls field in belonging model FIX: Indexing bugs for belonging ADD: getImage endpoint to get private images ADD: PrivateUrl field as virtual FIX: Minor bug in bucketDeleteFile service ADD: Word type to multer file checker --- components/belonging/belongingModel.js | 14 ++----- components/belonging/belongingService.js | 22 +++++----- components/file/fileController.js | 24 ++++++++++- components/file/fileModel.js | 4 ++ components/file/fileRoutes.js | 2 + components/file/fileService.js | 51 ++++++++++++++++++++++-- config/.env | 10 +++-- middlewares/multerMiddleware.js | 2 +- 8 files changed, 100 insertions(+), 29 deletions(-) diff --git a/components/belonging/belongingModel.js b/components/belonging/belongingModel.js index 4512ba7..36a11f1 100644 --- a/components/belonging/belongingModel.js +++ b/components/belonging/belongingModel.js @@ -35,10 +35,8 @@ const belongingSchema = new mongoose.Schema( required: false, }, }, - documentNames: [String], - imageNames: [String], - mainImageNames: [String], - mainImageUrl: String, + documentUrls: [String], + mainImageUrls: [String], tags: [String], isActive: { type: Boolean, @@ -66,15 +64,11 @@ const belongingSchema = new mongoose.Schema( belongingSchema.plugin(AutoIncrement, { inc_field: 'belongingCode' }) belongingSchema.methods.getDocumentsSignedUrls = function () { - return getMultipleSignedUrls(this.documentNames, 'belonging-documents') -} - -belongingSchema.methods.getImagesSignedUrls = function () { - return getMultipleSignedUrls(this.imageNames, 'belonging-images') + return getMultipleSignedUrls(this.documentUrls, 'belonging-documents') } belongingSchema.methods.getMainImagedSignedUrl = function () { - return getMultipleSignedUrls(this.mainImageNames, 'belonging-images') + return getMultipleSignedUrls(this.mainImageUrls, 'belonging-images') } belongingSchema.index({ location: '2dsphere' }) // Enable geospatial queries diff --git a/components/belonging/belongingService.js b/components/belonging/belongingService.js index 8bb3656..bad1181 100644 --- a/components/belonging/belongingService.js +++ b/components/belonging/belongingService.js @@ -20,13 +20,11 @@ const getBelonging = async (belongingId) => { } const documentUrls = await belonging.getDocumentsSignedUrls() - const imageUrls = await belonging.getImagesSignedUrls() const mainImageUrls = await belonging.getMainImagedSignedUrl() belonging = JSON.parse(JSON.stringify(belonging)) belonging.signedDocumentUrls = documentUrls - belonging.signedImageUrls = imageUrls belonging.signedMainImageUrls = mainImageUrls return belonging @@ -91,18 +89,21 @@ const indexAllBelongings = async () => { let documents = [] for (const belonging of belongings) { - const categoryArray = Array.isArray(belonging.category) ? belonging.category : belonging.category ? [belonging.category] : [] - const tags = belonging.tags.join(', ') + + const combinedTags = belonging.tags.join(' '); const data = { _id: belonging._id, - name: belonging.name, + isActive: belonging.isActive, belongingCode: belonging.belongingCode, - category: categoryArray.map((x) => x.englishName), - nativeCategory: categoryArray.map((x) => x.nativeName), + name: belonging.name, + category: belonging.category.englishName, + nativeCategory: belonging.category.nativeName, + imageUrl: belonging.mainImageUrls[0] || '', + documents: belonging.documentUrls, description: belonging.description, unit: belonging.unit, - tags: tags, + tags: combinedTags, coordinates: belonging.location.coordinates, createdAt: Math.floor(new Date(belonging.createdAt).getTime() / 1000), // Store as timestamp in seconds } @@ -124,13 +125,14 @@ const getTypeSenseSchema = () => { { name: '_id', type: 'string', facet: false, infix: false }, { name: 'name', type: 'string', facet: false, infix: true }, { name: 'belongingCode', type: 'int64', facet: false }, - { name: 'category', type: 'string[]', facet: true, infix: true }, - { name: 'nativeCategory', type: 'string[]', facet: false, infix: true }, + { name: 'category', type: 'string', facet: true, infix: true }, + { name: 'nativeCategory', type: 'string', facet: false, infix: true }, { name: 'description', type: 'string', facet: false }, { name: 'unit', type: 'string', facet: false, infix: true }, { name: 'tags', type: 'string', facet: false, infix: true }, { name: 'coordinates', type: 'geopoint' }, { name: 'createdAt', type: 'int64', facet: false }, + { name: 'imageUrl', type: 'string', facet: false }, ], default_sorting_field: 'createdAt', } diff --git a/components/file/fileController.js b/components/file/fileController.js index 44d1faa..76eb592 100644 --- a/components/file/fileController.js +++ b/components/file/fileController.js @@ -7,6 +7,7 @@ exports.bucketSingleUpload = catchAsync(async (req, res, next) => { res.status(200).json({ status: 'success', data: fileData, + bucketName: req.params.bucketName, }) }) @@ -27,6 +28,7 @@ exports.bucketMultiUpload = catchAsync(async (req, res, next) => { status: 'success', results: files.length, data: files, + bucketName: req.params.bucketName, }) }) @@ -49,11 +51,31 @@ exports.getSignedUrl = catchAsync(async (req, res, next) => { }) }) +exports.getImage = catchAsync(async (req, res, next) => { + const { bucketName, fileName } = req.params; + + const imageData = await fileService.getImage(bucketName, fileName); + + + if (!imageData) { + return res.status(404).json({ + status: 'fail', + message: 'Image not found' + }); + } + + // Set appropriate headers for the image response + res.set('Content-Type', imageData.contentType); + + res.send(imageData.buffer); +}); + exports.bucketDeleteFile = catchAsync(async (req, res, next) => { - await fileService.bucketDeleteFile(req.body, req.params.bucketName) + const { fileName } = await fileService.bucketDeleteFile(req.body, req.params.bucketName) res.status(200).json({ status: 'success', + data: fileName, }) }) diff --git a/components/file/fileModel.js b/components/file/fileModel.js index 562659a..122aa26 100644 --- a/components/file/fileModel.js +++ b/components/file/fileModel.js @@ -28,5 +28,9 @@ fileSchema.virtual('publicUrl').get(function () { return this.fileEndpoint + '/' + this.bucketName + '/' + this.fileName }) +fileSchema.virtual('privateUrl').get(function () { + return process.env.API_URL + '/api/files/get-image/' + this.bucketName + '/' + this.fileName +}) + const File = mongoose.model('file', fileSchema) module.exports = File diff --git a/components/file/fileRoutes.js b/components/file/fileRoutes.js index cc90e47..4a50d4f 100644 --- a/components/file/fileRoutes.js +++ b/components/file/fileRoutes.js @@ -35,6 +35,8 @@ router.get('/bucket/get-files', perm.requires('getbucketfiles'), fileController. router.get('/bucket/get-signed', perm.requires('getbucketfiles'), fileController.getSignedUrl) +router.get('/get-image/:bucketName/:fileName', perm.requires('getimages'), fileController.getImage); + router.delete('/bucket/delete-file/:bucketName', perm.requires('deletebucketfiles'), fileController.bucketDeleteFile) router.get('/admin/get-all', perm.requires('getallfiles'), fileController.getAllFiles) diff --git a/components/file/fileService.js b/components/file/fileService.js index 04079cd..be2b74c 100644 --- a/components/file/fileService.js +++ b/components/file/fileService.js @@ -52,10 +52,20 @@ const bucketMultiUpload = async (files, bucketName) => { } const bucketDeleteFile = async (fileName, bucketName) => { - await deleteObject(fileName, bucketName) - await deleteFile(fileName) + let exportedFileName = getLastUrlSegment(fileName) + await deleteObject(exportedFileName, bucketName) + await deleteFile(exportedFileName) - return null + return { fileName } +} + +const getLastUrlSegment = (url) => { + if (!url.includes('/')) { + return url + } + const cleanUrl = url.endsWith('/') ? url.slice(0, -1) : url + const segments = cleanUrl.split('/') + return segments[segments.length - 1] } const getAllFiles = async () => { @@ -100,6 +110,7 @@ const getSignedUrl = async (fileName, bucketName) => { if (!fileName) { return } + console.log(bucketName,fileName) return signedUrl(bucketName, fileName) } @@ -120,6 +131,39 @@ const moveFromTempToMain = async (files, targetBucket) => { await removeObject(tempBucket, file) } } + +const getImage = async (bucketName, fileName) => { + if (!fileName) { + throw new AppError('NoFileFound', 404, {}) + } + + + // Get signed URL from the file service + const signedUrl = await getSignedUrl(fileName, bucketName); + + if (!signedUrl) { + return null; + } + + // Fetch the image from the signed URL + const response = await fetch(signedUrl); + + if (!response.ok) { + throw new AppError('NoFileFound', 404, {}) + } + + // Get the content type from the response headers + const contentType = response.headers.get('content-type'); + + // Convert the response to a buffer + const buffer = await response.arrayBuffer(); + + // Return both the buffer and content type + return { + buffer: Buffer.from(buffer), + contentType + }; +}; module.exports = { moveFromTempToMain, getMultipleSignedUrls, @@ -133,4 +177,5 @@ module.exports = { bucketMultiUpload, bucketDeleteFile, bucketGetFiles, + getImage, } diff --git a/config/.env b/config/.env index ef01c2e..370c738 100644 --- a/config/.env +++ b/config/.env @@ -7,7 +7,9 @@ LOGIN_ATTEMPT_LIMIT=900 SUPER_ADMIN_ROLE_ID=66a2da5b9b989a8909948848 TYPE_SENSE_URL=bm-typesense.chbk.app TYPE_SENSE_API_KEY=rHV4vZePdfBYFC9q -AWS_ENDPOINT=bm-minio.chbk.app -AWS_ACCESS_KEY=EFYrmV0EiH4qwGERtbhJygNglaCZYtti -AWS_SECRET_KEY=ArkI5GDTId6GsQ0WvKFSp54lrx78gSW1 -AWS_REGION=default \ No newline at end of file +AWS_ENDPOINT=bm-minio-2.chbk.app +AWS_URL=https://bm-minio-2.chbk.app +AWS_ACCESS_KEY=lwcAHMbCea4jeKMtj6SOjMtmETY93rBb +AWS_SECRET_KEY=xXKrceoCV3Wx35d0JmULgNvHJ0UlTQnO +AWS_REGION=default +API_URL=http://localhost:3000 \ No newline at end of file diff --git a/middlewares/multerMiddleware.js b/middlewares/multerMiddleware.js index 78a5931..1967fae 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', 'pdf', 'doc', 'docx', 'xlsx', 'dwg'] +const types = ['png', 'jpg', 'jpeg', 'webp', 'pdf', 'doc', 'docx', 'xlsx', 'dwg', 'vnd.openxmlformats-officedocument.wordprocessingml.document'] const formatsText = types.join(' - ') const fileFilter = (req, file, cb) => {