feat(Microsoft Outlook Node): New operation sendAndWait (#12795)
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions

This commit is contained in:
Michael Kret 2025-01-23 18:38:24 +02:00 committed by GitHub
parent 3e9f24ddf4
commit f4bf55f0d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 179 additions and 5 deletions

View file

@ -0,0 +1,73 @@
import type { MockProxy } from 'jest-mock-extended';
import { mock } from 'jest-mock-extended';
import { SEND_AND_WAIT_OPERATION, type IExecuteFunctions } from 'n8n-workflow';
import { description } from '../../../../v2/actions/node.description';
import { MicrosoftOutlookV2 } from '../../../../v2/MicrosoftOutlookV2.node';
import * as transport from '../../../../v2/transport';
jest.mock('../../../../v2/transport', () => {
const originalModule = jest.requireActual('../../../../v2/transport');
return {
...originalModule,
microsoftApiRequest: jest.fn(async function (method: string) {
if (method === 'POST') {
return {};
}
}),
};
});
describe('Test MicrosoftOutlookV2, message => sendAndWait', () => {
let microsoftOutlook: MicrosoftOutlookV2;
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
beforeEach(() => {
microsoftOutlook = new MicrosoftOutlookV2(description);
mockExecuteFunctions = mock<IExecuteFunctions>();
});
afterEach(() => {
jest.clearAllMocks();
});
it('should send message and put execution to wait', async () => {
const items = [{ json: { data: 'test' } }];
//router
mockExecuteFunctions.getInputData.mockReturnValue(items);
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('message');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(SEND_AND_WAIT_OPERATION);
mockExecuteFunctions.putExecutionToWait.mockImplementation();
//operation
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('my@outlook.com');
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
//getSendAndWaitConfig
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('my message');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('my subject');
mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('http://localhost/waiting-webhook');
mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('nodeID');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({});
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('approval');
const result = await microsoftOutlook.execute.call(mockExecuteFunctions);
expect(result).toEqual([items]);
expect(transport.microsoftApiRequest).toHaveBeenCalledTimes(1);
expect(mockExecuteFunctions.putExecutionToWait).toHaveBeenCalledTimes(1);
expect(transport.microsoftApiRequest).toHaveBeenCalledWith('POST', '/sendMail', {
message: {
body: {
content: expect.stringContaining(
'href="http://localhost/waiting-webhook/nodeID?approved=true"',
),
contentType: 'html',
},
subject: 'my subject',
toRecipients: [{ emailAddress: { address: 'my@outlook.com' } }],
},
});
});
});

View file

@ -8,6 +8,7 @@ import type {
import { description } from './actions/node.description';
import { router } from './actions/router';
import { loadOptions, listSearch } from './methods';
import { sendAndWaitWebhook } from '../../../../utils/sendAndWait/utils';
export class MicrosoftOutlookV2 implements INodeType {
description: INodeTypeDescription;
@ -21,6 +22,8 @@ export class MicrosoftOutlookV2 implements INodeType {
methods = { loadOptions, listSearch };
webhook = sendAndWaitWebhook;
async execute(this: IExecuteFunctions) {
return await router.call(this);
}

View file

@ -1,4 +1,4 @@
import type { INodeProperties } from 'n8n-workflow';
import { SEND_AND_WAIT_OPERATION, type INodeProperties } from 'n8n-workflow';
import * as del from './delete.operation';
import * as get from './get.operation';
@ -6,9 +6,10 @@ import * as getAll from './getAll.operation';
import * as move from './move.operation';
import * as reply from './reply.operation';
import * as send from './send.operation';
import * as sendAndWait from './sendAndWait.operation';
import * as update from './update.operation';
export { del as delete, get, getAll, move, reply, send, update };
export { del as delete, get, getAll, move, reply, send, sendAndWait, update };
export const description: INodeProperties[] = [
{
@ -58,6 +59,12 @@ export const description: INodeProperties[] = [
description: 'Send a message',
action: 'Send a message',
},
{
name: 'Send and Wait for Response',
value: SEND_AND_WAIT_OPERATION,
description: 'Send a message and wait for response',
action: 'Send message and wait for response',
},
{
name: 'Update',
value: 'update',
@ -74,5 +81,6 @@ export const description: INodeProperties[] = [
...move.description,
...reply.description,
...send.description,
...sendAndWait.description,
...update.description,
];

View file

@ -0,0 +1,55 @@
import type {
IDataObject,
IExecuteFunctions,
INodeExecutionData,
INodeProperties,
} from 'n8n-workflow';
import { createEmailBody } from '../../../../../../utils/sendAndWait/email-templates';
import {
getSendAndWaitConfig,
getSendAndWaitProperties,
createButton,
} from '../../../../../../utils/sendAndWait/utils';
import { createMessage } from '../../helpers/utils';
import { microsoftApiRequest } from '../../transport';
export const description: INodeProperties[] = getSendAndWaitProperties([
{
displayName: 'To',
name: 'toRecipients',
description: 'Comma-separated list of email addresses of recipients',
type: 'string',
required: true,
default: '',
},
]);
export async function execute(this: IExecuteFunctions, index: number, items: INodeExecutionData[]) {
const toRecipients = this.getNodeParameter('toRecipients', index) as string;
const config = getSendAndWaitConfig(this);
const buttons: string[] = [];
for (const option of config.options) {
buttons.push(createButton(config.url, option.label, option.value, option.style));
}
const instanceId = this.getInstanceId();
const bodyContent = createEmailBody(config.message, buttons.join('\n'), instanceId);
const fields: IDataObject = {
subject: config.title,
bodyContent,
toRecipients,
bodyContentType: 'html',
};
const message: IDataObject = createMessage(fields);
const body: IDataObject = { message };
await microsoftApiRequest.call(this, 'POST', '/sendMail', body);
return items;
}

View file

@ -30,6 +30,26 @@ export const description: INodeTypeDescription = {
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'GET',
responseMode: 'onReceived',
responseData: '',
path: '={{ $nodeId }}',
restartWebhook: true,
isFullPath: true,
},
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
responseData: '',
path: '={{ $nodeId }}',
restartWebhook: true,
isFullPath: true,
},
],
properties: [
{
displayName: 'Resource',

View file

@ -7,7 +7,7 @@ type NodeMap = {
event: 'create' | 'delete' | 'get' | 'getAll' | 'update';
folder: 'create' | 'delete' | 'get' | 'getAll' | 'update';
folderMessage: 'getAll';
message: 'delete' | 'get' | 'getAll' | 'move' | 'update' | 'send' | 'reply';
message: 'delete' | 'get' | 'getAll' | 'move' | 'update' | 'send' | 'reply' | 'sendAndWait';
messageAttachment: 'add' | 'download' | 'getAll' | 'get';
};

View file

@ -1,5 +1,10 @@
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
import {
NodeApiError,
NodeOperationError,
SEND_AND_WAIT_OPERATION,
WAIT_INDEFINITELY,
} from 'n8n-workflow';
import * as calendar from './calendar';
import * as contact from './contact';
@ -25,6 +30,16 @@ export async function router(this: IExecuteFunctions) {
operation,
} as MicrosoftOutlook;
if (
microsoftOutlook.resource === 'message' &&
microsoftOutlook.operation === SEND_AND_WAIT_OPERATION
) {
await message[microsoftOutlook.operation].execute.call(this, 0, items);
await this.putExecutionToWait(WAIT_INDEFINITELY);
return [items];
}
for (let i = 0; i < items.length; i++) {
try {
switch (microsoftOutlook.resource) {

View file

@ -554,7 +554,7 @@ export function getSendAndWaitConfig(context: IExecuteFunctions): SendAndWaitCon
return config;
}
function createButton(url: string, label: string, approved: string, style: string) {
export function createButton(url: string, label: string, approved: string, style: string) {
let buttonStyle = BUTTON_STYLE_PRIMARY;
if (style === 'secondary') {
buttonStyle = BUTTON_STYLE_SECONDARY;