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

fix: account linking stats #813

Merged
merged 1 commit into from
Sep 14, 2023
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
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
Loading