Files
cal.diy/packages/features/bookings/lib/handleNewBooking/handleAppsStatus.ts
T
Udit TakkarandKeith Williams a52f7efe88 refactor: move functions to separate files (#15590)
* refactor: move functions to separate files

* chore: remove inline function

* chore: type error

* fix: type error

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
2024-06-27 22:05:11 +00:00

61 lines
1.9 KiB
TypeScript

import type { AdditionalInformation, AppsStatus } from "@calcom/types/Calendar";
import type { EventResult } from "@calcom/types/EventManager";
import type { ReqAppsStatus, Booking } from "../handleNewBooking";
export function handleAppsStatus(
results: EventResult<AdditionalInformation>[],
booking: (Booking & { appsStatus?: AppsStatus[] }) | null,
reqAppsStatus: ReqAppsStatus
): AppsStatus[] {
const resultStatus = mapResultsToAppsStatus(results);
if (reqAppsStatus === undefined) {
return updateBookingWithStatus(booking, resultStatus);
}
return calculateAggregatedAppsStatus(reqAppsStatus, resultStatus);
}
function mapResultsToAppsStatus(results: EventResult<AdditionalInformation>[]): AppsStatus[] {
return results.map((app) => ({
appName: app.appName,
type: app.type,
success: app.success ? 1 : 0,
failures: !app.success ? 1 : 0,
errors: app.calError ? [app.calError] : [],
warnings: app.calWarnings,
}));
}
function updateBookingWithStatus(
booking: (Booking & { appsStatus?: AppsStatus[] }) | null,
resultStatus: AppsStatus[]
): AppsStatus[] {
if (booking !== null) {
booking.appsStatus = resultStatus;
}
return resultStatus;
}
function calculateAggregatedAppsStatus(
reqAppsStatus: NonNullable<ReqAppsStatus>,
resultStatus: AppsStatus[]
): AppsStatus[] {
// From down here we can assume reqAppsStatus is not undefined anymore
// Other status exist, so this is the last booking of a series,
// proceeding to prepare the info for the event
const aggregatedStatus = reqAppsStatus.concat(resultStatus).reduce((acc, curr) => {
if (acc[curr.type]) {
acc[curr.type].success += curr.success;
acc[curr.type].errors = acc[curr.type].errors.concat(curr.errors);
acc[curr.type].warnings = acc[curr.type].warnings?.concat(curr.warnings || []);
} else {
acc[curr.type] = curr;
}
return acc;
}, {} as { [key: string]: AppsStatus });
return Object.values(aggregatedStatus);
}