331 lines
17 KiB
Java
331 lines
17 KiB
Java
package fr.titionfire.ffsaf.domain.service;
|
|
|
|
import fr.titionfire.ffsaf.data.model.*;
|
|
import fr.titionfire.ffsaf.data.repository.*;
|
|
import fr.titionfire.ffsaf.rest.data.SimpleAffiliation;
|
|
import fr.titionfire.ffsaf.rest.data.SimpleReqAffiliation;
|
|
import fr.titionfire.ffsaf.rest.from.AffiliationRequestForm;
|
|
import fr.titionfire.ffsaf.rest.from.AffiliationRequestSaveForm;
|
|
import fr.titionfire.ffsaf.utils.SequenceType;
|
|
import fr.titionfire.ffsaf.utils.Utils;
|
|
import io.quarkus.hibernate.reactive.panache.Panache;
|
|
import io.quarkus.hibernate.reactive.panache.common.WithSession;
|
|
import io.smallrye.mutiny.Uni;
|
|
import io.smallrye.mutiny.unchecked.Unchecked;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import jakarta.inject.Inject;
|
|
import jakarta.ws.rs.NotFoundException;
|
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
|
import org.hibernate.reactive.mutiny.Mutiny;
|
|
|
|
import java.util.List;
|
|
import java.util.stream.Stream;
|
|
|
|
@WithSession
|
|
@ApplicationScoped
|
|
public class AffiliationService {
|
|
|
|
@Inject
|
|
CombRepository combRepository;
|
|
|
|
@Inject
|
|
ClubRepository clubRepository;
|
|
|
|
@Inject
|
|
AffiliationRequestRepository repositoryRequest;
|
|
|
|
@Inject
|
|
AffiliationRepository repository;
|
|
|
|
@Inject
|
|
KeycloakService keycloakService;
|
|
|
|
@Inject
|
|
SequenceRepository sequenceRepository;
|
|
|
|
@Inject
|
|
LicenceRepository licenceRepository;
|
|
|
|
@ConfigProperty(name = "upload_dir")
|
|
String media;
|
|
|
|
public Uni<List<AffiliationRequestModel>> getAllReq() {
|
|
return repositoryRequest.listAll();
|
|
}
|
|
|
|
public Uni<String> save(AffiliationRequestForm form) {
|
|
AffiliationRequestModel affModel = form.toModel();
|
|
int currentSaison = Utils.getSaison();
|
|
|
|
// noinspection ResultOfMethodCallIgnored
|
|
return Uni.createFrom().item(affModel)
|
|
.invoke(Unchecked.consumer(model -> {
|
|
if (model.getSaison() != currentSaison && model.getSaison() != currentSaison + 1) {
|
|
throw new IllegalArgumentException("Saison not valid");
|
|
}
|
|
}))
|
|
.chain(() -> repositoryRequest.count("siret = ?1 and saison = ?2", affModel.getSiret(),
|
|
affModel.getSaison()))
|
|
.onItem().invoke(Unchecked.consumer(count -> {
|
|
if (count != 0) {
|
|
throw new IllegalArgumentException("Affiliation request already exists");
|
|
}
|
|
}))
|
|
.chain(() -> clubRepository.find("SIRET = ?1", affModel.getSiret()).firstResult().chain(club ->
|
|
repository.count("club = ?1 and saison = ?2", club, affModel.getSaison())))
|
|
.onItem().invoke(Unchecked.consumer(count -> {
|
|
if (count != 0) {
|
|
throw new IllegalArgumentException("Affiliation already exists");
|
|
}
|
|
}))
|
|
.map(o -> affModel)
|
|
.call(model -> ((model.getM1_lincence() != -1) ? combRepository.find("licence",
|
|
model.getM1_lincence()).count().invoke(count -> {
|
|
if (count == 0) {
|
|
throw new IllegalArgumentException("Licence membre n°1 inconnue");
|
|
}
|
|
}) : Uni.createFrom().nullItem())
|
|
)
|
|
.call(model -> ((model.getM2_lincence() != -1) ? combRepository.find("licence",
|
|
model.getM2_lincence()).count().invoke(count -> {
|
|
if (count == 0) {
|
|
throw new IllegalArgumentException("Licence membre n°2 inconnue");
|
|
}
|
|
}) : Uni.createFrom().nullItem())
|
|
)
|
|
.call(model -> ((model.getM3_lincence() != -1) ? combRepository.find("licence",
|
|
model.getM3_lincence()).count().invoke(count -> {
|
|
if (count == 0) {
|
|
throw new IllegalArgumentException("Licence membre n°3 inconnue");
|
|
}
|
|
}) : Uni.createFrom().nullItem())
|
|
).chain(model -> Panache.withTransaction(() -> repositoryRequest.persist(model)))
|
|
.onItem()
|
|
.invoke(model -> Uni.createFrom().future(Utils.replacePhoto(model.getId(), form.getLogo(), media,
|
|
"aff_request/logo")))
|
|
.onItem()
|
|
.invoke(model -> Uni.createFrom().future(Utils.replacePhoto(model.getId(), form.getStatus(), media,
|
|
"aff_request/status")))
|
|
.map(__ -> "Ok");
|
|
}
|
|
|
|
public Uni<?> saveAdmin(AffiliationRequestSaveForm form) {
|
|
return repositoryRequest.findById(form.getId())
|
|
.onItem().ifNull().failWith(new NotFoundException("Affiliation request not found"))
|
|
.map(model -> {
|
|
model.setName(form.getName());
|
|
model.setSiret(form.getSiret());
|
|
model.setRNA(form.getRna());
|
|
model.setAddress(form.getAddress());
|
|
|
|
if (form.getM1_mode() == 2) {
|
|
model.setM1_lname(form.getM1_lname());
|
|
model.setM1_fname(form.getM1_fname());
|
|
} else {
|
|
model.setM1_lincence(
|
|
form.getM1_lincence() == null ? 0 : Integer.parseInt(form.getM1_lincence()));
|
|
}
|
|
model.setM1_role(form.getM1_role());
|
|
if (form.getM1_email_mode() == 0)
|
|
model.setM1_email(form.getM1_email());
|
|
|
|
if (form.getM2_mode() == 2) {
|
|
model.setM2_lname(form.getM2_lname());
|
|
model.setM2_fname(form.getM2_fname());
|
|
} else {
|
|
model.setM2_lincence(
|
|
form.getM2_lincence() == null ? 0 : Integer.parseInt(form.getM2_lincence()));
|
|
}
|
|
model.setM2_role(form.getM2_role());
|
|
if (form.getM2_email_mode() == 0)
|
|
model.setM2_email(form.getM2_email());
|
|
|
|
if (form.getM3_mode() == 2) {
|
|
model.setM3_lname(form.getM3_lname());
|
|
model.setM3_fname(form.getM3_fname());
|
|
} else {
|
|
model.setM3_lincence(
|
|
form.getM3_lincence() == null ? 0 : Integer.parseInt(form.getM3_lincence()));
|
|
}
|
|
model.setM3_role(form.getM3_role());
|
|
if (form.getM3_email_mode() == 0)
|
|
model.setM3_email(form.getM3_email());
|
|
|
|
return model;
|
|
})
|
|
.chain(model -> Panache.withTransaction(() -> repositoryRequest.persist(model)))
|
|
.onItem()
|
|
.invoke(model -> Uni.createFrom().future(Utils.replacePhoto(model.getId(), form.getLogo(), media,
|
|
"aff_request/logo")))
|
|
.onItem()
|
|
.invoke(model -> Uni.createFrom().future(Utils.replacePhoto(model.getId(), form.getStatus(), media,
|
|
"aff_request/status")))
|
|
.map(__ -> "Ok");
|
|
}
|
|
|
|
private Uni<?> setMembre(AffiliationRequestSaveForm.Member member, ClubModel club, int saison) {
|
|
return Uni.createFrom().nullItem().chain(__ -> {
|
|
if (member.getMode() == 2) {
|
|
MembreModel membreModel = new MembreModel();
|
|
membreModel.setFname(member.getFname());
|
|
membreModel.setLname(member.getLname());
|
|
membreModel.setClub(club);
|
|
membreModel.setRole(member.getRole());
|
|
membreModel.setEmail(member.getEmail());
|
|
return Panache.withTransaction(() ->
|
|
combRepository.persist(membreModel)
|
|
.chain(m -> sequenceRepository.getNextValueInTransaction(SequenceType.Licence)
|
|
.invoke(l -> m.setLicence(Math.toIntExact(l)))
|
|
.chain(() -> combRepository.persist(m))));
|
|
} else {
|
|
return combRepository.find("licence", Integer.parseInt(member.getLicence())).firstResult()
|
|
.onItem().ifNull().switchTo(() -> {
|
|
MembreModel membreModel = new MembreModel();
|
|
membreModel.setFname(member.getFname());
|
|
membreModel.setLname(member.getLname());
|
|
return Panache.withTransaction(
|
|
() -> sequenceRepository.getNextValueInTransaction(SequenceType.Licence)
|
|
.invoke(l -> membreModel.setLicence(Math.toIntExact(l)))
|
|
.chain(() -> combRepository.persist(membreModel)));
|
|
})
|
|
.map(m -> {
|
|
m.setClub(club);
|
|
m.setRole(member.getRole());
|
|
m.setEmail(member.getEmail());
|
|
return m;
|
|
}).call(m -> Panache.withTransaction(() -> combRepository.persist(m)));
|
|
}
|
|
})
|
|
.call(m -> (m.getUserId() == null) ? keycloakService.initCompte(m.getId()) :
|
|
keycloakService.setClubGroupMembre(m, club))
|
|
.call(m -> Panache.withTransaction(() -> licenceRepository.persist(
|
|
new LicenceModel(null, m, saison, false, true))));
|
|
}
|
|
|
|
public Uni<?> accept(AffiliationRequestSaveForm form) {
|
|
return repositoryRequest.findById(form.getId())
|
|
.onItem().ifNull().failWith(new NotFoundException("Affiliation request not found"))
|
|
.chain(req ->
|
|
clubRepository.find("SIRET = ?1", form.getSiret()).firstResult()
|
|
.chain(model -> (model == null) ? acceptNew(form, req) : acceptOld(form, req, model))
|
|
.call(club -> setMembre(form.new Member(1), club, req.getSaison())
|
|
.call(__ -> setMembre(form.new Member(2), club, req.getSaison())
|
|
.call(___ -> setMembre(form.new Member(3), club, req.getSaison()))))
|
|
.onItem()
|
|
.invoke(model -> Uni.createFrom()
|
|
.future(Utils.replacePhoto(form.getId(), form.getLogo(), media,
|
|
"aff_request/logo")))
|
|
.onItem()
|
|
.invoke(model -> Uni.createFrom()
|
|
.future(Utils.replacePhoto(form.getId(), form.getStatus(), media,
|
|
"aff_request/status")))
|
|
.call(model -> Utils.moveMedia(form.getId(), model.getId(), media, "aff_request/logo",
|
|
"ppClub"))
|
|
.call(model -> Utils.moveMedia(form.getId(), model.getId(), media, "aff_request/status",
|
|
"clubStatus"))
|
|
)
|
|
.map(__ -> "Ok");
|
|
}
|
|
|
|
private Uni<ClubModel> acceptNew(AffiliationRequestSaveForm form, AffiliationRequestModel model) {
|
|
return Uni.createFrom().nullItem()
|
|
.chain(() -> {
|
|
ClubModel club = new ClubModel();
|
|
club.setName(form.getName());
|
|
club.setCountry("fr");
|
|
club.setSIRET(form.getSiret());
|
|
club.setRNA(form.getRna());
|
|
club.setAddress(form.getAddress());
|
|
club.setAffiliations(List.of(new AffiliationModel(null, club, model.getSaison())));
|
|
return Panache.withTransaction(() -> clubRepository.persist(club)
|
|
.chain(c -> sequenceRepository.getNextValueInTransaction(SequenceType.Affiliation)
|
|
.invoke(c::setNo_affiliation)
|
|
.chain(() -> clubRepository.persist(c))
|
|
.chain(() -> repositoryRequest.delete(model))
|
|
)
|
|
.chain(() -> repository.persist(new AffiliationModel(null, club, model.getSaison())))
|
|
.map(c -> club));
|
|
});
|
|
}
|
|
|
|
private Uni<ClubModel> acceptOld(AffiliationRequestSaveForm form, AffiliationRequestModel model, ClubModel club) {
|
|
return Uni.createFrom().nullItem()
|
|
.chain(() -> {
|
|
club.setName(form.getName());
|
|
club.setCountry("fr");
|
|
club.setSIRET(form.getSiret());
|
|
club.setRNA(form.getRna());
|
|
club.setAddress(form.getAddress());
|
|
return Panache.withTransaction(() -> clubRepository.persist(club)
|
|
.chain(() -> repository.persist(new AffiliationModel(null, club, model.getSaison())))
|
|
.chain(() -> repositoryRequest.delete(model)));
|
|
})
|
|
.map(__ -> club);
|
|
}
|
|
|
|
public Uni<SimpleReqAffiliation> getRequest(long id) {
|
|
return repositoryRequest.findById(id).map(SimpleReqAffiliation::fromModel)
|
|
.onItem().ifNull().failWith(new NotFoundException("Affiliation request not found"))
|
|
.call(out -> clubRepository.find("SIRET = ?1", out.getSiret()).firstResult().invoke(c -> {
|
|
if (c != null) {
|
|
out.setClub(c.getId());
|
|
out.setClub_name(c.getName());
|
|
out.setClub_no_aff(c.getNo_affiliation());
|
|
}
|
|
})
|
|
);
|
|
}
|
|
|
|
public Uni<List<SimpleAffiliation>> getCurrentSaisonAffiliation() {
|
|
return repositoryRequest.list("saison = ?1 or saison = ?1 + 1", Utils.getSaison())
|
|
.map(models -> models.stream()
|
|
.map(model -> new SimpleAffiliation(model.getId() * -1, model.getSiret(), model.getSaison(),
|
|
false)).toList())
|
|
.chain(aff -> repository.list("saison = ?1", Utils.getSaison())
|
|
.map(models -> models.stream().map(SimpleAffiliation::fromModel).toList())
|
|
.map(aff2 -> Stream.concat(aff2.stream(), aff.stream()).toList())
|
|
);
|
|
}
|
|
|
|
public Uni<List<SimpleAffiliation>> getAffiliation(long id) {
|
|
return clubRepository.findById(id)
|
|
.onItem().ifNull().failWith(new NotFoundException("Affiliation request not found"))
|
|
.call(model -> Mutiny.fetch(model.getAffiliations()))
|
|
.chain(model -> repositoryRequest.list("siret = ?1", model.getSIRET())
|
|
.map(reqs -> reqs.stream().map(req ->
|
|
new SimpleAffiliation(req.getId() * -1, model.getId(), req.getSaison(), false)))
|
|
.map(aff2 -> Stream.concat(aff2,
|
|
model.getAffiliations().stream().map(SimpleAffiliation::fromModel)).toList())
|
|
);
|
|
}
|
|
|
|
public Uni<SimpleAffiliation> setAffiliation(long id, int saison) {
|
|
return clubRepository.findById(id)
|
|
.onItem().ifNull().failWith(new NotFoundException("Club non trouver"))
|
|
.invoke(Unchecked.consumer(club -> {
|
|
if (club.getAffiliations().stream().anyMatch(affiliation -> affiliation.getSaison() == saison)) {
|
|
throw new IllegalArgumentException("Affiliation deja existante");
|
|
}
|
|
}))
|
|
.chain(club ->
|
|
Panache.withTransaction(() -> repository.persist(new AffiliationModel(null, club, saison))
|
|
.chain(c -> (club.getNo_affiliation() != null) ? Uni.createFrom().item(c) :
|
|
sequenceRepository.getNextValueInTransaction(SequenceType.Affiliation)
|
|
.invoke(club::setNo_affiliation)
|
|
.chain(() -> clubRepository.persist(club))
|
|
.map(o -> c)
|
|
)))
|
|
.map(SimpleAffiliation::fromModel);
|
|
}
|
|
|
|
public Uni<?> deleteAffiliation(long id) {
|
|
return Panache.withTransaction(() -> repository.deleteById(id));
|
|
}
|
|
|
|
public Uni<?> deleteReqAffiliation(long id) {
|
|
return Panache.withTransaction(() -> repositoryRequest.deleteById(id))
|
|
.call(__ -> Utils.deleteMedia(id, media, "aff_request/logo"))
|
|
.call(__ -> Utils.deleteMedia(id, media, "aff_request/status"));
|
|
}
|
|
}
|