Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Mocean node - N8N-3202 #3075

Merged
merged 9 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Mocean/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from 'n8n-core';

import {
IDataObject, NodeApiError, NodeOperationError,
IDataObject, JsonObject, NodeApiError, NodeOperationError,
} from 'n8n-workflow';

/**
Expand Down Expand Up @@ -47,6 +47,6 @@ export async function moceanApiRequest(this: IHookFunctions | IExecuteFunctions,
try {
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), (error as JsonObject));
}
}
102 changes: 88 additions & 14 deletions packages/nodes-base/nodes/Mocean/Mocean.node.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { IExecuteFunctions } from 'n8n-core';
import {
IExecuteFunctions,
} from 'n8n-core';

import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeCredentialTestResult,
INodeExecutionData,
INodeType,
INodeTypeDescription,
JsonObject,
NodeOperationError,
} from 'n8n-workflow';

import {moceanApiRequest} from './GenericFunctions';

import {
moceanApiRequest,
} from './GenericFunctions';

export class Mocean implements INodeType {
description: INodeTypeDescription = {
displayName: 'Mocean',
name: 'mocean',
icon: 'file:mocean.png',
subtitle: `={{$parameter["operation"] + ": " + $parameter["resource"]}}`,
icon: 'file:mocean.svg',
group: ['transform'],
version: 1,
description: 'Send SMS and voice messages via Mocean',
Expand All @@ -27,6 +36,7 @@ export class Mocean implements INodeType {
{
name: 'moceanApi',
required: true,
testedBy: 'moceanApiTest',
},
],
properties: [
Expand All @@ -36,7 +46,8 @@ export class Mocean implements INodeType {
displayName: 'Resource',
name: 'resource',
type: 'options',
options:[
noDataExpression: true,
options: [
{
name: 'SMS',
value: 'sms',
Expand All @@ -52,6 +63,7 @@ export class Mocean implements INodeType {
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
Expand All @@ -68,7 +80,7 @@ export class Mocean implements INodeType {
},
],
default: 'send',
description: 'The operation to perform.',
description: 'Operation to perform',
},
{
displayName: 'From',
Expand All @@ -88,7 +100,7 @@ export class Mocean implements INodeType {
],
},
},
description: 'The number to which to send the message',
description: 'Number to which to send the message',
},

{
Expand All @@ -109,14 +121,14 @@ export class Mocean implements INodeType {
],
},
},
description: 'The number from which to send the message',
description: 'Number from which to send the message',
},

{
displayName: 'Language',
name: 'language',
type: 'options',
options:[
options: [
{
name: 'Chinese Mandarin (China)',
value: 'cmn-CN',
Expand Down Expand Up @@ -169,10 +181,66 @@ export class Mocean implements INodeType {
],
},
},
description: 'The message to send',
description: 'Message to send',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
operation: [
'send',
],
resource: [
'sms',
],
},
},
default: {},
options: [
{
displayName: 'Delivery Report URL',
name: 'dlrUrl',
type: 'string',
default: '',
placeholder: '',
description: 'Delivery report URL',
},
],
},
],
};

methods = {
credentialTest: {
async moceanApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
const credentials = credential.data;
const query: IDataObject = {};
query['mocean-api-key'] = credentials!['mocean-api-key'];
query['mocean-api-secret'] = credentials!['mocean-api-secret'];

const options = {
method: 'GET',
qs: query,
uri: `https://rest.moceanapi.com/rest/2/account/balance`,
json: true,
};
try {
await this.helpers.request!(options);
} catch (error) {
return {
status: 'Error',
message: `Connection details not valid: ${(error as JsonObject).message}`,
};
}
return {
status: 'OK',
message: 'Authentication successful!',
};
},
},
};


Expand All @@ -185,6 +253,7 @@ export class Mocean implements INodeType {
let requesetMethod: string;
let resource: string;
let text: string;
let dlrUrl: string;
let dataKey: string;
// For Post
let body: IDataObject;
Expand All @@ -196,7 +265,7 @@ export class Mocean implements INodeType {
qs = {};
try {
resource = this.getNodeParameter('resource', itemIndex, '') as string;
operation = this.getNodeParameter('operation',itemIndex,'') as string;
operation = this.getNodeParameter('operation', itemIndex, '') as string;
text = this.getNodeParameter('message', itemIndex, '') as string;
requesetMethod = 'POST';
body['mocean-from'] = this.getNodeParameter('from', itemIndex, '') as string;
Expand All @@ -215,16 +284,21 @@ export class Mocean implements INodeType {
dataKey = 'voice';
body['mocean-command'] = JSON.stringify(command);
endpoint = '/rest/2/voice/dial';
} else if(resource === 'sms') {
} else if (resource === 'sms') {
dlrUrl = this.getNodeParameter('options.dlrUrl', itemIndex, '') as string;
dataKey = 'messages';
body['mocean-text'] = text;
if (dlrUrl !== '') {
body['mocean-dlr-url'] = dlrUrl;
body['mocean-dlr-mask'] = '1';
}
endpoint = '/rest/2/sms';
} else {
throw new NodeOperationError(this.getNode(), `Unknown resource ${resource}`);
}

if (operation === 'send') {
const responseData = await moceanApiRequest.call(this,requesetMethod,endpoint,body,qs);
const responseData = await moceanApiRequest.call(this, requesetMethod, endpoint, body, qs);

for (const item of responseData[dataKey] as IDataObject[]) {
item.type = resource;
Expand All @@ -236,7 +310,7 @@ export class Mocean implements INodeType {
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
returnData.push({ error: (error as JsonObject).message });
continue;
}
throw error;
Expand Down
Binary file removed packages/nodes-base/nodes/Mocean/mocean.png
Binary file not shown.
24 changes: 24 additions & 0 deletions packages/nodes-base/nodes/Mocean/mocean.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.