From ef33a33dd7a1135885ee74b6c4742e166cc570f8 Mon Sep 17 00:00:00 2001 From: Nitish Kumar <123248648+nitishkumar333@users.noreply.github.com> Date: Wed, 20 Sep 2023 20:22:30 +0530 Subject: [PATCH] User Update email already exists error fix (#1385) --- src/resolvers/Mutation/updateUserProfile.ts | 6 ++--- .../Mutation/updateUserProfile.spec.ts | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/resolvers/Mutation/updateUserProfile.ts b/src/resolvers/Mutation/updateUserProfile.ts index ae920bd481..b139260b3e 100644 --- a/src/resolvers/Mutation/updateUserProfile.ts +++ b/src/resolvers/Mutation/updateUserProfile.ts @@ -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, diff --git a/tests/resolvers/Mutation/updateUserProfile.spec.ts b/tests/resolvers/Mutation/updateUserProfile.spec.ts index 6ab0055a55..2559e8d642 100644 --- a/tests/resolvers/Mutation/updateUserProfile.spec.ts +++ b/tests/resolvers/Mutation/updateUserProfile.spec.ts @@ -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: {