mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
feat(Microsoft Outlook Node): New operation sendAndWait (#12795)
This commit is contained in:
@@ -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' } }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,6 +8,7 @@ import type {
|
|||||||
import { description } from './actions/node.description';
|
import { description } from './actions/node.description';
|
||||||
import { router } from './actions/router';
|
import { router } from './actions/router';
|
||||||
import { loadOptions, listSearch } from './methods';
|
import { loadOptions, listSearch } from './methods';
|
||||||
|
import { sendAndWaitWebhook } from '../../../../utils/sendAndWait/utils';
|
||||||
|
|
||||||
export class MicrosoftOutlookV2 implements INodeType {
|
export class MicrosoftOutlookV2 implements INodeType {
|
||||||
description: INodeTypeDescription;
|
description: INodeTypeDescription;
|
||||||
@@ -21,6 +22,8 @@ export class MicrosoftOutlookV2 implements INodeType {
|
|||||||
|
|
||||||
methods = { loadOptions, listSearch };
|
methods = { loadOptions, listSearch };
|
||||||
|
|
||||||
|
webhook = sendAndWaitWebhook;
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions) {
|
async execute(this: IExecuteFunctions) {
|
||||||
return await router.call(this);
|
return await router.call(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 del from './delete.operation';
|
||||||
import * as get from './get.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 move from './move.operation';
|
||||||
import * as reply from './reply.operation';
|
import * as reply from './reply.operation';
|
||||||
import * as send from './send.operation';
|
import * as send from './send.operation';
|
||||||
|
import * as sendAndWait from './sendAndWait.operation';
|
||||||
import * as update from './update.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[] = [
|
export const description: INodeProperties[] = [
|
||||||
{
|
{
|
||||||
@@ -58,6 +59,12 @@ export const description: INodeProperties[] = [
|
|||||||
description: 'Send a message',
|
description: 'Send a message',
|
||||||
action: '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',
|
name: 'Update',
|
||||||
value: 'update',
|
value: 'update',
|
||||||
@@ -74,5 +81,6 @@ export const description: INodeProperties[] = [
|
|||||||
...move.description,
|
...move.description,
|
||||||
...reply.description,
|
...reply.description,
|
||||||
...send.description,
|
...send.description,
|
||||||
|
...sendAndWait.description,
|
||||||
...update.description,
|
...update.description,
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -30,6 +30,26 @@ export const description: INodeTypeDescription = {
|
|||||||
required: true,
|
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: [
|
properties: [
|
||||||
{
|
{
|
||||||
displayName: 'Resource',
|
displayName: 'Resource',
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ type NodeMap = {
|
|||||||
event: 'create' | 'delete' | 'get' | 'getAll' | 'update';
|
event: 'create' | 'delete' | 'get' | 'getAll' | 'update';
|
||||||
folder: 'create' | 'delete' | 'get' | 'getAll' | 'update';
|
folder: 'create' | 'delete' | 'get' | 'getAll' | 'update';
|
||||||
folderMessage: 'getAll';
|
folderMessage: 'getAll';
|
||||||
message: 'delete' | 'get' | 'getAll' | 'move' | 'update' | 'send' | 'reply';
|
message: 'delete' | 'get' | 'getAll' | 'move' | 'update' | 'send' | 'reply' | 'sendAndWait';
|
||||||
messageAttachment: 'add' | 'download' | 'getAll' | 'get';
|
messageAttachment: 'add' | 'download' | 'getAll' | 'get';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
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 calendar from './calendar';
|
||||||
import * as contact from './contact';
|
import * as contact from './contact';
|
||||||
@@ -25,6 +30,16 @@ export async function router(this: IExecuteFunctions) {
|
|||||||
operation,
|
operation,
|
||||||
} as MicrosoftOutlook;
|
} 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++) {
|
for (let i = 0; i < items.length; i++) {
|
||||||
try {
|
try {
|
||||||
switch (microsoftOutlook.resource) {
|
switch (microsoftOutlook.resource) {
|
||||||
|
|||||||
@@ -554,7 +554,7 @@ export function getSendAndWaitConfig(context: IExecuteFunctions): SendAndWaitCon
|
|||||||
return config;
|
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;
|
let buttonStyle = BUTTON_STYLE_PRIMARY;
|
||||||
if (style === 'secondary') {
|
if (style === 'secondary') {
|
||||||
buttonStyle = BUTTON_STYLE_SECONDARY;
|
buttonStyle = BUTTON_STYLE_SECONDARY;
|
||||||
|
|||||||
Reference in New Issue
Block a user