Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions EventPlatform.WebApi/Controllers/CheckinController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public async Task<IActionResult> JoinOnlineEvent(string token)
{
if (string.IsNullOrEmpty(token)) return BadRequest("Token không hợp lệ.");

// 1. Tìm vé dựa trên Token
var registration = await _context.Registrations
.Include(r => r.TicketType)
.ThenInclude(tt => tt.Event)
Expand All @@ -36,7 +35,6 @@ public async Task<IActionResult> JoinOnlineEvent(string token)
if (registration == null)
return NotFound("Vé không tồn tại.");

// Kiểm tra thanh toán (Transaction thành công mới được vào)
var isPaid = registration.Transactions.Any(t => t.PaymentStatus == "Success");
if (!isPaid)
return Content("Lỗi: Vé chưa được thanh toán thành công.");
Expand Down Expand Up @@ -66,21 +64,35 @@ public async Task<IActionResult> JoinOnlineEvent(string token)
// [Authorize(Roles = "Admin,Speaker")] // Bỏ comment để chỉ cho phép BTC quét
public async Task<IActionResult> VerifyOfflineTicket([FromBody] CheckinRequest request)
{
// 1. Làm sạch dữ liệu đầu vào (Xóa khoảng trắng thừa)
var tokenToVerify = request.Token?.Trim();

if (string.IsNullOrEmpty(tokenToVerify))
{
return Ok(new { isValid = false, message = "❌ Token trống!" });
}

var registration = await _context.Registrations
.Include(r => r.User)
.Include(r => r.TicketType)
.ThenInclude(tt => tt.Event)
.Include(r => r.Transactions)
.FirstOrDefaultAsync(r => r.UniqueToken == request.Token);
.FirstOrDefaultAsync(r => r.UniqueToken == tokenToVerify);

// 3. Validate
if (registration == null)
return Ok(new { isValid = false, message = "❌ Mã vé không tồn tại!" });
{
return Ok(new
{
isValid = false,
message = $"❌ Mã vé không tồn tại! (Server nhận: '{tokenToVerify}')"
});
}

var isPaid = registration.Transactions.Any(t => t.PaymentStatus == "Success");
if (!isPaid)
return Ok(new { isValid = false, message = "⚠️ Vé chưa thanh toán!" });

// 4. Kiểm tra xem đã check-in trước đó chưa? (Chống dùng lại vé)
if (registration.CheckInTime != null)
{
return Ok(new
Expand All @@ -91,12 +103,10 @@ public async Task<IActionResult> VerifyOfflineTicket([FromBody] CheckinRequest r
});
}

// 5. Hợp lệ -> Update DB
registration.CheckInTime = DateTime.UtcNow;
_context.Registrations.Update(registration);
await _context.SaveChangesAsync();

// 6. Trả về thông tin khách để BTC đối chiếu
return Ok(new
{
isValid = true,
Expand Down