2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2020-08-15 14:25:15 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2020-08-15 14:25:15 -07:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2020-08-15 14:25:15 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
allFields,
|
|
|
|
cleanData,
|
|
|
|
googleApiRequest,
|
|
|
|
googleApiRequestAllItems,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { contactFields, contactOperations } from './ContactDescription';
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment';
|
2020-08-15 14:25:15 -07:00
|
|
|
|
|
|
|
export class GoogleContacts implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Google Contacts',
|
|
|
|
name: 'googleContacts',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-08-15 14:25:15 -07:00
|
|
|
icon: 'file:googleContacts.png',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Google Contacts API',
|
2020-08-15 14:25:15 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Google Contacts',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'googleContactsOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-08-15 14:25:15 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Contact',
|
|
|
|
value: 'contact',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'contact',
|
|
|
|
},
|
|
|
|
...contactOperations,
|
|
|
|
...contactFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the calendars to display them to user so that he can
|
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getGroups(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-08-15 14:25:15 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const groups = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'contactGroups',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/contactGroups',
|
2020-08-15 14:26:01 -07:00
|
|
|
);
|
2020-08-15 14:25:15 -07:00
|
|
|
for (const group of groups) {
|
|
|
|
const groupName = group.name;
|
|
|
|
const groupId = group.resourceName;
|
|
|
|
returnData.push({
|
|
|
|
name: groupName,
|
2020-10-22 06:46:03 -07:00
|
|
|
value: groupId,
|
2020-08-15 14:25:15 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-08-15 14:25:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-08-15 14:25:15 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-08-15 14:25:15 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'contact') {
|
|
|
|
//https://developers.google.com/calendar/v3/reference/events/insert
|
|
|
|
if (operation === 'create') {
|
|
|
|
const familyName = this.getNodeParameter('familyName', i) as string;
|
|
|
|
const givenName = this.getNodeParameter('givenName', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
names: [
|
|
|
|
{
|
|
|
|
familyName,
|
|
|
|
givenName,
|
|
|
|
middleName: '',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.middleName) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.names[0].middleName = additionalFields.middleName as string;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.honorificPrefix) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.names[0].honorificPrefix = additionalFields.honorificPrefix as string;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.honorificSuffix) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.names[0].honorificSuffix = additionalFields.honorificSuffix as string;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.companyUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const companyValues = (additionalFields.companyUi as IDataObject)
|
|
|
|
.companyValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.organizations = companyValues;
|
2020-08-15 14:25:15 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.phoneUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const phoneValues = (additionalFields.phoneUi as IDataObject)
|
|
|
|
.phoneValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.phoneNumbers = phoneValues;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.addressesUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const addressesValues = (additionalFields.addressesUi as IDataObject)
|
|
|
|
.addressesValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.addresses = addressesValues;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.relationsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const relationsValues = (additionalFields.relationsUi as IDataObject)
|
|
|
|
.relationsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.relations = relationsValues;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.eventsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const eventsValues = (additionalFields.eventsUi as IDataObject)
|
|
|
|
.eventsValues as IDataObject[];
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < eventsValues.length; index++) {
|
|
|
|
const [month, day, year] = moment(eventsValues[index].date as string)
|
2022-08-17 08:50:24 -07:00
|
|
|
.format('MM/DD/YYYY')
|
|
|
|
.split('/');
|
2022-12-02 12:54:28 -08:00
|
|
|
eventsValues[index] = {
|
2021-07-19 23:58:54 -07:00
|
|
|
date: {
|
|
|
|
day,
|
|
|
|
month,
|
|
|
|
year,
|
|
|
|
},
|
2022-12-02 12:54:28 -08:00
|
|
|
type: eventsValues[index].type,
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
body.events = eventsValues;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.birthday) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const [month, day, year] = moment(additionalFields.birthday as string)
|
|
|
|
.format('MM/DD/YYYY')
|
|
|
|
.split('/');
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.birthdays = [
|
|
|
|
{
|
|
|
|
date: {
|
|
|
|
day,
|
|
|
|
month,
|
|
|
|
year,
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
];
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.emailsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const emailsValues = (additionalFields.emailsUi as IDataObject)
|
|
|
|
.emailsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.emailAddresses = emailsValues;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.biographies) {
|
|
|
|
body.biographies = [
|
|
|
|
{
|
|
|
|
value: additionalFields.biographies,
|
|
|
|
contentType: 'TEXT_PLAIN',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.customFieldsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customFieldsValues = (additionalFields.customFieldsUi as IDataObject)
|
|
|
|
.customFieldsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.userDefined = customFieldsValues;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.group) {
|
|
|
|
const memberships = (additionalFields.group as string[]).map((groupId: string) => {
|
|
|
|
return {
|
|
|
|
contactGroupMembership: {
|
|
|
|
contactGroupResourceName: groupId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.memberships = memberships;
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/people:createContact',
|
2021-07-19 23:58:54 -07:00
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData.contactId = responseData.resourceName.split('/')[1];
|
2020-08-15 14:25:15 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developers.google.com/people/api/rest/v1/people/deleteContact
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
|
|
responseData = await googleApiRequest.call(
|
2020-08-15 14:25:15 -07:00
|
|
|
this,
|
2021-07-19 23:58:54 -07:00
|
|
|
'DELETE',
|
|
|
|
`/people/${contactId}:deleteContact`,
|
2020-08-15 14:25:15 -07:00
|
|
|
{},
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
//https://developers.google.com/people/api/rest/v1/people/get
|
|
|
|
if (operation === 'get') {
|
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
|
|
const fields = this.getNodeParameter('fields', i) as string[];
|
2022-11-18 05:31:38 -08:00
|
|
|
const rawData = this.getNodeParameter('rawData', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (fields.includes('*')) {
|
|
|
|
qs.personFields = allFields.join(',');
|
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.personFields = fields.join(',');
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'GET', `/people/${contactId}`, {}, qs);
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!rawData) {
|
|
|
|
responseData = cleanData(responseData)[0];
|
|
|
|
}
|
2020-08-15 14:25:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData.contactId = responseData.resourceName.split('/')[1];
|
2020-08-15 14:25:15 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developers.google.com/people/api/rest/v1/people.connections/list
|
2021-12-11 08:00:47 -08:00
|
|
|
//https://developers.google.com/people/api/rest/v1/people/searchContacts
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const fields = this.getNodeParameter('fields', i) as string[];
|
2022-12-02 12:54:28 -08:00
|
|
|
const options = this.getNodeParameter('options', i, {});
|
2022-11-18 05:31:38 -08:00
|
|
|
const rawData = this.getNodeParameter('rawData', i);
|
2021-12-11 08:00:47 -08:00
|
|
|
const useQuery = this.getNodeParameter('useQuery', i) as boolean;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const endpoint = useQuery ? ':searchContacts' : '/me/connections';
|
2021-12-11 08:00:47 -08:00
|
|
|
|
|
|
|
if (useQuery) {
|
|
|
|
qs.query = this.getNodeParameter('query', i) as string;
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.sortOrder) {
|
|
|
|
qs.sortOrder = options.sortOrder as number;
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (fields.includes('*')) {
|
|
|
|
qs.personFields = allFields.join(',');
|
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.personFields = fields.join(',');
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-12-11 08:00:47 -08:00
|
|
|
if (useQuery) {
|
|
|
|
qs.readMask = qs.personFields;
|
|
|
|
delete qs.personFields;
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
2022-08-17 08:50:24 -07:00
|
|
|
useQuery ? 'results' : 'connections',
|
2021-07-19 23:58:54 -07:00
|
|
|
'GET',
|
2021-12-11 08:00:47 -08:00
|
|
|
`/people${endpoint}`,
|
2021-07-19 23:58:54 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-12-11 08:00:47 -08:00
|
|
|
|
|
|
|
if (useQuery) {
|
|
|
|
responseData = responseData.map((result: IDataObject) => result.person);
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.pageSize = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(this, 'GET', `/people${endpoint}`, {}, qs);
|
2021-12-11 08:08:45 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData =
|
|
|
|
responseData.connections ||
|
|
|
|
responseData.results?.map((result: IDataObject) => result.person) ||
|
|
|
|
[];
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!rawData) {
|
|
|
|
responseData = cleanData(responseData);
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < responseData.length; index++) {
|
|
|
|
responseData[index].contactId = responseData[index].resourceName.split('/')[1];
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//https://developers.google.com/people/api/rest/v1/people/updateContact
|
|
|
|
if (operation === 'update') {
|
|
|
|
const updatePersonFields = [];
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const fields = this.getNodeParameter('fields', i) as string[];
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let etag;
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.etag) {
|
|
|
|
etag = updateFields.etag as string;
|
|
|
|
} else {
|
|
|
|
const data = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/people/${contactId}`,
|
|
|
|
{},
|
|
|
|
{ personFields: 'Names' },
|
|
|
|
);
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
etag = data.etag;
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (fields.includes('*')) {
|
|
|
|
qs.personFields = allFields.join(',');
|
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.personFields = fields.join(',');
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
etag,
|
2022-08-17 08:50:24 -07:00
|
|
|
names: [{}],
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.givenName) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.names[0].givenName = updateFields.givenName as string;
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.familyName) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.names[0].familyName = updateFields.familyName as string;
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.middleName) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.names[0].middleName = updateFields.middleName as string;
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.honorificPrefix) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.names[0].honorificPrefix = updateFields.honorificPrefix as string;
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.honorificSuffix) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.names[0].honorificSuffix = updateFields.honorificSuffix as string;
|
2020-09-03 02:21:23 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.companyUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const companyValues = (updateFields.companyUi as IDataObject)
|
|
|
|
.companyValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.organizations = companyValues;
|
|
|
|
updatePersonFields.push('organizations');
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.phoneUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const phoneValues = (updateFields.phoneUi as IDataObject)
|
|
|
|
.phoneValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.phoneNumbers = phoneValues;
|
|
|
|
updatePersonFields.push('phoneNumbers');
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.addressesUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const addressesValues = (updateFields.addressesUi as IDataObject)
|
|
|
|
.addressesValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.addresses = addressesValues;
|
|
|
|
updatePersonFields.push('addresses');
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.relationsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const relationsValues = (updateFields.relationsUi as IDataObject)
|
|
|
|
.relationsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.relations = relationsValues;
|
|
|
|
updatePersonFields.push('relations');
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.eventsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const eventsValues = (updateFields.eventsUi as IDataObject)
|
|
|
|
.eventsValues as IDataObject[];
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < eventsValues.length; index++) {
|
|
|
|
const [month, day, year] = moment(eventsValues[index].date as string)
|
2022-08-17 08:50:24 -07:00
|
|
|
.format('MM/DD/YYYY')
|
|
|
|
.split('/');
|
2022-12-02 12:54:28 -08:00
|
|
|
eventsValues[index] = {
|
2021-07-19 23:58:54 -07:00
|
|
|
date: {
|
|
|
|
day,
|
|
|
|
month,
|
|
|
|
year,
|
|
|
|
},
|
2022-12-02 12:54:28 -08:00
|
|
|
type: eventsValues[index].type,
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
body.events = eventsValues;
|
|
|
|
updatePersonFields.push('events');
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.birthday) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const [month, day, year] = moment(updateFields.birthday as string)
|
|
|
|
.format('MM/DD/YYYY')
|
|
|
|
.split('/');
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.birthdays = [
|
|
|
|
{
|
|
|
|
date: {
|
|
|
|
day,
|
|
|
|
month,
|
|
|
|
year,
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
];
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
updatePersonFields.push('birthdays');
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.emailsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const emailsValues = (updateFields.emailsUi as IDataObject)
|
|
|
|
.emailsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.emailAddresses = emailsValues;
|
|
|
|
updatePersonFields.push('emailAddresses');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.biographies) {
|
|
|
|
body.biographies = [
|
|
|
|
{
|
|
|
|
value: updateFields.biographies,
|
|
|
|
contentType: 'TEXT_PLAIN',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
updatePersonFields.push('biographies');
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.customFieldsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customFieldsValues = (updateFields.customFieldsUi as IDataObject)
|
|
|
|
.customFieldsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.userDefined = customFieldsValues;
|
|
|
|
updatePersonFields.push('userDefined');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateFields.group) {
|
|
|
|
const memberships = (updateFields.group as string[]).map((groupId: string) => {
|
|
|
|
return {
|
|
|
|
contactGroupMembership: {
|
|
|
|
contactGroupResourceName: groupId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
body.memberships = memberships;
|
|
|
|
updatePersonFields.push('memberships');
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if ((body.names as IDataObject[]).length > 0) {
|
|
|
|
updatePersonFields.push('names');
|
|
|
|
}
|
|
|
|
|
|
|
|
qs.updatePersonFields = updatePersonFields.join(',');
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PATCH',
|
|
|
|
`/people/${contactId}:updateContact`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2020-09-03 02:21:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData.contactId = responseData.resourceName.split('/')[1];
|
|
|
|
}
|
2020-09-03 02:21:23 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-08-15 14:25:15 -07:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
return this.prepareOutputData(returnData);
|
2020-08-15 14:25:15 -07:00
|
|
|
}
|
|
|
|
}
|