From c5420e4d4c4001daa7fbea24af919de93efdcafb Mon Sep 17 00:00:00 2001 From: sBubshait Date: Thu, 7 Aug 2025 03:50:30 +0300 Subject: [PATCH] feat: enable admins to create new invitation codes --- .../wesal/controller/AuthController.java | 28 +++++++++++++++++ .../wesal/service/InvitationCodeService.java | 31 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/backend/src/main/java/online/wesal/wesal/controller/AuthController.java b/backend/src/main/java/online/wesal/wesal/controller/AuthController.java index 062c923..ee7d983 100644 --- a/backend/src/main/java/online/wesal/wesal/controller/AuthController.java +++ b/backend/src/main/java/online/wesal/wesal/controller/AuthController.java @@ -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> 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 + )); + } + } } \ No newline at end of file diff --git a/backend/src/main/java/online/wesal/wesal/service/InvitationCodeService.java b/backend/src/main/java/online/wesal/wesal/service/InvitationCodeService.java index 2d6fdf9..69f8d1e 100644 --- a/backend/src/main/java/online/wesal/wesal/service/InvitationCodeService.java +++ b/backend/src/main/java/online/wesal/wesal/service/InvitationCodeService.java @@ -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(); + } } \ No newline at end of file