From 7a5701e7c819da48bd0c279aa5d7d0a547f26374 Mon Sep 17 00:00:00 2001 From: Sergio Gutierrez Villalba Date: Wed, 17 Jan 2024 17:39:59 +0100 Subject: [PATCH] refactor(folder): improve search speed to prevent duplicates when creating a folder --- src/app/services/folder.js | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/app/services/folder.js b/src/app/services/folder.js index cf80eda6..38e495e7 100644 --- a/src/app/services/folder.js +++ b/src/app/services/folder.js @@ -94,13 +94,10 @@ module.exports = (Model, App) => { throw Error('Invalid folder name'); } - // Encrypt folder name, TODO: use versioning for encryption - const cryptoFolderName = App.services.Crypt.encryptName(folderName, parentFolderId); - const exists = await Model.folder.findOne({ where: { parentId: { [Op.eq]: parentFolderId }, - name: { [Op.eq]: cryptoFolderName }, + plain_name: { [Op.eq]: folderName }, deleted: { [Op.eq]: false }, }, }); @@ -112,8 +109,7 @@ module.exports = (Model, App) => { throw new FolderAlreadyExistsError('Folder with the same name already exists'); } - // Since we upload everything in the same bucket, this line is no longer needed - // const bucket = await App.services.Inxt.CreateBucket(user.email, user.userId, user.mnemonic, cryptoFolderName) + const cryptoFolderName = App.services.Crypt.encryptName(folderName, parentFolderId); const folder = await user.createFolder({ name: cryptoFolderName, plain_name: folderName, @@ -142,8 +138,10 @@ module.exports = (Model, App) => { } const parentFolder = await Model.folder.findOne({ - id: { [Op.eq]: parentFolderId }, - user_id: { [Op.eq]: user.id }, + where: { + id: { [Op.eq]: parentFolderId }, + user_id: { [Op.eq]: user.id }, + } }); if (!parentFolder) { @@ -154,22 +152,16 @@ module.exports = (Model, App) => { throw Error('Invalid folder name'); } - // Encrypt folder name, TODO: use versioning for encryption - const cryptoFolderName = App.services.Crypt.encryptName(folderName, parentFolderId); - - const maybeExistentFolder = await Model.folder.findOne({ + const folder = await Model.folder.findOne({ where: { parentId: { [Op.eq]: parentFolderId }, - name: { [Op.eq]: cryptoFolderName }, + plain_name: { [Op.eq]: folderName }, deleted: { [Op.eq]: false }, }, }); + const folderExists = !!folder; - if (maybeExistentFolder) { - return { exists: true, folder: maybeExistentFolder }; - } - - return { exists: false, folder: null }; + return { exists: folderExists, folder }; }; // Requires stored procedure