const SyncbackTask = require('./syncback-task') const TaskHelpers = require('./task-helpers') class MoveToFolderIMAP extends SyncbackTask { description() { return `MoveToFolder`; } run(db, imap) { const threadId = this.syncbackRequestObject().props.threadId const toFolderId = this.syncbackRequestObject().props.folderId const eachMsg = ({message}) => { return db.Category.findById(toFolderId).then((category) => { return imap.move(message.CategoryId, category.name) }) } return TaskHelpers.forEachMessageInThread({threadId, db, imap, callback: eachMsg}) // for (const {message, category} of msgGenerator) { // imap.moveAsync(messageId) // } // const {Category, Thread} = db; // const threadId = this.syncbackRequestObject().props.threadId // const toFolderId = this.syncbackRequestObject().props.folderId // // const thread = Thread.findById(threadId); // const toFolder = Category.findById(toFolderId); // // const msgsInCategories = {}; // // thread.getMessages((messages) => { // for (const message of messages) { // if (!msgsInCategories[message.CategoryId]) { // msgsInCategories[message.CategoryId] = [message.messageId]; // } else { // msgsInCategories.push(message.messageId); // } // } // for (const categoryId of Object.keys(msgsInCategories)) { // Category.findById(categoryId).then((category) => { // imap.openBox(category, false); // for (const messageId of msgsInCategories[categoryId]) { // imap.moveAsync(messageId, toCategoryName); // } // imap.closeBox(); // }) // } // }) } } module.exports = MoveToFolderIMAP