Skip to content

Commit

Permalink
fix: account linking stats (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc authored Sep 14, 2023
1 parent 91673ad commit da21390
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -2882,19 +2882,28 @@ public void unlinkAccounts_Transaction(AppIdentifier appIdentifier, TransactionC

@Override
public boolean checkIfUsesAccountLinking(AppIdentifier appIdentifier) throws StorageQueryException {
// TODO
return false;
try {
return GeneralQueries.checkIfUsesAccountLinking(this, appIdentifier);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
public int countUsersThatHaveMoreThanOneLoginMethodAndActiveSince(AppIdentifier appIdentifier, long sinceTime) throws StorageQueryException {
// TODO
return 0;
try {
return ActiveUsersQueries.countUsersActiveSinceAndHasMoreThanOneLoginMethod(this, appIdentifier, sinceTime);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
public int getUsersCountWithMoreThanOneLoginMethod(AppIdentifier appIdentifier) throws StorageQueryException {
// TODO
return 0;
try {
return GeneralQueries.getUsersCountWithMoreThanOneLoginMethod(this, appIdentifier);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ public static int countUsersActiveSince(Start start, AppIdentifier appIdentifier
});
}

public static int countUsersActiveSinceAndHasMoreThanOneLoginMethod(Start start, AppIdentifier appIdentifier, long sinceTime)
throws SQLException, StorageQueryException {
String QUERY = "SELECT count(1) as c FROM ("
+ " SELECT count(user_id) as num_login_methods, app_id, primary_or_recipe_user_id"
+ " FROM " + Config.getConfig(start).getUsersTable()
+ " WHERE primary_or_recipe_user_id IN ("
+ " SELECT user_id FROM " + Config.getConfig(start).getUserLastActiveTable()
+ " WHERE app_id = ? AND last_active_time >= ?"
+ " )"
+ " GROUP BY app_id, primary_or_recipe_user_id"
+ ") uc WHERE num_login_methods > 1";
return execute(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setLong(2, sinceTime);
}, result -> {
if (result.next()) {
return result.getInt("c");
}
return 0;
});
}

public static int countUsersEnabledTotp(Start start, AppIdentifier appIdentifier) throws SQLException, StorageQueryException {
String QUERY = "SELECT COUNT(*) as total FROM " + Config.getConfig(start).getTotpUsersTable()
+ " WHERE app_id = ?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,35 @@ public static String[] getAllTablesInTheDatabaseThatHasDataForAppId(Start start,
return result.toArray(new String[0]);
}

public static int getUsersCountWithMoreThanOneLoginMethod(Start start, AppIdentifier appIdentifier)
throws SQLException, StorageQueryException {
String QUERY = "SELECT COUNT (1) as c FROM ("
+ " SELECT COUNT(user_id) as num_login_methods "
+ " FROM " + getConfig(start).getUsersTable()
+ " WHERE app_id = ? "
+ " GROUP BY (app_id, primary_or_recipe_user_id) "
+ ") as nloginmethods WHERE num_login_methods > 1";

return execute(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
}, result -> {
return result.next() ? result.getInt("c") : 0;
});
}

public static boolean checkIfUsesAccountLinking(Start start, AppIdentifier appIdentifier)
throws SQLException, StorageQueryException {
String QUERY = "SELECT 1 FROM "
+ getConfig(start).getUsersTable()
+ " WHERE app_id = ? AND is_linked_or_is_a_primary_user = true LIMIT 1";

return execute(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
}, result -> {
return result.next();
});
}

private static class AllAuthRecipeUsersResultHolder {
String userId;
String tenantId;
Expand Down

0 comments on commit da21390

Please sign in to comment.