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

separated checking for solana and etherium users #1876

Merged
merged 1 commit into from
Nov 26, 2024
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
2 changes: 2 additions & 0 deletions src/resolvers/projectResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions src/resolvers/projectResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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));
}

kkatusic marked this conversation as resolved.
Show resolved Hide resolved
const update = await ProjectUpdate.findOne({ where: { id: updateId } });
if (!update)
throw new Error(
Expand Down
48 changes: 42 additions & 6 deletions src/resolvers/userResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
kkatusic marked this conversation as resolved.
Show resolved Hide resolved
}
// 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));
}
kkatusic marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -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]);
}
kkatusic marked this conversation as resolved.
Show resolved Hide resolved

if (isEmailAlreadyUsed && isEmailAlreadyUsed.id !== user.id) {
if (
isEmailAlreadyUsed &&
isEmailAlreadyUsed.length > 0 &&
isEmailAlreadyUsed.id !== user.id
kkatusic marked this conversation as resolved.
Show resolved Hide resolved
) {
return 'EMAIL_EXIST';
}

Expand Down