fix: reduce abnormal spacing in calendar invite (#19290)

* fix: reduce abnormal spacing in calendar invite

* add test for CalEventParser's getRichDescription

---------

Co-authored-by: Tushar Bhatt <[email protected]>
This commit is contained in:
amritandTushar Bhatt authored and GitHub committed 2025-02-20 21:27:33 +05:30
1 parent a44e7b2371
commit 3f042a1077
2 files changed
+130 -69

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
import type { TFunction } from "next-i18next";
import { describe, expect, it } from "vitest";
import type { CalendarEvent } from "@calcom/types/Calendar";
import { getRichDescription } from "./CalEventParser";
describe("getRichDescription", () => {
const t = ((key: string, _args?: Record<string, unknown>) => key) as TFunction;
const mockCalEvent: CalendarEvent = {
type: "test",
title: "Test Event",
description: "Test description",
startTime: "2023-01-01T10:00:00Z",
endTime: "2023-01-01T11:00:00Z",
organizer: {
email: "[email protected]",
name: "Test Organizer",
timeZone: "America/New_York",
language: { translate: t, locale: "en" },
},
attendees: [
{
email: "[email protected]",
name: "Test Attendee",
timeZone: "America/New_York",
language: { translate: t, locale: "en" },
},
],
location: "https://zoom.us/j/123456brahhh",
additionalNotes: "Some additional notes",
};
it("should format description without extra whitespace", () => {
const description = getRichDescription(mockCalEvent, t);
console.log("Description:", description);
console.log(
"Lines:",
description.split("\n").map((line) => `"${line}"`)
);
// Description should not have more than 2 consecutive newlines
expect(description).not.toMatch(/\n{3,}/);
// Each non-empty line should have content
const lines = description.split("\n").filter((line) => line.trim());
lines.forEach((line) => {
expect(line.trim().length).toBeGreaterThan(0);
});
// Verify content is present and properly formatted
expect(description).toContain("what:");
expect(description).toContain("Test Event");
expect(description).toContain("where:");
expect(description).toContain("https://zoom.us/j/123456brahhh");
});
it("should include payment info when present", () => {
const eventWithPayment = {
...mockCalEvent,
paymentInfo: {
link: "https://payment.test",
},
};
const description = getRichDescription(eventWithPayment, t);
expect(description).toContain("pay_now:\nhttps://payment.test");
});
it("should handle empty optional fields", () => {
const eventWithoutOptionals = {
...mockCalEvent,
description: "",
additionalNotes: "",
location: "",
};
const description = getRichDescription(eventWithoutOptionals, t);
expect(description).not.toContain("description:");
expect(description).not.toContain("additional_notes:");
// Should still contain required fields
expect(description).toContain("what:");
expect(description).toContain("who:");
});
});
+43 -69
View File
@@ -13,10 +13,7 @@ const translator = short();
// The odd indentation in this file is necessary because otherwise the leading tabs will be applied into the event description.
export const getWhat = (calEvent: Pick<CalendarEvent, "title">, t: TFunction) => {
return `
${t("what")}:
${calEvent.title}
`;
return `${t("what")}:\n${calEvent.title}`;
};
export const getWhen = (
@@ -28,14 +25,8 @@ export const getWhen = (
const attendeeTimezone = calEvent.attendees?.[0]?.timeZone ?? defaultTimezone;
return calEvent.seatsPerTimeSlot
? `
${t("organizer_timezone")}:
${organizerTimezone}
`
: `
${t("invitee_timezone")}:
${attendeeTimezone}
`;
? `${t("organizer_timezone")}:\n${organizerTimezone}`
: `${t("invitee_timezone")}:\n${attendeeTimezone}`;
};
export const getWho = (
@@ -50,42 +41,32 @@ export const getWho = (
attendeesFromCalEvent = [];
}
const attendees = attendeesFromCalEvent
.map((attendee) => {
return `
${attendee?.name || t("guest")}
${!isSmsCalEmail(attendee.email) ? `${attendee.email}\n` : `${attendee.phoneNumber}\n`}
`;
})
.join("");
.map(
(attendee) =>
`${attendee?.name || t("guest")}\n${
!isSmsCalEmail(attendee.email) ? attendee.email : attendee.phoneNumber
}`
)
.join("\n");
const organizer = `
${calEvent.organizer.name} - ${t("organizer")}
${calEvent.organizer.email}
`;
const organizer = `${calEvent.organizer.name} - ${t("organizer")}\n${calEvent.organizer.email}`;
const teamMembers = calEvent.team?.members
? calEvent.team.members.map((member) => {
return `
${member.name} - ${t("team_member")}
${member.email}
`;
})
? calEvent.team.members
.map((member) => `${member.name} - ${t("team_member")}\n${member.email}`)
.join("\n")
: [];
return `
${t("who")}:
${organizer + attendees + teamMembers.join("")}
`;
return `${t("who")}:\n${organizer}${attendees ? `\n${attendees}` : ""}${
teamMembers.length ? `\n${teamMembers}` : ""
}`;
};
export const getAdditionalNotes = (calEvent: Pick<CalendarEvent, "additionalNotes">, t: TFunction) => {
if (!calEvent.additionalNotes) {
return "";
}
return `
${t("additional_notes")}:
${calEvent.additionalNotes}
`;
return `${t("additional_notes")}:\n${calEvent.additionalNotes}`;
};
export const getUserFieldsResponses = (calEvent: Parameters<typeof getLabelValueMapFromResponses>[0]) => {
@@ -131,10 +112,9 @@ export const getDescription = (calEvent: Pick<CalendarEvent, "description">, t:
return "";
}
const plainText = calEvent.description.replace(/<\/?[^>]+(>|$)/g, "").replace(/_/g, " ");
return `\n${t("description")}
${plainText}
`;
return `${t("description")}\n${plainText}`;
};
export const getLocation = (
calEvent: Parameters<typeof getVideoCallUrlFromCalEvent>[0] & Parameters<typeof getProviderName>[0]
) => {
@@ -336,39 +316,33 @@ export const getRichDescription = (
) => {
const t = t_ ?? calEvent.organizer.language.translate;
return `
${getCancellationReason(calEvent, t)}
${getWhat(calEvent, t)}
${getWhen(calEvent, t)}
${getWho(calEvent, t)}
${t("where")}:
${getLocation(calEvent)}
${getDescription(calEvent, t)}
${getAdditionalNotes(calEvent, t)}
${getUserFieldsResponses(calEvent)}
${includeAppStatus ? getAppsStatus(calEvent, t) : ""}
${
// TODO: Only the original attendee can make changes to the event
// Guests cannot
calEvent.seatsPerTimeSlot ? "" : getManageLink(calEvent, t)
}
${
calEvent.paymentInfo
? `
${t("pay_now")}:
${calEvent.paymentInfo.link}
`
: ""
}
`.trim();
// Join all parts with single newlines and remove extra whitespace
const parts = [
getCancellationReason(calEvent, t),
getWhat(calEvent, t),
getWhen(calEvent, t),
getWho(calEvent, t),
`${t("where")}:\n${getLocation(calEvent)}`,
getDescription(calEvent, t),
getAdditionalNotes(calEvent, t),
getUserFieldsResponses(calEvent),
includeAppStatus ? getAppsStatus(calEvent, t) : "",
// TODO: Only the original attendee can make changes to the event
// Guests cannot
calEvent.seatsPerTimeSlot ? "" : getManageLink(calEvent, t),
calEvent.paymentInfo ? `${t("pay_now")}:\n${calEvent.paymentInfo.link}` : "",
]
.filter(Boolean) // Remove empty strings
.join("\n\n") // Double newline between major sections
.replace(/\n{3,}/g, "\n\n") // Ensure no more than double newlines
.trim();
return parts;
};
export const getCancellationReason = (calEvent: Pick<CalendarEvent, "cancellationReason">, t: TFunction) => {
if (!calEvent.cancellationReason) return "";
return `
${t("cancellation_reason")}:
${calEvent.cancellationReason}
`;
return `${t("cancellation_reason")}:\n${calEvent.cancellationReason}`;
};
export const isDailyVideoCall = (calEvent: Pick<CalendarEvent, "videoCallData">): boolean => {