feat(insc): add frontend
This commit is contained in:
@@ -1,26 +1,31 @@
|
||||
package fr.titionfire.ffsaf.domain.service;
|
||||
|
||||
import fr.titionfire.ffsaf.data.model.CompetitionModel;
|
||||
import fr.titionfire.ffsaf.data.repository.ClubRepository;
|
||||
import fr.titionfire.ffsaf.data.repository.CompetitionRepository;
|
||||
import fr.titionfire.ffsaf.data.repository.MatchRepository;
|
||||
import fr.titionfire.ffsaf.data.repository.PouleRepository;
|
||||
import fr.titionfire.ffsaf.data.model.MembreModel;
|
||||
import fr.titionfire.ffsaf.data.repository.*;
|
||||
import fr.titionfire.ffsaf.net2.ServerCustom;
|
||||
import fr.titionfire.ffsaf.net2.data.SimpleCompet;
|
||||
import fr.titionfire.ffsaf.net2.request.SReqCompet;
|
||||
import fr.titionfire.ffsaf.rest.data.CompetitionData;
|
||||
import fr.titionfire.ffsaf.rest.data.RegisterRequestData;
|
||||
import fr.titionfire.ffsaf.rest.data.SimpleCompetData;
|
||||
import fr.titionfire.ffsaf.rest.data.SimpleRegisterComb;
|
||||
import fr.titionfire.ffsaf.rest.exception.DBadRequestException;
|
||||
import fr.titionfire.ffsaf.rest.exception.DForbiddenException;
|
||||
import fr.titionfire.ffsaf.utils.CompetitionSystem;
|
||||
import fr.titionfire.ffsaf.utils.RegisterEmbeddable;
|
||||
import fr.titionfire.ffsaf.utils.SecurityCtx;
|
||||
import io.quarkus.cache.Cache;
|
||||
import io.quarkus.cache.CacheName;
|
||||
import io.quarkus.hibernate.reactive.panache.Panache;
|
||||
import io.quarkus.hibernate.reactive.panache.common.WithSession;
|
||||
import io.smallrye.mutiny.Multi;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import io.smallrye.mutiny.unchecked.Unchecked;
|
||||
import io.vertx.mutiny.core.Vertx;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.hibernate.reactive.mutiny.Mutiny;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
|
||||
import java.util.*;
|
||||
@@ -42,6 +47,9 @@ public class CompetitionService {
|
||||
@Inject
|
||||
KeycloakService keycloakService;
|
||||
|
||||
@Inject
|
||||
CombRepository combRepository;
|
||||
|
||||
@Inject
|
||||
ServerCustom serverCustom;
|
||||
|
||||
@@ -51,6 +59,14 @@ public class CompetitionService {
|
||||
@Inject
|
||||
Vertx vertx;
|
||||
|
||||
@Inject
|
||||
@CacheName("safca-config")
|
||||
Cache cache;
|
||||
|
||||
@Inject
|
||||
@CacheName("safca-have-access")
|
||||
Cache cacheAccess;
|
||||
|
||||
public Uni<CompetitionData> getById(SecurityCtx securityCtx, Long id) {
|
||||
if (id == 0) {
|
||||
return Uni.createFrom()
|
||||
@@ -138,7 +154,7 @@ public class CompetitionService {
|
||||
|
||||
return Panache.withTransaction(() -> repository.persist(model));
|
||||
}).map(CompetitionData::fromModel)
|
||||
.call(__ -> permService.cacheAccess.invalidate(securityCtx.getSubject()));
|
||||
.call(__ -> cacheAccess.invalidate(securityCtx.getSubject()));
|
||||
} else {
|
||||
return permService.hasEditPerm(securityCtx, data.getId())
|
||||
.chain(model -> {
|
||||
@@ -160,10 +176,71 @@ public class CompetitionService {
|
||||
}))
|
||||
.chain(__ -> Panache.withTransaction(() -> repository.persist(model)));
|
||||
}).map(CompetitionData::fromModel)
|
||||
.call(__ -> permService.cacheAccess.invalidate(securityCtx.getSubject()));
|
||||
.call(__ -> cacheAccess.invalidate(securityCtx.getSubject()));
|
||||
}
|
||||
}
|
||||
|
||||
public Uni<List<SimpleRegisterComb>> getRegister(SecurityCtx securityCtx, Long id) {
|
||||
return permService.hasEditPerm(securityCtx, id)
|
||||
.chain(c -> Mutiny.fetch(c.getInsc()))
|
||||
.onItem().transformToMulti(Multi.createFrom()::iterable)
|
||||
.onItem().call(combModel -> Mutiny.fetch(combModel.getMembre().getLicences()))
|
||||
.map(combModel -> SimpleRegisterComb.fromModel(combModel, combModel.getMembre().getLicences()))
|
||||
.collect().asList();
|
||||
}
|
||||
|
||||
public Uni<SimpleRegisterComb> addRegisterComb(SecurityCtx securityCtx, Long id, RegisterRequestData data) {
|
||||
return permService.hasEditPerm(securityCtx, id)
|
||||
.chain(c -> findComb(data.getLicence(), data.getFname(), data.getLname())
|
||||
.chain(combModel -> Mutiny.fetch(c.getInsc())
|
||||
.chain(Unchecked.function(insc -> {
|
||||
Optional<RegisterEmbeddable> opt = insc.stream()
|
||||
.filter(m -> m.getMembre().equals(combModel)).findAny();
|
||||
|
||||
RegisterEmbeddable r;
|
||||
if (opt.isPresent()) {
|
||||
r = opt.get();
|
||||
r.setWeight(data.getWeight());
|
||||
r.setOverCategory(data.getOverCategory());
|
||||
} else {
|
||||
r = new RegisterEmbeddable(combModel, data.getWeight(), data.getOverCategory());
|
||||
insc.add(r);
|
||||
}
|
||||
return Panache.withTransaction(() -> repository.persist(c)).map(__ -> r);
|
||||
}))))
|
||||
.chain(r -> Mutiny.fetch(r.getMembre().getLicences())
|
||||
.map(licences -> SimpleRegisterComb.fromModel(r, licences)));
|
||||
}
|
||||
|
||||
private Uni<MembreModel> findComb(Long licence, String fname, String lname) {
|
||||
if (licence != null && licence != 0) {
|
||||
return combRepository.find("licence = ?1", licence).firstResult()
|
||||
.invoke(Unchecked.consumer(combModel -> {
|
||||
if (combModel == null)
|
||||
throw new DForbiddenException("Licence " + licence + " non trouvé");
|
||||
}));
|
||||
} else {
|
||||
if (fname == null || lname == null)
|
||||
return Uni.createFrom().failure(new DBadRequestException("Nom et prénom requis"));
|
||||
return combRepository.find("LOWER(lname) LIKE LOWER(?1) OR LOWER(fname) LIKE LOWER(?2)", lname,
|
||||
fname).firstResult()
|
||||
.invoke(Unchecked.consumer(combModel -> {
|
||||
if (combModel == null)
|
||||
throw new DForbiddenException("Combattant " + fname + " " + lname + " non trouvé");
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public Uni<Void> removeRegisterComb(SecurityCtx securityCtx, Long id, Long combId) {
|
||||
return permService.hasEditPerm(securityCtx, id)
|
||||
.chain(c -> Mutiny.fetch(c.getInsc())
|
||||
.chain(Unchecked.function(insc -> {
|
||||
if (insc.removeIf(m -> m.getMembre().getId().equals(combId)))
|
||||
return Panache.withTransaction(() -> repository.persist(c)).map(__ -> null);
|
||||
throw new DBadRequestException("Combattant non inscrit");
|
||||
})));
|
||||
}
|
||||
|
||||
public Uni<?> delete(SecurityCtx securityCtx, Long id) {
|
||||
return repository.findById(id).invoke(Unchecked.consumer(c -> {
|
||||
if (!securityCtx.getSubject().equals(c.getOwner()) || securityCtx.roleHas("federation_admin"))
|
||||
@@ -180,7 +257,7 @@ public class CompetitionService {
|
||||
() -> pouleRepository.delete("compet = ?1", competitionModel)))
|
||||
.chain(model -> Panache.withTransaction(() -> repository.delete("id", model.getId())))
|
||||
.invoke(o -> SReqCompet.rmCompet(serverCustom.clients, id))
|
||||
.call(__ -> permService.cache.invalidate(id));
|
||||
.call(__ -> cache.invalidate(id));
|
||||
}
|
||||
|
||||
public Uni<SimpleCompetData> getSafcaData(SecurityCtx securityCtx, Long id) {
|
||||
@@ -234,15 +311,20 @@ public class CompetitionService {
|
||||
}))
|
||||
.invoke(simpleCompet -> SReqCompet.sendUpdate(serverCustom.clients, simpleCompet))
|
||||
.call(simpleCompet -> permService.getSafcaConfig(data.getId())
|
||||
.call(c -> Uni.join().all(Stream.concat(
|
||||
Stream.concat(
|
||||
c.admin().stream().filter(uuid -> !simpleCompet.admin().contains(uuid)),
|
||||
simpleCompet.admin().stream().filter(uuid -> !c.admin().contains(uuid))),
|
||||
Stream.concat(
|
||||
c.table().stream().filter(uuid -> !simpleCompet.table().contains(uuid)),
|
||||
simpleCompet.table().stream().filter(uuid -> !c.table().contains(uuid))))
|
||||
.map(uuid -> permService.cacheAccess.invalidate(uuid.toString())).toList())
|
||||
.andCollectFailures()))
|
||||
.call(__ -> permService.cache.invalidate(data.getId()));
|
||||
.call(c -> {
|
||||
List<Uni<Void>> list = Stream.concat(
|
||||
Stream.concat(
|
||||
c.admin().stream().filter(uuid -> !simpleCompet.admin().contains(uuid)),
|
||||
simpleCompet.admin().stream().filter(uuid -> !c.admin().contains(uuid))),
|
||||
Stream.concat(
|
||||
c.table().stream().filter(uuid -> !simpleCompet.table().contains(uuid)),
|
||||
simpleCompet.table().stream().filter(uuid -> !c.table().contains(uuid)))
|
||||
).map(uuid -> cacheAccess.invalidate(uuid.toString())).toList();
|
||||
|
||||
if (list.isEmpty())
|
||||
return Uni.createFrom().nullItem();
|
||||
return Uni.join().all(list).andCollectFailures();
|
||||
}))
|
||||
.call(__ -> cache.invalidate(data.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user