Skip to content

Commit

Permalink
feat(Microsoft Outlook Node): New operation sendAndWait (#12795)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored Jan 23, 2025
1 parent 3e9f24d commit f4bf55f
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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' } }],
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +22,8 @@ export class MicrosoftOutlookV2 implements INodeType {

methods = { loadOptions, listSearch };

webhook = sendAndWaitWebhook;

async execute(this: IExecuteFunctions) {
return await router.call(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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';
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[] = [
{
Expand Down Expand Up @@ -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',
Expand All @@ -74,5 +81,6 @@ export const description: INodeProperties[] = [
...move.description,
...reply.description,
...send.description,
...sendAndWait.description,
...update.description,
];
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
};

Expand Down
17 changes: 16 additions & 1 deletion packages/nodes-base/nodes/Microsoft/Outlook/v2/actions/router.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/utils/sendAndWait/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f4bf55f

Please sign in to comment.