diff --git a/credentials/PlutioApi.credentials.ts b/credentials/PlutioApi.credentials.ts index c87656d..e26e800 100644 --- a/credentials/PlutioApi.credentials.ts +++ b/credentials/PlutioApi.credentials.ts @@ -1,12 +1,43 @@ import { ICredentialType, INodeProperties, + ICredentialDataDecryptedObject, + IAuthenticateGeneric, + IExecuteFunctions, + ICredentialTestRequest, + NodeApiError, + IDataObject, } from 'n8n-workflow'; export class PlutioApi implements ICredentialType { name = 'plutioApi'; displayName = 'Plutio API'; + extends = ['oAuth2Api']; properties: INodeProperties[] = [ + // { + // displayName: 'Grant Type', + // name: 'grantType', + // type: 'hidden', + // default: 'client_credentials', + // }, + // { + // displayName: 'Scope', + // name: 'scope', + // type: 'hidden', + // default: '*', + // }, + // { + // displayName: 'Authentication', + // name: 'authentication', + // type: 'hidden', + // default: 'header', + // }, + // { + // displayName: 'Access Token URL', + // name: 'accessTokenUrl', + // type: 'hidden', + // default: 'https://api.plutio.com/v1.10/oauth/token', + // }, { displayName: 'Client ID', name: 'clientId', @@ -29,4 +60,57 @@ export class PlutioApi implements ICredentialType { default: '', }, ]; + + // async preAuthentication(this: IExecuteFunctions, credentials: ICredentialDataDecryptedObject) { + // const endpoint = 'api.plutio.com/v1.10'; + // const returnData: IDataObject[] = []; + // let access_token; + // try { + // access_token = await this.helpers.httpRequest({ + // method: 'POST', + // url: `https://${endpoint}/oauth/token`, + // headers: { + // 'Content-Type': 'application/x-www-form-urlencoded', + // 'business': `${credentials.business}`, + // }, + // body: { + // 'client_id': `${credentials.clientId}`, + // 'client_secret': `${credentials.clientSecret}`, + // 'grant_type': 'client_credentials', + // }, + // json: true, + // }) as {access_token: IDataObject}; + // if (Array.isArray(access_token)) { + // returnData.push.apply(returnData, access_token as IDataObject[]); + // } else { + // if (access_token === undefined) { + // access_token = { + // success: true, + // }; + // } + // returnData.push(access_token as IDataObject); + // } + // if (returnData[0].accessToken) { + // return {accessToken: returnData[0].accessToken}; + // } + // } catch (error) { + // throw new NodeApiError(this.getNode(), error); + // } + // } + // authenticate: IAuthenticateGeneric = { + // type: 'generic', + // properties: { + // headers: { + // 'Content-Type': 'application/json', + // 'Business': '={{$credentials.business}}', + // 'Authorization': '=Bearer {{$credentials.accessToken}}', + // }, + // }, + // }; + // test: ICredentialTestRequest = { + // request: { + // baseURL: 'https://api.plutio.com/v1.10', + // url: '/templates', + // }, + // }; } diff --git a/nodes/Plutio/GenericFunctions.ts b/nodes/Plutio/GenericFunctions.ts index 1f18a1e..df1a2b8 100644 --- a/nodes/Plutio/GenericFunctions.ts +++ b/nodes/Plutio/GenericFunctions.ts @@ -17,8 +17,11 @@ async function plutioApiRequestToken(this: IExecuteFunctions | ILoadOptionsFunct const clientId = `${credentials.clientId}`; const clientSecret = `${credentials.clientSecret}`; const business = `${credentials.business}`; - +<<<<<<< HEAD const endpoint = 'api.plutio.com/v1.10'; +======= + const endpoint = 'api.plutio.com/v1.9'; +>>>>>>> master const returnData: IDataObject[] = []; let responseData; @@ -59,7 +62,11 @@ async function plutioApiRequestToken(this: IExecuteFunctions | ILoadOptionsFunct // Rest API function for plutio node. export async function plutioApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any +<<<<<<< HEAD const endpoint = 'api.plutio.com/v1.10'; +======= + const endpoint = 'api.plutio.com/v1.9'; +>>>>>>> master const credentials = await this.getCredentials('plutioApi'); const plutioApiToken = await plutioApiRequestToken.call(this); const business = `${credentials.business}`; diff --git a/nodes/Plutio/Plutio.node.ts b/nodes/Plutio/Plutio.node.ts index 4d82ad6..b3753a1 100644 --- a/nodes/Plutio/Plutio.node.ts +++ b/nodes/Plutio/Plutio.node.ts @@ -826,6 +826,26 @@ export class Plutio implements INodeType { } } if ('project' === resource) { + if ('getCklph' === operation) { + const contributor = this.getNodeParameter('contributor', i) as IDataObject; + + if (contributor) { + let contributorId; + const users = await plutioApiRequest.call(this, 'GET', '/people'); + for (const user of users) { + if (user.contactEmails[0].address === contributor || contributor === user._id) { + contributorId = user._id; + } + } + qs.contributors = contributorId as string; + } + const project = await plutioApiRequest.call(this, 'GET', '/projects', {}, qs); + if (project.length === 1) { + responseData = project; + } else { + responseData = {'name': 'default'}; + } + } if ('get' === operation) { const options = this.getNodeParameter('options', i) as IDataObject; @@ -977,7 +997,7 @@ export class Plutio implements INodeType { body.contributors = contributor as [string]; } - responseData = await plutioApiRequest.call(this, 'PUT', '/projects', body); + responseData = await plutioApiRequest.call(this, 'PUT', '/projects', body);; } if ('move' === operation) { const _id = this.getNodeParameter('_id', 0) as string; diff --git a/nodes/Plutio/descriptions/ProjectDescription.ts b/nodes/Plutio/descriptions/ProjectDescription.ts index ef3c511..01464d8 100644 --- a/nodes/Plutio/descriptions/ProjectDescription.ts +++ b/nodes/Plutio/descriptions/ProjectDescription.ts @@ -41,6 +41,12 @@ export const projectOperations: INodeProperties[] = [ description: 'Get projects', action: 'Get a project', }, + // { + // name: 'Chykalophia', + // value: 'getCklph', + // description: 'Get projects by contributor\'s email for CKLPH', + // action: 'Get a project', + // }, { name: 'Move', value: 'move', @@ -54,7 +60,7 @@ export const projectOperations: INodeProperties[] = [ action: 'Update a project', }, ], - default: 'get', + default: 'getCklph', }, ]; @@ -108,7 +114,7 @@ export const projectDescription: INodeProperties[] = [ displayName: 'Template Name or ID', name: 'templateId', type: 'options', - description: 'Choose from the list, or specify an ID using an expression', + description: 'Choose from the list, or specify an ID using an expression', default: '', typeOptions: { loadOptionsMethod: 'getProjectTemplateId', @@ -146,7 +152,7 @@ export const projectDescription: INodeProperties[] = [ displayName: 'Custom Field Name or ID', name: '_id', type: 'options', - description: 'Choose from the list, or specify an ID using an expression', + description: 'Choose from the list, or specify an ID using an expression', default: '', typeOptions: { loadOptionsMethod: 'getProjectCustomField', @@ -192,7 +198,7 @@ export const projectDescription: INodeProperties[] = [ displayName: 'Contributors: Name or ID', name: 'value', type: 'options', - description: 'Choose from the list, or specify an ID using an expression', + description: 'Choose from the list, or specify an ID using an expression', default: '', typeOptions: { loadOptionsMethod: 'getUsers', @@ -242,7 +248,7 @@ export const projectDescription: INodeProperties[] = [ displayName: 'Contributor\'s Email\, Name or ID', name: 'contributor', type: 'options', - description: 'Choose from the list, or specify an ID using an expression', + description: 'Choose from the list, or specify an ID using an expression', default: '', typeOptions: { loadOptionsMethod: 'getEmails', @@ -252,7 +258,7 @@ export const projectDescription: INodeProperties[] = [ displayName: 'Project Name or ID', name: '_id', type: 'options', - description: 'Choose from the list, or specify an ID using an expression', + description: 'Choose from the list, or specify an ID using an expression', default: '', typeOptions: { loadOptionsMethod: 'getProjectId', @@ -260,4 +266,24 @@ export const projectDescription: INodeProperties[] = [ }, ], }, + { + displayName: 'Contributor\'s Email\, Name or ID', + name: 'contributor', + type: 'options', + description: 'Choose from the list, or specify an ID using an expression', + default: '', + typeOptions: { + loadOptionsMethod: 'getEmails', + }, + displayOptions: { + show: { + resource: [ + 'project', + ], + operation: [ + 'getCklph', + ], + }, + }, + }, ];