Skip to content

Commit

Permalink
Changes based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsnxyz committed May 2, 2024
1 parent bd3aa83 commit 0f21fff
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 48 deletions.
14 changes: 5 additions & 9 deletions src/test/triggers/triggerInvoiceCreated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const invoiceResponse: InvoiceApiResponse = {
},
};

const membershipProfile = {
const membership = {
id: "membership-1",
email: "[email protected]",
};
Expand Down Expand Up @@ -114,9 +114,7 @@ describe("triggerInvoiceCreated", () => {
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, membershipProfile);
api1Scope.get("/api/memberships/membership-1").reply(200, membership);
api2Scope
.get(/\/spaces\/space-1\/invoices/)
.reply(200, { data: [invoiceResponse] });
Expand All @@ -129,8 +127,8 @@ describe("triggerInvoiceCreated", () => {
{
...attributes,
id: "1",
membershipId: "membership-1",
membership: {
membershipId: "membership-1",
email: "[email protected]",
},
},
Expand All @@ -142,9 +140,7 @@ describe("triggerInvoiceCreated", () => {
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, membershipProfile);
api1Scope.get("/api/memberships/membership-1").reply(200, membership);
const results = await appTester(
triggerInvoiceCreated.operation.perform as any,
bundle as any,
Expand All @@ -157,9 +153,9 @@ describe("triggerInvoiceCreated", () => {
...attributes,
id: "1",
membership: {
membershipId: "membership-1",
email: "[email protected]",
},
membershipId: "membership-1",
},
]);
});
Expand Down
14 changes: 10 additions & 4 deletions src/triggers/triggerInvoiceCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ async function parsePayload(
bundle: KontentBundle<{}>,
): Promise<InvoiceOutput[]> {
const invoiceId = bundle.cleanedRequest.url.split("/").pop();
const api1MembershipUrl =
const api1MembershipsUrl =
new URL(bundle.cleanedRequest.url).origin + "/api/memberships";
const response = await getInvoiceFromApi2(z, invoiceId, api1MembershipUrl);
const response = await getInvoiceFromApi2(z, invoiceId);
if (response) {
return [apiResponseToInvoiceOutput(response)];
return [await apiResponseToInvoiceOutput(z, response, api1MembershipsUrl)];
} else {
return [];
}
Expand All @@ -71,7 +71,13 @@ const trigger: HookTrigger = {
bundle: KontentBundle<SubscribeBundleInputType>,
): Promise<InvoiceOutput[]> => {
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,
Expand Down
3 changes: 0 additions & 3 deletions src/types/api-responses.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ export type BaseInvoiceProperties = {
payableAmount: string;
paidAmount: string;
totalAmount: Amount;
membership?: {
email: string;
};
};

export type InvoiceApiResponse = {
Expand Down
7 changes: 6 additions & 1 deletion src/types/outputs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ export type MembershipOutput = {
payment_method_name: string | null;
};

export type InvoiceMembershipOutput = {
membershipId: string;
email: string | null;
};

export type InvoiceOutput = BaseInvoiceProperties & {
membershipId?: string;
id: string;
membership?: InvoiceMembershipOutput;
};
24 changes: 20 additions & 4 deletions src/utils/api-to-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
MembershipOutput,
InvoiceOutput,
} from "../types/outputs";
import { ExternalBookingWithResourceApiResponse } from "./api";
import { ZObject } from "zapier-platform-core";
import { get } from "lodash";
import {
ExternalBookingWithResourceApiResponse,
loadMembershipEmailOnInvoice,
} from "./api";

export function apiResponseToMembershipOutput(
membership: MembershipApiResponse,
Expand All @@ -27,14 +32,25 @@ export function apiResponseToMembershipOutput(
};
}

export function apiResponseToInvoiceOutput(
export async function apiResponseToInvoiceOutput(
z: ZObject,
invoice: InvoiceApiResponse,
): InvoiceOutput {
api1MembershipsUrl: string,
): Promise<InvoiceOutput> {
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 = await loadMembershipEmailOnInvoice(z, url, membershipId);
return {
...attributes,
id: invoice.id,
membershipId: invoice.relationships?.membership?.data?.id ?? undefined,
membership,
};
}

Expand Down
36 changes: 10 additions & 26 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
UserApiResponse,
InvoiceApiResponse,
} from "../types/api-responses";
import { InvoiceMembershipOutput } from "../types/outputs";

type Space = {
id: string;
Expand Down Expand Up @@ -163,40 +164,26 @@ export const listRecentInvoices = async (
"filter[to]": to,
},
});
const invoices = response.data.data as InvoiceApiResponse[];
const membershipPromises: Promise<InvoiceApiResponse>[] = [];
const api1MembershipUrl = `https://${subdomain}.cobot.me/api/memberships`;
invoices.forEach((invoice) => {
membershipPromises.push(
loadMembershipEmailOnInvoice(z, api1MembershipUrl, invoice),
);
});
await Promise.all(membershipPromises);
return invoices;
return response.data.data;
};

const loadMembershipEmailOnInvoice = async (
export const loadMembershipEmailOnInvoice = async (
z: ZObject,
api1MembershipUrl: string,
invoice: InvoiceApiResponse,
): Promise<InvoiceApiResponse> => {
const membershipId = get(invoice, "relationships.membership.data.id");
if (!membershipId) {
return invoice;
}
membershipUrl: string,
membershipId: string,
): Promise<InvoiceMembershipOutput> => {
const response = await z.request({
url: `${api1MembershipUrl}/${membershipId}`,
url: membershipUrl,
method: "GET",
headers: {
Accept: "application/vnd.api+json",
},
});
const email = response.data.email;
const membership = {
return {
email,
membershipId,
};
invoice.attributes = { ...invoice.attributes, membership };
return invoice;
};

export const listRecentExternalBookings = async (
Expand Down Expand Up @@ -266,7 +253,6 @@ export type ExternalBookingWithResourceApiResponse =
export const getInvoiceFromApi2 = async (
z: ZObject,
invoiceId: string,
api1MembershipUrl: string,
): Promise<InvoiceApiResponse | null> => {
const response = await z.request({
url: `https://api.cobot.me/invoices/${invoiceId}`,
Expand All @@ -278,9 +264,7 @@ export const getInvoiceFromApi2 = async (
if (response.status === 404) {
return null;
}
const invoice = response.data.data as InvoiceApiResponse;
await loadMembershipEmailOnInvoice(z, api1MembershipUrl, invoice);
return invoice;
return response.data.data;
};

export const getExternalBooking = async (
Expand Down
5 changes: 4 additions & 1 deletion src/utils/samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export const invoiceSample: InvoiceOutput = {
taxId: "DE12345",
taxIdName: "UID",
customerNumber: "100",
membershipId: "c9a99a71ac8df98d29de357180d273d3",
membership: {
membershipId: "14c12f62ac8df98d29de357180d673e1",
email: "[email protected]",
},
recipientAddress: {
name: "Jane Smith",
company: "Acme Inc.",
Expand Down

0 comments on commit 0f21fff

Please sign in to comment.