2023-10-26 05:12:05 -03:00
|
|
|
import type { ParsedUrlQuery } from "querystring";
|
|
|
|
import { stringify } from "querystring";
|
|
|
|
|
2023-10-17 00:06:46 -03:00
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
|
|
import type { RedirectType } from "@calcom/prisma/client";
|
|
|
|
|
2023-10-17 16:00:48 -03:00
|
|
|
const log = logger.getSubLogger({ prefix: ["lib", "getTemporaryOrgRedirect"] });
|
2023-10-17 00:06:46 -03:00
|
|
|
export const getTemporaryOrgRedirect = async ({
|
|
|
|
slug,
|
|
|
|
redirectType,
|
|
|
|
eventTypeSlug,
|
2023-10-26 05:12:05 -03:00
|
|
|
currentQuery,
|
2023-10-17 00:06:46 -03:00
|
|
|
}: {
|
|
|
|
slug: string;
|
|
|
|
redirectType: RedirectType;
|
|
|
|
eventTypeSlug: string | null;
|
2023-10-26 05:12:05 -03:00
|
|
|
currentQuery: ParsedUrlQuery;
|
2023-10-17 00:06:46 -03:00
|
|
|
}) => {
|
|
|
|
const prisma = (await import("@calcom/prisma")).default;
|
|
|
|
log.debug(
|
|
|
|
`Looking for redirect for`,
|
|
|
|
safeStringify({
|
|
|
|
slug,
|
|
|
|
redirectType,
|
|
|
|
eventTypeSlug,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const redirect = await prisma.tempOrgRedirect.findUnique({
|
|
|
|
where: {
|
|
|
|
from_type_fromOrgId: {
|
|
|
|
type: redirectType,
|
|
|
|
from: slug,
|
|
|
|
fromOrgId: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (redirect) {
|
|
|
|
log.debug(`Redirecting ${slug} to ${redirect.toUrl}`);
|
2023-10-26 05:12:05 -03:00
|
|
|
const newDestinationWithoutQuery = eventTypeSlug ? `${redirect.toUrl}/${eventTypeSlug}` : redirect.toUrl;
|
|
|
|
const currentQueryString = stringify(currentQuery);
|
2023-10-17 00:06:46 -03:00
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
permanent: false,
|
2023-10-26 05:12:05 -03:00
|
|
|
destination: `${newDestinationWithoutQuery}${currentQueryString ? `?${currentQueryString}` : ""}`,
|
2023-10-17 00:06:46 -03:00
|
|
|
},
|
|
|
|
} as const;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|