Skip to content

Commit

Permalink
Handle getEarliestEvent when no bounce events
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Dec 12, 2024
1 parent 0fb2404 commit 168c06b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
18 changes: 18 additions & 0 deletions app/analytics/__tests__/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,24 @@ describe("AnalyticsEngineAPI", () => {
earliestBounce: new Date(mockBounceTimestamp),
});
});

test("returns only earliest event when no bounces found", async () => {
const mockEventTimestamp = "2024-01-01T10:00:00Z";

// Mock responses for both queries
fetch.mockResolvedValueOnce(
createFetchResponse({
ok: true,
data: [{ earliestEvent: mockEventTimestamp, isBounce: 0 }],
}),
);

const result = await api.getEarliestEvents("test-site");
expect(result).toEqual({
earliestEvent: new Date(mockEventTimestamp),
earliestBounce: null,
});
});
});
});

Expand Down
17 changes: 11 additions & 6 deletions app/analytics/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,16 +686,21 @@ export class AnalyticsEngineAPI {

const data = responseData.data;

const earliestEvent = data.filter(
const earliestEvent = data.find(
(row) => row["isBounce"] === 0,
)[0]["earliestEvent"];
const earliestBounce = data.filter(
)?.earliestEvent;

const earliestBounce = data.find(
(row) => row["isBounce"] === 1,
)[0]["earliestEvent"];
)?.earliestEvent;

resolve({
earliestEvent: new Date(earliestEvent),
earliestBounce: new Date(earliestBounce),
earliestEvent: earliestEvent
? new Date(earliestEvent)
: null,
earliestBounce: earliestBounce
? new Date(earliestBounce)
: null,
});
})();
});
Expand Down

0 comments on commit 168c06b

Please sign in to comment.