From b77a5004e0d9bd9534f55ea2fa6dd9ef830b88cc Mon Sep 17 00:00:00 2001 From: kkatusic Date: Tue, 26 Nov 2024 15:38:19 +0100 Subject: [PATCH] separated checking for solana and etherium users --- src/resolvers/projectResolver.test.ts | 2 ++ src/resolvers/projectResolver.ts | 25 ++++++++++++++ src/resolvers/userResolver.ts | 48 +++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/resolvers/projectResolver.test.ts b/src/resolvers/projectResolver.test.ts index 322308734..1bfad93fa 100644 --- a/src/resolvers/projectResolver.test.ts +++ b/src/resolvers/projectResolver.test.ts @@ -5529,6 +5529,7 @@ function editProjectUpdateTestCases() { walletAddress: generateRandomEtheriumAddress(), loginType: 'wallet', firstName: 'testEditProjectUpdateFateme', + isEmailVerified: true, }).save(); const accessToken = await generateTestAccessToken(user.id); const projectUpdateCount = await ProjectUpdate.count(); @@ -5644,6 +5645,7 @@ function deleteProjectUpdateTestCases() { walletAddress: generateRandomEtheriumAddress(), loginType: 'wallet', firstName: 'testDeleteProjectUpdateFateme', + isEmailVerified: true, }).save(); const accessToken = await generateTestAccessToken(user.id); const projectUpdateCount = await ProjectUpdate.count(); diff --git a/src/resolvers/projectResolver.ts b/src/resolvers/projectResolver.ts index 5025787be..e9740260d 100644 --- a/src/resolvers/projectResolver.ts +++ b/src/resolvers/projectResolver.ts @@ -1576,6 +1576,11 @@ export class ProjectResolver { if (!owner) throw new Error(i18n.__(translationErrorMessagesKeys.USER_NOT_FOUND)); + // Check if user email is verified + if (owner && !owner.isEmailVerified) { + throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED)); + } + const project = await findProjectById(projectId); if (!project) @@ -1631,6 +1636,16 @@ export class ProjectResolver { ); } + const owner = await findUserById(user.userId); + + if (!owner) + throw new Error(i18n.__(translationErrorMessagesKeys.USER_NOT_FOUND)); + + // Check if user email is verified + if (owner && !owner.isEmailVerified) { + throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED)); + } + const update = await ProjectUpdate.findOne({ where: { id: updateId } }); if (!update) throw new Error( @@ -1663,6 +1678,16 @@ export class ProjectResolver { i18n.__(translationErrorMessagesKeys.AUTHENTICATION_REQUIRED), ); + const owner = await findUserById(user.userId); + + if (!owner) + throw new Error(i18n.__(translationErrorMessagesKeys.USER_NOT_FOUND)); + + // Check if user email is verified + if (owner && !owner.isEmailVerified) { + throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED)); + } + const update = await ProjectUpdate.findOne({ where: { id: updateId } }); if (!update) throw new Error( diff --git a/src/resolvers/userResolver.ts b/src/resolvers/userResolver.ts index 3043c6880..305afea18 100644 --- a/src/resolvers/userResolver.ts +++ b/src/resolvers/userResolver.ts @@ -32,6 +32,7 @@ import { getOrttoPersonAttributes } from '../adapters/notifications/Notification import { retrieveActiveQfRoundUserMBDScore } from '../repositories/qfRoundRepository'; import { getLoggedInUser } from '../services/authorizationServices'; import { generateRandomNumericCode } from '../utils/utils'; +import { isSolanaAddress } from '../utils/networks'; @ObjectType() class UserRelatedAddressResponse { @@ -173,11 +174,11 @@ export class UserResolver { if (location !== undefined) { dbUser.location = location; } - // Check if user email is verified and it's not the first update + // Check if user email is verified if (!dbUser.isEmailVerified) { throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED)); } - // Check if old email is verified and user entered new one and it's not the first update + // Check if old email is verified and user entered new one if (dbUser.isEmailVerified && email !== dbUser.email) { throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED)); } @@ -289,11 +290,46 @@ export class UserResolver { } // Check do we have an email already in the database and is it verified - const isEmailAlreadyUsed = await User.findOne({ - where: { email: email, isEmailVerified: true }, - }); + // We need here to check if user wallet solana address or not + // User can have sam email for solana end ethereum wallet + const isSolanaAddressCheck = user?.walletAddress + ? isSolanaAddress(user.walletAddress) + : false; + let isEmailAlreadyUsed; + if (isSolanaAddressCheck) { + const rawQuery = ` + SELECT * + FROM public."user" + WHERE "email" = $1 + AND "isEmailVerified" = true + AND ( + "walletAddress" = LEFT("walletAddress", 43) OR + "walletAddress" = LEFT("walletAddress", 44) + ) + AND "walletAddress" ~ '^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$' + LIMIT 1 + `; + + isEmailAlreadyUsed = await User.query(rawQuery, [email]); + } else { + const rawQuery = ` + SELECT * + FROM public."user" + WHERE "email" = $1 + AND "isEmailVerified" = true + AND "walletAddress" = LEFT("walletAddress", 42) + AND "walletAddress" ~ '^0x[0-9a-fA-F]{40}$' + LIMIT 1 + `; + + isEmailAlreadyUsed = await User.query(rawQuery, [email]); + } - if (isEmailAlreadyUsed && isEmailAlreadyUsed.id !== user.id) { + if ( + isEmailAlreadyUsed && + isEmailAlreadyUsed.length > 0 && + isEmailAlreadyUsed.id !== user.id + ) { return 'EMAIL_EXIST'; }