Skip to content

Commit e043a5e

Browse files
authored
Async import of the appStore packages (#8087)
1 parent bff28e5 commit e043a5e

File tree

12 files changed

+82
-107
lines changed

12 files changed

+82
-107
lines changed

packages/app-store/_utils/getCalendar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import appStore from "..";
66

77
const log = logger.getChildLogger({ prefix: ["CalendarManager"] });
88

9-
export const getCalendar = (credential: CredentialPayload | null): Calendar | null => {
9+
export const getCalendar = async (credential: CredentialPayload | null): Promise<Calendar | null> => {
1010
if (!credential || !credential.key) return null;
1111
let { type: calendarType } = credential;
1212
if (calendarType?.endsWith("_other_calendar")) {
1313
calendarType = calendarType.split("_other_calendar")[0];
1414
}
15-
const calendarApp = appStore[calendarType.split("_").join("") as keyof typeof appStore];
15+
const calendarApp = await appStore[calendarType.split("_").join("") as keyof typeof appStore];
1616
if (!(calendarApp && "lib" in calendarApp && "CalendarService" in calendarApp.lib)) {
1717
log.warn(`calendar of type ${calendarType} is not implemented`);
1818
return null;

packages/app-store/index.ts

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,33 @@
1-
// import * as example from "./_example";
2-
import * as applecalendar from "./applecalendar";
3-
import * as caldavcalendar from "./caldavcalendar";
4-
import * as closecom from "./closecom";
5-
import * as dailyvideo from "./dailyvideo";
6-
import * as exchange2013calendar from "./exchange2013calendar";
7-
import * as exchange2016calendar from "./exchange2016calendar";
8-
import * as exchangecalendar from "./exchangecalendar";
9-
import * as facetime from "./facetime";
10-
import * as giphy from "./giphy";
11-
import * as googlecalendar from "./googlecalendar";
12-
import * as googlevideo from "./googlevideo";
13-
import * as hubspot from "./hubspot";
14-
import * as huddle01video from "./huddle01video";
15-
import * as jitsivideo from "./jitsivideo";
16-
import * as larkcalendar from "./larkcalendar";
17-
import * as office365calendar from "./office365calendar";
18-
import * as office365video from "./office365video";
19-
import * as plausible from "./plausible";
20-
import * as salesforce from "./salesforce";
21-
import * as sendgrid from "./sendgrid";
22-
import * as stripepayment from "./stripepayment";
23-
import * as sylapsvideo from "./sylapsvideo";
24-
import * as tandemvideo from "./tandemvideo";
25-
import * as vital from "./vital";
26-
import * as wipemycalother from "./wipemycalother";
27-
import * as zapier from "./zapier";
28-
import * as zohocrm from "./zohocrm";
29-
import * as zoomvideo from "./zoomvideo";
30-
311
const appStore = {
32-
// example,
33-
applecalendar,
34-
caldavcalendar,
35-
closecom,
36-
dailyvideo,
37-
googlecalendar,
38-
googlevideo,
39-
hubspot,
40-
huddle01video,
41-
jitsivideo,
42-
sylapsvideo,
43-
larkcalendar,
44-
office365calendar,
45-
office365video,
46-
plausible,
47-
salesforce,
48-
zohocrm,
49-
sendgrid,
50-
stripepayment,
51-
tandemvideo,
52-
vital,
53-
zoomvideo,
54-
wipemycalother,
55-
giphy,
56-
zapier,
57-
exchange2013calendar,
58-
exchange2016calendar,
59-
exchangecalendar,
60-
facetime,
2+
// example: import("./example"),
3+
applecalendar: import("./applecalendar"),
4+
caldavcalendar: import("./caldavcalendar"),
5+
closecom: import("./closecom"),
6+
dailyvideo: import("./dailyvideo"),
7+
googlecalendar: import("./googlecalendar"),
8+
googlevideo: import("./googlevideo"),
9+
hubspot: import("./hubspot"),
10+
huddle01video: import("./huddle01video"),
11+
jitsivideo: import("./jitsivideo"),
12+
larkcalendar: import("./larkcalendar"),
13+
office365calendar: import("./office365calendar"),
14+
office365video: import("./office365video"),
15+
plausible: import("./plausible"),
16+
salesforce: import("./salesforce"),
17+
zohocrm: import("./zohocrm"),
18+
sendgrid: import("./sendgrid"),
19+
stripepayment: import("./stripepayment"),
20+
tandemvideo: import("./tandemvideo"),
21+
vital: import("./vital"),
22+
zoomvideo: import("./zoomvideo"),
23+
wipemycalother: import("./wipemycalother"),
24+
giphy: import("./giphy"),
25+
zapier: import("./zapier"),
26+
exchange2013calendar: import("./exchange2013calendar"),
27+
exchange2016calendar: import("./exchange2016calendar"),
28+
exchangecalendar: import("./exchangecalendar"),
29+
facetime: import("./facetime"),
30+
sylapsvideo: import("./sylapsvideo"),
6131
};
6232

6333
export default appStore;

packages/app-store/vital/lib/reschedule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
122122
(ref) => !!credentialsMap.get(ref.type)
123123
);
124124
try {
125-
bookingRefsFiltered.forEach((bookingRef) => {
125+
bookingRefsFiltered.forEach(async (bookingRef) => {
126126
if (bookingRef.uid) {
127127
if (bookingRef.type.endsWith("_calendar")) {
128-
const calendar = getCalendar(credentialsMap.get(bookingRef.type));
128+
const calendar = await getCalendar(credentialsMap.get(bookingRef.type));
129129
return calendar?.deleteEvent(bookingRef.uid, builder.calendarEvent);
130130
} else if (bookingRef.type.endsWith("_video")) {
131131
return deleteMeeting(credentialsMap.get(bookingRef.type), bookingRef.uid);

packages/app-store/wipemycalother/lib/reschedule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
122122
(ref) => !!credentialsMap.get(ref.type)
123123
);
124124
try {
125-
bookingRefsFiltered.forEach((bookingRef) => {
125+
bookingRefsFiltered.forEach(async (bookingRef) => {
126126
if (bookingRef.uid) {
127127
if (bookingRef.type.endsWith("_calendar")) {
128-
const calendar = getCalendar(credentialsMap.get(bookingRef.type));
128+
const calendar = await getCalendar(credentialsMap.get(bookingRef.type));
129129
return calendar?.deleteEvent(bookingRef.uid, builder.calendarEvent);
130130
} else if (bookingRef.type.endsWith("_video")) {
131131
return deleteMeeting(credentialsMap.get(bookingRef.type), bookingRef.uid);

packages/core/CalendarManager.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const getCalendarCredentials = (credentials: Array<CredentialPayload>) =>
2828
const calendar = getCalendar(credential);
2929
return app.variant === "calendar" ? [{ integration: app, credential, calendar }] : [];
3030
});
31+
3132
return credentials.length ? credentials : [];
3233
});
3334

@@ -43,8 +44,8 @@ export const getConnectedCalendars = async (
4344
const connectedCalendars = await Promise.all(
4445
calendarCredentials.map(async (item) => {
4546
try {
46-
const { calendar, integration, credential } = item;
47-
47+
const { integration, credential } = item;
48+
const calendar = await item.calendar;
4849
// Don't leak credentials to the client
4950
const credentialId = credential.id;
5051
if (!calendar) {
@@ -138,7 +139,7 @@ export const getCachedResults = async (
138139
selectedCalendars: SelectedCalendar[]
139140
): Promise<EventBusyDate[][]> => {
140141
const calendarCredentials = withCredentials.filter((credential) => credential.type.endsWith("_calendar"));
141-
const calendars = calendarCredentials.map((credential) => getCalendar(credential));
142+
const calendars = await Promise.all(calendarCredentials.map((credential) => getCalendar(credential)));
142143
performance.mark("getBusyCalendarTimesStart");
143144
const results = calendars.map(async (c, i) => {
144145
/** Filter out nulls */
@@ -229,7 +230,7 @@ export const createEvent = async (
229230
calEvent: CalendarEvent
230231
): Promise<EventResult<NewCalendarEventType>> => {
231232
const uid: string = getUid(calEvent);
232-
const calendar = getCalendar(credential);
233+
const calendar = await getCalendar(credential);
233234
let success = true;
234235
let calError: string | undefined = undefined;
235236

@@ -280,7 +281,7 @@ export const updateEvent = async (
280281
externalCalendarId: string | null
281282
): Promise<EventResult<NewCalendarEventType>> => {
282283
const uid = getUid(calEvent);
283-
const calendar = getCalendar(credential);
284+
const calendar = await getCalendar(credential);
284285
let success = false;
285286
let calError: string | undefined = undefined;
286287
let calWarnings: string[] | undefined = [];
@@ -326,12 +327,12 @@ export const updateEvent = async (
326327
};
327328
};
328329

329-
export const deleteEvent = (
330+
export const deleteEvent = async (
330331
credential: CredentialPayload,
331332
uid: string,
332333
event: CalendarEvent
333334
): Promise<unknown> => {
334-
const calendar = getCalendar(credential);
335+
const calendar = await getCalendar(credential);
335336
if (calendar) {
336337
return calendar.deleteEvent(uid, event);
337338
}

packages/core/EventManager.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ export default class EventManager {
485485
id: oldCalendarEvent.credentialId,
486486
},
487487
});
488-
const calendar = getCalendar(calendarCredential);
489-
488+
const calendar = await getCalendar(calendarCredential);
490489
await calendar?.deleteEvent(oldCalendarEvent.uid, event, oldCalendarEvent.externalCalendarId);
491490
}
492491
}

packages/core/videoClient.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,25 @@ const log = logger.getChildLogger({ prefix: ["[lib] videoClient"] });
1818
const translator = short();
1919

2020
// factory
21-
const getVideoAdapters = (withCredentials: CredentialPayload[]): VideoApiAdapter[] =>
22-
withCredentials.reduce<VideoApiAdapter[]>((acc, cred) => {
21+
const getVideoAdapters = async (withCredentials: CredentialPayload[]): Promise<VideoApiAdapter[]> => {
22+
const videoAdapters: VideoApiAdapter[] = [];
23+
24+
for (const cred of withCredentials) {
2325
const appName = cred.type.split("_").join(""); // Transform `zoom_video` to `zoomvideo`;
24-
const app = appStore[appName as keyof typeof appStore];
26+
const app = await appStore[appName as keyof typeof appStore];
27+
2528
if (app && "lib" in app && "VideoApiAdapter" in app.lib) {
2629
const makeVideoApiAdapter = app.lib.VideoApiAdapter as VideoApiAdapterFactory;
2730
const videoAdapter = makeVideoApiAdapter(cred);
28-
acc.push(videoAdapter);
29-
return acc;
31+
videoAdapters.push(videoAdapter);
3032
}
31-
return acc;
32-
}, []);
33+
}
34+
35+
return videoAdapters;
36+
};
3337

34-
const getBusyVideoTimes = (withCredentials: CredentialPayload[]) =>
35-
Promise.all(getVideoAdapters(withCredentials).map((c) => c?.getAvailability())).then((results) =>
38+
const getBusyVideoTimes = async (withCredentials: CredentialPayload[]) =>
39+
Promise.all((await getVideoAdapters(withCredentials)).map((c) => c?.getAvailability())).then((results) =>
3640
results.reduce((acc, availability) => acc.concat(availability), [] as (EventBusyDate | undefined)[])
3741
);
3842

@@ -45,7 +49,7 @@ const createMeeting = async (credential: CredentialWithAppName, calEvent: Calend
4549
);
4650
}
4751

48-
const videoAdapters = getVideoAdapters([credential]);
52+
const videoAdapters = await getVideoAdapters([credential]);
4953
const [firstVideoAdapter] = videoAdapters;
5054
let createdMeeting;
5155
let returnObject: {
@@ -104,7 +108,7 @@ const updateMeeting = async (
104108

105109
let success = true;
106110

107-
const [firstVideoAdapter] = getVideoAdapters([credential]);
111+
const [firstVideoAdapter] = await getVideoAdapters([credential]);
108112
const updatedMeeting =
109113
credential && bookingRef
110114
? await firstVideoAdapter?.updateMeeting(bookingRef, calEvent).catch(async (e) => {
@@ -135,9 +139,9 @@ const updateMeeting = async (
135139
};
136140
};
137141

138-
const deleteMeeting = (credential: CredentialPayload, uid: string): Promise<unknown> => {
142+
const deleteMeeting = async (credential: CredentialPayload, uid: string): Promise<unknown> => {
139143
if (credential) {
140-
const videoAdapter = getVideoAdapters([credential])[0];
144+
const videoAdapter = (await getVideoAdapters([credential]))[0];
141145
// There are certain video apps with no video adapter defined. e.g. riverby,whereby
142146
if (videoAdapter) {
143147
return videoAdapter.deleteMeeting(uid);
@@ -155,7 +159,7 @@ const createMeetingWithCalVideo = async (calEvent: CalendarEvent) => {
155159
} catch (e) {
156160
return;
157161
}
158-
const [videoAdapter] = getVideoAdapters([
162+
const [videoAdapter] = await getVideoAdapters([
159163
{
160164
id: 0,
161165
appId: "daily-video",
@@ -178,7 +182,7 @@ const getRecordingsOfCalVideoByRoomName = async (
178182
console.error("Error: Cal video provider is not installed.");
179183
return;
180184
}
181-
const [videoAdapter] = getVideoAdapters([
185+
const [videoAdapter] = await getVideoAdapters([
182186
{
183187
id: 0,
184188
appId: "daily-video",
@@ -199,7 +203,7 @@ const getDownloadLinkOfCalVideoByRecordingId = async (recordingId: string) => {
199203
console.error("Error: Cal video provider is not installed.");
200204
return;
201205
}
202-
const [videoAdapter] = getVideoAdapters([
206+
const [videoAdapter] = await getVideoAdapters([
203207
{
204208
id: 0,
205209
appId: "daily-video",

packages/features/bookings/lib/handleCancelBooking.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ async function handler(req: CustomRequest) {
240240
integrationsToDelete.push(deleteMeeting(credential, reference.uid));
241241
}
242242
if (reference.type.includes("_calendar")) {
243-
const calendar = getCalendar(credential);
243+
const calendar = await getCalendar(credential);
244244
if (calendar) {
245245
integrationsToDelete.push(
246246
calendar?.deleteEvent(reference.uid, evt, reference.externalCalendarId)
@@ -262,7 +262,7 @@ async function handler(req: CustomRequest) {
262262
);
263263
}
264264
if (reference.type.includes("_calendar")) {
265-
const calendar = getCalendar(credential);
265+
const calendar = await getCalendar(credential);
266266
if (calendar) {
267267
integrationsToDelete.push(
268268
calendar?.updateEvent(reference.uid, updatedEvt, reference.externalCalendarId)
@@ -449,7 +449,7 @@ async function handler(req: CustomRequest) {
449449
(credential) => credential.id === credentialId
450450
);
451451
if (calendarCredential) {
452-
const calendar = getCalendar(calendarCredential);
452+
const calendar = await getCalendar(calendarCredential);
453453
if (
454454
bookingToDelete.eventType?.recurringEvent &&
455455
bookingToDelete.recurringEventId &&
@@ -458,7 +458,7 @@ async function handler(req: CustomRequest) {
458458
bookingToDelete.user.credentials
459459
.filter((credential) => credential.type.endsWith("_calendar"))
460460
.forEach(async (credential) => {
461-
const calendar = getCalendar(credential);
461+
const calendar = await getCalendar(credential);
462462
for (const updBooking of updatedBookings) {
463463
const bookingRef = updBooking.references.find((ref) => ref.type.includes("_calendar"));
464464
if (bookingRef) {
@@ -474,12 +474,13 @@ async function handler(req: CustomRequest) {
474474
}
475475
} else {
476476
// For bookings made before the refactor we go through the old behaviour of running through each calendar credential
477-
bookingToDelete.user.credentials
478-
.filter((credential) => credential.type.endsWith("_calendar"))
479-
.forEach((credential) => {
480-
const calendar = getCalendar(credential);
481-
apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId) as Promise<unknown>);
482-
});
477+
const calendarCredentials = bookingToDelete.user.credentials.filter((credential) =>
478+
credential.type.endsWith("_calendar")
479+
);
480+
for (const credential of calendarCredentials) {
481+
const calendar = await getCalendar(credential);
482+
apiDeletes.push(calendar?.deleteEvent(uid, evt, externalCalendarId) as Promise<unknown>);
483+
}
483484
}
484485
}
485486

@@ -585,7 +586,7 @@ async function handler(req: CustomRequest) {
585586
}
586587

587588
// Posible to refactor TODO:
588-
const paymentApp = appStore[paymentAppCredential?.app?.dirName as keyof typeof appStore];
589+
const paymentApp = await appStore[paymentAppCredential?.app?.dirName as keyof typeof appStore];
589590
if (!(paymentApp && "lib" in paymentApp && "PaymentService" in paymentApp.lib)) {
590591
console.warn(`payment App service of type ${paymentApp} is not implemented`);
591592
return null;

packages/features/bookings/lib/handleNewBooking.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ async function handler(
885885
integrationsToDelete.push(deleteMeeting(credential, reference.uid));
886886
}
887887
if (reference.type.includes("_calendar") && originalBookingEvt) {
888-
const calendar = getCalendar(credential);
888+
const calendar = await getCalendar(credential);
889889
if (calendar) {
890890
integrationsToDelete.push(
891891
calendar?.deleteEvent(reference.uid, originalBookingEvt, reference.externalCalendarId)

packages/lib/payment/deletePayment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const deletePayment = async (
1313
} | null;
1414
}
1515
): Promise<boolean> => {
16-
const paymentApp = appStore[paymentAppCredentials?.app?.dirName as keyof typeof appStore];
16+
const paymentApp = await appStore[paymentAppCredentials?.app?.dirName as keyof typeof appStore];
1717
if (!(paymentApp && "lib" in paymentApp && "PaymentService" in paymentApp.lib)) {
1818
console.warn(`payment App service of type ${paymentApp} is not implemented`);
1919
return false;

0 commit comments

Comments
 (0)