Skip to content

Commit

Permalink
clean up booking output
Browse files Browse the repository at this point in the history
- flatten data, add types
- remove code duplication
  • Loading branch information
langalex committed Dec 6, 2023
1 parent 1583188 commit 7a73458
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 80 deletions.
51 changes: 16 additions & 35 deletions src/triggers/triggerBookingCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
} from "../utils/api";
import { getSubdomainField } from "../fields/getSudomainsField";
import { SubscribeBundleInputType } from "../types/subscribeType";
import { bookingSample } from "../utils/samples";
import { BookingOutput } from "../types/outputs";
import { apiResponseToBookingOutput } from "../utils/api-to-output";

const hookLabel = "Booking Created";
const event = "created_booking";
Expand All @@ -30,16 +33,16 @@ async function unsubscribeHookExecute(
return unsubscribeHook(z, bundle, webhook?.id ?? "");
}

async function parsePayload(z: ZObject, bundle: KontentBundle<{}>) {
var booking;
async function parsePayload(
z: ZObject,
bundle: KontentBundle<{}>,
): Promise<BookingOutput[]> {
if (bundle.cleanedRequest) {
booking = await apiCallUrl(z, bundle.cleanedRequest.url);
booking.from = new Date(booking.from).toISOString();
booking.to = new Date(booking.to).toISOString();
booking.created_at = new Date(booking.created_at).toISOString();
booking.updated_at = new Date(booking.updated_at).toISOString();
const booking = await apiCallUrl(z, bundle.cleanedRequest.url);
return [apiResponseToBookingOutput(booking)];
} else {
return [];
}
return [booking];
}

export default {
Expand All @@ -58,35 +61,13 @@ export default {
performUnsubscribe: unsubscribeHookExecute,

perform: parsePayload,
performList: (
performList: async (
z: ZObject,
bundle: KontentBundle<SubscribeBundleInputType>,
) => listRecentBookings(z, bundle),

sample: {
id: "d58b612aaa62619aae546dd336587eb2",
from: "2012-04-12T12:00:00.000Z",
to: "2012-04-12T18:00:00.000Z",
tax_rate: "20.0",
title: "test booking",
resource: {
name: "Meeting Room",
id: "12345",
},
price: 10.0,
has_custom_price: false,
currency: "EUR",
accounting_code: "B1",
membership: {
id: "123498y452346",
name: "John Doe",
},
url: "https://co-up.cobot.me/api/bookings/20723075",
can_change: true,
comments: "coffee please",
units: 1,
created_at: "2012-04-12T12:00:00.000Z",
updated_at: "2012-04-12T12:00:00.000Z",
): Promise<BookingOutput[]> => {
const apiBookings = await listRecentBookings(z, bundle);
return apiBookings.map((b) => apiResponseToBookingOutput(b));
},
sample: bookingSample,
},
} as const;
51 changes: 16 additions & 35 deletions src/triggers/triggerBookingWillBegin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
} from "../utils/api";
import { SubscribeBundleInputType } from "../types/subscribeType";
import { getSubdomainField } from "../fields/getSudomainsField";
import { apiResponseToBookingOutput } from "../utils/api-to-output";
import { BookingOutput } from "../types/outputs";
import { bookingSample } from "../utils/samples";

const hookLabel = "Booking Will Begin";
const event = "booking_will_begin";
Expand All @@ -30,16 +33,16 @@ async function unsubscribeHookExecute(
return unsubscribeHook(z, bundle, webhook?.id ?? "");
}

async function parsePayload(z: ZObject, bundle: KontentBundle<{}>) {
var booking;
async function parsePayload(
z: ZObject,
bundle: KontentBundle<{}>,
): Promise<BookingOutput[]> {
if (bundle.cleanedRequest) {
booking = await apiCallUrl(z, bundle.cleanedRequest.url);
booking.from = new Date(booking.from).toISOString();
booking.to = new Date(booking.to).toISOString();
booking.created_at = new Date(booking.created_at).toISOString();
booking.updated_at = new Date(booking.updated_at).toISOString();
const booking = await apiCallUrl(z, bundle.cleanedRequest.url);
return [apiResponseToBookingOutput(booking)];
} else {
return [];
}
return [booking];
}

export default {
Expand All @@ -58,35 +61,13 @@ export default {
performUnsubscribe: unsubscribeHookExecute,

perform: parsePayload,
performList: (
performList: async (
z: ZObject,
bundle: KontentBundle<SubscribeBundleInputType>,
) => listRecentBookings(z, bundle),

sample: {
id: "d58b612aaa62619aae546dd336587eb2",
from: "2012-04-12T12:00:00.000Z",
to: "2012-04-12T12:00:00.000Z",
tax_rate: "20.0",
title: "test booking",
resource: {
name: "Meeting Room",
id: "12345",
},
price: 10.0,
has_custom_price: false,
currency: "EUR",
accounting_code: "B1",
membership: {
id: "123498y452346",
name: "John Doe",
},
url: "https://co-up.cobot.me/api/bookings/20723075",
can_change: true,
comments: "coffee please",
units: 1,
created_at: "2012-04-12T12:00:00.000Z",
updated_at: "2012-04-12T12:00:00.000Z",
): Promise<BookingOutput[]> => {
const apiBookings = await listRecentBookings(z, bundle);
return apiBookings.map((b) => apiResponseToBookingOutput(b));
},
sample: bookingSample,
},
} as const;
19 changes: 19 additions & 0 deletions src/types/api-responses.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ export type MembershipApiResponse = {
starts_at: string;
canceled_at: string | null;
};

export type BookingApiResponse = {
id: string;
from: string;
to: string;
title: string | null;
resource: {
name: string;
id: string;
};
membership: {
id: string;
name: string;
} | null;
comments: string | null;
price: number;
currency: string;
units: number;
};
13 changes: 13 additions & 0 deletions src/types/outputs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ export type MembershipOutput = {
plan_name: string;
payment_method_name: string | null;
};

export type BookingOutput = {
id: string;
from: string;
to: string;
title: string | null;
resource_name: string;
price: string;
currency: string;
comments: string | null;
units: number;
member_name: string | null;
};
27 changes: 27 additions & 0 deletions src/utils/api-to-output.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
BookingApiResponse,
MembershipApiResponse,
} from "../types/api-responses";
import { BookingOutput, MembershipOutput } from "../types/outputs";

export function apiResponseToMembershipOutput(
membership: MembershipApiResponse,
): MembershipOutput {
Expand All @@ -11,3 +17,24 @@ export function apiResponseToMembershipOutput(
payment_method_name: membership.payment_method?.name ?? null,
};
}

export function apiResponseToBookingOutput(
booking: BookingApiResponse,
): BookingOutput {
return {
id: booking.id,
from: timeToIso8601(booking.from),
to: timeToIso8601(booking.to),
title: booking.title,
resource_name: booking.resource.name,
price: booking.price.toString(),
currency: booking.currency,
comments: booking.comments,
units: booking.units,
member_name: booking.membership?.name ?? null,
};
}

const timeToIso8601 = (time: string): string => {
return new Date(time).toISOString();
};
15 changes: 6 additions & 9 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
import { InputData as ActivityInputData } from "../creates/activity";
import { get } from "lodash";
import { DateTime } from "luxon";
import { MembershipApiResponse } from "../types/api-responses";
import {
BookingApiResponse,
MembershipApiResponse,
} from "../types/api-responses";

export const subscribeHook = async (
z: ZObject,
Expand Down Expand Up @@ -51,7 +54,7 @@ export const apiCallUrl = async (z: ZObject, url: string) => {
export const listRecentBookings = async (
z: ZObject,
bundle: KontentBundle<SubscribeBundleInputType>,
) => {
): Promise<BookingApiResponse[]> => {
const url = `https://${bundle.inputData.subdomain}.cobot.me/api/bookings`;
const [from, to] = getDateRange();
try {
Expand All @@ -64,13 +67,7 @@ export const listRecentBookings = async (
limit: 1,
},
});
return response.data.map((booking) => {
booking.from = new Date(booking.from).toISOString();
booking.to = new Date(booking.to).toISOString();
booking.created_at = new Date(booking.created_at).toISOString();
booking.updated_at = new Date(booking.updated_at).toISOString();
return booking;
});
return response.data;
} catch (error) {
return [];
}
Expand Down
16 changes: 15 additions & 1 deletion src/utils/samples.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { MembershipOutput } from "../types/outputs";
import exp = require("constants");
import { BookingOutput, MembershipOutput } from "../types/outputs";

export const bookingSample: BookingOutput = {
id: "d58b612aaa62619aae546dd336587eb2",
from: "2012-04-12T12:00:00.000Z",
to: "2012-04-12T18:00:00.000Z",
title: "test booking",
resource_name: "Meeting Room",
price: "10.0",
currency: "EUR",
member_name: "John Doe",
comments: "coffee please",
units: 1,
};

export const membershipSample: MembershipOutput = {
id: "003b37a3-f205-5d9e-9caf-c4ca612075d4",
Expand Down

0 comments on commit 7a73458

Please sign in to comment.