Skip to content

Commit

Permalink
fix: time joined
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 20, 2023
1 parent 81408dd commit 62aecb6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ public static boolean addUserIdToTenant_Transaction(Start start, Connection sqlC
pst.setString(10, tenantIdentifier.getTenantId());
pst.setString(11, userId);
});

GeneralQueries.updateTimeJoinedForPrimaryUser_Transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), finalAccountLinkingInfo.primaryUserId);
}

{ // emailpassword_user_to_tenant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,10 @@ public static boolean doesUserIdExist(Start start, AppIdentifier appIdentifier,
// We query both tables cause there is a case where a primary user ID exists, but its associated
// recipe user ID has been deleted AND there are other recipe user IDs linked to this primary user ID already.
String QUERY = "SELECT 1 FROM " + Config.getConfig(start).getAppIdToUserIdTable()
+ " WHERE app_id = ? AND user_id = ? UNION SELECT 1 FROM " + Config.getConfig(start).getUsersTable() +
" WHERE app_id = ? AND primary_or_recipe_user_id = ?";
+ " WHERE app_id = ? AND user_id = ?";
return execute(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
pst.setString(3, appIdentifier.getAppId());
pst.setString(4, userId);
}, ResultSet::next);
}

Expand All @@ -568,13 +565,10 @@ public static boolean doesUserIdExist_Transaction(Start start, Connection sqlCon
// We query both tables cause there is a case where a primary user ID exists, but its associated
// recipe user ID has been deleted AND there are other recipe user IDs linked to this primary user ID already.
String QUERY = "SELECT 1 FROM " + Config.getConfig(start).getAppIdToUserIdTable()
+ " WHERE app_id = ? AND user_id = ? UNION SELECT 1 FROM " + Config.getConfig(start).getUsersTable() +
" WHERE app_id = ? AND primary_or_recipe_user_id = ?";
+ " WHERE app_id = ? AND user_id = ?";
return execute(sqlCon, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
pst.setString(3, appIdentifier.getAppId());
pst.setString(4, userId);
}, ResultSet::next);
}

Expand Down Expand Up @@ -926,29 +920,9 @@ public static void linkAccounts_Transaction(Start start, Connection sqlCon, AppI
pst.setString(3, recipeUserId);
});
}
{ // update primary_or_recipe_user_time_joined to min time joined
// Query like postgres causes issue in mysql, so we have to fetch min time joined first on a separate query and then update it
String QUERY1 = "SELECT MIN(time_joined) AS min_time_joined FROM " +
Config.getConfig(start).getUsersTable() + " WHERE app_id = ? AND primary_or_recipe_user_id = ?";
long minTimeJoined = execute(sqlCon, QUERY1, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, primaryUserId);
}, result -> {
if (result.next()) {
return result.getLong("min_time_joined");
}
return 0L;
});

String QUERY = "UPDATE " + Config.getConfig(start).getUsersTable() +
" SET primary_or_recipe_user_time_joined = ? WHERE " +
" app_id = ? AND primary_or_recipe_user_id = ?";
update(sqlCon, QUERY, pst -> {
pst.setLong(1, minTimeJoined);
pst.setString(2, appIdentifier.getAppId());
pst.setString(3, primaryUserId);
});
}
updateTimeJoinedForPrimaryUser_Transaction(start, sqlCon, appIdentifier, primaryUserId);

{
String QUERY = "UPDATE " + Config.getConfig(start).getAppIdToUserIdTable() +
" SET is_linked_or_is_a_primary_user = true, primary_or_recipe_user_id = ? WHERE app_id = ? AND " +
Expand Down Expand Up @@ -977,29 +951,9 @@ public static void unlinkAccounts_Transaction(Start start, Connection sqlCon, Ap
pst.setString(3, recipeUserId);
});
}
{ // update primary_or_recipe_user_time_joined to min time joined
// Query like postgres causes issue in mysql, so we have to fetch min time joined first on a separate query and then update it
String QUERY1 = "SELECT MIN(time_joined) AS min_time_joined FROM " +
Config.getConfig(start).getUsersTable() + " WHERE app_id = ? AND primary_or_recipe_user_id = ?";
long minTimeJoined = execute(sqlCon, QUERY1, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, primaryUserId);
}, result -> {
if (result.next()) {
return result.getLong("min_time_joined");
}
return 0L;
});

String QUERY = "UPDATE " + Config.getConfig(start).getUsersTable() +
" SET primary_or_recipe_user_time_joined = ? WHERE " +
" app_id = ? AND primary_or_recipe_user_id = ?";
update(sqlCon, QUERY, pst -> {
pst.setLong(1, minTimeJoined);
pst.setString(2, appIdentifier.getAppId());
pst.setString(3, primaryUserId);
});
}
updateTimeJoinedForPrimaryUser_Transaction(start, sqlCon, appIdentifier, primaryUserId);

{
String QUERY = "UPDATE " + Config.getConfig(start).getAppIdToUserIdTable() +
" SET is_linked_or_is_a_primary_user = false, primary_or_recipe_user_id = ?" +
Expand Down Expand Up @@ -1620,14 +1574,38 @@ public static AccountLinkingInfo getAccountLinkingInfo_Transaction(Start start,
String primaryUserId1 = result.getString("primary_or_recipe_user_id");
boolean isLinked1 = result.getBoolean("is_linked_or_is_a_primary_user");
return new AccountLinkingInfo(primaryUserId1, isLinked1);

}
return null;
});
}
return accountLinkingInfo;
}

public static void updateTimeJoinedForPrimaryUser_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, String primaryUserId)
throws SQLException, StorageQueryException {
// Query like postgres causes issue in mysql, so we have to fetch min time joined first on a separate query and then update it
String QUERY1 = "SELECT MIN(time_joined) AS min_time_joined FROM " +
Config.getConfig(start).getUsersTable() + " WHERE app_id = ? AND primary_or_recipe_user_id = ?";
long minTimeJoined = execute(sqlCon, QUERY1, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, primaryUserId);
}, result -> {
if (result.next()) {
return result.getLong("min_time_joined");
}
return 0L;
});

String QUERY = "UPDATE " + Config.getConfig(start).getUsersTable() +
" SET primary_or_recipe_user_time_joined = ? WHERE " +
" app_id = ? AND primary_or_recipe_user_id = ?";
update(sqlCon, QUERY, pst -> {
pst.setLong(1, minTimeJoined);
pst.setString(2, appIdentifier.getAppId());
pst.setString(3, primaryUserId);
});
}

private static class AllAuthRecipeUsersResultHolder {
String userId;
String tenantId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,8 @@ public static boolean addUserIdToTenant_Transaction(Start start, Connection sqlC
pst.setString(10, tenantIdentifier.getTenantId());
pst.setString(11, userInfo.id);
});

GeneralQueries.updateTimeJoinedForPrimaryUser_Transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), accountLinkingInfo.primaryUserId);
}

{ // passwordless_user_to_tenant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ public static boolean addUserIdToTenant_Transaction(Start start, Connection sqlC
pst.setString(10, tenantIdentifier.getTenantId());
pst.setString(11, userInfo.id);
});

GeneralQueries.updateTimeJoinedForPrimaryUser_Transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), accountLinkingInfo.primaryUserId);
}

{ // thirdparty_user_to_tenant
Expand Down

0 comments on commit 62aecb6

Please sign in to comment.