Skip to content

Commit

Permalink
User Update email already exists error fix (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitishkumar333 authored Sep 20, 2023
1 parent 0312cf8 commit ef33a33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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

0 comments on commit ef33a33

Please sign in to comment.