Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/api/user/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ const mockEventService: jest.Mocked<Partial<EventService>> = {
};

const memberUpdateMock = jest.fn();
const memberUpdateManyMock = jest.fn();
const mockMemberPrisma: any = {
// Only the parts used by UserService need to be mocked
member: {
create: jest.fn(),
update: memberUpdateMock,
updateMany: memberUpdateManyMock,
},
};

Expand Down Expand Up @@ -1565,6 +1567,7 @@ describe('UserService', () => {
jest.clearAllMocks();

memberUpdateMock.mockResolvedValue(undefined);
memberUpdateManyMock.mockResolvedValue({ count: 1 });

// Mock checkEmailAvailabilityForUser
mockCheckEmail = jest
Expand Down Expand Up @@ -1656,8 +1659,8 @@ describe('UserService', () => {
{ userId: 1, handle: 'testuser' },
);
expect(result).toEqual(mockUser);
expect(memberUpdateMock).toHaveBeenCalledWith({
where: { userId },
expect(memberUpdateManyMock).toHaveBeenCalledWith({
where: { userId: BigInt(userId) },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The use of BigInt for userId in the where clause is inconsistent with other parts of the code where userId is used as a number. Ensure that the data type is consistent across the application to avoid potential type-related issues.

data: { email: newEmail.toLowerCase() },
});
});
Expand Down Expand Up @@ -1892,7 +1895,7 @@ describe('UserService', () => {
},
);

memberUpdateMock.mockRejectedValueOnce(
memberUpdateManyMock.mockRejectedValueOnce(
new Error('Member update failed'),
);

Expand All @@ -1903,8 +1906,8 @@ describe('UserService', () => {
);

expect(result).toEqual(mockUser);
expect(memberUpdateMock).toHaveBeenCalledWith({
where: { userId },
expect(memberUpdateManyMock).toHaveBeenCalledWith({
where: { userId: BigInt(userId) },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The use of BigInt for userId in the where clause is inconsistent with other parts of the code where userId is used as a number. Ensure that the data type is consistent across the application to avoid potential type-related issues.

data: { email: newEmail.toLowerCase() },
});
expect(loggerErrorSpy).toHaveBeenCalledWith(
Expand Down
9 changes: 5 additions & 4 deletions src/api/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1558,17 +1558,18 @@ export class UserService {
}

if (emailChanged) {
const memberUserId = BigInt(userId);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
The conversion of userId to BigInt is necessary for the where clause in the memberPrisma.member.update method. Ensure that userId is always a valid number that can be safely converted to BigInt to avoid runtime errors.

try {
await this.memberPrisma.member.update({
where: { userId },
const updateResult = await this.memberPrisma.member.update({
where: { userId: memberUserId },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[💡 maintainability]
Consider adding error handling for the updateResult to ensure that the update operation was successful. This can help in diagnosing issues if the update fails silently.

data: { email: normalizedEmail },
});
this.logger.log(
`Updated members.member email to ${normalizedEmail} for user ${userId}`,
`Updated members.member email to ${normalizedEmail} for user ${memberUserId}`,
);
} catch (error) {
this.logger.error(
`Failed to update members.member email for user ${userId}: ${error.message}`,
`Failed to update members.member email for user ${memberUserId}: ${error.message}`,
error.stack,
);
}
Expand Down
Loading