@@ -259,26 +259,50 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
259259 return new ArrayList <>();
260260 }
261261 List <String > emails = new ArrayList <>();
262- List <String > userIds = new ArrayList <>();
263- Map <String , String > userIdToEmailMap = new HashMap <>();
262+ List <String > supertokensUserIds = new ArrayList <>();
264263 for (UserIdAndEmail ue : userIdAndEmail ) {
265264 emails .add (ue .email );
266- userIds .add (ue .userId );
265+ supertokensUserIds .add (ue .userId );
267266 }
267+
268+ // We have external user id stored in the email verification table, so we need to fetch the mapped userids for
269+ // calculating the verified emails
270+
271+ HashMap <String , String > supertokensUserIdToExternalUserIdMap = UserIdMappingQueries .getUserIdMappingWithUserIds_Transaction (start ,
272+ sqlCon , supertokensUserIds );
273+ HashMap <String , String > externalUserIdToSupertokensUserIdMap = new HashMap <>();
274+
275+ List <String > supertokensOrExternalUserIdsToQuery = new ArrayList <>();
276+ for (String userId : supertokensUserIds ) {
277+ if (supertokensUserIdToExternalUserIdMap .containsKey (userId )) {
278+ supertokensOrExternalUserIdsToQuery .add (supertokensUserIdToExternalUserIdMap .get (userId ));
279+ externalUserIdToSupertokensUserIdMap .put (supertokensUserIdToExternalUserIdMap .get (userId ), userId );
280+ } else {
281+ supertokensOrExternalUserIdsToQuery .add (userId );
282+ externalUserIdToSupertokensUserIdMap .put (userId , userId );
283+ }
284+ }
285+
286+ Map <String , String > supertokensOrExternalUserIdToEmailMap = new HashMap <>();
268287 for (UserIdAndEmail ue : userIdAndEmail ) {
269- if (userIdToEmailMap .containsKey (ue .userId )) {
288+ String supertokensOrExternalUserId = ue .userId ;
289+ if (supertokensUserIdToExternalUserIdMap .containsKey (supertokensOrExternalUserId )) {
290+ supertokensOrExternalUserId = supertokensUserIdToExternalUserIdMap .get (supertokensOrExternalUserId );
291+ }
292+ if (supertokensOrExternalUserIdToEmailMap .containsKey (supertokensOrExternalUserId )) {
270293 throw new RuntimeException ("Found a bug!" );
271294 }
272- userIdToEmailMap .put (ue . userId , ue .email );
295+ supertokensOrExternalUserIdToEmailMap .put (supertokensOrExternalUserId , ue .email );
273296 }
297+
274298 String QUERY = "SELECT * FROM " + getConfig (start ).getEmailVerificationTable ()
275- + " WHERE app_id = ? AND user_id IN (" + Utils .generateCommaSeperatedQuestionMarks (userIds .size ()) +
299+ + " WHERE app_id = ? AND user_id IN (" + Utils .generateCommaSeperatedQuestionMarks (supertokensOrExternalUserIdsToQuery .size ()) +
276300 ") AND email IN (" + Utils .generateCommaSeperatedQuestionMarks (emails .size ()) + ")" ;
277301
278302 return execute (sqlCon , QUERY , pst -> {
279303 pst .setString (1 , appIdentifier .getAppId ());
280304 int index = 2 ;
281- for (String userId : userIds ) {
305+ for (String userId : supertokensOrExternalUserIdsToQuery ) {
282306 pst .setString (index ++, userId );
283307 }
284308 for (String email : emails ) {
@@ -287,10 +311,10 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
287311 }, result -> {
288312 List <String > res = new ArrayList <>();
289313 while (result .next ()) {
290- String userId = result .getString ("user_id" );
314+ String supertokensOrExternalUserId = result .getString ("user_id" );
291315 String email = result .getString ("email" );
292- if (Objects .equals (userIdToEmailMap .get (userId ), email )) {
293- res .add (userId );
316+ if (Objects .equals (supertokensOrExternalUserIdToEmailMap .get (supertokensOrExternalUserId ), email )) {
317+ res .add (externalUserIdToSupertokensUserIdMap . get ( supertokensOrExternalUserId ) );
294318 }
295319 }
296320 return res ;
@@ -300,30 +324,51 @@ public static List<String> isEmailVerified_transaction(Start start, Connection s
300324 public static List <String > isEmailVerified (Start start , AppIdentifier appIdentifier ,
301325 List <UserIdAndEmail > userIdAndEmail )
302326 throws SQLException , StorageQueryException {
327+
303328 if (userIdAndEmail .isEmpty ()) {
304329 return new ArrayList <>();
305330 }
306331 List <String > emails = new ArrayList <>();
307- List <String > userIds = new ArrayList <>();
308- Map < String , String > userIdToEmailMap = new HashMap <>();
332+ List <String > supertokensUserIds = new ArrayList <>();
333+
309334 for (UserIdAndEmail ue : userIdAndEmail ) {
310335 emails .add (ue .email );
311- userIds .add (ue .userId );
336+ supertokensUserIds .add (ue .userId );
337+ }
338+ // We have external user id stored in the email verification table, so we need to fetch the mapped userids for
339+ // calculating the verified emails
340+ HashMap <String , String > supertokensUserIdToExternalUserIdMap = UserIdMappingQueries .getUserIdMappingWithUserIds (start ,
341+ supertokensUserIds );
342+ HashMap <String , String > externalUserIdToSupertokensUserIdMap = new HashMap <>();
343+ List <String > supertokensOrExternalUserIdsToQuery = new ArrayList <>();
344+ for (String userId : supertokensUserIds ) {
345+ if (supertokensUserIdToExternalUserIdMap .containsKey (userId )) {
346+ supertokensOrExternalUserIdsToQuery .add (supertokensUserIdToExternalUserIdMap .get (userId ));
347+ externalUserIdToSupertokensUserIdMap .put (supertokensUserIdToExternalUserIdMap .get (userId ), userId );
348+ } else {
349+ supertokensOrExternalUserIdsToQuery .add (userId );
350+ externalUserIdToSupertokensUserIdMap .put (userId , userId );
351+ }
312352 }
353+
354+ Map <String , String > supertokensOrExternalUserIdToEmailMap = new HashMap <>();
313355 for (UserIdAndEmail ue : userIdAndEmail ) {
314- if (userIdToEmailMap .containsKey (ue .userId )) {
356+ String supertokensOrExternalUserId = ue .userId ;
357+ if (supertokensUserIdToExternalUserIdMap .containsKey (supertokensOrExternalUserId )) {
358+ supertokensOrExternalUserId = supertokensUserIdToExternalUserIdMap .get (supertokensOrExternalUserId );
359+ }
360+ if (supertokensOrExternalUserIdToEmailMap .containsKey (supertokensOrExternalUserId )) {
315361 throw new RuntimeException ("Found a bug!" );
316362 }
317- userIdToEmailMap .put (ue . userId , ue .email );
363+ supertokensOrExternalUserIdToEmailMap .put (supertokensOrExternalUserId , ue .email );
318364 }
319365 String QUERY = "SELECT * FROM " + getConfig (start ).getEmailVerificationTable ()
320- + " WHERE app_id = ? AND user_id IN (" + Utils .generateCommaSeperatedQuestionMarks (userIds .size ()) +
366+ + " WHERE app_id = ? AND user_id IN (" + Utils .generateCommaSeperatedQuestionMarks (supertokensOrExternalUserIdsToQuery .size ()) +
321367 ") AND email IN (" + Utils .generateCommaSeperatedQuestionMarks (emails .size ()) + ")" ;
322-
323368 return execute (start , QUERY , pst -> {
324369 pst .setString (1 , appIdentifier .getAppId ());
325370 int index = 2 ;
326- for (String userId : userIds ) {
371+ for (String userId : supertokensOrExternalUserIdsToQuery ) {
327372 pst .setString (index ++, userId );
328373 }
329374 for (String email : emails ) {
@@ -332,10 +377,10 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
332377 }, result -> {
333378 List <String > res = new ArrayList <>();
334379 while (result .next ()) {
335- String userId = result .getString ("user_id" );
380+ String supertokensOrExternalUserId = result .getString ("user_id" );
336381 String email = result .getString ("email" );
337- if (Objects .equals (userIdToEmailMap .get (userId ), email )) {
338- res .add (userId );
382+ if (Objects .equals (supertokensOrExternalUserIdToEmailMap .get (supertokensOrExternalUserId ), email )) {
383+ res .add (externalUserIdToSupertokensUserIdMap . get ( supertokensOrExternalUserId ) );
339384 }
340385 }
341386 return res ;
0 commit comments