fix(Pipedrive Node): Fix resolve properties when multi option field is used (#3277)

* Fixed Pipedrive properties not resolving when using multiple options field

*  Improvements

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
This commit is contained in:
Jonathan Bennetts 2022-05-16 09:44:00 +01:00 committed by GitHub
parent c75d58c18a
commit 7eb12615cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,6 +18,7 @@ import {
export interface ICustomInterface {
name: string;
key: string;
field_type: string;
options?: Array<{
id: number;
label: string;
@ -171,7 +172,6 @@ export async function pipedriveGetCustomProperties(this: IHookFunctions | IExecu
for (const customPropertyData of responseData.data) {
customProperties[customPropertyData.key] = customPropertyData;
}
return customProperties;
}
@ -226,30 +226,57 @@ export function pipedriveResolveCustomProperties(customProperties: ICustomProper
// Itterate over all keys and replace the custom ones
for (const key of Object.keys(item)) {
if (customProperties[key] !== undefined) {
// Is a custom property
customPropertyData = customProperties[key];
// Check if also the value has to be resolved or just the key
if (item[key] !== null && item[key] !== undefined && customPropertyData.options !== undefined && Array.isArray(customPropertyData.options)) {
// Has an option key so get the actual option-value
const propertyOption = customPropertyData.options.find(option => option.id.toString() === item[key]!.toString());
// value is not set, so nothing to resolve
if (item[key] === null) {
item[customPropertyData.name] = item[key];
delete item[key];
continue;
}
if ([
'date',
'address',
'double',
'monetary',
'org',
'people',
'phone',
'text',
'time',
'user',
'varchar',
'varchar_auto',
'int',
'time',
'timerange',
].includes(customPropertyData.field_type)) {
item[customPropertyData.name as string] = item[key];
delete item[key];
// type options
} else if (['enum', 'visible_to'].includes(customPropertyData.field_type) && customPropertyData.options) {
const propertyOption = customPropertyData.options.find(option => option.id.toString() === item[key]!.toString());
if (propertyOption !== undefined) {
item[customPropertyData.name as string] = propertyOption.label;
delete item[key];
}
} else {
// Does already represent the actual value or is null
item[customPropertyData.name as string] = item[key];
// type multioptions
} else if (['set'].includes(customPropertyData.field_type) && customPropertyData.options) {
const selectedIds = (item[key] as string).split(',');
const selectedLabels = customPropertyData.options.
filter(option => selectedIds.includes(option.id.toString())).
map(option => option.label);
item[customPropertyData.name] = selectedLabels;
delete item[key];
}
}
}
}
export function sortOptionParameters(optionParameters: INodePropertyOptions[]): INodePropertyOptions[] {
optionParameters.sort((a, b) => {
const aName = a.name.toLowerCase();