2023-03-06 12:14:54 +00:00
|
|
|
import type { TFunction } from "next-i18next";
|
2021-11-08 11:04:12 +00:00
|
|
|
|
2022-08-26 00:48:50 +00:00
|
|
|
import { guessEventLocationType } from "@calcom/app-store/locations";
|
2023-03-09 15:11:16 +00:00
|
|
|
import type { Prisma } from "@calcom/prisma/client";
|
2022-08-26 00:48:50 +00:00
|
|
|
|
2023-02-01 12:10:16 +00:00
|
|
|
export type EventNameObjectType = {
|
2021-11-08 11:04:12 +00:00
|
|
|
attendeeName: string;
|
|
|
|
eventType: string;
|
|
|
|
eventName?: string | null;
|
|
|
|
host: string;
|
2022-06-06 12:48:13 +00:00
|
|
|
location?: string;
|
2023-03-09 15:11:16 +00:00
|
|
|
bookingFields?: Prisma.JsonObject;
|
2021-11-08 11:04:12 +00:00
|
|
|
t: TFunction;
|
|
|
|
};
|
|
|
|
|
2023-02-03 10:20:10 +00:00
|
|
|
export function getEventName(eventNameObj: EventNameObjectType, forAttendeeView = false) {
|
2022-06-06 12:48:13 +00:00
|
|
|
if (!eventNameObj.eventName)
|
|
|
|
return eventNameObj.t("event_between_users", {
|
|
|
|
eventName: eventNameObj.eventType,
|
|
|
|
host: eventNameObj.host,
|
|
|
|
attendeeName: eventNameObj.attendeeName,
|
|
|
|
});
|
|
|
|
|
|
|
|
let eventName = eventNameObj.eventName;
|
2022-08-26 00:48:50 +00:00
|
|
|
let locationString = eventNameObj.location || "";
|
2022-06-06 12:48:13 +00:00
|
|
|
|
2023-02-03 10:20:10 +00:00
|
|
|
if (eventNameObj.eventName.includes("{Location}") || eventNameObj.eventName.includes("{LOCATION}")) {
|
2022-08-26 00:48:50 +00:00
|
|
|
const eventLocationType = guessEventLocationType(eventNameObj.location);
|
|
|
|
if (eventLocationType) {
|
|
|
|
locationString = eventLocationType.label;
|
2022-06-06 12:48:13 +00:00
|
|
|
}
|
2023-02-01 12:10:16 +00:00
|
|
|
eventName = eventName.replace("{Location}", locationString);
|
2023-02-03 10:20:10 +00:00
|
|
|
eventName = eventName.replace("{LOCATION}", locationString);
|
2022-06-06 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:11:16 +00:00
|
|
|
let dynamicEventName = eventName
|
|
|
|
// Need this for compatibility with older event names
|
|
|
|
.replaceAll("{Event type title}", eventNameObj.eventType)
|
|
|
|
.replaceAll("{Scheduler}", eventNameObj.attendeeName)
|
|
|
|
.replaceAll("{Organiser}", eventNameObj.host)
|
|
|
|
.replaceAll("{USER}", eventNameObj.attendeeName)
|
|
|
|
.replaceAll("{ATTENDEE}", eventNameObj.attendeeName)
|
|
|
|
.replaceAll("{HOST}", eventNameObj.host)
|
|
|
|
.replaceAll("{HOST/ATTENDEE}", forAttendeeView ? eventNameObj.host : eventNameObj.attendeeName);
|
|
|
|
|
|
|
|
const customInputvariables = dynamicEventName.match(/\{(.+?)}/g)?.map((variable) => {
|
|
|
|
return variable.replace("{", "").replace("}", "");
|
|
|
|
});
|
|
|
|
|
|
|
|
customInputvariables?.forEach((variable) => {
|
|
|
|
if (eventNameObj.bookingFields) {
|
|
|
|
Object.keys(eventNameObj.bookingFields).forEach((bookingField) => {
|
|
|
|
if (variable === bookingField) {
|
|
|
|
let fieldValue;
|
|
|
|
if (eventNameObj.bookingFields) {
|
|
|
|
fieldValue =
|
|
|
|
eventNameObj.bookingFields[bookingField as keyof typeof eventNameObj.bookingFields]?.toString();
|
|
|
|
}
|
|
|
|
dynamicEventName = dynamicEventName.replace(`{${variable}}`, fieldValue || "");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return dynamicEventName;
|
2021-06-15 15:26:16 +00:00
|
|
|
}
|
2023-03-06 12:14:54 +00:00
|
|
|
|
2023-03-09 15:11:16 +00:00
|
|
|
export const validateCustomEventName = (
|
|
|
|
value: string,
|
|
|
|
message: string,
|
|
|
|
bookingFields?: Prisma.JsonObject
|
|
|
|
) => {
|
|
|
|
let customInputVariables: string[] = [];
|
|
|
|
if (bookingFields) {
|
|
|
|
customInputVariables = Object.keys(bookingFields).map((customInput) => {
|
|
|
|
return `{${customInput}}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const validVariables = customInputVariables.concat([
|
|
|
|
"{Event type title}",
|
|
|
|
"{Organiser}",
|
|
|
|
"{Scheduler}",
|
|
|
|
"{Location}",
|
|
|
|
]);
|
2023-03-06 12:14:54 +00:00
|
|
|
const matches = value.match(/\{([^}]+)\}/g);
|
|
|
|
if (matches?.length) {
|
|
|
|
for (const item of matches) {
|
|
|
|
if (!validVariables.includes(item)) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|