cloud env check
All checks were successful
Deploy API / deploy (ubuntu-latest, 2.44.0) (push) Successful in 43s

This commit is contained in:
Braydon 2024-09-19 08:22:57 -04:00
parent 8e47214257
commit 955b035687
3 changed files with 23 additions and 2 deletions

@ -13,8 +13,14 @@ public final class EnvironmentUtils {
*/
@Getter private static final boolean production;
/**
* Is the app running in a "cloud" environment?
*/
@Getter private static final boolean cloud;
static {
String appEnv = System.getenv("APP_ENV");
String cloudEnv = System.getenv("APP_CLOUD");
production = appEnv != null && (appEnv.equals("production"));
cloud = cloudEnv != null && (cloudEnv.equals("true"));
}
}

@ -1,5 +1,6 @@
package cc.pulseapp.api.service;
import cc.pulseapp.api.common.EnvironmentUtils;
import cc.pulseapp.api.exception.impl.BadRequestException;
import cc.pulseapp.api.model.Feature;
import cc.pulseapp.api.model.IGenericResponse;
@ -65,10 +66,18 @@ public final class OrganizationService {
if (!Feature.ORG_CREATION_ENABLED.isEnabled()) {
throw new BadRequestException(Error.ORG_CREATION_DISABLED);
}
List<Organization> ownedOrgs = orgRepository.findByOwnerSnowflake(owner.getSnowflake());
// Ensure the org name isn't taken
if (orgRepository.findByNameIgnoreCase(name) != null) {
if (ownedOrgs.stream().anyMatch(org -> org.getName().equalsIgnoreCase(name))) {
throw new BadRequestException(Error.ORG_NAME_TAKEN);
}
// Handle cloud environment checks
if (EnvironmentUtils.isCloud()) {
if (ownedOrgs.size() >= owner.getTier().getMaxOrganizations()) {
throw new BadRequestException(Error.MAX_ORGS_REACHED);
}
}
// Create the org and return it
slug = slug.trim().replaceAll("-+$", ""); // Trim slug trailing dashes
return orgRepository.save(new Organization(snowflakeService.generateSnowflake(), name, slug, null, owner.getSnowflake()));
@ -95,6 +104,7 @@ public final class OrganizationService {
*/
private enum Error implements IGenericResponse {
ORG_CREATION_DISABLED,
ORG_NAME_TAKEN
ORG_NAME_TAKEN,
MAX_ORGS_REACHED
}
}

@ -1,5 +1,6 @@
package cc.pulseapp.api.service;
import cc.pulseapp.api.common.EnvironmentUtils;
import cc.pulseapp.api.exception.impl.BadRequestException;
import cc.pulseapp.api.model.Feature;
import cc.pulseapp.api.model.IGenericResponse;
@ -53,6 +54,10 @@ public final class StatusPageService {
if (pageRepository.findByNameIgnoreCase(name) != null) {
throw new BadRequestException(Error.STATUS_PAGE_NAME_TAKEN);
}
// Handle cloud environment checks
if (EnvironmentUtils.isCloud()) {
// TODO: do UserTier#maxStatusPages check
}
// Create the status page and return it
String slug = name.replace(" ", "-") +
"-" + ThreadLocalRandom.current().nextInt(10000, 99999);