fix: Fairness when routing (#18550)
* Partial push of new filters * Let's always return the passed host array when a filter doesn't need to be applied * fix: isFixed typing in same round robin host check * Add main bulk of rewritten logic for routed fairness support * Remove console.log * Add getAttributeWithEnabledWeights to AttributeRepository * Add OOO entry repository * Use getOrderedListOfLuckyUsers in lead threshold filtering * Shortfall already includes weight * test against minShortfall * first attempt to fix fallbacks * Added testcase updates * Partially fix filterHosts test * Some test updates * Removed email field from normalization * fix no available users found error * fix unit test * remove fallback from same host reschedule return * fixes and improvements for findQualifiedHosts * include fallback logic in handleNewBooking * use fallback in handleNewBooking * fix for contact owner in handleNewBooking * WIP - comment * pass routingFormResponseId * fix type error * pass routingFormResponse * fixes in treshold logic * remove reason * merge qualified and fallback availabilities * fix type error * add and fix tests * clean up test file * fix filterHostsBySameRoundRobinHost tests * fix findQualifiedHosts.test.ts * fix getRoutedUsers.test.ts * fix tests * fix isRerouting check * fix start time for two weeks check * fix fallback hosts * fix test * fallback hosts should also include qualified hosts * fix type error * fix typos * add contact owner to fallbacks * fix typo * fix qualifiedRRUsers * set fixed users when no hosts * add tets + code clean up * add test for reschedule with same host * fix typo * add more tests for findQualifiedHosts * remove skip from test * add comment * remove unused code * fix fallback users in loadAndValidateUsers * improve naming for fallback * rename missed fallback variable * fix reschedule with same rr host in handleNewBooking * fixes for fallback slots * add more tests + small fixes for contact owner * fix tests and type errors * add logs for fallback * add type:json to logger * Fix small merge issue * fix end time if two weeks are cut off * add more tests for contact owner availability check * add new logic for two weeks fallback * fix reschedule same RR host test --------- Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]>
This commit is contained in:
22 files changed
+1971
-704
No files matched your search
@@ -311,11 +311,244 @@ describe("getSchedule", () => {
|
||||
}
|
||||
);
|
||||
});
|
||||
test("correctly get slots for event with only round robin hosts - When no availability is found", async () => {
|
||||
vi.setSystemTime("2024-05-21T00:00:13Z");
|
||||
test("correctly get slots when contact owner has no availability in first two weeks", async () => {
|
||||
vi.setSystemTime("2024-05-01T00:00:13Z");
|
||||
|
||||
const plus1DateString = "2024-05-22";
|
||||
const plus2DateString = "2024-05-23";
|
||||
const startTime = "2024-05-01";
|
||||
const endTime = "2024-06-01";
|
||||
const dateWithinTwoWeeks = "2024-05-10";
|
||||
const dateAfterTwoWeeks = "2024-05-22";
|
||||
|
||||
const crmCredential = {
|
||||
id: 1,
|
||||
type: "salesforce_crm",
|
||||
key: {
|
||||
clientId: "test-client-id",
|
||||
},
|
||||
userId: 1,
|
||||
teamId: null,
|
||||
appId: "salesforce",
|
||||
invalid: false,
|
||||
user: { email: "[email protected]" },
|
||||
};
|
||||
|
||||
await createCredentials([crmCredential]);
|
||||
|
||||
mockCrmApp("salesforce", {
|
||||
getContacts: [
|
||||
{
|
||||
id: "contact-id",
|
||||
email: "[email protected]",
|
||||
ownerEmail: "[email protected]",
|
||||
},
|
||||
],
|
||||
createContacts: [{ id: "contact-id", email: "[email protected]" }],
|
||||
});
|
||||
|
||||
await createBookingScenario({
|
||||
eventTypes: [
|
||||
{
|
||||
id: 1,
|
||||
slotInterval: 60,
|
||||
length: 60,
|
||||
hosts: [
|
||||
{
|
||||
userId: 101,
|
||||
isFixed: false,
|
||||
},
|
||||
{
|
||||
userId: 102,
|
||||
isFixed: false,
|
||||
},
|
||||
],
|
||||
schedulingType: "ROUND_ROBIN",
|
||||
metadata: {
|
||||
apps: {
|
||||
salesforce: {
|
||||
enabled: true,
|
||||
appCategories: ["crm"],
|
||||
roundRobinLeadSkip: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
users: [
|
||||
{
|
||||
...TestData.users.example,
|
||||
email: "[email protected]",
|
||||
id: 101,
|
||||
schedules: [TestData.schedules.IstWorkHoursWithFirstTwoWeeksUnavailable(startTime)],
|
||||
},
|
||||
{
|
||||
...TestData.users.example,
|
||||
email: "[email protected]",
|
||||
id: 102,
|
||||
schedules: [TestData.schedules.IstMorningShift],
|
||||
defaultScheduleId: 2,
|
||||
},
|
||||
],
|
||||
bookings: [],
|
||||
});
|
||||
|
||||
const scheduleWithLeadSkip = await getSchedule({
|
||||
input: {
|
||||
eventTypeId: 1,
|
||||
eventTypeSlug: "",
|
||||
startTime: `${startTime}T18:30:00.000Z`,
|
||||
endTime: `${endTime}T18:29:59.999Z`,
|
||||
timeZone: Timezones["+5:30"],
|
||||
isTeamEvent: true,
|
||||
teamMemberEmail: "[email protected]",
|
||||
orgSlug: null,
|
||||
},
|
||||
});
|
||||
|
||||
// [email protected] isn't available in the first two weeks, so we fallback to all hosts
|
||||
expect(scheduleWithLeadSkip).toHaveTimeSlots(
|
||||
[
|
||||
`04:30:00.000Z`,
|
||||
`05:30:00.000Z`,
|
||||
`06:30:00.000Z`,
|
||||
`07:30:00.000Z`,
|
||||
`08:30:00.000Z`,
|
||||
`09:30:00.000Z`,
|
||||
`10:30:00.000Z`,
|
||||
`11:30:00.000Z`,
|
||||
// `12:30:00.000Z`,
|
||||
// `13:30:00.000Z`,
|
||||
// `14:30:00.000Z`,
|
||||
// `15:30:00.000Z`,
|
||||
],
|
||||
{
|
||||
dateString: dateWithinTwoWeeks,
|
||||
}
|
||||
);
|
||||
|
||||
// slots from from both hosts because [email protected] isn't available in the first two weeks, so we fallback to all hosts
|
||||
expect(scheduleWithLeadSkip).toHaveTimeSlots(
|
||||
[
|
||||
`04:30:00.000Z`,
|
||||
`05:30:00.000Z`,
|
||||
`06:30:00.000Z`,
|
||||
`07:30:00.000Z`,
|
||||
`08:30:00.000Z`,
|
||||
`09:30:00.000Z`,
|
||||
`10:30:00.000Z`,
|
||||
`11:30:00.000Z`,
|
||||
`12:30:00.000Z`,
|
||||
`13:30:00.000Z`,
|
||||
`14:30:00.000Z`,
|
||||
`15:30:00.000Z`,
|
||||
],
|
||||
{
|
||||
dateString: dateAfterTwoWeeks,
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
test("correctly get slots for event when contact owner has no availability for the whole month but is available the first two weeks", async () => {
|
||||
vi.setSystemTime("2024-05-01T00:00:13Z");
|
||||
|
||||
const startTime = "2024-07-01";
|
||||
const endTime = "2024-07-31";
|
||||
|
||||
const crmCredential = {
|
||||
id: 1,
|
||||
type: "salesforce_crm",
|
||||
key: {
|
||||
clientId: "test-client-id",
|
||||
},
|
||||
userId: 1,
|
||||
teamId: null,
|
||||
appId: "salesforce",
|
||||
invalid: false,
|
||||
user: { email: "[email protected]" },
|
||||
};
|
||||
|
||||
await createCredentials([crmCredential]);
|
||||
|
||||
mockCrmApp("salesforce", {
|
||||
getContacts: [
|
||||
{
|
||||
id: "contact-id",
|
||||
email: "[email protected]",
|
||||
ownerEmail: "[email protected]",
|
||||
},
|
||||
],
|
||||
createContacts: [{ id: "contact-id", email: "[email protected]" }],
|
||||
});
|
||||
|
||||
await createBookingScenario({
|
||||
eventTypes: [
|
||||
{
|
||||
id: 1,
|
||||
slotInterval: 60,
|
||||
length: 60,
|
||||
hosts: [
|
||||
{
|
||||
userId: 101,
|
||||
isFixed: false,
|
||||
},
|
||||
{
|
||||
userId: 102,
|
||||
isFixed: false,
|
||||
},
|
||||
],
|
||||
schedulingType: "ROUND_ROBIN",
|
||||
metadata: {
|
||||
apps: {
|
||||
salesforce: {
|
||||
enabled: true,
|
||||
appCategories: ["crm"],
|
||||
roundRobinLeadSkip: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
users: [
|
||||
{
|
||||
...TestData.users.example,
|
||||
email: "[email protected]",
|
||||
id: 101,
|
||||
schedules: [TestData.schedules.IstNotAvailableForFullMonth("2024-07")], // not available full July
|
||||
},
|
||||
{
|
||||
...TestData.users.example,
|
||||
email: "[email protected]",
|
||||
id: 102,
|
||||
schedules: [TestData.schedules.IstMorningShift],
|
||||
defaultScheduleId: 2,
|
||||
},
|
||||
],
|
||||
bookings: [],
|
||||
});
|
||||
|
||||
const scheduleWithLeadSkip = await getSchedule({
|
||||
input: {
|
||||
eventTypeId: 1,
|
||||
eventTypeSlug: "",
|
||||
startTime: `${startTime}T18:30:00.000Z`,
|
||||
endTime: `${endTime}T18:29:59.999Z`,
|
||||
timeZone: Timezones["+5:30"],
|
||||
isTeamEvent: true,
|
||||
teamMemberEmail: "[email protected]",
|
||||
orgSlug: null,
|
||||
},
|
||||
});
|
||||
|
||||
// slots from [email protected] because even though user isn't available in July, user is available in the first two weeks
|
||||
expect(scheduleWithLeadSkip).toHaveDateDisabled({
|
||||
dateString: "2024-07-05",
|
||||
});
|
||||
});
|
||||
|
||||
test("correctly get slots for event when contact owner has no availability at all", async () => {
|
||||
vi.setSystemTime("2024-05-01T00:00:13Z");
|
||||
|
||||
const startTime = "2024-07-01";
|
||||
const endTime = "2024-07-31";
|
||||
|
||||
const crmCredential = {
|
||||
id: 1,
|
||||
@@ -393,8 +626,8 @@ describe("getSchedule", () => {
|
||||
input: {
|
||||
eventTypeId: 1,
|
||||
eventTypeSlug: "",
|
||||
startTime: `${plus1DateString}T18:30:00.000Z`,
|
||||
endTime: `${plus2DateString}T18:29:59.999Z`,
|
||||
startTime: `${startTime}T18:30:00.000Z`,
|
||||
endTime: `${endTime}T18:29:59.999Z`,
|
||||
timeZone: Timezones["+5:30"],
|
||||
isTeamEvent: true,
|
||||
teamMemberEmail: "[email protected]",
|
||||
@@ -402,25 +635,8 @@ describe("getSchedule", () => {
|
||||
},
|
||||
});
|
||||
|
||||
// only slots where [email protected] is available
|
||||
expect(scheduleWithLeadSkip).toHaveDateDisabled({
|
||||
dateString: plus2DateString,
|
||||
});
|
||||
|
||||
const scheduleWithoutLeadSkip = await getSchedule({
|
||||
input: {
|
||||
eventTypeId: 1,
|
||||
eventTypeSlug: "",
|
||||
startTime: `${plus1DateString}T18:30:00.000Z`,
|
||||
endTime: `${plus2DateString}T18:29:59.999Z`,
|
||||
timeZone: Timezones["+5:30"],
|
||||
isTeamEvent: true,
|
||||
orgSlug: null,
|
||||
},
|
||||
});
|
||||
|
||||
// slots where either one of the rr hosts is available
|
||||
expect(scheduleWithoutLeadSkip).toHaveTimeSlots(
|
||||
// slots from [email protected] because [email protected] isn't available also not the first two weeks
|
||||
expect(scheduleWithLeadSkip).toHaveTimeSlots(
|
||||
[
|
||||
`04:30:00.000Z`,
|
||||
`05:30:00.000Z`,
|
||||
@@ -430,16 +646,207 @@ describe("getSchedule", () => {
|
||||
`09:30:00.000Z`,
|
||||
`10:30:00.000Z`,
|
||||
`11:30:00.000Z`,
|
||||
`12:30:00.000Z`,
|
||||
`13:30:00.000Z`,
|
||||
`14:30:00.000Z`,
|
||||
`15:30:00.000Z`,
|
||||
],
|
||||
{
|
||||
dateString: plus2DateString,
|
||||
dateString: "2024-07-05",
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
test("correctly gets slot for event when contact owner is available in the first two weeks and the next month is loaded (within the two weeks)", async () => {
|
||||
vi.setSystemTime("2024-05-23T00:00:13Z");
|
||||
|
||||
const startTime = "2024-06-01";
|
||||
const endTime = "2024-06-31";
|
||||
|
||||
const crmCredential = {
|
||||
id: 1,
|
||||
type: "salesforce_crm",
|
||||
key: {
|
||||
clientId: "test-client-id",
|
||||
},
|
||||
userId: 1,
|
||||
teamId: null,
|
||||
appId: "salesforce",
|
||||
invalid: false,
|
||||
user: { email: "[email protected]" },
|
||||
};
|
||||
|
||||
await createCredentials([crmCredential]);
|
||||
|
||||
mockCrmApp("salesforce", {
|
||||
getContacts: [
|
||||
{
|
||||
id: "contact-id",
|
||||
email: "[email protected]",
|
||||
ownerEmail: "[email protected]",
|
||||
},
|
||||
],
|
||||
createContacts: [{ id: "contact-id", email: "[email protected]" }],
|
||||
});
|
||||
|
||||
await createBookingScenario({
|
||||
eventTypes: [
|
||||
{
|
||||
id: 1,
|
||||
slotInterval: 60,
|
||||
length: 60,
|
||||
hosts: [
|
||||
{
|
||||
userId: 101,
|
||||
isFixed: false,
|
||||
},
|
||||
{
|
||||
userId: 102,
|
||||
isFixed: false,
|
||||
},
|
||||
],
|
||||
schedulingType: "ROUND_ROBIN",
|
||||
metadata: {
|
||||
apps: {
|
||||
salesforce: {
|
||||
enabled: true,
|
||||
appCategories: ["crm"],
|
||||
roundRobinLeadSkip: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
users: [
|
||||
{
|
||||
...TestData.users.example,
|
||||
email: "[email protected]",
|
||||
id: 101,
|
||||
schedules: [TestData.schedules.IstNotAvailableForFullMonth("2024-06")],
|
||||
},
|
||||
{
|
||||
...TestData.users.example,
|
||||
email: "[email protected]",
|
||||
id: 102,
|
||||
schedules: [TestData.schedules.IstMorningShift],
|
||||
defaultScheduleId: 2,
|
||||
},
|
||||
],
|
||||
bookings: [],
|
||||
});
|
||||
|
||||
const scheduleWithLeadSkip = await getSchedule({
|
||||
input: {
|
||||
eventTypeId: 1,
|
||||
eventTypeSlug: "",
|
||||
startTime: `${startTime}T18:30:00.000Z`,
|
||||
endTime: `${endTime}T18:29:59.999Z`,
|
||||
timeZone: Timezones["+5:30"],
|
||||
isTeamEvent: true,
|
||||
teamMemberEmail: "[email protected]",
|
||||
orgSlug: null,
|
||||
},
|
||||
});
|
||||
|
||||
// slots for [email protected], because user is available in the first two weeks (prev month)
|
||||
expect(scheduleWithLeadSkip).toHaveDateDisabled({
|
||||
dateString: "2024-06-03",
|
||||
});
|
||||
});
|
||||
|
||||
test("correctly gets slot for event when contact owner is available in the first two weeks and it's end of the month", async () => {
|
||||
vi.setSystemTime("2024-05-23T00:00:13Z");
|
||||
|
||||
const startTime = "2024-05-23";
|
||||
const endTime = "2024-05-31";
|
||||
|
||||
const crmCredential = {
|
||||
id: 1,
|
||||
type: "salesforce_crm",
|
||||
key: {
|
||||
clientId: "test-client-id",
|
||||
},
|
||||
userId: 1,
|
||||
teamId: null,
|
||||
appId: "salesforce",
|
||||
invalid: false,
|
||||
user: { email: "[email protected]" },
|
||||
};
|
||||
|
||||
await createCredentials([crmCredential]);
|
||||
|
||||
mockCrmApp("salesforce", {
|
||||
getContacts: [
|
||||
{
|
||||
id: "contact-id",
|
||||
email: "[email protected]",
|
||||
ownerEmail: "[email protected]",
|
||||
},
|
||||
],
|
||||
createContacts: [{ id: "contact-id", email: "[email protected]" }],
|
||||
});
|
||||
|
||||
await createBookingScenario({
|
||||
eventTypes: [
|
||||
{
|
||||
id: 1,
|
||||
slotInterval: 60,
|
||||
length: 60,
|
||||
hosts: [
|
||||
{
|
||||
userId: 101,
|
||||
isFixed: false,
|
||||
},
|
||||
{
|
||||
userId: 102,
|
||||
isFixed: false,
|
||||
},
|
||||
],
|
||||
schedulingType: "ROUND_ROBIN",
|
||||
metadata: {
|
||||
apps: {
|
||||
salesforce: {
|
||||
enabled: true,
|
||||
appCategories: ["crm"],
|
||||
roundRobinLeadSkip: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
users: [
|
||||
{
|
||||
...TestData.users.example,
|
||||
email: "[email protected]",
|
||||
id: 101,
|
||||
schedules: [TestData.schedules.IstNotAvailableForFullMonth("2024-05")],
|
||||
},
|
||||
{
|
||||
...TestData.users.example,
|
||||
email: "[email protected]",
|
||||
id: 102,
|
||||
schedules: [TestData.schedules.IstMorningShift],
|
||||
defaultScheduleId: 2,
|
||||
},
|
||||
],
|
||||
bookings: [],
|
||||
});
|
||||
|
||||
const scheduleWithLeadSkip = await getSchedule({
|
||||
input: {
|
||||
eventTypeId: 1,
|
||||
eventTypeSlug: "",
|
||||
startTime: `${startTime}T18:30:00.000Z`,
|
||||
endTime: `${endTime}T18:29:59.999Z`,
|
||||
timeZone: Timezones["+5:30"],
|
||||
isTeamEvent: true,
|
||||
teamMemberEmail: "[email protected]",
|
||||
orgSlug: null,
|
||||
},
|
||||
});
|
||||
|
||||
// slots for [email protected], because user is available in the first two weeks (next month)
|
||||
expect(scheduleWithLeadSkip).toHaveDateDisabled({
|
||||
dateString: "2024-05-29",
|
||||
});
|
||||
});
|
||||
|
||||
test("Negative case(i.e. skipContactOwner is true) - when teamMemberEmail is provided but not used", async () => {
|
||||
vi.setSystemTime("2024-05-21T00:00:13Z");
|
||||
|
||||
|
||||
@@ -1169,6 +1169,76 @@ export const TestData = {
|
||||
availability: [],
|
||||
timeZone: Timezones["+5:30"],
|
||||
},
|
||||
IstNotAvailableForFullMonth: (monthYear: string) => {
|
||||
const [year, month] = monthYear.split("-").map(Number); // Expecting format 'YYYY-MM'
|
||||
const startOfMonth = new Date(year, month - 1, 1);
|
||||
const endOfMonth = new Date(year, month, 0);
|
||||
const availability: {
|
||||
days: number[];
|
||||
startTime: Date;
|
||||
endTime: Date;
|
||||
date: string | null;
|
||||
}[] = [
|
||||
{
|
||||
days: [0, 1, 2, 3, 4, 5, 6],
|
||||
startTime: new Date("1970-01-01T18:00:00.000Z"),
|
||||
endTime: new Date("1970-01-01T22:00:00.000Z"),
|
||||
date: null,
|
||||
},
|
||||
];
|
||||
// Generate unavailable dates for the entire month
|
||||
const currentDate = new Date(startOfMonth);
|
||||
while (currentDate <= endOfMonth) {
|
||||
const dateString = currentDate.toISOString().split("T")[0];
|
||||
availability.push({
|
||||
days: [],
|
||||
startTime: new Date(`${dateString}T00:00:00.000Z`),
|
||||
endTime: new Date(`${dateString}T00:00:00.000Z`),
|
||||
date: dateString,
|
||||
});
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
}
|
||||
|
||||
return {
|
||||
name: "Unavailable for the entire month, then available from 18:00AM to 22:00 IST",
|
||||
availability,
|
||||
timeZone: Timezones["+5:30"],
|
||||
};
|
||||
},
|
||||
IstWorkHoursWithFirstTwoWeeksUnavailable: (dateString: string) => {
|
||||
const date = new Date(dateString);
|
||||
const availability: {
|
||||
days: number[];
|
||||
startTime: Date;
|
||||
endTime: Date;
|
||||
date: string | null;
|
||||
}[] = [
|
||||
{
|
||||
days: [0, 1, 2, 3, 4, 5, 6],
|
||||
startTime: new Date("1970-01-01T18:00:00.000Z"),
|
||||
endTime: new Date("1970-01-01T22:00:00.000Z"),
|
||||
date: null,
|
||||
},
|
||||
];
|
||||
|
||||
// Generate dateoverride for each day in thes first two weeks
|
||||
for (let i = 0; i < 15; i++) {
|
||||
const dateString = date.toISOString().split("T")[0];
|
||||
availability.push({
|
||||
days: [],
|
||||
startTime: new Date(`${dateString}T00:00:00.000Z`),
|
||||
endTime: new Date(`${dateString}T00:00:00.000Z`),
|
||||
date: dateString,
|
||||
});
|
||||
date.setDate(date.getDate() + 1);
|
||||
}
|
||||
|
||||
return {
|
||||
name: "Unavailable for the first two weeks, then available from 18:00 to 22:00 IST",
|
||||
availability,
|
||||
timeZone: Timezones["+5:30"],
|
||||
};
|
||||
},
|
||||
IstWorkHoursWithDateOverride: (dateString: string) => ({
|
||||
name: "9:30AM to 6PM in India - 4:00AM to 12:30PM in GMT but with a Date Override for 2PM to 6PM IST(in GST time it is 8:30AM to 12:30PM)",
|
||||
availability: [
|
||||
|
||||
Reference in new issue
Block a user