imap-modify.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @node imap-modify
  3. * @name IMAP Modify Email
  4. * @category email
  5. * @version 1.0.1
  6. * @description Modify emails on an IMAP server - mark as read/unread, flag, delete, or move.
  7. * @icon edit
  8. */
  9. const configSchema = {
  10. type: 'object',
  11. properties: {
  12. credentialId: {
  13. type: 'string',
  14. title: 'IMAP Credential',
  15. description: 'Select the IMAP credential to use for authentication',
  16. dynamicOptions: {
  17. source: 'credentials',
  18. filter: { type: 'imap' }
  19. }
  20. },
  21. mailbox: {
  22. type: 'string',
  23. title: 'Mailbox',
  24. description: 'The mailbox containing the email',
  25. default: 'INBOX'
  26. },
  27. uid: {
  28. type: 'string',
  29. title: 'Email UID',
  30. description: 'The unique identifier of the email to modify (can be provided from input)'
  31. },
  32. action: {
  33. type: 'string',
  34. title: 'Action',
  35. description: 'The action to perform on the email',
  36. enum: ['markRead', 'markUnread', 'flag', 'unflag', 'delete', 'move'],
  37. default: 'markRead'
  38. },
  39. targetMailbox: {
  40. type: 'string',
  41. title: 'Target Mailbox',
  42. description: 'The target mailbox for move action (e.g., Archive, Trash)'
  43. }
  44. },
  45. required: ['credentialId', 'action']
  46. };
  47. const inputSchema = {
  48. type: 'object',
  49. properties: {
  50. uid: {
  51. type: 'string',
  52. description: 'Email UID to modify'
  53. },
  54. uids: {
  55. type: 'array',
  56. items: { type: 'string' },
  57. description: 'Array of email UIDs to modify (batch operation)'
  58. },
  59. mailbox: {
  60. type: 'string',
  61. description: 'Override mailbox from input'
  62. },
  63. action: {
  64. type: 'string',
  65. description: 'Override action from input'
  66. }
  67. }
  68. };
  69. const outputSchema = {
  70. type: 'object',
  71. properties: {
  72. success: { type: 'boolean' },
  73. action: { type: 'string' },
  74. processed: { type: 'integer' },
  75. results: { type: 'array' }
  76. }
  77. };
  78. const outputs = [
  79. { name: 'main', displayName: 'Result', type: 'object', color: '#f59e0b' }
  80. ];
  81. module.exports = {
  82. configSchema,
  83. inputSchema,
  84. outputSchema,
  85. outputs,
  86. async execute(config, input, context) {
  87. const credentialId = config.credentialId;
  88. const mailbox = input.mailbox || config.mailbox || 'INBOX';
  89. const action = input.action || config.action;
  90. const targetMailbox = config.targetMailbox;
  91. if (!credentialId) {
  92. throw new Error('IMAP credential is required');
  93. }
  94. if (!action) {
  95. throw new Error('Action is required');
  96. }
  97. // Support both single UID and array of UIDs
  98. let uids = [];
  99. if (input.uids && Array.isArray(input.uids)) {
  100. uids = input.uids;
  101. } else if (input.uid) {
  102. uids = [input.uid];
  103. } else if (config.uid) {
  104. uids = [config.uid];
  105. }
  106. if (uids.length === 0) {
  107. throw new Error('At least one email UID is required');
  108. }
  109. // Validate move action has target
  110. if (action === 'move' && !targetMailbox) {
  111. throw new Error('Target mailbox is required for move action');
  112. }
  113. const results = [];
  114. let successCount = 0;
  115. // Process each UID
  116. for (const uid of uids) {
  117. const modifyOptions = {
  118. credentialId,
  119. mailbox,
  120. uid,
  121. action
  122. };
  123. if (action === 'move') {
  124. modifyOptions.targetMailbox = targetMailbox;
  125. }
  126. const modifyResult = smartbotic.imap.modify(modifyOptions);
  127. results.push({
  128. uid,
  129. success: modifyResult.success,
  130. error: modifyResult.error
  131. });
  132. if (modifyResult.success) {
  133. successCount++;
  134. }
  135. }
  136. return {
  137. success: successCount === uids.length,
  138. action,
  139. mailbox,
  140. processed: uids.length,
  141. succeeded: successCount,
  142. failed: uids.length - successCount,
  143. results
  144. };
  145. }
  146. };