/** * @node imap-modify * @name IMAP Modify Email * @category email * @version 1.0.1 * @description Modify emails on an IMAP server - mark as read/unread, flag, delete, or move. * @icon edit */ const configSchema = { type: 'object', properties: { credentialId: { type: 'string', title: 'IMAP Credential', description: 'Select the IMAP credential to use for authentication', dynamicOptions: { source: 'credentials', filter: { type: 'imap' } } }, mailbox: { type: 'string', title: 'Mailbox', description: 'The mailbox containing the email', default: 'INBOX' }, uid: { type: 'string', title: 'Email UID', description: 'The unique identifier of the email to modify (can be provided from input)' }, action: { type: 'string', title: 'Action', description: 'The action to perform on the email', enum: ['markRead', 'markUnread', 'flag', 'unflag', 'delete', 'move'], default: 'markRead' }, targetMailbox: { type: 'string', title: 'Target Mailbox', description: 'The target mailbox for move action (e.g., Archive, Trash)' } }, required: ['credentialId', 'action'] }; const inputSchema = { type: 'object', properties: { uid: { type: 'string', description: 'Email UID to modify' }, uids: { type: 'array', items: { type: 'string' }, description: 'Array of email UIDs to modify (batch operation)' }, mailbox: { type: 'string', description: 'Override mailbox from input' }, action: { type: 'string', description: 'Override action from input' } } }; const outputSchema = { type: 'object', properties: { success: { type: 'boolean' }, action: { type: 'string' }, processed: { type: 'integer' }, results: { type: 'array' } } }; const outputs = [ { name: 'main', displayName: 'Result', type: 'object', color: '#f59e0b' } ]; module.exports = { configSchema, inputSchema, outputSchema, outputs, async execute(config, input, context) { const credentialId = config.credentialId; const mailbox = input.mailbox || config.mailbox || 'INBOX'; const action = input.action || config.action; const targetMailbox = config.targetMailbox; if (!credentialId) { throw new Error('IMAP credential is required'); } if (!action) { throw new Error('Action is required'); } // Support both single UID and array of UIDs let uids = []; if (input.uids && Array.isArray(input.uids)) { uids = input.uids; } else if (input.uid) { uids = [input.uid]; } else if (config.uid) { uids = [config.uid]; } if (uids.length === 0) { throw new Error('At least one email UID is required'); } // Validate move action has target if (action === 'move' && !targetMailbox) { throw new Error('Target mailbox is required for move action'); } const results = []; let successCount = 0; // Process each UID for (const uid of uids) { const modifyOptions = { credentialId, mailbox, uid, action }; if (action === 'move') { modifyOptions.targetMailbox = targetMailbox; } const modifyResult = smartbotic.imap.modify(modifyOptions); results.push({ uid, success: modifyResult.success, error: modifyResult.error }); if (modifyResult.success) { successCount++; } } return { success: successCount === uids.length, action, mailbox, processed: uids.length, succeeded: successCount, failed: uids.length - successCount, results }; } };