diff --git a/sample-gladly-webhook-integration/services/__tests__/integration.test.js b/sample-gladly-webhook-integration/services/__tests__/integration.test.js index 966f22d..e4e1553 100644 --- a/sample-gladly-webhook-integration/services/__tests__/integration.test.js +++ b/sample-gladly-webhook-integration/services/__tests__/integration.test.js @@ -16,11 +16,24 @@ describe("integration.js", () => { { id: "profile-1", email: "test@example.com" }, { id: "profile-2", email: "test@example.com" }, ]; + const mockConversations = { + "profile-1": [ + { id: "conv-1", status: "OPEN" }, + { id: "conv-2", status: "CLOSED" }, + ], + "profile-2": [], + }; const getCustomerProfilesSpy = jest .spyOn(gladlyApiClient, "getCustomerProfiles") .mockResolvedValue(mockCustomerProfiles); + const getConversationsSpy = jest + .spyOn(gladlyApiClient, "getConversations") + .mockImplementation(async (customerProfileId) => { + return mockConversations[customerProfileId]; + }); + const attachSummaryToActionItemSpy = jest .spyOn(osanoApiClient, "attachSummaryToActionItem") .mockResolvedValue(undefined); @@ -32,9 +45,23 @@ describe("integration.js", () => { await summarizeData(actionItemId, email); expect(getCustomerProfilesSpy).toHaveBeenCalledWith(email); + expect(getConversationsSpy).toHaveBeenCalledTimes(2); + expect(getConversationsSpy).toHaveBeenCalledWith("profile-1"); + expect(getConversationsSpy).toHaveBeenCalledWith("profile-2"); expect(attachSummaryToActionItemSpy).toHaveBeenCalledWith( actionItemId, - JSON.stringify(mockCustomerProfiles) + JSON.stringify([ + { + id: "profile-1", + email: "test@example.com", + conversations: mockConversations["profile-1"], + }, + { + id: "profile-2", + email: "test@example.com", + conversations: [], + }, + ]) ); expect(markActionItemCompletedSpy).toHaveBeenCalledWith( actionItemId, @@ -42,6 +69,38 @@ describe("integration.js", () => { ); }); + it("should not attach a partial summary or complete the action item when conversations cannot be retrieved", async () => { + const email = "test@example.com"; + const actionItemId = 1; + + jest + .spyOn(gladlyApiClient, "getCustomerProfiles") + .mockResolvedValue([{ id: "profile-1" }]); + + jest + .spyOn(gladlyApiClient, "getConversations") + .mockRejectedValue( + new Error( + "Customer profile-1 has more conversations than returned (Gladly-Limited-Data: true). Manual processing required." + ) + ); + + const attachSummaryToActionItemSpy = jest + .spyOn(osanoApiClient, "attachSummaryToActionItem") + .mockResolvedValue(undefined); + + const markActionItemCompletedSpy = jest + .spyOn(osanoApiClient, "markActionItemCompleted") + .mockResolvedValue(undefined); + + await expect(summarizeData(actionItemId, email)).rejects.toThrow( + "Manual processing required." + ); + + expect(attachSummaryToActionItemSpy).not.toHaveBeenCalled(); + expect(markActionItemCompletedSpy).not.toHaveBeenCalled(); + }); + it("should mark action item as completed when no customer profiles are found", async () => { const email = "test@example.com"; const actionItemId = 1; diff --git a/sample-gladly-webhook-integration/services/integration.js b/sample-gladly-webhook-integration/services/integration.js index fda8f65..728db46 100644 --- a/sample-gladly-webhook-integration/services/integration.js +++ b/sample-gladly-webhook-integration/services/integration.js @@ -21,8 +21,15 @@ export const summarizeData = async (actionItemId, email) => { return; } + // Retrieve the conversations for each customer profile so they are included in the summary + const summary = []; + for (const customerProfile of customerProfiles) { + const conversations = await getConversations(customerProfile.id); + summary.push({ ...customerProfile, conversations }); + } + // Attach the summary file to the action item using the Osano Action Items API - const fileContent = JSON.stringify(customerProfiles); + const fileContent = JSON.stringify(summary); await attachSummaryToActionItem(actionItemId, fileContent); // Mark the action item as completed using the Osano Action Items API