Skip to content
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

UserUpdate email already exists error fix #1385

Merged
merged 1 commit into from
Sep 20, 2023
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
6 changes: 3 additions & 3 deletions src/resolvers/Mutation/updateUserProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export const updateUserProfile: MutationResolvers["updateUserProfile"] = async (
);
}

if (args.data?.email !== undefined) {
const userWithEmailExists = await User.find({
if (args.data?.email && args.data?.email !== currentUser?.email) {
const userWithEmailExists = await User.findOne({
email: args.data?.email,
});

if (userWithEmailExists.length > 0) {
if (userWithEmailExists) {
throw new errors.ConflictError(
requestContext.translate(EMAIL_ALREADY_EXISTS_ERROR.MESSAGE),
EMAIL_ALREADY_EXISTS_ERROR.MESSAGE,
Expand Down
23 changes: 23 additions & 0 deletions tests/resolvers/Mutation/updateUserProfile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,29 @@ describe("resolvers -> Mutation -> updateUserProfile", () => {
}
});

it(`updates if email not changed by user`, async () => {
const args: MutationUpdateUserProfileArgs = {
data: {
email: testUser.email,
},
};

const context = {
userId: testUser._id,
};

const updateUserProfilePayload = await updateUserProfileResolver?.(
{},
args,
context
);

expect(updateUserProfilePayload).toEqual({
...testUser.toObject(),
image: null,
});
});

it(`updates current user's user object when any single argument(email) is given w/0 changing other fields `, async () => {
const args: MutationUpdateUserProfileArgs = {
data: {
Expand Down
Loading