only change password, docs and mail left

This commit is contained in:
Jose
2026-01-30 22:37:39 +01:00
parent 33b3a460ca
commit 4303caaf74
10 changed files with 131 additions and 48 deletions

View File

@@ -41,7 +41,7 @@ public class MemberController {
}
return ResponseEntity.ok(
memberService.getMyProfile(principal.getUserId())
memberService.getMyProfile(principal.getUserId())
);
}

View File

@@ -30,6 +30,7 @@ public class RequestMapper {
Request entity = new Request();
entity.setType(dto.getType());
entity.setUserId(dto.getUserId());
entity.setName(dto.getName());
entity.setStatus((byte) 0);
if (dto.getMetadata() != null) {

View File

@@ -47,9 +47,12 @@ public class IncomeService {
if (income.getUserId() == null) {
throw new BadRequestException("El identificador de usuario es obligatorio");
}
if (income.getConcept() == null || income.getConcept().isBlank()) {
if (income.getConcept() == null) {
throw new BadRequestException("El concepto es obligatorio");
}
if (income.getConcept().isBlank() || income.getConcept().isEmpty()) {
throw new ValidationException("concept", "El concepto no puede ir vacío");
}
if (income.getAmount() == null || income.getAmount().signum() <= 0) {
throw new ValidationException("amount", "La cantidad debe ser positiva");
}

View File

@@ -60,11 +60,17 @@ public class RequestService {
throw new BadRequestException("El tipo de solicitud es obligatorio");
}
if (request.getType() == 2 && hasCollaboratorRequest(request.getUserId())) { // tiene soli de collab
if (request.getType() == 1 && hasUnregisterRequest(request.getUserId())) {
throw new ConflictException("Ya tienes una solicitud, espera que se acepte o se elimine al ser rechazada");
}
if (request.getType() == 1 && hasGreenhouseRequest(request.getUserId())) { // tiene soli de invernadero
if ((request.getType() == 2 || request.getType() == 3) &&
hasCollaboratorRequest(request.getUserId())) { // tiene soli de collab
throw new ConflictException("Ya tienes una solicitud, espera que se acepte o se elimine al ser rechazada");
}
if ((request.getType() == 4 || request.getType() == 5) &&
hasGreenhouseRequest(request.getUserId())) { // tiene soli de invernadero
throw new ConflictException("Ya tienes una solicitud, espera que se acepte o se elimine al ser rechazada");
}
@@ -119,11 +125,16 @@ public class RequestService {
public boolean hasGreenhouseRequest(UUID userId) {
return getByUserId(userId).stream()
.anyMatch(r -> r.getType() == 1);
.anyMatch(r -> r.getType() == 4 || r.getType() == 5);
}
public boolean hasCollaboratorRequest(UUID userId) {
return getByUserId(userId).stream()
.anyMatch(r -> r.getType() == 2);
.anyMatch(r -> r.getType() == 2 || r.getType() == 3);
}
public boolean hasUnregisterRequest(UUID userId) {
return getByUserId(userId).stream()
.anyMatch(r -> r.getType() == 1);
}
}

View File

@@ -2,27 +2,45 @@ package net.miarma.backend.huertos.validation;
import net.miarma.backend.huertos.model.RequestMetadata;
import net.miarma.backlib.exception.BadRequestException;
import net.miarma.backlib.exception.ValidationException;
import java.util.regex.Pattern;
public class RequestValidator {
private static final Pattern DNI_PATTERN = Pattern.compile("\\d{8}[A-Za-z]");
private static final Pattern EMAIL_PATTERN = Pattern.compile("^[\\w.%+-]+@[\\w.-]+\\.[a-zA-Z]{2,6}$");
private static final Pattern PHONE_PATTERN = Pattern.compile("^\\+?\\d{9,15}$");
public static void validate(RequestMetadata metadata, Byte requestType) {
if (metadata.getRequestId() == null) {
throw new BadRequestException("Estos metadatos deben pertenecer a una solicitud (falta ID)");
}
if (isBlank(metadata.getDisplayName())) {
throw new BadRequestException("El nombre es obligatorio");
throw new BadRequestException("El nombre a mostrar es obligatorio");
} else if (metadata.getDisplayName().length() < 3) {
throw new ValidationException("displayName", "El nombre a mostrar debe tener al menos 3 caracteres");
}
if (isBlank(metadata.getDni())) {
throw new BadRequestException("El DNI es obligatorio");
} else if (!DNI_PATTERN.matcher(metadata.getDni()).matches()) {
throw new ValidationException("dni", "Formato de DNI inválido (ej: 12345678A)");
} else if (!DniValidator.isValid(metadata.getDni())) {
throw new ValidationException("dni", "Este DNI no es un DNI real");
}
if (isBlank(metadata.getEmail())) {
throw new BadRequestException("El email es obligatorio");
} else if (!EMAIL_PATTERN.matcher(metadata.getEmail()).matches()) {
throw new ValidationException("email", "Email inválido");
}
if (isBlank(metadata.getUsername())) {
throw new BadRequestException("El username es obligatorio");
throw new BadRequestException("El usuario es obligatorio");
} else if (metadata.getUsername().length() < 3) {
throw new ValidationException("username", "El usuario debe tener al menos 3 caracteres");
}
if (metadata.getType() == null) {
@@ -46,6 +64,10 @@ public class RequestValidator {
throw new BadRequestException("La dirección, código postal y ciudad son obligatorios");
}
}
if (metadata.getPhone() != null && !PHONE_PATTERN.matcher(metadata.getPhone()).matches()) {
throw new ValidationException("phone", "Teléfono inválido (debe tener entre 9 y 15 dígitos, opcional +)");
}
}
private static boolean isBlank(String s) {