diff --git a/src/v1/index.ts b/src/v1/index.ts index da991db..4c9ed08 100644 --- a/src/v1/index.ts +++ b/src/v1/index.ts @@ -6,6 +6,7 @@ import { Fields } from './fields.ts' import { FieldValues } from './field_values.ts' import { FieldValueChanges } from './field_value_changes.ts' import { Organizations } from './organizations.ts' +import { Persons } from './persons.ts' export type * as ListEntries from './list_entries.ts' export type * as Lists from './lists.ts' export type * as Fields from './fields.ts' @@ -51,6 +52,7 @@ export class Affinity { this.fieldValues = new FieldValues(this.axios) this.fieldValueChanges = new FieldValueChanges(this.axios) this.organizations = new Organizations(this.axios) + this.persons = new Persons(this.axios) } public readonly auth: Auth @@ -66,4 +68,6 @@ export class Affinity { public readonly fieldValueChanges: FieldValueChanges public readonly organizations: Organizations + + public readonly persons: Persons } diff --git a/src/v1/tests/__snapshots__/persons_test.ts.snap b/src/v1/tests/__snapshots__/persons_test.ts.snap new file mode 100644 index 0000000..04b2893 --- /dev/null +++ b/src/v1/tests/__snapshots__/persons_test.ts.snap @@ -0,0 +1,43 @@ +export const snapshot = {}; + +snapshot[`persons > can search for persons 1`] = ` +{ + next_page_token: null, + persons: [ + { + emails: [ + "j-m@p-a.com", + ], + first_name: "Joscha", + id: 198383654, + last_name: "Test", + primary_email: "j-m@p-a.com", + type: 0, + }, + { + emails: [ + "some@group.calendar.google.com", + ], + first_name: "Someone's", + id: 191703462, + last_name: "and Else's appointments", + primary_email: "some@group.calendar.google.com", + type: 0, + }, + { + emails: [ + "j@p-a.com", + "j@f.com", + "jf@j.com", + "j@c.com", + "jf@a.com", + ], + first_name: "Joscha", + id: 54576635, + last_name: "Feth", + primary_email: "j@p-a.com", + type: 1, + }, + ], +} +`; diff --git a/src/v1/tests/fixtures/persons/search.raw.response.json b/src/v1/tests/fixtures/persons/search.raw.response.json new file mode 100644 index 0000000..56c3b79 --- /dev/null +++ b/src/v1/tests/fixtures/persons/search.raw.response.json @@ -0,0 +1,35 @@ +{ + "next_page_token": null, + "persons": [ + { + "emails": ["j-m@p-a.com"], + "first_name": "Joscha", + "id": 198383654, + "last_name": "Test", + "primary_email": "j-m@p-a.com", + "type": 0 + }, + { + "emails": ["some@group.calendar.google.com"], + "first_name": "Someone's", + "id": 191703462, + "last_name": "and Else's appointments", + "primary_email": "some@group.calendar.google.com", + "type": 0 + }, + { + "emails": [ + "j@p-a.com", + "j@f.com", + "jf@j.com", + "j@c.com", + "jf@a.com" + ], + "first_name": "Joscha", + "id": 54576635, + "last_name": "Feth", + "primary_email": "j@p-a.com", + "type": 1 + } + ] +} diff --git a/src/v1/tests/persons_test.ts b/src/v1/tests/persons_test.ts new file mode 100644 index 0000000..0b185ff --- /dev/null +++ b/src/v1/tests/persons_test.ts @@ -0,0 +1,156 @@ +import { assertSnapshot } from '@std/testing/snapshot' +import { afterEach, beforeEach, describe, it } from '@std/testing/bdd' + +import axios from 'axios' +import MockAdapter from 'axios-mock-adapter' +import { Affinity } from '../index.ts' +import { getRawFixture } from './get_raw_fixture.ts' +import { apiKey, isLiveRun } from './env.ts' +import { personFieldsUrl, personsUrl } from '../urls.ts' +import type { SearchPersonsRequest } from '../persons.ts' + +describe('persons', () => { + let mock: MockAdapter + let affinity: Affinity + + beforeEach(() => { + if (!isLiveRun()) { + mock = new MockAdapter(axios, { onNoMatch: 'throwException' }) + } + affinity = new Affinity(apiKey() || 'api_key') + }) + afterEach(() => { + mock?.reset() + }) + + // it('can get a specific person', async (t) => { + // const person_id = 64779194 + // mock?.onGet(personsUrl(person_id)).reply( + // 200, + // await getRawFixture('persons/get.raw.response.json'), + // ) + // const res = await affinity.persons.get({ person_id }) + // await assertSnapshot(t, res) + // }) + + it('can search for persons', async (t) => { + const request = { term: 'joscha' } + mock?.onGet(personsUrl(), { params: request }).reply( + 200, + await getRawFixture('persons/search.raw.response.json'), + ) + const res = await affinity.persons.search(request) + await assertSnapshot(t, res) + }) + + // it('can search for persons with the appropriate dates', async (t) => { + // const myDate = new Date(1717428411010) + // const request: SearchPersonsRequest = { + // min_first_email_date: myDate, + // term: 'joscha', + // } + // mock?.onGet(personsUrl(), { + // params: { + // term: request.term, + // min_first_email_date: myDate.toISOString(), + // }, + // }).reply( + // 200, + // await getRawFixture('persons/search.raw.response.json'), + // ) + // const res = await affinity.persons.search(request) + // await assertSnapshot(t, res) + // }) + + // it('can create a new person', async (t) => { + // const data = { + // name: 'Acme Corporation', + // domain: 'acme.co', + // person_ids: [38706], + // } + // mock?.onPost(personsUrl()).reply( + // 201, + // await getRawFixture('persons/create.raw.response.json'), + // ) + // const res = await affinity.persons.create(data) + // await assertSnapshot(t, res) + // }) + + // it('can update an person', async (t) => { + // const data = { + // person_id: 120611418, + // name: 'Acme Corp.', + // person_ids: [38706, 89734], + // } + // mock?.onPut(personsUrl(data.person_id)).reply( + // 200, + // await getRawFixture('persons/update.raw.response.json'), + // ) + // const res = await affinity.persons.update(data) + // await assertSnapshot(t, res) + // }) + + // it('can delete an person', async (t) => { + // const person_id = 120611418 + // mock?.onDelete(personsUrl(person_id)).reply(200, { + // success: true, + // }) + // const res = await affinity.persons.delete({ person_id }) + // await assertSnapshot(t, res) + // }) + + // it('can get global person fields', async (t) => { + // mock?.onGet(personFieldsUrl()).reply( + // 200, + // await getRawFixture('persons/get_fields.raw.response.json'), + // ) + // const res = await affinity.persons.getFields() + // await assertSnapshot(t, res) + // }) + + // it('iterates over all persons', async (t) => { + // const params: SearchPersonsRequest = { + // term: 'fridel', + // page_size: 1, + // } + + // { + // // set up pages sequentially, each referencing the one after + // const { default: pages } = await import( + // './fixtures/persons/paginated.iterator.combined.response.json', + // { + // with: { + // type: 'json', + // }, + // } + // ) + + // pages.forEach((page, i) => { + // const { next_page_token: previous_page_token } = pages[i - 1] || + // {} + // const data: SearchPersonsRequest = { + // ...params, + // } + // if (previous_page_token) { + // data.page_token = previous_page_token + // } + // // console.log('Setting up page', params, page.list_entries) + // mock?.onGet(personsUrl(), { + // params: data, + // }).reply( + // 200, + // page, + // ) + // }) + // } + + // let page = 0 + // for await ( + // const entries of affinity.persons.searchIterator(params) + // ) { + // await assertSnapshot(t, entries, { + // name: `page ${++page} of persons`, + // }) + // } + // }) +})