UNPKG

n8n-nodes-instantly-dev

Version:

n8n community node for Instantly API v2 - DEV TESTING VERSION

199 lines 9.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UniboxOperations = void 0; const n8n_workflow_1 = require("n8n-workflow"); const generic_functions_1 = require("../../generic.functions"); /** * Unibox operations handler for Instantly API v2 * * These operations manage messages in the Instantly Unibox (inbox). * They are for viewing, replying to, and managing received/sent emails. * * NOTE: These are NOT for configuring SMTP email accounts - use Account operations for that. */ class UniboxOperations { /** * Get many Unibox messages with pagination and filtering support */ static async getMany(context, itemIndex) { const returnAll = context.getNodeParameter('returnAll', itemIndex, false); const limit = context.getNodeParameter('limit', itemIndex, 50); // Validate limit doesn't exceed 100 if (limit > 100) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Limit cannot exceed 100. Instantly API has a maximum limit of 100.', { itemIndex }); } // Build query parameters const queryParams = {}; // Optional filters const search = context.getNodeParameter('search', itemIndex, ''); const campaignId = context.getNodeParameter('campaignId', itemIndex, ''); const eaccount = context.getNodeParameter('eaccount', itemIndex, ''); const isUnread = context.getNodeParameter('isUnread', itemIndex, undefined); const hasReminder = context.getNodeParameter('hasReminder', itemIndex, undefined); const mode = context.getNodeParameter('mode', itemIndex, ''); const emailType = context.getNodeParameter('emailType', itemIndex, ''); const lead = context.getNodeParameter('lead', itemIndex, ''); const sortOrder = context.getNodeParameter('sortOrder', itemIndex, 'desc'); if (search) queryParams.search = search; if (campaignId) queryParams.campaign_id = campaignId; if (eaccount) queryParams.eaccount = eaccount; if (isUnread !== undefined) queryParams.is_unread = isUnread; if (hasReminder !== undefined) queryParams.has_reminder = hasReminder; if (mode) queryParams.mode = mode; if (emailType) queryParams.email_type = emailType; if (lead) queryParams.lead = lead; if (sortOrder) queryParams.sort_order = sortOrder; if (returnAll) { // Get all Unibox messages with pagination // Note: When filters are applied, we need to manually paginate let allMessages = []; let startingAfter; let hasMore = true; while (hasMore) { const paginationParams = { ...queryParams, limit: 100 }; if (startingAfter) { paginationParams.starting_after = startingAfter; } const response = await generic_functions_1.instantlyApiRequest.call(context, 'GET', '/api/v2/emails', {}, paginationParams); // Handle response structure let itemsData = []; if (response.items && Array.isArray(response.items)) { itemsData = response.items; } else if (response.data && Array.isArray(response.data)) { itemsData = response.data; } else if (Array.isArray(response)) { itemsData = response; } if (itemsData.length > 0) { allMessages = allMessages.concat(itemsData); // Get the last item's ID for pagination const lastItem = itemsData[itemsData.length - 1]; startingAfter = lastItem.id || lastItem._id; // Check if there are more items if (itemsData.length < 100) { hasMore = false; } } else { hasMore = false; } } return { items: allMessages }; } else { // Get single page with specified limit queryParams.limit = limit; return await generic_functions_1.instantlyApiRequest.call(context, 'GET', '/api/v2/emails', {}, queryParams); } } /** * Get single Unibox message by ID */ static async get(context, itemIndex) { const messageId = context.getNodeParameter('messageId', itemIndex); return await generic_functions_1.instantlyApiRequest.call(context, 'GET', `/api/v2/emails/${messageId}`); } /** * Reply to a Unibox message */ static async reply(context, itemIndex) { const replyToUuid = context.getNodeParameter('replyToUuid', itemIndex); const eaccount = context.getNodeParameter('eaccount', itemIndex); const subject = context.getNodeParameter('subject', itemIndex); // Body can be HTML or text const bodyHtml = context.getNodeParameter('bodyHtml', itemIndex, ''); const bodyText = context.getNodeParameter('bodyText', itemIndex, ''); // Optional fields const ccAddressEmailList = context.getNodeParameter('ccAddressEmailList', itemIndex, ''); const bccAddressEmailList = context.getNodeParameter('bccAddressEmailList', itemIndex, ''); const reminderTs = context.getNodeParameter('reminderTs', itemIndex, ''); const assignedTo = context.getNodeParameter('assignedTo', itemIndex, ''); // Build request body const body = { reply_to_uuid: replyToUuid, eaccount, subject, body: {} }; // Add body content (at least one is required) if (bodyHtml) body.body.html = bodyHtml; if (bodyText) body.body.text = bodyText; // Validate that at least one body type is provided if (!bodyHtml && !bodyText) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Either HTML body or text body must be provided', { itemIndex }); } // Add optional fields if (ccAddressEmailList) body.cc_address_email_list = ccAddressEmailList; if (bccAddressEmailList) body.bcc_address_email_list = bccAddressEmailList; if (reminderTs) body.reminder_ts = reminderTs; if (assignedTo) body.assigned_to = assignedTo; return await generic_functions_1.instantlyApiRequest.call(context, 'POST', '/api/v2/emails/reply', body); } /** * Update a Unibox message */ static async update(context, itemIndex) { const messageId = context.getNodeParameter('messageId', itemIndex); // Build update fields const updateFields = {}; const subject = context.getNodeParameter('subject', itemIndex, ''); const reminderTs = context.getNodeParameter('reminderTs', itemIndex, ''); const isUnread = context.getNodeParameter('isUnread', itemIndex, undefined); const isFocused = context.getNodeParameter('isFocused', itemIndex, undefined); const iStatus = context.getNodeParameter('iStatus', itemIndex, undefined); if (subject) updateFields.subject = subject; if (reminderTs) updateFields.reminder_ts = reminderTs; if (isUnread !== undefined) updateFields.is_unread = isUnread; if (isFocused !== undefined) updateFields.is_focused = isFocused; if (iStatus !== undefined) updateFields.i_status = iStatus; // Validate that at least one field is being updated if (Object.keys(updateFields).length === 0) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'At least one field must be provided for update', { itemIndex }); } return await generic_functions_1.instantlyApiRequest.call(context, 'PATCH', `/api/v2/emails/${messageId}`, updateFields); } /** * Delete a Unibox message */ static async delete(context, itemIndex) { const messageId = context.getNodeParameter('messageId', itemIndex); return await generic_functions_1.instantlyApiRequest.call(context, 'DELETE', `/api/v2/emails/${messageId}`); } /** * Get unread Unibox message count */ static async getUnreadCount(context, itemIndex) { return await generic_functions_1.instantlyApiRequest.call(context, 'GET', '/api/v2/emails/unread/count'); } /** * Mark Unibox thread as read */ static async markThreadAsRead(context, itemIndex) { const threadId = context.getNodeParameter('threadId', itemIndex); return await generic_functions_1.instantlyApiRequest.call(context, 'POST', `/api/v2/emails/threads/${threadId}/mark-as-read`); } } exports.UniboxOperations = UniboxOperations; //# sourceMappingURL=UniboxOperations.js.map