mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-02 13:15:27 +00:00
Added a daily recurring job to cleanup tournaments. Fixed final score fields not loading without page refresh on mat stats page and added a cypress test for it.
This commit is contained in:
36
app/jobs/tournament_cleanup_job.rb
Normal file
36
app/jobs/tournament_cleanup_job.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
class TournamentCleanupJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform
|
||||
# Remove or clean up tournaments based on age and match status
|
||||
process_old_tournaments
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def process_old_tournaments
|
||||
# Get all tournaments older than 1 week that have a user_id
|
||||
old_tournaments = Tournament.where('date < ? AND user_id IS NOT NULL', 1.week.ago.to_date)
|
||||
|
||||
old_tournaments.each do |tournament|
|
||||
# Check if it has any non-BYE finished matches
|
||||
has_real_matches = tournament.matches.where(finished: 1).where.not(win_type: 'BYE').exists?
|
||||
|
||||
if has_real_matches
|
||||
|
||||
# 1. Remove all school delegates
|
||||
tournament.schools.each do |school|
|
||||
school.delegates.destroy_all
|
||||
end
|
||||
|
||||
# 2. Remove all tournament delegates
|
||||
tournament.delegates.destroy_all
|
||||
|
||||
# 3. Set user_id to null
|
||||
tournament.update(user_id: nil)
|
||||
else
|
||||
tournament.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,6 @@
|
||||
<script>
|
||||
// ############### Score field changer and form validation
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
function initializeScoreFields() {
|
||||
const winTypeSelect = document.getElementById("match_win_type");
|
||||
const winnerSelect = document.getElementById("match_winner_id");
|
||||
const submitButton = document.getElementById("update-match-btn");
|
||||
@@ -9,6 +9,9 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
const validationAlerts = document.getElementById("validation-alerts");
|
||||
const pinTimeTip = document.getElementById("pin-time-tip");
|
||||
|
||||
// If elements don't exist, don't proceed
|
||||
if (!winTypeSelect || !dynamicScoreInput || !finalScoreField) return;
|
||||
|
||||
// Variables to persist scores across win type changes
|
||||
let storedScores = {
|
||||
winnerScore: "0",
|
||||
@@ -111,7 +114,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
|
||||
function validateForm() {
|
||||
const winType = winTypeSelect.value;
|
||||
const winner = winnerSelect.value;
|
||||
const winner = winnerSelect ? winnerSelect.value : null;
|
||||
let isValid = true;
|
||||
let alertMessage = "";
|
||||
|
||||
@@ -156,57 +159,65 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
alertMessage += "Please select a winner.<br>";
|
||||
}
|
||||
|
||||
if (!isValid) {
|
||||
validationAlerts.innerHTML = alertMessage;
|
||||
validationAlerts.style.display = "block";
|
||||
} else {
|
||||
validationAlerts.innerHTML = ""; // Clear alerts
|
||||
validationAlerts.style.display = "none";
|
||||
if (validationAlerts) {
|
||||
if (!isValid) {
|
||||
validationAlerts.innerHTML = alertMessage;
|
||||
validationAlerts.style.display = "block";
|
||||
} else {
|
||||
validationAlerts.innerHTML = ""; // Clear alerts
|
||||
validationAlerts.style.display = "none";
|
||||
}
|
||||
}
|
||||
|
||||
submitButton.disabled = !isValid;
|
||||
if (submitButton) {
|
||||
submitButton.disabled = !isValid;
|
||||
}
|
||||
}
|
||||
|
||||
document.querySelector("form").addEventListener("submit", (event) => {
|
||||
const winType = winTypeSelect.value;
|
||||
if (document.querySelector("form")) {
|
||||
document.querySelector("form").addEventListener("submit", (event) => {
|
||||
const winType = winTypeSelect.value;
|
||||
|
||||
if (winType === "Pin") {
|
||||
const minuteInput = document.getElementById("minutes");
|
||||
const secondInput = document.getElementById("seconds");
|
||||
if (winType === "Pin") {
|
||||
const minuteInput = document.getElementById("minutes");
|
||||
const secondInput = document.getElementById("seconds");
|
||||
|
||||
if (minuteInput && secondInput) {
|
||||
const minutes = minuteInput.value.padStart(2, "0");
|
||||
const seconds = secondInput.value.padStart(2, "0");
|
||||
finalScoreField.value = `${minutes}:${seconds}`;
|
||||
if (minuteInput && secondInput) {
|
||||
const minutes = minuteInput.value.padStart(2, "0");
|
||||
const seconds = secondInput.value.padStart(2, "0");
|
||||
finalScoreField.value = `${minutes}:${seconds}`;
|
||||
} else {
|
||||
finalScoreField.value = ""; // Clear if no inputs
|
||||
}
|
||||
} else if (
|
||||
winType === "Decision" ||
|
||||
winType === "Major" ||
|
||||
winType === "Tech Fall"
|
||||
) {
|
||||
const winnerScoreInput = document.getElementById("winner-score");
|
||||
const loserScoreInput = document.getElementById("loser-score");
|
||||
|
||||
if (winnerScoreInput && loserScoreInput) {
|
||||
const winnerScore = winnerScoreInput.value || "0";
|
||||
const loserScore = loserScoreInput.value || "0";
|
||||
finalScoreField.value = `${winnerScore}-${loserScore}`;
|
||||
} else {
|
||||
finalScoreField.value = ""; // Clear if no inputs
|
||||
}
|
||||
} else {
|
||||
finalScoreField.value = ""; // Clear if no inputs
|
||||
finalScoreField.value = ""; // Reset for other win types
|
||||
}
|
||||
} else if (
|
||||
winType === "Decision" ||
|
||||
winType === "Major" ||
|
||||
winType === "Tech Fall"
|
||||
) {
|
||||
const winnerScoreInput = document.getElementById("winner-score");
|
||||
const loserScoreInput = document.getElementById("loser-score");
|
||||
|
||||
if (winnerScoreInput && loserScoreInput) {
|
||||
const winnerScore = winnerScoreInput.value || "0";
|
||||
const loserScore = loserScoreInput.value || "0";
|
||||
finalScoreField.value = `${winnerScore}-${loserScore}`;
|
||||
} else {
|
||||
finalScoreField.value = ""; // Clear if no inputs
|
||||
}
|
||||
} else {
|
||||
finalScoreField.value = ""; // Reset for other win types
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
winTypeSelect.addEventListener("change", updateScoreInput);
|
||||
winnerSelect.addEventListener("change", validateForm);
|
||||
if (winnerSelect) {
|
||||
winnerSelect.addEventListener("change", validateForm);
|
||||
}
|
||||
|
||||
updateScoreInput();
|
||||
validateForm();
|
||||
});
|
||||
}
|
||||
|
||||
// Helper function to create text inputs
|
||||
function createTextInput(id, placeholder, label) {
|
||||
@@ -226,4 +237,8 @@ function createTextInput(id, placeholder, label) {
|
||||
container.appendChild(input);
|
||||
return container;
|
||||
}
|
||||
|
||||
// Initialize on both DOMContentLoaded and turbolinks:load
|
||||
document.addEventListener("DOMContentLoaded", initializeScoreFields);
|
||||
document.addEventListener("turbolinks:load", initializeScoreFields);
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user