Type fixing is my middle name

This commit is contained in:
zomars 2022-04-13 16:47:46 -06:00
parent 1e25877777
commit 4c2595294b
5 changed files with 45 additions and 139 deletions

View File

@ -0,0 +1,33 @@
import { PrismaClient } from "@prisma/client";
import { Prisma } from "@calcom/prisma/client";
async function getBooking(prisma: PrismaClient, uid: string) {
const booking = await prisma.booking.findFirst({
where: {
uid,
},
select: {
startTime: true,
description: true,
attendees: {
select: {
email: true,
name: true,
},
},
},
});
if (booking) {
// @NOTE: had to do this because Server side cant return [Object objects]
// probably fixable with json.stringify -> json.parse
booking["startTime"] = (booking?.startTime as Date)?.toISOString() as unknown as Date;
}
return booking;
}
export type GetBookingType = Prisma.PromiseReturnType<typeof getBooking>;
export default getBooking;

View File

@ -8,6 +8,7 @@ import { useLocale } from "@calcom/lib/hooks/useLocale";
import { asStringOrNull } from "@lib/asStringOrNull";
import { getWorkingHours } from "@lib/availability";
import getBooking, { GetBookingType } from "@lib/getBooking";
import prisma from "@lib/prisma";
import { inferSSRProps } from "@lib/types/inferSSRProps";
@ -262,41 +263,9 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
eventTypeObject.schedule = null;
eventTypeObject.availability = [];
type Booking = {
startTime: Date | string;
description: string | null;
attendees?: {
email: string;
name: string;
}[];
} | null;
// @NOTE: being used several times refactor to exported function
async function getBooking(rescheduleUid: string): Promise<Booking> {
return await prisma.booking.findFirst({
where: {
uid: rescheduleUid,
},
select: {
startTime: true,
description: true,
attendees: {
select: {
email: true,
name: true,
},
},
},
});
}
let booking: Booking | null = null;
let booking: GetBookingType | null = null;
if (rescheduleUid) {
booking = await getBooking(rescheduleUid);
if (booking) {
// @NOTE: had to do this because Server side cant return [Object objects]
// probably fixable with json.stringify -> json.parse
booking["startTime"] = (booking?.startTime as Date)?.toISOString();
}
booking = await getBooking(prisma, rescheduleUid);
}
const dynamicNames = isDynamicGroup

View File

@ -1,4 +1,3 @@
import { Prisma } from "@prisma/client";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
@ -15,6 +14,7 @@ import {
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { asStringOrThrow } from "@lib/asStringOrNull";
import getBooking, { GetBookingType } from "@lib/getBooking";
import prisma from "@lib/prisma";
import { inferSSRProps } from "@lib/types/inferSSRProps";
@ -148,42 +148,9 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
};
})[0];
type Booking = {
startTime: Date | string;
description: string | null;
attendees: {
email: string;
name: string;
}[];
} | null;
// @NOTE: being used several times refactor to exported function
async function getBooking(): Promise<Booking> {
return await prisma.booking.findFirst({
where: {
uid: asStringOrThrow(context.query.rescheduleUid),
},
select: {
startTime: true,
description: true,
attendees: {
select: {
email: true,
name: true,
},
},
},
});
}
let booking: Booking | null = null;
let booking: GetBookingType | null = null;
if (context.query.rescheduleUid) {
booking = await getBooking();
if (booking) {
// @NOTE: had to do this because Server side cant return [Object objects]
// probably fixable with json.stringify -> json.parse
booking["startTime"] = (booking?.startTime as Date)?.toISOString();
}
booking = await getBooking(prisma, context.query.rescheduleUid as string);
}
const isDynamicGroupBooking = users.length > 1;

View File

@ -5,6 +5,7 @@ import { UserPlan } from "@calcom/prisma/client";
import { asStringOrNull } from "@lib/asStringOrNull";
import { getWorkingHours } from "@lib/availability";
import getBooking, { GetBookingType } from "@lib/getBooking";
import prisma from "@lib/prisma";
import { inferSSRProps } from "@lib/types/inferSSRProps";
@ -111,41 +112,9 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
eventTypeObject.availability = [];
type Booking = {
startTime: Date | string;
description: string | null;
attendees?: {
email: string;
name: string;
}[];
} | null;
// @NOTE: being used several times refactor to exported function
async function getBooking(rescheduleUid: string): Promise<Booking> {
return await prisma.booking.findFirst({
where: {
uid: rescheduleUid,
},
select: {
startTime: true,
description: true,
attendees: {
select: {
email: true,
name: true,
},
},
},
});
}
let booking: Booking | null = null;
let booking: GetBookingType | null = null;
if (rescheduleUid) {
booking = await getBooking(rescheduleUid);
if (booking) {
// @NOTE: had to do this because Server side cant return [Object objects]
// probably fixable with json.stringify -> json.parse
booking["startTime"] = (booking?.startTime as Date)?.toISOString();
}
booking = await getBooking(prisma, rescheduleUid);
}
return {

View File

@ -5,6 +5,7 @@ import { JSONObject } from "superjson/dist/types";
import { getLocationLabels } from "@calcom/app-store/utils";
import { asStringOrThrow } from "@lib/asStringOrNull";
import getBooking, { GetBookingType } from "@lib/getBooking";
import prisma from "@lib/prisma";
import { inferSSRProps } from "@lib/types/inferSSRProps";
@ -75,42 +76,9 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
};
})[0];
type Booking = {
startTime: Date | string;
description: string | null;
attendees: {
email: string;
name: string;
}[];
} | null;
// @NOTE: being used several times refactor to exported function
async function getBooking(): Promise<Booking> {
return prisma.booking.findFirst({
where: {
uid: asStringOrThrow(context.query.rescheduleUid),
},
select: {
startTime: true,
description: true,
attendees: {
select: {
email: true,
name: true,
},
},
},
});
}
let booking: Booking | null = null;
let booking: GetBookingType | null = null;
if (context.query.rescheduleUid) {
booking = await getBooking();
if (booking) {
// @NOTE: had to do this because Server side cant return [Object objects]
// probably fixable with json.stringify -> json.parse
booking["startTime"] = (booking?.startTime as Date)?.toISOString();
}
booking = await getBooking(prisma, context.query.rescheduleUid as string);
}
const t = await getTranslation(context.locale ?? "en", "common");