diff --git a/package.json b/package.json index 4522287..acde106 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cobot-zapier", - "version": "2.2.0", + "version": "2.2.1", "description": "", "main": "index.js", "scripts": { diff --git a/src/test/triggers/triggerInvoiceCreated.test.ts b/src/test/triggers/triggerInvoiceCreated.test.ts index ae34bdc..929a30d 100644 --- a/src/test/triggers/triggerInvoiceCreated.test.ts +++ b/src/test/triggers/triggerInvoiceCreated.test.ts @@ -79,6 +79,11 @@ const invoiceResponse: InvoiceApiResponse = { }, }; +const membership = { + id: "membership-1", + email: "test@best.com", +}; + const appTester = createAppTester(App); nock.disableNetConnect(); const trigger = App.triggers[triggerInvoiceCreated.key] as HookTrigger; @@ -106,30 +111,36 @@ describe("triggerInvoiceCreated", () => { const userResponse: UserApiResponse = { included: [{ id: "space-1", attributes: { subdomain: "trial" } }], }; - const scope = nock("https://api.cobot.me"); - scope.get("/user?include=adminOf").reply(200, userResponse); - scope + const api2Scope = nock("https://api.cobot.me"); + const api1Scope = nock("https://trial.cobot.me"); + api2Scope.get("/user?include=adminOf").reply(200, userResponse); + api1Scope.get("/api/memberships/membership-1").reply(200, membership); + api2Scope .get(/\/spaces\/space-1\/invoices/) .reply(200, { data: [invoiceResponse] }); const listRecentEvents = trigger.operation.performList; const results = await appTester(listRecentEvents as any, bundle as any); - + expect(nock.isDone()).toBe(true); expect(results).toStrictEqual([ { ...attributes, id: "1", - membershipId: "membership-1", + membership: { + id: "membership-1", + email: "test@best.com", + }, }, ]); }); it("triggers on new invoice", async () => { const bundle = prepareBundle(); - const scope = nock("https://api.cobot.me"); - scope.get("/invoices/12345").reply(200, { data: invoiceResponse }); - + const api1Scope = nock("https://trial.cobot.me"); + const api2Scope = nock("https://api.cobot.me"); + api2Scope.get("/invoices/12345").reply(200, { data: invoiceResponse }); + api1Scope.get("/api/memberships/membership-1").reply(200, membership); const results = await appTester( triggerInvoiceCreated.operation.perform as any, bundle as any, @@ -141,7 +152,10 @@ describe("triggerInvoiceCreated", () => { { ...attributes, id: "1", - membershipId: "membership-1", + membership: { + id: "membership-1", + email: "test@best.com", + }, }, ]); }); diff --git a/src/triggers/triggerInvoiceCreated.ts b/src/triggers/triggerInvoiceCreated.ts index f157a7c..af0afbb 100644 --- a/src/triggers/triggerInvoiceCreated.ts +++ b/src/triggers/triggerInvoiceCreated.ts @@ -40,9 +40,11 @@ async function parsePayload( bundle: KontentBundle<{}>, ): Promise { const invoiceId = bundle.cleanedRequest.url.split("/").pop(); + const api1MembershipsUrl = + new URL(bundle.cleanedRequest.url).origin + "/api/memberships"; const response = await getInvoiceFromApi2(z, invoiceId); if (response) { - return [apiResponseToInvoiceOutput(response)]; + return [await apiResponseToInvoiceOutput(z, response, api1MembershipsUrl)]; } else { return []; } @@ -69,7 +71,13 @@ const trigger: HookTrigger = { bundle: KontentBundle, ): Promise => { const invoices = await listRecentInvoices(z, bundle); - return invoices.map((invoice) => apiResponseToInvoiceOutput(invoice)); + const subdomain = bundle.inputData.subdomain; + const api1MembershipsUrl = `https://${subdomain}.cobot.me/api/memberships`; + const invoiceOutputPromises = invoices.map((invoice) => + apiResponseToInvoiceOutput(z, invoice, api1MembershipsUrl), + ); + const invoiceOutputs = await Promise.all(invoiceOutputPromises); + return invoiceOutputs; }, sample: invoiceSample, diff --git a/src/types/outputs.d.ts b/src/types/outputs.d.ts index 53d4881..b360184 100644 --- a/src/types/outputs.d.ts +++ b/src/types/outputs.d.ts @@ -58,7 +58,12 @@ export type MembershipOutput = { payment_method_name: string | null; }; +export type InvoiceMembershipOutput = { + id: string; + email: string | null; +}; + export type InvoiceOutput = BaseInvoiceProperties & { - membershipId?: string; id: string; + membership?: InvoiceMembershipOutput; }; diff --git a/src/utils/api-to-output.ts b/src/utils/api-to-output.ts index 2c8738d..7c34fdd 100644 --- a/src/utils/api-to-output.ts +++ b/src/utils/api-to-output.ts @@ -11,7 +11,9 @@ import { MembershipOutput, InvoiceOutput, } from "../types/outputs"; -import { ExternalBookingWithResourceApiResponse } from "./api"; +import { ZObject } from "zapier-platform-core"; +import { get } from "lodash"; +import { ExternalBookingWithResourceApiResponse, apiCallUrl } from "./api"; export function apiResponseToMembershipOutput( membership: MembershipApiResponse, @@ -27,14 +29,28 @@ export function apiResponseToMembershipOutput( }; } -export function apiResponseToInvoiceOutput( +export async function apiResponseToInvoiceOutput( + z: ZObject, invoice: InvoiceApiResponse, -): InvoiceOutput { + api1MembershipsUrl: string, +): Promise { const attributes = invoice.attributes; + const membershipId = get(invoice, "relationships.membership.data.id"); + if (!membershipId) { + return { + ...attributes, + id: invoice.id, + }; + } + const url = `${api1MembershipsUrl}/${membershipId}`; + const membership: MembershipApiResponse = await apiCallUrl(z, url); return { ...attributes, id: invoice.id, - membershipId: invoice.relationships?.membership?.data?.id ?? undefined, + membership: { + id: membershipId, + email: membership.email, + }, }; } diff --git a/src/utils/api.ts b/src/utils/api.ts index b25e7a0..6c12f19 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -16,6 +16,7 @@ import { UserApiResponse, InvoiceApiResponse, } from "../types/api-responses"; +import { InvoiceMembershipOutput } from "../types/outputs"; type Space = { id: string; @@ -244,7 +245,7 @@ export const getInvoiceFromApi2 = async ( if (response.status === 404) { return null; } - return response.data.data as InvoiceApiResponse; + return response.data.data; }; export const getExternalBooking = async ( diff --git a/src/utils/samples.ts b/src/utils/samples.ts index f81b6ea..8bd4598 100644 --- a/src/utils/samples.ts +++ b/src/utils/samples.ts @@ -74,7 +74,10 @@ export const invoiceSample: InvoiceOutput = { taxId: "DE12345", taxIdName: "UID", customerNumber: "100", - membershipId: "c9a99a71ac8df98d29de357180d273d3", + membership: { + id: "14c12f62ac8df98d29de357180d673e1", + email: "joe@doe.com", + }, recipientAddress: { name: "Jane Smith", company: "Acme Inc.",