Files
twenty/packages/twenty-zapier/src/utils/handleQueryParams.ts
martmull 29bd74feea 7203 support emails links phones in zapier inputs 2 (#7562)
## Done
- add `EMAILS`, `PHONES`, `LINKS`, `RICH_TEXT`, `POSITION`, and `ARRAY`
field support in Twenty zapier integration
- fix `twenty-zapier` package tests and requirements

## Emails
<img width="791" alt="image"
src="https://github.com/user-attachments/assets/7987a1a2-6076-4715-9221-d4a1898b7634">

## Links
<img width="797" alt="image"
src="https://github.com/user-attachments/assets/b94ce972-fae2-4953-b9e8-79c0478f5f60">

## Phones
<img width="789" alt="image"
src="https://github.com/user-attachments/assets/7234eaaf-40b8-4772-8880-c58ba47618c5">

## Array
<img width="834" alt="image"
src="https://github.com/user-attachments/assets/99cb6795-e428-40ea-9c3a-d52561c2c6e1">
2024-10-10 15:32:06 +02:00

51 lines
1.6 KiB
TypeScript

import { InputData } from '../utils/data.types';
const OBJECT_SUBFIELD_NAMES = ['secondaryLinks', 'additionalPhones'];
const formatArrayInputData = (
key: string,
arrayInputData: InputData,
): string => {
if (OBJECT_SUBFIELD_NAMES.includes(key)) {
return `${arrayInputData[key].join('","')}`;
}
return `"${arrayInputData[key].join('","')}"`;
};
const handleQueryParams = (inputData: InputData): string => {
const formattedInputData: InputData = {};
Object.keys(inputData).forEach((key) => {
if (key.includes('__')) {
const [objectKey, nestedObjectKey] = key.split('__');
if (formattedInputData[objectKey]) {
formattedInputData[objectKey][nestedObjectKey] = inputData[key];
} else {
formattedInputData[objectKey] = { [nestedObjectKey]: inputData[key] };
}
} else {
formattedInputData[key] = inputData[key];
}
});
let result = '';
Object.keys(formattedInputData).forEach((key) => {
let quote = '';
if (Array.isArray(formattedInputData[key])) {
result = result.concat(
`${key}: [${formatArrayInputData(key, formattedInputData)}], `,
);
} else if (typeof formattedInputData[key] === 'object') {
result = result.concat(
`${key}: {${handleQueryParams(formattedInputData[key])}}, `,
);
} else {
if (typeof formattedInputData[key] === 'string') quote = '"';
result = result.concat(
`${key}: ${quote}${formattedInputData[key]}${quote}, `,
);
}
});
if (result.length) result = result.slice(0, -2); // Remove the last ', '
return result;
};
export default handleQueryParams;