-
Notifications
You must be signed in to change notification settings - Fork 9.8k
fix: Host schedule not updating upon updating/deleting default schedule #20750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
39001e6
fix: host schdule not updating
anikdhabal c1b683f
Update util.ts
anikdhabal 3e3bf4f
tweak
anikdhabal f4c031f
Merge branch 'main' into host-schedule
anikdhabal d0a392b
Update delete.handler.ts
anikdhabal 4758936
add test
anikdhabal 41b0b42
Merge branch 'main' into host-schedule
anikdhabal 68fee64
update
anikdhabal 1f54ca4
Update util.test.ts
anikdhabal 17da897
Merge branch 'main' into host-schedule
anikdhabal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import prisma from "@calcom/prisma"; | ||
|
||
export class HostRepository { | ||
static async updateHostsSchedule(userId: number, oldScheduleId: number, newScheduleId: number) { | ||
return await prisma.host.updateMany({ | ||
where: { | ||
userId, | ||
scheduleId: oldScheduleId, | ||
}, | ||
data: { | ||
scheduleId: newScheduleId, | ||
}, | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
packages/trpc/server/routers/viewer/availability/util.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import { describe, expect, it, vi, beforeEach } from "vitest"; | ||
|
||
import { PrismaClient } from "@calcom/prisma"; | ||
|
||
import { | ||
getDefaultScheduleId, | ||
hasDefaultSchedule, | ||
setupDefaultSchedule, | ||
updateHostsWithNewDefaultSchedule, | ||
} from "./util"; | ||
|
||
vi.mock("@calcom/prisma", () => { | ||
const mockPrisma = { | ||
user: { | ||
findUnique: vi.fn(), | ||
update: vi.fn(), | ||
}, | ||
schedule: { | ||
findFirst: vi.fn(), | ||
}, | ||
host: { | ||
updateMany: vi.fn(), | ||
}, | ||
}; | ||
return { | ||
__esModule: true, | ||
default: mockPrisma, | ||
PrismaClient: vi.fn(() => mockPrisma), | ||
}; | ||
}); | ||
|
||
describe("Availability Utils", () => { | ||
let prisma: PrismaClient; | ||
|
||
beforeEach(() => { | ||
prisma = new PrismaClient(); | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("getDefaultScheduleId", () => { | ||
it("should return defaultScheduleId if user has one", async () => { | ||
const userId = 1; | ||
const defaultScheduleId = 123; | ||
|
||
(prisma.user.findUnique as any).mockResolvedValue({ | ||
defaultScheduleId, | ||
}); | ||
|
||
const result = await getDefaultScheduleId(userId, prisma); | ||
|
||
expect(prisma.user.findUnique).toHaveBeenCalledWith({ | ||
where: { id: userId }, | ||
select: { defaultScheduleId: true }, | ||
}); | ||
expect(result).toBe(defaultScheduleId); | ||
}); | ||
|
||
it("should find and return first schedule if user has no defaultScheduleId", async () => { | ||
const userId = 1; | ||
const scheduleId = 456; | ||
|
||
(prisma.user.findUnique as any).mockResolvedValue({ | ||
defaultScheduleId: null, | ||
}); | ||
|
||
(prisma.schedule.findFirst as any).mockResolvedValue({ | ||
id: scheduleId, | ||
}); | ||
|
||
const result = await getDefaultScheduleId(userId, prisma); | ||
|
||
expect(prisma.user.findUnique).toHaveBeenCalledWith({ | ||
where: { id: userId }, | ||
select: { defaultScheduleId: true }, | ||
}); | ||
expect(prisma.schedule.findFirst).toHaveBeenCalledWith({ | ||
where: { userId }, | ||
select: { id: true }, | ||
}); | ||
expect(result).toBe(scheduleId); | ||
}); | ||
|
||
it("should throw error if no schedules found", async () => { | ||
const userId = 1; | ||
|
||
(prisma.user.findUnique as any).mockResolvedValue({ | ||
defaultScheduleId: null, | ||
}); | ||
|
||
(prisma.schedule.findFirst as any).mockResolvedValue(null); | ||
|
||
await expect(getDefaultScheduleId(userId, prisma)).rejects.toThrow("No schedules found for user"); | ||
}); | ||
}); | ||
|
||
describe("hasDefaultSchedule", () => { | ||
it("should return true if user has defaultScheduleId", async () => { | ||
const user = { id: 1, defaultScheduleId: 123 }; | ||
|
||
const result = await hasDefaultSchedule(user, prisma); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return true if user has a schedule", async () => { | ||
const user = { id: 1, defaultScheduleId: null }; | ||
|
||
(prisma.schedule.findFirst as any).mockResolvedValue({ | ||
id: 456, | ||
}); | ||
|
||
const result = await hasDefaultSchedule(user, prisma); | ||
|
||
expect(prisma.schedule.findFirst).toHaveBeenCalledWith({ | ||
where: { userId: user.id }, | ||
}); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return false if user has no defaultScheduleId and no schedules", async () => { | ||
const user = { id: 1, defaultScheduleId: null }; | ||
|
||
(prisma.schedule.findFirst as any).mockResolvedValue(null); | ||
|
||
const result = await hasDefaultSchedule(user, prisma); | ||
|
||
expect(prisma.schedule.findFirst).toHaveBeenCalledWith({ | ||
where: { userId: user.id }, | ||
}); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("setupDefaultSchedule", () => { | ||
it("should update user with new defaultScheduleId", async () => { | ||
const userId = 1; | ||
const scheduleId = 123; | ||
const updatedUser = { id: userId, defaultScheduleId: scheduleId }; | ||
|
||
(prisma.user.update as any).mockResolvedValue(updatedUser); | ||
|
||
const result = await setupDefaultSchedule(userId, scheduleId, prisma); | ||
|
||
expect(prisma.user.update).toHaveBeenCalledWith({ | ||
where: { id: userId }, | ||
data: { defaultScheduleId: scheduleId }, | ||
}); | ||
expect(result).toEqual(updatedUser); | ||
}); | ||
}); | ||
|
||
describe("updateHostsWithNewDefaultSchedule", () => { | ||
it("should update hosts with new scheduleId", async () => { | ||
const userId = 1; | ||
const oldScheduleId = 123; | ||
const newScheduleId = 456; | ||
const updateResult = { count: 2 }; // 2 hosts updated | ||
|
||
(prisma.host.updateMany as any).mockResolvedValue(updateResult); | ||
|
||
const result = await updateHostsWithNewDefaultSchedule(userId, oldScheduleId, newScheduleId, prisma); | ||
|
||
expect(prisma.host.updateMany).toHaveBeenCalledWith({ | ||
where: { | ||
userId, | ||
scheduleId: oldScheduleId, | ||
}, | ||
data: { | ||
scheduleId: newScheduleId, | ||
}, | ||
}); | ||
expect(result).toEqual(updateResult); | ||
}); | ||
|
||
it("should handle case with no hosts to update", async () => { | ||
const userId = 1; | ||
const oldScheduleId = 123; | ||
const newScheduleId = 456; | ||
const updateResult = { count: 0 }; // No hosts updated | ||
|
||
(prisma.host.updateMany as any).mockResolvedValue(updateResult); | ||
|
||
const result = await updateHostsWithNewDefaultSchedule(userId, oldScheduleId, newScheduleId, prisma); | ||
|
||
expect(prisma.host.updateMany).toHaveBeenCalledWith({ | ||
where: { | ||
userId, | ||
scheduleId: oldScheduleId, | ||
}, | ||
data: { | ||
scheduleId: newScheduleId, | ||
}, | ||
}); | ||
expect(result).toEqual(updateResult); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.