UNPKG

@nu-art/google-services

Version:
102 lines (101 loc) 4.43 kB
"use strict"; /* * Permissions management system, define access level for each of * your server apis, and restrict users by giving them access levels * * Copyright (C) 2020 Adam van der Kruk aka TacB0sS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ModuleBE_GoogleContacts = exports.ModuleBE_GoogleContacts_Class = exports.standardPhotoProperties = exports.standardProperties = void 0; const ts_common_1 = require("@nu-art/ts-common"); const googleapis_1 = require("googleapis"); const ModuleBE_Auth_1 = require("./ModuleBE_Auth"); exports.standardProperties = [ 'addresses', 'birthdays', 'emailAddresses', 'genders', 'names', 'phoneNumbers', 'userDefined' ]; exports.standardPhotoProperties = [ 'addresses', 'birthdays', 'emailAddresses', 'genders', 'names', 'phoneNumbers', 'userDefined', 'photos' ]; class ModuleBE_GoogleContacts_Class extends ts_common_1.Module { constructor() { super('ModuleBE_GoogleContacts'); this.getContactById = async (userEmail, contactId, authKey) => { const query = { resourceName: contactId, personFields: `${exports.standardProperties.join(',')},metadata` }; return (await this.createContactApi(userEmail, authKey).people.get(query)).data; }; this.delete = async (userEmail, resource, authKey) => { const contactsApi = await this.createContactApi(userEmail, authKey); return (await contactsApi.people.deleteContact({ resourceName: resource })).data; }; this.list = async (userEmail, pageToken, authKey) => { const query = { // Only valid resourceName according to https://developers.google.com/people/api/restv1/people.connections/list pageToken, resourceName: 'people/me', pageSize: 1000, personFields: exports.standardProperties.join(',') }; return (await this.createContactApi(userEmail, authKey).people.connections.list(query)).data; }; this.create = async (userEmail, record, authKey) => { return (await this.createContactApi(userEmail, authKey).people.createContact({ requestBody: record })).data; }; this.update = async (userEmail, record, authKey) => { const updateRequest = { resourceName: record.resourceName, updatePersonFields: exports.standardProperties.join(','), requestBody: record }; return (await this.createContactApi(userEmail, authKey).people.updateContact(updateRequest)).data; }; this.updatePhoto = async (userEmail, person, contactImageBase64, authKey) => { const updatePhoto = { resourceName: person.resourceName, requestBody: { photoBytes: contactImageBase64, personFields: exports.standardPhotoProperties.join(',') } }; return (await this.createContactApi(userEmail, authKey).people.updateContactPhoto(updatePhoto)).data; }; this.createContactApi = (userEmail, authKey) => { const finalAuthKey = authKey || this.config.authKey; if (!finalAuthKey) throw new ts_common_1.ImplementationMissingException('missing authkey for google contacts api'); const auth = ModuleBE_Auth_1.ModuleBE_Auth.getAuth(finalAuthKey, ['https://www.googleapis.com/auth/contacts'], 'v1', { subject: userEmail }); return new googleapis_1.people_v1.People(auth); }; } init() { } } exports.ModuleBE_GoogleContacts_Class = ModuleBE_GoogleContacts_Class; exports.ModuleBE_GoogleContacts = new ModuleBE_GoogleContacts_Class();