Skip to content

Commit 66c5f81

Browse files
fix: remove singular query that was using slow OR query (calcom#24715)
## What does this PR do? <!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Removes and replaces the singular query that was still using the slow OR query from prisma. It was mainly used on the public booking page and triggered after someone entered their email (600ms debounce delay). ## Visual Demo (For contributors especially) Internal change only nothing changes on the UI or business logic. #### Video Demo (if applicable): N/A #### Image Demo (if applicable): N/A ## Mandatory Tasks (DO NOT REMOVE) - [x] I have self-reviewed the code (A decent size PR without self-review might be rejected). - [x] I have updated the developer docs in /docs if this PR makes changes that would require a [documentation change](https://cal.com/docs). If N/A, write N/A here and check the checkbox. - [x] I confirm automated tests are in place that prove my fix is effective or that my feature works. ## How should this be tested? <!-- Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. Write details that help to start the tests --> Basically the same as calcom#24298 since it touches relevant code. 1. Have a user that has the setting to prevent impersonation on. 2. Go to another users public booking page of any meeting. 3. Try to enter the email of the first user into the booking for. 4. Wait 1 second and make sure the button shows "Verify email". ## Checklist <!-- Remove bullet points below that don't apply to you --> - I haven't read the [contributing guide](https://github.com/calcom/cal.com/blob/main/CONTRIBUTING.md) - My code doesn't follow the style guidelines of this project - I haven't commented my code, particularly in hard-to-understand areas - I haven't checked if my changes generate no new warnings
1 parent 81d9d84 commit 66c5f81

3 files changed

Lines changed: 5 additions & 34 deletions

File tree

packages/features/bookings/lib/handleNewBooking/test/booking-validations.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ vi.mock("@calcom/trpc/server/routers/viewer/auth/util", () => ({
2828
verifyCodeUnAuthenticated: vi.fn(),
2929
}));
3030

31-
const { mockFindManyByEmailsWithEmailVerificationSettings, mockFindByEmailWithEmailVerificationSetting } =
32-
vi.hoisted(() => ({
33-
mockFindManyByEmailsWithEmailVerificationSettings: vi.fn(),
34-
mockFindByEmailWithEmailVerificationSetting: vi.fn(),
35-
}));
31+
const { mockFindManyByEmailsWithEmailVerificationSettings } = vi.hoisted(() => ({
32+
mockFindManyByEmailsWithEmailVerificationSettings: vi.fn(),
33+
}));
3634

3735
vi.mock("@calcom/features/users/repositories/UserRepository", async (importOriginal) => {
3836
const actual = await importOriginal();
@@ -45,7 +43,6 @@ vi.mock("@calcom/features/users/repositories/UserRepository", async (importOrigi
4543
const realInstance = new OriginalUserRepository(prisma);
4644
realInstance.findManyByEmailsWithEmailVerificationSettings =
4745
mockFindManyByEmailsWithEmailVerificationSettings;
48-
realInstance.findByEmailWithEmailVerificationSetting = mockFindByEmailWithEmailVerificationSetting;
4946
return realInstance;
5047
}),
5148
};
@@ -61,7 +58,6 @@ function resetBlacklistedEmails() {
6158

6259
beforeEach(() => {
6360
mockFindManyByEmailsWithEmailVerificationSettings.mockResolvedValue([]);
64-
mockFindByEmailWithEmailVerificationSetting.mockResolvedValue(null);
6561
});
6662

6763
afterEach(() => {

packages/features/users/repositories/UserRepository.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -276,32 +276,6 @@ export class UserRepository {
276276
});
277277
return user;
278278
}
279-
async findByEmailWithEmailVerificationSetting({ email }: { email: string }) {
280-
const user = await this.prismaClient.user.findFirst({
281-
where: {
282-
OR: [
283-
{
284-
email: email.toLowerCase(),
285-
emailVerified: { not: null },
286-
},
287-
{
288-
secondaryEmails: {
289-
some: {
290-
email: email.toLowerCase(),
291-
emailVerified: { not: null },
292-
},
293-
},
294-
},
295-
],
296-
},
297-
select: {
298-
id: true,
299-
email: true,
300-
requiresBookerEmailVerification: true,
301-
},
302-
});
303-
return user;
304-
}
305279

306280
async findManyByEmailsWithEmailVerificationSettings({ emails }: { emails: string[] }) {
307281
const normalizedEmails = emails.map((e) => e.toLowerCase());

packages/trpc/server/routers/publicViewer/checkIfUserEmailVerificationRequired.handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const checkEmailVerificationRequired = async ({
3434
}
3535

3636
const userRepo = new UserRepository(prisma);
37-
const user = await userRepo.findByEmailWithEmailVerificationSetting({ email: baseEmail });
37+
const users = await userRepo.findManyByEmailsWithEmailVerificationSettings({ emails: [baseEmail] });
38+
const user = users[0];
3839

3940
if (user?.requiresBookerEmailVerification && baseEmail.toLowerCase() !== userSessionEmail?.toLowerCase()) {
4041
log.warn(`user email requiring verification: ${baseEmail}`);

0 commit comments

Comments
 (0)