package de.tavolio.member; import de.tavolio.AuthenticationService; import de.tavolio.account.AccountEntity; import de.tavolio.account.AccountRepo; import de.tavolio.account.AccountStatus; import de.tavolio.member.dto.Membership; import de.tavolio.member.dto.MembershipCreation; import io.quarkus.security.UnauthorizedException; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.ws.rs.BadRequestException; import org.jboss.logging.Logger; import java.time.ZonedDateTime; import java.util.List; @ApplicationScoped public class MembershipService { @Inject Logger LOG; @Inject AuthenticationService authenticationService; @Inject MembershipRepo membershipRepo; @Inject MembershipMapper membershipMapper; @Inject AccountRepo accountRepo; @Transactional public Membership create(TenantType tenantType, String tenantId, MembershipCreation membershipCreation) { switch (tenantType) { case ORGANISATION -> { if (membershipCreation.role().equals(MembershipRole.OWNER)) { if (authenticationService.isSuperUser()) { AccountEntity account = accountRepo.findById(membershipCreation.accountId()); MembershipEntity membership = MembershipEntity.init(); membership.setAccount(account); membership.setRole(membershipCreation.role()); membership.setTenantType(TenantType.ORGANISATION); membership.setTenantId(tenantId); membership.setMemberSince(ZonedDateTime.now()); membershipRepo.persist(membership); account.setStatus(AccountStatus.REGISTERED); accountRepo.persist(account); return membershipMapper.map(membership); } LOG.errorf("Membership with role 'Owner' cannot be created without superuser permissions"); throw new UnauthorizedException(); } } case RESTAURANT -> { } default -> { } } throw new BadRequestException(); } public List findByTenantType(TenantType tenantType) { return membershipMapper.map(membershipRepo.findByTenantTypeAndAccount(tenantType, authenticationService.requireUser())); } public List findByTenantTypeAndTenantId(TenantType tenantType, String tenantId) { return membershipMapper.map(membershipRepo.findByTenantTypeAndTenantId(tenantType, tenantId)); } }