Add update operation (#905)

*  Add update operation

*  Small improvement
This commit is contained in:
Ricardo Espinoza 2020-09-03 05:21:23 -04:00 committed by GitHub
parent cd0b2851bd
commit 8a93bdbdcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 827 additions and 0 deletions

View file

@ -35,6 +35,11 @@ export const contactOperations = [
value: 'getAll',
description: 'Retrieve all contacts',
},
{
name: 'Update',
value: 'update',
description: 'Update a contact',
},
],
default: 'create',
description: 'The operation to perform.'
@ -347,6 +352,18 @@ export const contactFields = [
},
default: [],
},
{
displayName: 'Honorific Prefix',
name: 'honorificPrefix',
type: 'string',
default: '',
},
{
displayName: 'Honorific Suffix',
name: 'honorificSuffix',
type: 'string',
default: '',
},
{
displayName: 'Middle Name',
name: 'middleName',
@ -935,4 +952,628 @@ export const contactFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* contact:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'Contact ID',
name: 'contactId',
type: 'string',
required: true,
displayOptions: {
show: {
operation: [
'update',
],
resource: [
'contact',
],
},
},
default: '',
},
{
displayName: 'Fields',
name: 'fields',
type: 'multiOptions',
options: [
{
name: '*',
value: '*',
},
{
name: 'Addresses',
value: 'addresses',
},
{
name: 'Biographies',
value: 'biographies',
},
{
name: 'Birthdays',
value: 'birthdays',
},
{
name: 'Cover Photos',
value: 'coverPhotos',
},
{
name: 'Email Addresses',
value: 'emailAddresses',
},
{
name: 'Events',
value: 'events',
},
{
name: 'Genders',
value: 'genders',
},
{
name: 'IM Clients',
value: 'imClients',
},
{
name: 'Interests',
value: 'interests',
},
{
name: 'Locales',
value: 'locales',
},
{
name: 'Memberships',
value: 'memberships',
},
{
name: 'Metadata',
value: 'metadata',
},
{
name: 'Names',
value: 'names',
},
{
name: 'Nicknames',
value: 'nicknames',
},
{
name: 'Occupations',
value: 'occupations',
},
{
name: 'Organizations',
value: 'organizations',
},
{
name: 'Phone Numbers',
value: 'phoneNumbers',
},
{
name: 'Photos',
value: 'photos',
},
{
name: 'Relations',
value: 'relations',
},
{
name: 'Residences',
value: 'residences',
},
{
name: 'Sip Addresses',
value: 'sipAddresses',
},
{
name: 'Skills',
value: 'skills',
},
{
name: 'URLs',
value: 'urls',
},
{
name: 'User Defined',
value: 'userDefined',
},
],
displayOptions: {
show: {
operation: [
'update',
],
resource: [
'contact',
],
},
},
default: '',
description: 'A field mask to restrict which fields on each person are returned. Multiple fields can be specified by separating them with commas.',
},
{
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
operation: [
'update',
],
resource: [
'contact',
],
},
},
options: [
{
displayName: 'Etag',
name: 'etag',
type: 'string',
default: '',
description: 'The etag field in the person is nedded to make sure the contact has not changed since your last read',
},
{
displayName: 'Family Name',
name: 'familyName',
type: 'string',
default: '',
},
{
displayName: 'Given Name',
name: 'givenName',
type: 'string',
default: '',
},
{
displayName: 'Addresses',
name: 'addressesUi',
placeholder: 'Add Address',
type: 'fixedCollection',
default: {},
options: [
{
displayName: 'Address',
name: 'addressesValues',
values: [
{
displayName: 'Street Address',
name: 'streetAddress',
type: 'string',
default: '',
},
{
displayName: 'City',
name: 'city',
type: 'string',
default: '',
description: 'City',
},
{
displayName: 'Region',
name: 'region',
type: 'string',
default: '',
description: 'Region',
},
{
displayName: 'Country Code',
name: 'countryCode',
type: 'string',
default: '',
},
{
displayName: 'Postal Code',
name: 'postalCode',
type: 'string',
default: '',
description: 'Postal code',
},
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Home',
value: 'home',
},
{
name: 'Work',
value: 'work',
},
{
name: 'Other',
value: 'other',
},
],
default: '',
},
],
},
],
},
{
displayName: 'Birthday',
name: 'birthday',
type: 'dateTime',
default: '',
},
{
displayName: 'Company',
name: 'companyUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Company',
typeOptions: {
multipleValues: true
},
options: [
{
name: 'companyValues',
displayName: 'Company',
values: [
{
displayName: 'Current',
name: 'current',
type: 'boolean',
default: false,
},
{
displayName: 'Domain',
name: 'domain',
type: 'string',
default: '',
},
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
},
{
displayName: 'Title',
name: 'title',
type: 'string',
default: '',
},
],
},
],
},
{
displayName: 'Custom Fields',
name: 'customFieldsUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Custom Field',
typeOptions: {
multipleValues: true
},
options: [
{
name: 'customFieldsValues',
displayName: 'Custom Field',
values: [
{
displayName: 'Key',
name: 'key',
type: 'string',
description: 'The end user specified key of the user defined data.',
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
description: 'The end user specified value of the user defined data.',
default: '',
},
],
},
],
},
{
displayName: 'Emails',
name: 'emailsUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Email',
typeOptions: {
multipleValues: true
},
options: [
{
name: 'emailsValues',
displayName: 'Email',
values: [
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Home',
value: 'home',
},
{
name: 'Work',
value: 'work',
},
{
name: 'Other',
value: 'other',
},
],
default: '',
description: `The type of the email address. The type can be custom or one of these predefined values`,
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'The email address.',
},
],
},
],
},
{
displayName: 'Events',
name: 'eventsUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Event',
description: 'An event related to the person.',
typeOptions: {
multipleValues: true
},
options: [
{
name: 'eventsValues',
displayName: 'Event',
values: [
{
displayName: 'Date',
name: 'date',
type: 'dateTime',
default: '',
description: 'The date of the event.',
},
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Anniversary',
value: 'anniversary',
},
{
name: 'Other',
value: 'other',
},
],
default: '',
description: `The type of the event. The type can be custom or one of these predefined values`,
},
],
},
],
},
{
displayName: 'File As',
name: 'fileAs',
type: 'string',
default: '',
description: 'The name that should be used to sort the person in a list.',
},
{
displayName: 'Group',
name: 'group',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getGroups',
},
default: [],
},
{
displayName: 'Honorific Prefix',
name: 'honorificPrefix',
type: 'string',
default: '',
},
{
displayName: 'Honorific Suffix',
name: 'honorificSuffix',
type: 'string',
default: '',
},
{
displayName: 'Middle Name',
name: 'middleName',
type: 'string',
default: '',
},
{
displayName: 'Notes',
name: 'biographies',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
},
{
displayName: 'Phone',
name: 'phoneUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Phone',
typeOptions: {
multipleValues: true
},
options: [
{
name: 'phoneValues',
displayName: 'Phone',
values: [
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Home',
value: 'home',
},
{
name: 'Work',
value: 'work',
},
{
name: 'Mobile',
value: 'mobile',
},
{
name: 'Home Fax',
value: 'homeFax',
},
{
name: 'Work Fax',
value: 'workFax',
},
{
name: 'Other Fax',
value: 'otherFax',
},
{
name: 'Pager',
value: 'pager',
},
{
name: 'Work Mobile',
value: 'workMobile',
},
{
name: 'Work Pager',
value: 'workPager',
},
{
name: 'Main',
value: 'main',
},
{
name: 'Google Voice',
value: 'googleVoice',
},
{
name: 'Other',
value: 'other',
},
],
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'The phone number.',
},
],
},
],
},
{
displayName: 'Relations',
name: 'relationsUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Relation',
typeOptions: {
multipleValues: true
},
options: [
{
name: 'relationsValues',
displayName: 'Relation',
values: [
{
displayName: 'Person',
name: 'person',
type: 'string',
default: '',
description: 'The name of the other person this relation refers to.',
},
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Assistant',
value: 'assistant',
},
{
name: 'Brother',
value: 'brother',
},
{
name: 'Child',
value: 'child',
},
{
name: 'Domestic Partner',
value: 'domesticPartner',
},
{
name: 'Father',
value: 'father',
},
{
name: 'Friend',
value: 'friend',
},
{
name: 'Manager',
value: 'manager',
},
{
name: 'Mother',
value: 'mother',
},
{
name: 'Parent',
value: 'parent',
},
{
name: 'Referred By',
value: 'referredBy',
},
{
name: 'Relative',
value: 'relative',
},
{
name: 'Sister',
value: 'sister',
},
{
name: 'Spouse',
value: 'spouse',
},
],
default: '',
description: `The person's relation to the other person. The type can be custom or one of these predefined values`,
},
],
},
],
},
],
},
] as INodeProperties[];

View file

@ -123,6 +123,16 @@ export class GoogleContacts implements INodeType {
body.names[0].middleName = additionalFields.middleName as string;
}
if (additionalFields.honorificPrefix) {
//@ts-ignore
body.names[0].honorificPrefix = additionalFields.honorificPrefix as string;
}
if (additionalFields.honorificSuffix) {
//@ts-ignore
body.names[0].honorificSuffix = additionalFields.honorificSuffix as string;
}
if (additionalFields.companyUi) {
const companyValues = (additionalFields.companyUi as IDataObject).companyValues as IDataObject[];
body.organizations = companyValues;
@ -298,6 +308,182 @@ export class GoogleContacts implements INodeType {
responseData[i].contactId = responseData[i].resourceName.split('/')[1];
}
}
//https://developers.google.com/people/api/rest/v1/people/updateContact
if (operation === 'update') {
const updatePersonFields = [];
const contactId = this.getNodeParameter('contactId', i) as string;
const fields = this.getNodeParameter('fields', i) as string[];
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
let etag;
if (updateFields.etag) {
etag = updateFields.etag as string;
} else {
const data = await googleApiRequest.call(
this,
'GET',
`/people/${contactId}`,
{},
{ personFields: 'Names' },
);
etag = data.etag;
}
if (fields.includes('*')) {
qs.personFields = allFields.join(',');
} else {
qs.personFields = (fields as string[]).join(',');
}
const body: IDataObject = {
etag,
names: [
{},
],
};
if (updateFields.givenName) {
//@ts-ignore
body.names[0].givenName = updateFields.givenName as string;
}
if (updateFields.familyName) {
//@ts-ignore
body.names[0].familyName = updateFields.familyName as string;
}
if (updateFields.middleName) {
//@ts-ignore
body.names[0].middleName = updateFields.middleName as string;
}
if (updateFields.honorificPrefix) {
//@ts-ignore
body.names[0].honorificPrefix = updateFields.honorificPrefix as string;
}
if (updateFields.honorificSuffix) {
//@ts-ignore
body.names[0].honorificSuffix = updateFields.honorificSuffix as string;
}
if (updateFields.companyUi) {
const companyValues = (updateFields.companyUi as IDataObject).companyValues as IDataObject[];
body.organizations = companyValues;
updatePersonFields.push('organizations');
}
if (updateFields.phoneUi) {
const phoneValues = (updateFields.phoneUi as IDataObject).phoneValues as IDataObject[];
body.phoneNumbers = phoneValues;
updatePersonFields.push('phoneNumbers');
}
if (updateFields.addressesUi) {
const addressesValues = (updateFields.addressesUi as IDataObject).addressesValues as IDataObject[];
body.addresses = addressesValues;
updatePersonFields.push('addresses');
}
if (updateFields.relationsUi) {
const relationsValues = (updateFields.relationsUi as IDataObject).relationsValues as IDataObject[];
body.relations = relationsValues;
updatePersonFields.push('relations');
}
if (updateFields.eventsUi) {
const eventsValues = (updateFields.eventsUi as IDataObject).eventsValues as IDataObject[];
for (let i = 0; i < eventsValues.length; i++) {
const [month, day, year] = moment(eventsValues[i].date as string).format('MM/DD/YYYY').split('/');
eventsValues[i] = {
date: {
day,
month,
year,
},
type: eventsValues[i].type,
};
}
body.events = eventsValues;
updatePersonFields.push('events');
}
if (updateFields.birthday) {
const [month, day, year] = moment(updateFields.birthday as string).format('MM/DD/YYYY').split('/');
body.birthdays = [
{
date: {
day,
month,
year
}
}
];
updatePersonFields.push('birthdays');
}
if (updateFields.emailsUi) {
const emailsValues = (updateFields.emailsUi as IDataObject).emailsValues as IDataObject[];
body.emailAddresses = emailsValues;
updatePersonFields.push('emailAddresses');
}
if (updateFields.biographies) {
body.biographies = [
{
value: updateFields.biographies,
contentType: 'TEXT_PLAIN',
},
];
updatePersonFields.push('biographies');
}
if (updateFields.customFieldsUi) {
const customFieldsValues = (updateFields.customFieldsUi as IDataObject).customFieldsValues as IDataObject[];
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');
}
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
);
responseData.contactId = responseData.resourceName.split('/')[1];
}
}
}
if (Array.isArray(responseData)) {