diff --git a/src/resolvers/userResolver.test.ts b/src/resolvers/userResolver.test.ts index 6c57cc616..8ed404683 100644 --- a/src/resolvers/userResolver.test.ts +++ b/src/resolvers/userResolver.test.ts @@ -355,8 +355,7 @@ function updateUserTestCases() { const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); const accessToken = await generateTestAccessToken(user.id); const updateUserData = { - firstName: 'firstName', - lastName: 'lastName', + fullName: 'firstName lastName', email: 'giveth@gievth.com', avatar: 'pinata address', url: 'website url', @@ -379,22 +378,18 @@ function updateUserTestCases() { id: user.id, }, }); - assert.equal(updatedUser?.firstName, updateUserData.firstName); - assert.equal(updatedUser?.lastName, updateUserData.lastName); + assert.equal(updatedUser?.firstName, updateUserData.fullName.split(' ')[0]); + assert.equal(updatedUser?.lastName, updateUserData.fullName.split(' ')[1]); assert.equal(updatedUser?.email, updateUserData.email); assert.equal(updatedUser?.avatar, updateUserData.avatar); assert.equal(updatedUser?.url, updateUserData.url); - assert.equal( - updatedUser?.name, - updateUserData.firstName + ' ' + updateUserData.lastName, - ); + assert.equal(updatedUser?.name, updateUserData.fullName); }); it('should update user with sending all data and then call userByAddress query', async () => { const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); const accessToken = await generateTestAccessToken(user.id); const updateUserData = { - firstName: 'firstName', - lastName: 'lastName', + fullName: 'firstName lastName', email: 'giveth@gievth.com', avatar: 'pinata address', url: 'website url', @@ -417,18 +412,15 @@ function updateUserTestCases() { id: user.id, }, }); - assert.equal(updatedUser?.firstName, updateUserData.firstName); - assert.equal(updatedUser?.lastName, updateUserData.lastName); + assert.equal(updatedUser?.firstName, updateUserData.fullName.split(' ')[0]); + assert.equal(updatedUser?.lastName, updateUserData.fullName.split(' ')[1]); assert.equal(updatedUser?.email, updateUserData.email); assert.equal(updatedUser?.avatar, updateUserData.avatar); assert.equal(updatedUser?.url, updateUserData.url); - assert.equal( - updatedUser?.name, - updateUserData.firstName + ' ' + updateUserData.lastName, - ); + assert.equal(updatedUser?.name, updateUserData.fullName); }); - it('should fail when dont sending firstName and lastName', async () => { + it('should fail when dont sending fullName', async () => { const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); const accessToken = await generateTestAccessToken(user.id); const updateUserData = { @@ -451,14 +443,14 @@ function updateUserTestCases() { assert.equal( result.data.errors[0].message, - errorMessages.BOTH_FIRST_NAME_AND_LAST_NAME_CANT_BE_EMPTY, + errorMessages.FULL_NAME_CAN_NOT_BE_EMPTY, ); }); it('should fail when email is invalid', async () => { const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); const accessToken = await generateTestAccessToken(user.id); const updateUserData = { - firstName: 'firstName', + fullName: 'fullName', email: 'giveth', avatar: 'pinata address', url: 'website url', @@ -482,7 +474,7 @@ function updateUserTestCases() { const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); const accessToken = await generateTestAccessToken(user.id); const updateUserData = { - firstName: 'firstName', + fullName: 'fullName', email: 'giveth @ giveth.com', avatar: 'pinata address', url: 'website url', @@ -502,40 +494,11 @@ function updateUserTestCases() { assert.equal(result.data.errors[0].message, errorMessages.INVALID_EMAIL); }); - it('should fail when sending empty string for firstName', async () => { - const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); - const accessToken = await generateTestAccessToken(user.id); - const updateUserData = { - firstName: '', - lastName: 'test lastName', - email: 'giveth @ giveth.com', - avatar: 'pinata address', - url: 'website url', - }; - const result = await axios.post( - graphqlUrl, - { - query: updateUser, - variables: updateUserData, - }, - { - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }, - ); - - assert.equal( - result.data.errors[0].message, - errorMessages.FIRSTNAME_CANT_BE_EMPTY_STRING, - ); - }); - it('should fail when sending empty string for lastName', async () => { + it('should fail when sending empty string for fullName', async () => { const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); const accessToken = await generateTestAccessToken(user.id); const updateUserData = { - lastName: '', - firstName: 'firstName', + fullName: '', email: 'giveth @ giveth.com', avatar: 'pinata address', url: 'website url', @@ -555,94 +518,121 @@ function updateUserTestCases() { assert.equal( result.data.errors[0].message, - errorMessages.LASTNAME_CANT_BE_EMPTY_STRING, + errorMessages.FULL_NAME_CAN_NOT_BE_EMPTY, ); }); - - it('should update user and name of user when sending just lastName', async () => { - const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); - const firstName = 'firstName'; - user.firstName = firstName; - user.name = firstName; - await user.save(); - const accessToken = await generateTestAccessToken(user.id); - const updateUserData = { - email: 'giveth@gievth.com', - avatar: 'pinata address', - url: 'website url', - lastName: new Date().getTime().toString(), - }; - const result = await axios.post( - graphqlUrl, - { - query: updateUser, - variables: updateUserData, - }, - { - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }, - ); - - assert.isTrue(result.data.data.updateUser); - const updatedUser = await User.findOne({ - where: { - id: user.id, - }, - }); - assert.equal(updatedUser?.email, updateUserData.email); - assert.equal(updatedUser?.avatar, updateUserData.avatar); - assert.equal(updatedUser?.url, updateUserData.url); - assert.equal(updatedUser?.name, firstName + ' ' + updateUserData.lastName); - assert.equal(updatedUser?.firstName, firstName); - }); - - it('should update user and name of user when sending just firstName', async () => { - const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); - const lastName = 'lastName'; - user.lastName = lastName; - user.name = lastName; - await user.save(); - const accessToken = await generateTestAccessToken(user.id); - const updateUserData = { - email: 'giveth@gievth.com', - avatar: 'pinata address', - url: 'website url', - firstName: new Date().getTime().toString(), - }; - const result = await axios.post( - graphqlUrl, - { - query: updateUser, - variables: updateUserData, - }, - { - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }, - ); - - assert.isTrue(result.data.data.updateUser); - const updatedUser = await User.findOne({ - where: { - id: user.id, - }, - }); - assert.equal(updatedUser?.email, updateUserData.email); - assert.equal(updatedUser?.avatar, updateUserData.avatar); - assert.equal(updatedUser?.url, updateUserData.url); - assert.equal(updatedUser?.name, updateUserData.firstName + ' ' + lastName); - assert.equal(updatedUser?.lastName, lastName); - }); + // it('should fail when sending empty string for lastName', async () => { + // const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); + // const accessToken = await generateTestAccessToken(user.id); + // const updateUserData = { + // lastName: '', + // firstName: 'firstName', + // email: 'giveth @ giveth.com', + // avatar: 'pinata address', + // url: 'website url', + // }; + // const result = await axios.post( + // graphqlUrl, + // { + // query: updateUser, + // variables: updateUserData, + // }, + // { + // headers: { + // Authorization: `Bearer ${accessToken}`, + // }, + // }, + // ); + // + // assert.equal( + // result.data.errors[0].message, + // errorMessages.LASTNAME_CANT_BE_EMPTY_STRING, + // ); + // }); + // + // it('should update user and name of user when sending just lastName', async () => { + // const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); + // const firstName = 'firstName'; + // user.firstName = firstName; + // user.name = firstName; + // await user.save(); + // const accessToken = await generateTestAccessToken(user.id); + // const updateUserData = { + // email: 'giveth@gievth.com', + // avatar: 'pinata address', + // url: 'website url', + // lastName: new Date().getTime().toString(), + // }; + // const result = await axios.post( + // graphqlUrl, + // { + // query: updateUser, + // variables: updateUserData, + // }, + // { + // headers: { + // Authorization: `Bearer ${accessToken}`, + // }, + // }, + // ); + // + // assert.isTrue(result.data.data.updateUser); + // const updatedUser = await User.findOne({ + // where: { + // id: user.id, + // }, + // }); + // assert.equal(updatedUser?.email, updateUserData.email); + // assert.equal(updatedUser?.avatar, updateUserData.avatar); + // assert.equal(updatedUser?.url, updateUserData.url); + // assert.equal(updatedUser?.name, firstName + ' ' + updateUserData.lastName); + // assert.equal(updatedUser?.firstName, firstName); + // }); + // + // it('should update user and name of user when sending just firstName', async () => { + // const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); + // const lastName = 'lastName'; + // user.lastName = lastName; + // user.name = lastName; + // await user.save(); + // const accessToken = await generateTestAccessToken(user.id); + // const updateUserData = { + // email: 'giveth@gievth.com', + // avatar: 'pinata address', + // url: 'website url', + // firstName: new Date().getTime().toString(), + // }; + // const result = await axios.post( + // graphqlUrl, + // { + // query: updateUser, + // variables: updateUserData, + // }, + // { + // headers: { + // Authorization: `Bearer ${accessToken}`, + // }, + // }, + // ); + // + // assert.isTrue(result.data.data.updateUser); + // const updatedUser = await User.findOne({ + // where: { + // id: user.id, + // }, + // }); + // assert.equal(updatedUser?.email, updateUserData.email); + // assert.equal(updatedUser?.avatar, updateUserData.avatar); + // assert.equal(updatedUser?.url, updateUserData.url); + // assert.equal(updatedUser?.name, updateUserData.firstName + ' ' + lastName); + // assert.equal(updatedUser?.lastName, lastName); + // }); it('should accept empty string for all fields except email', async () => { const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); const accessToken = await generateTestAccessToken(user.id); const updateUserData = { - firstName: 'test firstName', - lastName: 'test lastName', + fullName: 'firstName lastName', avatar: '', url: '', }; @@ -664,8 +654,8 @@ function updateUserTestCases() { id: user.id, }, }); - assert.equal(updatedUser?.firstName, updateUserData.firstName); - assert.equal(updatedUser?.lastName, updateUserData.lastName); + assert.equal(updatedUser?.firstName, updateUserData.fullName.split(' ')[0]); + assert.equal(updatedUser?.lastName, updateUserData.fullName.split(' ')[1]); assert.equal(updatedUser?.avatar, updateUserData.avatar); assert.equal(updatedUser?.url, updateUserData.url); }); diff --git a/src/resolvers/userResolver.ts b/src/resolvers/userResolver.ts index a1947e841..a90756095 100644 --- a/src/resolvers/userResolver.ts +++ b/src/resolvers/userResolver.ts @@ -31,7 +31,7 @@ import { import { logger } from '../utils/logger'; import { isWalletAddressInPurpleList } from '../repositories/projectAddressRepository'; import { addressHasDonated } from '../repositories/donationRepository'; -import { getOrttoPersonAttributes } from '../adapters/notifications/NotificationCenterAdapter'; +// import { getOrttoPersonAttributes } from '../adapters/notifications/NotificationCenterAdapter'; import { retrieveActiveQfRoundUserMBDScore } from '../repositories/qfRoundRepository'; @ObjectType() @@ -131,13 +131,12 @@ export class UserResolver { @Mutation(_returns => Boolean) async updateUser( - @Arg('firstName', { nullable: true }) firstName: string, - @Arg('lastName', { nullable: true }) lastName: string, + @Arg('fullName', { nullable: true }) fullName: string, @Arg('location', { nullable: true }) location: string, @Arg('email', { nullable: true }) email: string, @Arg('url', { nullable: true }) url: string, @Arg('avatar', { nullable: true }) avatar: string, - @Arg('newUser', { nullable: true }) newUser: boolean, + // @Arg('newUser', { nullable: true }) newUser: boolean, @Ctx() { req: { user } }: ApolloContext, ): Promise { if (!user) @@ -148,29 +147,18 @@ export class UserResolver { if (!dbUser) { return false; } - if (!dbUser.name && !firstName && !lastName) { - throw new Error( - i18n.__( - translationErrorMessagesKeys.BOTH_FIRST_NAME_AND_LAST_NAME_CANT_BE_EMPTY, - ), - ); - } - if (firstName === '') { - throw new Error( - i18n.__(translationErrorMessagesKeys.FIRSTNAME_CANT_BE_EMPTY_STRING), - ); - } - if (lastName === '') { + + if (!fullName || fullName === '') { throw new Error( - i18n.__(translationErrorMessagesKeys.LASTNAME_CANT_BE_EMPTY_STRING), + i18n.__(translationErrorMessagesKeys.FULL_NAME_CAN_NOT_BE_EMPTY), ); } - if (firstName) { - dbUser.firstName = firstName; - } - if (lastName) { - dbUser.lastName = lastName; - } + dbUser.name = fullName.trim(); + const [first, ...rest] = fullName.split(' '); + dbUser.firstName = first; + dbUser.lastName = rest.join(' ') || ''; + + // Update other fields if (location !== undefined) { dbUser.location = location; } @@ -203,19 +191,18 @@ export class UserResolver { dbUser.avatar = avatar; } - dbUser.name = `${dbUser.firstName || ''} ${dbUser.lastName || ''}`.trim(); await dbUser.save(); - const orttoPerson = getOrttoPersonAttributes({ - firstName: dbUser.firstName, - lastName: dbUser.lastName, - email: dbUser.email, - userId: dbUser.id.toString(), - }); - await getNotificationAdapter().updateOrttoPeople([orttoPerson]); - if (newUser) { - await getNotificationAdapter().createOrttoProfile(dbUser); - } + // const orttoPerson = getOrttoPersonAttributes({ + // firstName: dbUser.firstName, + // lastName: dbUser.lastName, + // email: dbUser.email, + // userId: dbUser.id.toString(), + // }); + // await getNotificationAdapter().updateOrttoPeople([orttoPerson]); + // if (newUser) { + // await getNotificationAdapter().createOrttoProfile(dbUser); + // } return true; } diff --git a/src/utils/errorMessages.ts b/src/utils/errorMessages.ts index d61253b89..064c9fbec 100644 --- a/src/utils/errorMessages.ts +++ b/src/utils/errorMessages.ts @@ -180,6 +180,7 @@ export const errorMessages = { INCORRECT_CODE: 'The verification code you entered is incorrect.', NO_EMAIL_VERIFICATION_DATA: 'No email verification data found', CODE_EXPIRED: 'The verification code has expired. Please request a new code.', + FULL_NAME_CAN_NOT_BE_EMPTY: 'fullName cant be empty string', }; export const translationErrorMessagesKeys = { @@ -328,4 +329,5 @@ export const translationErrorMessagesKeys = { INCORRECT_CODE: 'INCORRECT_CODE', NO_EMAIL_VERIFICATION_DATA: 'NO_EMAIL_VERIFICATION_DATA', CODE_EXPIRED: 'CODE_EXPIRED', + FULL_NAME_CAN_NOT_BE_EMPTY: 'FULL_NAME_CAN_NOT_BE_EMPTY', }; diff --git a/src/utils/locales/en.json b/src/utils/locales/en.json index 8ebbc0f08..70798fa86 100644 --- a/src/utils/locales/en.json +++ b/src/utils/locales/en.json @@ -105,5 +105,6 @@ "NO_EMAIL_PROVIDED": "No email address provided.", "INCORRECT_CODE": "The verification code you entered is incorrect.", "NO_EMAIL_VERIFICATION_DATA": "No email verification data found", - "CODE_EXPIRED": "The verification code has expired. Please request a new code." + "CODE_EXPIRED": "The verification code has expired. Please request a new code.", + "FULL_NAME_CAN_NOT_BE_EMPTY": "fullName cant be empty string" } diff --git a/src/utils/locales/es.json b/src/utils/locales/es.json index 3bb846d92..e606b61e3 100644 --- a/src/utils/locales/es.json +++ b/src/utils/locales/es.json @@ -102,5 +102,6 @@ "NO_EMAIL_PROVIDED": "No se ha proporcionado una dirección de correo electrónico.", "INCORRECT_CODE": "El código de verificación que ingresaste es incorrecto.", "NO_EMAIL_VERIFICATION_DATA": "No se encontraron datos de verificación de correo electrónico.", - "CODE_EXPIRED": "El código de verificación ha expirado. Por favor, solicita un nuevo código." + "CODE_EXPIRED": "El código de verificación ha expirado. Por favor, solicita un nuevo código.", + "FULL_NAME_CAN_NOT_BE_EMPTY": "El nombre completo no puede estar vacío." } diff --git a/test/graphqlQueries.ts b/test/graphqlQueries.ts index 714fb51f7..1b4cf3630 100644 --- a/test/graphqlQueries.ts +++ b/test/graphqlQueries.ts @@ -1134,19 +1134,15 @@ export const updateUser = ` $url: String $location: String $email: String - $lastName: String - $firstName: String + $fullName: String $avatar: String - $newUser: Boolean ) { updateUser( url: $url location: $location email: $email - firstName: $firstName - lastName: $lastName + fullName: $fullName avatar: $avatar - newUser: $newUser ) } `;