Skip to content

Commit f0f168e

Browse files
fix: improve updatehostineventname function (calcom#21734)
Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>
1 parent dd37caf commit f0f168e

2 files changed

Lines changed: 171 additions & 8 deletions

File tree

packages/lib/event.test.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { TFunction } from "i18next";
22
import { describe, expect, it, vi } from "vitest";
33

44
import * as event from "./event";
5+
import { updateHostInEventName } from "./event";
56

67
describe("event tests", () => {
78
describe("fn: getEventName", () => {
@@ -423,4 +424,128 @@ describe("event tests", () => {
423424
expect(event.validateCustomEventName("foo{nonsenseField}bar")).toBe("{nonsenseField}");
424425
});
425426
});
427+
428+
describe("fn: updateHostInEventName", () => {
429+
const oldHost = "John Doe";
430+
const newHost = "Jane Smith";
431+
432+
it("should replace full host name with spaces", () => {
433+
const eventName = "Meeting with John Doe";
434+
const result = updateHostInEventName(eventName, oldHost, newHost);
435+
expect(result).toBe("Meeting with Jane Smith");
436+
});
437+
438+
it("should replace full host name with dots", () => {
439+
const eventName = "Meeting with John.Doe";
440+
const result = updateHostInEventName(eventName, oldHost, newHost);
441+
expect(result).toBe("Meeting with Jane.Smith");
442+
});
443+
444+
it("should replace full host name with hyphens", () => {
445+
const eventName = "Meeting with John-Doe";
446+
const result = updateHostInEventName(eventName, oldHost, newHost);
447+
expect(result).toBe("Meeting with Jane-Smith");
448+
});
449+
450+
it("should replace full host name with underscores", () => {
451+
const eventName = "Meeting with John_Doe";
452+
const result = updateHostInEventName(eventName, oldHost, newHost);
453+
expect(result).toBe("Meeting with Jane_Smith");
454+
});
455+
456+
it("should replace first name only", () => {
457+
const eventName = "John's presentation";
458+
const result = updateHostInEventName(eventName, oldHost, newHost);
459+
expect(result).toBe("Jane's presentation");
460+
});
461+
462+
it("should replace first name at the beginning", () => {
463+
const eventName = "John will present tomorrow";
464+
const result = updateHostInEventName(eventName, oldHost, newHost);
465+
expect(result).toBe("Jane will present tomorrow");
466+
});
467+
468+
it("should replace first name at the end", () => {
469+
const eventName = "Presentation by John";
470+
const result = updateHostInEventName(eventName, oldHost, newHost);
471+
expect(result).toBe("Presentation by Jane");
472+
});
473+
474+
it("should handle different cases", () => {
475+
const eventName = "meeting with john.doe";
476+
const result = updateHostInEventName(eventName, oldHost, newHost);
477+
expect(result).toBe("meeting with Jane.Smith");
478+
});
479+
480+
it("should handle mixed cases", () => {
481+
const eventName = "Meeting with JOHN DOE";
482+
const result = updateHostInEventName(eventName, oldHost, newHost);
483+
expect(result).toBe("Meeting with Jane Smith");
484+
});
485+
486+
it("should not replace partial matches", () => {
487+
const eventName = "Meeting with Johnson";
488+
const result = updateHostInEventName(eventName, oldHost, newHost);
489+
expect(result).toBe("Meeting with Johnson");
490+
});
491+
492+
it("should handle empty event name", () => {
493+
const result = updateHostInEventName("", oldHost, newHost);
494+
expect(result).toBe("");
495+
});
496+
497+
it("should handle event name without host", () => {
498+
const eventName = "Team meeting";
499+
const result = updateHostInEventName(eventName, oldHost, newHost);
500+
expect(result).toBe("Team meeting");
501+
});
502+
503+
it("should handle single word names", () => {
504+
const eventName = "Meeting with John";
505+
const result = updateHostInEventName(eventName, "John", "Jane");
506+
expect(result).toBe("Meeting with Jane");
507+
});
508+
509+
it("should handle names with multiple parts", () => {
510+
const eventName = "Meeting with John van der Berg";
511+
const result = updateHostInEventName(eventName, "John van der Berg", "Jane Smith");
512+
expect(result).toBe("Meeting with Jane Smith");
513+
});
514+
515+
it("should handle names with apostrophes", () => {
516+
const eventName = "Meeting with John O'Connor";
517+
const result = updateHostInEventName(eventName, "John O'Connor", "Jane Smith");
518+
expect(result).toBe("Meeting with Jane Smith");
519+
});
520+
521+
it("should handle names with special characters", () => {
522+
const eventName = "Meeting with Jean-Pierre Dupont";
523+
const result = updateHostInEventName(eventName, "Jean-Pierre Dupont", "Jane Smith");
524+
expect(result).toBe("Meeting with Jane Smith");
525+
});
526+
527+
it("should prioritize full name over first name", () => {
528+
const eventName = "John.Doe and John are coming";
529+
const result = updateHostInEventName(eventName, "John Doe", "Jane Smith");
530+
expect(result).toBe("Jane.Smith and John are coming");
531+
});
532+
533+
it("should only replace first match", () => {
534+
const eventName = "John.Doe and John Doe meeting";
535+
const result = updateHostInEventName(eventName, "John Doe", "Jane Smith");
536+
expect(result).toBe("Jane.Smith and John Doe meeting");
537+
});
538+
539+
it("should handle different new name structure", () => {
540+
const eventName = "Meeting with John.Doe";
541+
const result = updateHostInEventName(eventName, "John Doe", "Jane");
542+
expect(result).toBe("Meeting with Jane");
543+
});
544+
545+
it("should handle multiple occurrences with priority", () => {
546+
const eventName = "John and John.Doe and John_Doe";
547+
const result = updateHostInEventName(eventName, oldHost, newHost);
548+
expect(result).toBe("John and Jane.Smith and John_Doe");
549+
});
550+
});
426551
});

packages/lib/event.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,56 @@ export function getEventName(eventNameObj: EventNameObjectType, forAttendeeView
111111
}
112112

113113
export function updateHostInEventName(eventName: string, oldHost: string, newHost: string) {
114-
const oldHostFirstName = oldHost.split(" ")[0];
115-
const newHostFirstName = newHost.split(" ")[0];
116-
117-
if (!eventName.includes(oldHost) && !eventName.includes(oldHostFirstName)) {
118-
return eventName;
119-
}
114+
const oldParts = oldHost.trim().split(/\s+/);
115+
const newParts = newHost.trim().split(/\s+/);
116+
117+
// Handle cases where names might have different number of parts
118+
const oldFirst = oldParts[0];
119+
const oldLast = oldParts.slice(1).join(" ");
120+
const newFirst = newParts[0];
121+
const newLast = newParts.slice(1).join(" ");
122+
123+
const formats = [
124+
// Full name patterns (prioritize these first)
125+
...(oldLast
126+
? [
127+
{
128+
pattern: `${oldFirst}.${oldLast}`,
129+
replacement: newLast ? `${newFirst}.${newLast}` : newFirst,
130+
},
131+
{
132+
pattern: `${oldFirst}-${oldLast}`,
133+
replacement: newLast ? `${newFirst}-${newLast}` : newFirst,
134+
},
135+
{
136+
pattern: `${oldFirst}_${oldLast}`,
137+
replacement: newLast ? `${newFirst}_${newLast}` : newFirst,
138+
},
139+
{
140+
pattern: `${oldFirst} ${oldLast}`,
141+
replacement: newLast ? `${newFirst} ${newLast}` : newFirst,
142+
},
143+
]
144+
: []),
145+
// First name only (last to avoid partial matches)
146+
{
147+
pattern: oldFirst,
148+
replacement: newFirst,
149+
},
150+
];
120151

121152
let updatedEventName = eventName;
122153

123-
updatedEventName = updatedEventName.replace(oldHost, newHost);
154+
for (const { pattern, replacement } of formats) {
155+
// Escape special regex characters in the pattern
156+
const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
157+
const regex = new RegExp(`\\b${escapedPattern}\\b`, "gi");
124158

125-
updatedEventName = updatedEventName.replace(oldHostFirstName, newHostFirstName);
159+
if (regex.test(updatedEventName)) {
160+
updatedEventName = updatedEventName.replace(regex, replacement);
161+
return updatedEventName;
162+
}
163+
}
126164

127165
return updatedEventName;
128166
}

0 commit comments

Comments
 (0)