From 3f042a1077760662d68d0cd02609810cad1c996b Mon Sep 17 00:00:00 2001 From: amrit Date: Thu, 20 Feb 2025 21:27:33 +0530 Subject: [PATCH] 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 <95581504+TusharBhatt1@users.noreply.github.com> --- packages/lib/CalEventParser.test.ts | 87 +++++++++++++++++++++ packages/lib/CalEventParser.ts | 112 +++++++++++----------------- 2 files changed, 130 insertions(+), 69 deletions(-) create mode 100644 packages/lib/CalEventParser.test.ts diff --git a/packages/lib/CalEventParser.test.ts b/packages/lib/CalEventParser.test.ts new file mode 100644 index 0000000000..8732bd3e18 --- /dev/null +++ b/packages/lib/CalEventParser.test.ts @@ -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) => 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: "test@example.com", + name: "Test Organizer", + timeZone: "America/New_York", + language: { translate: t, locale: "en" }, + }, + attendees: [ + { + email: "attendee@example.com", + 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:"); + }); +}); diff --git a/packages/lib/CalEventParser.ts b/packages/lib/CalEventParser.ts index 53f24fa25d..3de4a1016b 100644 --- a/packages/lib/CalEventParser.ts +++ b/packages/lib/CalEventParser.ts @@ -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, 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, t: TFunction) => { if (!calEvent.additionalNotes) { return ""; } - return ` -${t("additional_notes")}: -${calEvent.additionalNotes} - `; + return `${t("additional_notes")}:\n${calEvent.additionalNotes}`; }; export const getUserFieldsResponses = (calEvent: Parameters[0]) => { @@ -131,10 +112,9 @@ export const getDescription = (calEvent: Pick, 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[0] & Parameters[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, t: TFunction) => { if (!calEvent.cancellationReason) return ""; - return ` -${t("cancellation_reason")}: -${calEvent.cancellationReason} - `; + return `${t("cancellation_reason")}:\n${calEvent.cancellationReason}`; }; export const isDailyVideoCall = (calEvent: Pick): boolean => {