Skip to content

Commit

Permalink
default account env var
Browse files Browse the repository at this point in the history
  • Loading branch information
marioserrano09 committed Dec 9, 2021
1 parent 86031f6 commit 0074211
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package tools.dynamia.modules.saas.services.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tools.dynamia.commons.SimpleCache;
Expand Down Expand Up @@ -62,6 +63,9 @@ public class AccountServiceAPIImpl extends AbstractService implements AccountSer
@Autowired
private AccountContext accountContext;

@Autowired
private Environment environment;

private final SimpleCache<Long, AccountDTO> accountCache = new SimpleCache<>();
private final SimpleCache<String, Long> domainCache = new SimpleCache<>();

Expand Down Expand Up @@ -92,6 +96,8 @@ public AccountDTO getAccount(Long accountId) {
if (dto == null) {
Account account = crudService().findSingle(Account.class,
QueryParameters.with("id", accountId).add("status", QueryConditions.isNotNull()));


if (account != null) {
dto = account.toDTO();
accountCache.add(accountId, dto);
Expand Down Expand Up @@ -285,6 +291,12 @@ public Long getAccountIdByDomain(String domain) {
accountId = account.getId();
}
}
if (account == null && "true".equals(environment.getProperty("useDefaultAccount"))) {
account = service.getDefaultAccount();
accountId = account.getId();
}


if (accountId != null) {
domainCache.add(domain, accountId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
Expand Down Expand Up @@ -60,6 +61,9 @@ public class AccountServiceImpl implements AccountService, ApplicationListener<C
@Autowired
private EntityManagerFactoryInfo entityManagerFactoryInfo;

@Autowired
private Environment environment;


@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
Expand Down Expand Up @@ -89,12 +93,15 @@ public Account getAccountByCustomDomain(String domain) {
@Override
public Account getAccount(HttpServletRequest request) {
String host = request.getServerName();
String subdomain = host.substring(0, host.indexOf("."));
String subdomain = host.contains(".") ? host.substring(0, host.indexOf(".")) : host;

Account account = getAccount(subdomain);
if (account == null) {
account = getAccountByCustomDomain(host);
}
if (account == null && "true".equals(environment.getProperty("useDefaultAccount"))) {
account = getDefaultAccount();
}
return account;
}

Expand Down

0 comments on commit 0074211

Please sign in to comment.