2022-10-19 16:11:50 +00:00
|
|
|
import { AppsStatus } from "@calcom/types/Calendar";
|
|
|
|
|
2022-05-05 21:16:25 +00:00
|
|
|
import * as fetch from "@lib/core/http/fetch-wrapper";
|
|
|
|
import { BookingCreateBody, BookingResponse } from "@lib/types/booking";
|
|
|
|
|
2022-11-08 20:59:44 +00:00
|
|
|
type ExtendedBookingCreateBody = BookingCreateBody & {
|
|
|
|
noEmail?: boolean;
|
|
|
|
recurringCount?: number;
|
|
|
|
appsStatus?: AppsStatus[] | undefined;
|
|
|
|
allRecurringDates?: string[];
|
|
|
|
currentRecurringIndex?: number;
|
|
|
|
};
|
2022-05-05 21:16:25 +00:00
|
|
|
|
|
|
|
const createRecurringBooking = async (data: ExtendedBookingCreateBody[]) => {
|
2022-10-19 16:11:50 +00:00
|
|
|
const createdBookings: BookingResponse[] = [];
|
2022-11-08 20:59:44 +00:00
|
|
|
const allRecurringDates: string[] = data.map((booking) => booking.start);
|
|
|
|
let appsStatus: AppsStatus[] | undefined = undefined;
|
2022-10-19 16:11:50 +00:00
|
|
|
// Reversing to accumulate results for noEmail instances first, to then lastly, create the
|
|
|
|
// emailed booking taking into account accumulated results to send app status accurately
|
2022-11-12 16:46:27 +00:00
|
|
|
for (let key = data.length - 1; key >= 0; key--) {
|
2022-10-19 16:11:50 +00:00
|
|
|
const booking = data[key];
|
2022-11-12 16:46:27 +00:00
|
|
|
if (key === 0) {
|
2022-10-19 16:11:50 +00:00
|
|
|
const calcAppsStatus: { [key: string]: AppsStatus } = createdBookings
|
|
|
|
.flatMap((book) => (book.appsStatus !== undefined ? book.appsStatus : []))
|
|
|
|
.reduce((prev, curr) => {
|
|
|
|
if (prev[curr.type]) {
|
|
|
|
prev[curr.type].failures += curr.failures;
|
|
|
|
prev[curr.type].success += curr.success;
|
|
|
|
} else {
|
|
|
|
prev[curr.type] = curr;
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
}, {} as { [key: string]: AppsStatus });
|
2022-11-08 20:59:44 +00:00
|
|
|
appsStatus = Object.values(calcAppsStatus);
|
2022-10-19 16:11:50 +00:00
|
|
|
}
|
2022-11-08 20:59:44 +00:00
|
|
|
|
|
|
|
const response = await fetch.post<ExtendedBookingCreateBody, BookingResponse>("/api/book/event", {
|
|
|
|
...booking,
|
|
|
|
appsStatus,
|
|
|
|
allRecurringDates,
|
|
|
|
currentRecurringIndex: key,
|
2022-11-12 16:46:27 +00:00
|
|
|
noEmail: key !== 0,
|
2022-11-08 20:59:44 +00:00
|
|
|
});
|
|
|
|
createdBookings.push(response);
|
2022-10-19 16:11:50 +00:00
|
|
|
}
|
|
|
|
return createdBookings;
|
2022-05-05 21:16:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default createRecurringBooking;
|