Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Nov 1, 2023
1 parent f451208 commit 701292a
Showing 1 changed file with 48 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,50 +259,50 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
return new ArrayList<>();
}
List<String> emails = new ArrayList<>();
List<String> userIds = new ArrayList<>();
Map<String, String> userIdToEmailMap = new HashMap<>();
List<String> supertokensUserIds = new ArrayList<>();
for (UserIdAndEmail ue : userIdAndEmail) {
emails.add(ue.email);
userIds.add(ue.userId);
supertokensUserIds.add(ue.userId);
}

// We have external user id stored in the email verification table, so we need to fetch the mapped userids for
// calculating the verified emails

HashMap<String, String> userIdMappings = UserIdMappingQueries.getUserIdMappingWithUserIds_Transaction(start,
sqlCon, userIds);
HashMap<String, String> externalUserIdMappings = new HashMap<>();
HashMap<String, String> supertokensUserIdToExternalUserIdMap = UserIdMappingQueries.getUserIdMappingWithUserIds_Transaction(start,
sqlCon, supertokensUserIds);
HashMap<String, String> externalUserIdToSupertokensUserIdMap = new HashMap<>();

List<String> userIdsToQuery = new ArrayList<>();
for (String userId : userIds) {
if (userIdMappings.containsKey(userId)) {
userIdsToQuery.add(userIdMappings.get(userId));
externalUserIdMappings.put(userIdMappings.get(userId), userId);
List<String> supertokensOrExternalUserIdsToQuery = new ArrayList<>();
for (String userId : supertokensUserIds) {
if (supertokensUserIdToExternalUserIdMap.containsKey(userId)) {
supertokensOrExternalUserIdsToQuery.add(supertokensUserIdToExternalUserIdMap.get(userId));
externalUserIdToSupertokensUserIdMap.put(supertokensUserIdToExternalUserIdMap.get(userId), userId);
} else {
userIdsToQuery.add(userId);
externalUserIdMappings.put(userId, userId);
supertokensOrExternalUserIdsToQuery.add(userId);
externalUserIdToSupertokensUserIdMap.put(userId, userId);
}
}

Map<String, String> supertokensOrExternalUserIdToEmailMap = new HashMap<>();
for (UserIdAndEmail ue : userIdAndEmail) {
String userId = ue.userId;
if (userIdMappings.containsKey(userId)) {
userId = userIdMappings.get(userId);
String supertokensOrExternalUserId = ue.userId;
if (supertokensUserIdToExternalUserIdMap.containsKey(supertokensOrExternalUserId)) {
supertokensOrExternalUserId = supertokensUserIdToExternalUserIdMap.get(supertokensOrExternalUserId);
}
if (userIdToEmailMap.containsKey(userId)) {
if (supertokensOrExternalUserIdToEmailMap.containsKey(supertokensOrExternalUserId)) {
throw new RuntimeException("Found a bug!");
}
userIdToEmailMap.put(userId, ue.email);
supertokensOrExternalUserIdToEmailMap.put(supertokensOrExternalUserId, ue.email);
}

String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable()
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIdsToQuery.size()) +
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(supertokensOrExternalUserIdsToQuery.size()) +
") AND email IN (" + Utils.generateCommaSeperatedQuestionMarks(emails.size()) + ")";

return execute(sqlCon, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
int index = 2;
for (String userId : userIdsToQuery) {
for (String userId : supertokensOrExternalUserIdsToQuery) {
pst.setString(index++, userId);
}
for (String email : emails) {
Expand All @@ -311,10 +311,10 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
}, result -> {
List<String> res = new ArrayList<>();
while (result.next()) {
String userId = result.getString("user_id");
String supertokensOrExternalUserId = result.getString("user_id");
String email = result.getString("email");
if (Objects.equals(userIdToEmailMap.get(userId), email)) {
res.add(externalUserIdMappings.get(userId));
if (Objects.equals(supertokensOrExternalUserIdToEmailMap.get(supertokensOrExternalUserId), email)) {
res.add(externalUserIdToSupertokensUserIdMap.get(supertokensOrExternalUserId));
}
}
return res;
Expand All @@ -324,53 +324,51 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
public static List<String> isEmailVerified(Start start, AppIdentifier appIdentifier,
List<UserIdAndEmail> userIdAndEmail)
throws SQLException, StorageQueryException {

if (userIdAndEmail.isEmpty()) {
return new ArrayList<>();
}
List<String> emails = new ArrayList<>();
List<String> userIds = new ArrayList<>();
Map<String, String> userIdToEmailMap = new HashMap<>();
List<String> supertokensUserIds = new ArrayList<>();

for (UserIdAndEmail ue : userIdAndEmail) {
emails.add(ue.email);
userIds.add(ue.userId);
supertokensUserIds.add(ue.userId);
}

// We have external user id stored in the email verification table, so we need to fetch the mapped userids for
// calculating the verified emails

HashMap<String, String> userIdMappings = UserIdMappingQueries.getUserIdMappingWithUserIds(start,
userIds);
HashMap<String, String> externalUserIdMappings = new HashMap<>();

List<String> userIdsToQuery = new ArrayList<>();
for (String userId : userIds) {
if (userIdMappings.containsKey(userId)) {
userIdsToQuery.add(userIdMappings.get(userId));
externalUserIdMappings.put(userIdMappings.get(userId), userId);
HashMap<String, String> supertokensUserIdToExternalUserIdMap = UserIdMappingQueries.getUserIdMappingWithUserIds(start,
supertokensUserIds);
HashMap<String, String> externalUserIdToSupertokensUserIdMap = new HashMap<>();
List<String> supertokensOrExternalUserIdsToQuery = new ArrayList<>();
for (String userId : supertokensUserIds) {
if (supertokensUserIdToExternalUserIdMap.containsKey(userId)) {
supertokensOrExternalUserIdsToQuery.add(supertokensUserIdToExternalUserIdMap.get(userId));
externalUserIdToSupertokensUserIdMap.put(supertokensUserIdToExternalUserIdMap.get(userId), userId);
} else {
userIdsToQuery.add(userId);
externalUserIdMappings.put(userId, userId);
supertokensOrExternalUserIdsToQuery.add(userId);
externalUserIdToSupertokensUserIdMap.put(userId, userId);
}
}

Map<String, String> supertokensOrExternalUserIdToEmailMap = new HashMap<>();
for (UserIdAndEmail ue : userIdAndEmail) {
String userId = ue.userId;
if (userIdMappings.containsKey(userId)) {
userId = userIdMappings.get(userId);
String supertokensOrExternalUserId = ue.userId;
if (supertokensUserIdToExternalUserIdMap.containsKey(supertokensOrExternalUserId)) {
supertokensOrExternalUserId = supertokensUserIdToExternalUserIdMap.get(supertokensOrExternalUserId);
}
if (userIdToEmailMap.containsKey(userId)) {
if (supertokensOrExternalUserIdToEmailMap.containsKey(supertokensOrExternalUserId)) {
throw new RuntimeException("Found a bug!");
}
userIdToEmailMap.put(userId, ue.email);
supertokensOrExternalUserIdToEmailMap.put(supertokensOrExternalUserId, ue.email);
}
String QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTable()
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(supertokensOrExternalUserIdsToQuery.size()) +
") AND email IN (" + Utils.generateCommaSeperatedQuestionMarks(emails.size()) + ")";

return execute(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
int index = 2;
for (String userId : userIdsToQuery) {
for (String userId : supertokensOrExternalUserIdsToQuery) {
pst.setString(index++, userId);
}
for (String email : emails) {
Expand All @@ -379,10 +377,10 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
}, result -> {
List<String> res = new ArrayList<>();
while (result.next()) {
String userId = result.getString("user_id");
String supertokensOrExternalUserId = result.getString("user_id");
String email = result.getString("email");
if (Objects.equals(userIdToEmailMap.get(userId), email)) {
res.add(externalUserIdMappings.get(userId));
if (Objects.equals(supertokensOrExternalUserIdToEmailMap.get(supertokensOrExternalUserId), email)) {
res.add(externalUserIdToSupertokensUserIdMap.get(supertokensOrExternalUserId));
}
}
return res;
Expand Down

0 comments on commit 701292a

Please sign in to comment.