Skip to content

Commit

Permalink
Update account links using AccountAdapter, LinkUserAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy committed Nov 2, 2023
1 parent 62fc881 commit cd0107f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import me.mastercapexd.auth.database.model.AccountLink;
import me.mastercapexd.auth.database.model.AuthAccount;

public class AccountLinkAdapter extends AccountLink {
public class LinkUserAdapter extends AccountLink {

public AccountLinkAdapter(LinkUser linkUser, AuthAccount account) {
public LinkUserAdapter(LinkUser linkUser, AuthAccount account) {
super(linkUser.getLinkType().getName(), linkUser.getLinkUserInfo().getIdentificator().asString(), linkUser.getLinkUserInfo().isConfirmationEnabled(),
account);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.bivashy.auth.api.account.Account;
import com.bivashy.auth.api.config.database.schema.TableSettings;
Expand All @@ -21,7 +20,7 @@

import me.mastercapexd.auth.database.DatabaseHelper;
import me.mastercapexd.auth.database.adapter.AccountAdapter;
import me.mastercapexd.auth.database.adapter.AccountLinkAdapter;
import me.mastercapexd.auth.database.adapter.LinkUserAdapter;
import me.mastercapexd.auth.database.model.AccountLink;
import me.mastercapexd.auth.database.model.AuthAccount;

Expand Down Expand Up @@ -75,30 +74,33 @@ private static DatabaseFieldConfig createFieldConfig(TableSettings settings, Str

public void updateAccountLinks(Account account) {
DEFAULT_EXCEPTION_CATCHER.execute(() -> {
List<AccountLink> existingAccountLinks = account.getLinkUsers().stream().map(AccountLinkAdapter::new).collect(Collectors.toList());
AuthAccount accountAdapter = new AccountAdapter(account);
callBatchTasks(() -> {
AuthAccount adapter = new AccountAdapter(account);
for (LinkUser linkUser : account.getLinkUsers()) {
String linkTypeName = linkUser.getLinkType().getName();
String linkUserId = Optional.ofNullable(linkUser.getLinkUserInfo())
.map(LinkUserInfo::getIdentificator)
.map(LinkUserIdentificator::asString)
.orElse(linkUser.getLinkType().getDefaultIdentificator().asString());
boolean linkEnabled = linkUser.getLinkUserInfo().isConfirmationEnabled();
Optional<AccountLink> accountLinkOptional = existingAccountLinks.stream()
.filter(accountLink -> accountLink.getLinkType().equals(linkTypeName))
.findFirst();

if (accountLinkOptional.isPresent()) {
AccountLink accountLink = accountLinkOptional.get();
accountLink.setLinkEnabled(linkEnabled);
accountLink.setLinkUserId(linkUserId);

AccountLink accountLink = new LinkUserAdapter(linkUser, accountAdapter);

AccountLink updateId = queryBuilder()
.where().eq(AccountLink.ACCOUNT_ID_FIELD_KEY, accountAdapter.getId())
.and().eq(AccountLink.LINK_TYPE_FIELD_KEY, accountLink.getLinkType())
.queryForFirst();

if (updateId != null) {
accountLink.setId(updateId.getId());
if (accountLink.equals(updateId))
continue;
update(accountLink);
continue;
}

AccountLink accountLink = new AccountLink(linkTypeName, linkUserId, linkEnabled, adapter);
create(accountLink);
AccountLink newAccountLink = new AccountLink(linkTypeName, linkUserId, linkEnabled, accountAdapter);
create(newAccountLink);
}
return null;
});
Expand Down

0 comments on commit cd0107f

Please sign in to comment.