2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject } from 'n8n-workflow';
|
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function googleApiRequest(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-08-15 14:25:15 -07:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://people.googleapis.com/v1${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-08-15 14:25:15 -07:00
|
|
|
};
|
|
|
|
try {
|
|
|
|
if (Object.keys(headers).length !== 0) {
|
|
|
|
options.headers = Object.assign({}, options.headers, headers);
|
|
|
|
}
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers.requestOAuth2.call(this, 'googleContactsOAuth2Api', options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-08-15 14:25:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function googleApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-08-15 14:25:15 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
query.pageSize = 100;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, query);
|
2022-12-02 12:54:28 -08:00
|
|
|
query.pageToken = responseData.nextPageToken;
|
2020-08-15 14:25:15 -07:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
2022-12-02 12:54:28 -08:00
|
|
|
} while (responseData.nextPageToken !== undefined && responseData.nextPageToken !== '');
|
2020-08-15 14:25:15 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2020-08-15 14:26:01 -07:00
|
|
|
export const allFields = [
|
2020-08-15 14:25:15 -07:00
|
|
|
'addresses',
|
|
|
|
'biographies',
|
|
|
|
'birthdays',
|
|
|
|
'coverPhotos',
|
|
|
|
'emailAddresses',
|
|
|
|
'events',
|
|
|
|
'genders',
|
|
|
|
'imClients',
|
|
|
|
'interests',
|
|
|
|
'locales',
|
|
|
|
'memberships',
|
|
|
|
'metadata',
|
|
|
|
'names',
|
|
|
|
'nicknames',
|
|
|
|
'occupations',
|
|
|
|
'organizations',
|
|
|
|
'phoneNumbers',
|
|
|
|
'photos',
|
|
|
|
'relations',
|
|
|
|
'residences',
|
|
|
|
'sipAddresses',
|
|
|
|
'skills',
|
|
|
|
'urls',
|
|
|
|
'userDefined',
|
|
|
|
];
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function cleanData(responseData: any) {
|
2020-08-15 14:25:15 -07:00
|
|
|
const fields = ['emailAddresses', 'phoneNumbers', 'relations', 'events', 'addresses'];
|
|
|
|
const newResponseData = [];
|
|
|
|
if (!Array.isArray(responseData)) {
|
|
|
|
responseData = [responseData];
|
|
|
|
}
|
2020-08-15 14:26:01 -07:00
|
|
|
for (let y = 0; y < responseData.length; y++) {
|
2022-12-02 06:25:21 -08:00
|
|
|
const object: { [key: string]: any } = {};
|
2020-08-15 14:25:15 -07:00
|
|
|
for (const key of Object.keys(responseData[y])) {
|
|
|
|
if (key === 'metadata') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key === 'photos') {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData[y][key] = responseData[y][key].map((photo: IDataObject) => photo.url);
|
2020-08-15 14:25:15 -07:00
|
|
|
}
|
|
|
|
if (key === 'names') {
|
|
|
|
delete responseData[y][key][0].metadata;
|
|
|
|
responseData[y][key] = responseData[y][key][0];
|
|
|
|
}
|
|
|
|
if (key === 'memberships') {
|
|
|
|
for (let i = 0; i < responseData[y][key].length; i++) {
|
|
|
|
responseData[y][key][i] = responseData[y][key][i].metadata.source.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (key === 'birthdays') {
|
|
|
|
for (let i = 0; i < responseData[y][key].length; i++) {
|
|
|
|
const { year, month, day } = responseData[y][key][i].date;
|
|
|
|
responseData[y][key][i] = `${month}/${day}/${year}`;
|
|
|
|
}
|
|
|
|
responseData[y][key] = responseData[y][key][0];
|
|
|
|
}
|
|
|
|
if (key === 'userDefined' || key === 'organizations' || key === 'biographies') {
|
|
|
|
for (let i = 0; i < responseData[y][key].length; i++) {
|
|
|
|
delete responseData[y][key][i].metadata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fields.includes(key)) {
|
2022-12-02 06:25:21 -08:00
|
|
|
const value: { [key: string]: any } = {};
|
2020-08-15 14:25:15 -07:00
|
|
|
for (const data of responseData[y][key]) {
|
|
|
|
let result;
|
|
|
|
if (value[data.type] === undefined) {
|
|
|
|
value[data.type] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key === 'relations') {
|
|
|
|
result = data.person;
|
|
|
|
} else if (key === 'events') {
|
|
|
|
const { year, month, day } = data.date;
|
|
|
|
result = `${month}/${day}/${year}`;
|
|
|
|
} else if (key === 'addresses') {
|
|
|
|
delete data.metadata;
|
|
|
|
result = data;
|
|
|
|
} else {
|
|
|
|
result = data.value;
|
|
|
|
}
|
|
|
|
value[data.type].push(result);
|
|
|
|
delete data.type;
|
|
|
|
}
|
|
|
|
if (Object.keys(value).length > 0) {
|
|
|
|
object[key] = value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
object[key] = responseData[y][key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newResponseData.push(object);
|
|
|
|
}
|
|
|
|
return newResponseData;
|
|
|
|
}
|