Contact update

This commit is contained in:
Rupenieks
2020-05-05 11:08:30 +02:00
parent e17df72dc6
commit a5821bdd14
4 changed files with 691 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ import {
import {
IDataObject,
} from 'n8n-workflow';
import { IContactUpdate } from './ContactInterface';
export async function agileCrmApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> {
@@ -46,6 +47,86 @@ export async function agileCrmApiRequest(this: IHookFunctions | IExecuteFunction
throw error;
}
}
export async function agileCrmApiRequestUpdate(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string = 'PUT', endpoint?: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> {
const baseUri = 'https://n8nio.agilecrm.com/dev/';
const credentials = this.getCredentials('agileCrmApi');
const options: OptionsWithUri = {
method,
headers: {
'Accept': 'application/json',
},
body: {id: body.id},
auth: {
username: credentials!.email as string,
password: credentials!.apiKey as string
},
uri: uri || baseUri,
json: true
};
let successfulUpdates = [];
let lastSuccesfulUpdateReturn : any;
let payload : IContactUpdate = body;
try {
// Due to API, we must update each property separately
if(payload.properties){
options.body.properties = payload.properties;
options.uri = baseUri + 'api/contacts/edit-properties';
lastSuccesfulUpdateReturn = await this.helpers.request!(options);
// Iterate trough properties and show them as individial updates instead of only vague "properties"
payload.properties?.map((property : any) => {
successfulUpdates.push(`${property.name} `);
})
delete options.body.properties;
}
if(payload.lead_score){
options.body.lead_score = payload.lead_score;
options.uri = baseUri + 'api/contacts/edit/lead-score';
lastSuccesfulUpdateReturn = await this.helpers.request!(options);
successfulUpdates.push('lead_score');
delete options.body.lead_score;
}
if(body.tags){
options.body.tags = payload.tags;
options.uri = baseUri + 'api/contacts/edit/tagaas';
lastSuccesfulUpdateReturn = await this.helpers.request!(options);
payload.tags?.map((tag : string) => {
successfulUpdates.push(`(Tag) ${tag} `);
})
delete options.body.tags;
}
if(body.star_value){
options.body.star_value = payload.star_value;
options.uri = baseUri + 'api/contacts/edit/add-star';
lastSuccesfulUpdateReturn = await this.helpers.request!(options);
successfulUpdates.push('star_value');
delete options.body.star_value;
}
return lastSuccesfulUpdateReturn;
} catch (error) {
if (error.response && error.response.body && error.response.body.errors) {
const errorMessages = error.response.body.errors.map((e: IDataObject) => e.message);
throw new Error(`AgileCRM error response [${error.statusCode}]: ${errorMessages.join(' | ')}`);
}
throw new Error(`Not all items updated. Updated items: ${successfulUpdates.join(' , ')} \n \n` + error);
}
}
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any