feat: enable admins to create new invitation codes
This commit is contained in:
parent
e85a3bebb8
commit
c5420e4d4c
@ -12,6 +12,7 @@ import online.wesal.wesal.dto.CheckInvitationRequest;
|
||||
import online.wesal.wesal.dto.RegisterRequest;
|
||||
import online.wesal.wesal.dto.RegisterResponse;
|
||||
import online.wesal.wesal.entity.User;
|
||||
import online.wesal.wesal.entity.InvitationCode;
|
||||
import online.wesal.wesal.service.AuthService;
|
||||
import online.wesal.wesal.service.UserService;
|
||||
import online.wesal.wesal.service.InvitationCodeService;
|
||||
@ -192,4 +193,31 @@ public class AuthController {
|
||||
return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/admin/createInvitationCode")
|
||||
@Operation(summary = "Create invitation code (Admin only)", description = "Generates a new invitation code - requires admin privileges")
|
||||
public ResponseEntity<Map<String, Object>> createInvitationCode() {
|
||||
try {
|
||||
InvitationCode invitationCode = invitationCodeService.generateInvitationCode();
|
||||
|
||||
return ResponseEntity.ok(Map.of(
|
||||
"status", true,
|
||||
"message", "Invitation code created successfully",
|
||||
"data", invitationCode
|
||||
));
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
String message;
|
||||
if (e.getMessage().contains("Unable to generate unique")) {
|
||||
message = "Failed to generate unique invitation code. Please try again.";
|
||||
} else {
|
||||
message = "Failed to create invitation code. Please try again.";
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(Map.of(
|
||||
"status", false,
|
||||
"message", message
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -12,6 +12,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class InvitationCodeService {
|
||||
@ -66,4 +67,34 @@ public class InvitationCodeService {
|
||||
|
||||
return savedUser;
|
||||
}
|
||||
|
||||
public InvitationCode generateInvitationCode() {
|
||||
String code;
|
||||
int attempts = 0;
|
||||
int maxAttempts = 100;
|
||||
|
||||
// Generate a unique 6-character code
|
||||
do {
|
||||
code = generateRandomCode();
|
||||
attempts++;
|
||||
if (attempts > maxAttempts) {
|
||||
throw new RuntimeException("Unable to generate unique invitation code after " + maxAttempts + " attempts");
|
||||
}
|
||||
} while (invitationCodeRepository.existsByCode(code));
|
||||
|
||||
InvitationCode invitationCode = new InvitationCode(code);
|
||||
return invitationCodeRepository.save(invitationCode);
|
||||
}
|
||||
|
||||
private String generateRandomCode() {
|
||||
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
Random random = new Random();
|
||||
StringBuilder code = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
code.append(characters.charAt(random.nextInt(characters.length())));
|
||||
}
|
||||
|
||||
return code.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user