Skip to content

Commit

Permalink
feat: Add ProcessBulkImportUsers cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 committed Mar 20, 2024
1 parent e2930b3 commit ca2472a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/supertokens/pluginInterface/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public interface Storage {
// if silent is true, do not log anything out on the console
void constructor(String processId, boolean silent, boolean isTesting);

Storage createBulkImportProxyStorageInstance();

void loadConfig(JsonObject jsonConfig, Set<LOG_LEVEL> logLevels, TenantIdentifier tenantIdentifier) throws InvalidConfigException;

// this returns a unique ID based on the db's connection URI and table prefix such that
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/supertokens/pluginInterface/StorageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package io.supertokens.pluginInterface;

import io.supertokens.pluginInterface.authRecipe.AuthRecipeStorage;
import io.supertokens.pluginInterface.authRecipe.sqlStorage.AuthRecipeSQLStorage;
import io.supertokens.pluginInterface.bulkimport.sqlStorage.BulkImportSQLStorage;
import io.supertokens.pluginInterface.dashboard.sqlStorage.DashboardSQLStorage;
import io.supertokens.pluginInterface.emailpassword.sqlStorage.EmailPasswordSQLStorage;
import io.supertokens.pluginInterface.emailverification.sqlStorage.EmailVerificationSQLStorage;
Expand Down Expand Up @@ -133,4 +133,12 @@ public static MultitenancyStorage getMultitenancyStorage(Storage storage) {
}
return (MultitenancyStorage) storage;
}

public static BulkImportSQLStorage getBulkImportStorage(Storage storage) {
if (storage.getType() != STORAGE_TYPE.SQL) {
// we only support SQL for now
throw new UnsupportedOperationException("");
}
return (BulkImportSQLStorage) storage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ List<BulkImportUser> getBulkImportUsers(AppIdentifier appIdentifier, @Nonnull In
*/
List<String> deleteBulkImportUsers(AppIdentifier appIdentifier, @Nonnull String[] bulkImportUserIds) throws StorageQueryException;

/**
* Returns the users from the bulk_import_users table for processing
*/
List<BulkImportUser> getBulkImportUsersForProcessing(AppIdentifier appIdentifier, @Nonnull Integer limit) throws StorageQueryException;

public enum BULK_IMPORT_USER_STATUS {
NEW, PROCESSING, FAILED
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ public class BulkImportUser {
public String id;
public String externalUserId;
public JsonObject userMetadata;
public List<String> userRoles;
public List<UserRole> userRoles;
public List<TotpDevice> totpDevices;
public List<LoginMethod> loginMethods;

// Following fields come from the DB Record.
public BULK_IMPORT_USER_STATUS status;
public String errorMessage;
public Long createdAt;
public Long updatedAt;

public BulkImportUser(String id, String externalUserId, JsonObject userMetadata, List<String> userRoles,
public BulkImportUser(String id, String externalUserId, JsonObject userMetadata, List<UserRole> userRoles,
List<TotpDevice> totpDevices, List<LoginMethod> loginMethods) {
this.id = id;
this.externalUserId = externalUserId;
Expand All @@ -46,7 +47,7 @@ public BulkImportUser(String id, String externalUserId, JsonObject userMetadata,
this.loginMethods = loginMethods;
}

public static BulkImportUser fromTesting_fromJson(JsonObject jsonObject) {
public static BulkImportUser forTesting_fromJson(JsonObject jsonObject) {
return new Gson().fromJson(jsonObject, BulkImportUser.class);
}

Expand All @@ -60,10 +61,11 @@ public String toRawDataForDbStorage() {
return jsonObject.toString();
}

public static BulkImportUser fromRawDataFromDbStorage(String id, String rawData, BULK_IMPORT_USER_STATUS status, long createdAt, long updatedAt) {
public static BulkImportUser fromRawDataFromDbStorage(String id, String rawData, BULK_IMPORT_USER_STATUS status, String errorMessage, long createdAt, long updatedAt) {
BulkImportUser user = new Gson().fromJson(rawData, BulkImportUser.class);
user.id = id;
user.status = status;
user.errorMessage = errorMessage;
user.createdAt = createdAt;
user.updatedAt = updatedAt;
return user;
Expand All @@ -73,6 +75,16 @@ public JsonObject toJsonObject() {
return new Gson().fromJson(new Gson().toJson(this), JsonObject.class);
}

public static class UserRole {
public String role;
public List<String> tenantIds;

public UserRole(String role, List<String> tenantIds) {
this.role = role;
this.tenantIds = tenantIds;
}
}

public static class TotpDevice {
public String secretKey;
public int period;
Expand All @@ -88,7 +100,7 @@ public TotpDevice(String secretKey, int period, int skew, String deviceName) {
}

public static class LoginMethod {
public String tenantId;
public List<String> tenantIds;
public boolean isVerified;
public boolean isPrimary;
public long timeJoinedInMSSinceEpoch;
Expand All @@ -99,11 +111,12 @@ public static class LoginMethod {
public String thirdPartyId;
public String thirdPartyUserId;
public String phoneNumber;
public String superTokensOrExternalUserId;

public LoginMethod(String tenantId, String recipeId, boolean isVerified, boolean isPrimary,
public LoginMethod(List<String> tenantIds, String recipeId, boolean isVerified, boolean isPrimary,
long timeJoinedInMSSinceEpoch, String email, String passwordHash, String hashingAlgorithm,
String thirdPartyId, String thirdPartyUserId, String phoneNumber) {
this.tenantId = tenantId;
this.tenantIds = tenantIds;
this.recipeId = recipeId;
this.isVerified = isVerified;
this.isPrimary = isPrimary;
Expand All @@ -116,4 +129,4 @@ public LoginMethod(String tenantId, String recipeId, boolean isVerified, boolean
this.phoneNumber = phoneNumber;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.supertokens.pluginInterface.bulkimport.sqlStorage;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import io.supertokens.pluginInterface.bulkimport.BulkImportStorage;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand All @@ -30,5 +31,10 @@ public interface BulkImportSQLStorage extends BulkImportStorage, SQLStorage {
* Update the status of the users in the bulk_import_users table
*/
void updateBulkImportUserStatus_Transaction(AppIdentifier appIdentifier,
TransactionConnection con, @Nonnull String[] bulkImportUserIds, @Nonnull BULK_IMPORT_USER_STATUS status) throws StorageQueryException;
TransactionConnection con, @Nonnull String[] bulkImportUserIds, @Nonnull BULK_IMPORT_USER_STATUS status, @Nullable String errorMessage) throws StorageQueryException;

/**
* Delete users by id from the bulk_import_users table
*/
void deleteBulkImportUser_Transaction(AppIdentifier appIdentifier, TransactionConnection con, @Nonnull String bulkImportUserId) throws StorageQueryException;
}

0 comments on commit ca2472a

Please sign in to comment.