From 00475a5ad6010bcfa884ae1ce66c06e8534dc0f9 Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Fri, 26 Jul 2024 09:40:01 +0100 Subject: [PATCH] fix: persons ednpoint; dates search --- .../tests/__snapshots__/persons_test.ts.snap | 45 +++++++++++++++++++ .../search_with_dates.raw.response.json | 30 +++++++++++++ src/v1/tests/organizations_test.ts | 2 + src/v1/tests/persons_test.ts | 38 ++++++++-------- ...transform_interaction_date_response_raw.ts | 18 +++++--- 5 files changed, 110 insertions(+), 23 deletions(-) create mode 100644 src/v1/tests/fixtures/persons/search_with_dates.raw.response.json diff --git a/src/v1/tests/__snapshots__/persons_test.ts.snap b/src/v1/tests/__snapshots__/persons_test.ts.snap index 5bfe43c..9976da8 100644 --- a/src/v1/tests/__snapshots__/persons_test.ts.snap +++ b/src/v1/tests/__snapshots__/persons_test.ts.snap @@ -85,6 +85,51 @@ snapshot[`persons > can search for persons 1`] = ` } `; +snapshot[`persons > can search for persons with the appropriate dates 1`] = ` +{ + persons: [ + { + emails: [ + "j@f.com", + ], + first_name: "Joscha", + id: 12345678, + interaction_dates: { + first_email_date: 2021-05-31T16:00:37.000Z, + first_event_date: 2021-05-20T06:30:00.000Z, + last_chat_message_date: null, + last_email_date: 2022-05-12T08:01:31.000Z, + last_event_date: 2022-07-08T07:00:00.000Z, + last_interaction_date: 2022-07-08T07:00:00.000Z, + next_event_date: null, + }, + interactions: { + first_email: { + date: 2021-05-31T16:00:37.000Z, + }, + first_event: { + date: 2021-05-20T06:30:00.000Z, + }, + last_chat_message: null, + last_email: { + date: 2022-05-12T08:01:31.000Z, + }, + last_event: { + date: 2022-07-08T07:00:00.000Z, + }, + last_interaction: { + date: 2022-07-08T07:00:00.000Z, + }, + next_event: null, + }, + last_name: "Feth", + primary_email: "j@f.com", + type: 0, + }, + ], +} +`; + snapshot[`persons > can create a new person 1`] = ` { emails: [ diff --git a/src/v1/tests/fixtures/persons/search_with_dates.raw.response.json b/src/v1/tests/fixtures/persons/search_with_dates.raw.response.json new file mode 100644 index 0000000..1834110 --- /dev/null +++ b/src/v1/tests/fixtures/persons/search_with_dates.raw.response.json @@ -0,0 +1,30 @@ +{ + "persons": [ + { + "id": 12345678, + "type": 0, + "first_name": "Joscha", + "last_name": "Feth", + "primary_email": "j@f.com", + "emails": ["j@f.com"], + "interaction_dates": { + "first_email_date": "2021-05-31T09:00:37.000-07:00", + "first_event_date": "2021-05-19T23:30:00.000-07:00", + "last_email_date": "2022-05-12T01:01:31.000-07:00", + "last_event_date": "2022-07-08T00:00:00.000-07:00", + "last_chat_message_date": null, + "last_interaction_date": "2022-07-08T00:00:00.000-07:00", + "next_event_date": null + }, + "interactions": { + "first_email": { "date": "2021-05-31T09:00:37.000-07:00" }, + "last_email": { "date": "2022-05-12T01:01:31.000-07:00" }, + "first_event": { "date": "2021-05-19T23:30:00.000-07:00" }, + "next_event": null, + "last_event": { "date": "2022-07-08T00:00:00.000-07:00" }, + "last_chat_message": null, + "last_interaction": { "date": "2022-07-08T00:00:00.000-07:00" } + } + } + ] +} diff --git a/src/v1/tests/organizations_test.ts b/src/v1/tests/organizations_test.ts index 3e45164..33b5754 100644 --- a/src/v1/tests/organizations_test.ts +++ b/src/v1/tests/organizations_test.ts @@ -48,11 +48,13 @@ describe('organizations', () => { const request: SearchOrganizationsRequest = { min_first_email_date: myDate, term: 'affinity', + with_interaction_dates: true, } mock?.onGet(organizationsUrl(), { params: { term: request.term, min_first_email_date: myDate.toISOString(), + with_interaction_dates: true, }, }).reply( 200, diff --git a/src/v1/tests/persons_test.ts b/src/v1/tests/persons_test.ts index aa911e5..a6d6756 100644 --- a/src/v1/tests/persons_test.ts +++ b/src/v1/tests/persons_test.ts @@ -43,24 +43,26 @@ describe('persons', () => { 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 search for persons with the appropriate dates', async (t) => { + const myDate = new Date(0) + const request: SearchPersonsRequest = { + min_first_email_date: myDate, + term: 'Joscha', + with_interaction_dates: true, + } + mock?.onGet(personsUrl(), { + params: { + term: request.term, + min_first_email_date: myDate.toISOString(), + with_interaction_dates: true, + }, + }).reply( + 200, + await getRawFixture('persons/search_with_dates.raw.response.json'), + ) + const res = await affinity.persons.search(request) + await assertSnapshot(t, res) + }) it('can create a new person', async (t) => { const data = { diff --git a/src/v1/transform_interaction_date_response_raw.ts b/src/v1/transform_interaction_date_response_raw.ts index cf0ea62..18928ae 100644 --- a/src/v1/transform_interaction_date_response_raw.ts +++ b/src/v1/transform_interaction_date_response_raw.ts @@ -22,17 +22,25 @@ export function transformInteractionDateResponseRaw< if (interaction_dates) { dates.interaction_dates = Object.fromEntries( Object.entries(interaction_dates).map( - ([key, value]) => [key, new Date(value)], + ([key, value]) => [ + key, + value ? new Date(value) : null, + ], ), ) as Record } if (interactions) { dates.interactions = Object.fromEntries( Object.entries(interactions).map( - ([key, value]) => [key, { - ...value, - date: new Date(value.date), - }], + ([key, value]) => [ + key, + value + ? { + ...value, + date: new Date(value.date), + } + : null, + ], ), ) as Record }