Skip to content

Commit

Permalink
Edit person validation warning in ticket workbench (thewca#10375)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljames-dj authored Jan 3, 2025
1 parent ec318a2 commit f86e5d0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 19 deletions.
14 changes: 14 additions & 0 deletions app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ def update_status
end
render json: { success: true }
end

def edit_person_validators
ticket = Ticket.find(params.require(:ticket_id))
dob_validation_issues = []

ticket.metadata.tickets_edit_person_fields.each do |edit_person_field|
case edit_person_field[:field_name]
when TicketsEditPersonField.field_names[:dob]
dob_validation_issues = ResultsValidators::PersonsValidator.dob_validations(Date.parse(edit_person_field[:new_value]))
end
end

render json: { dob: dob_validation_issues }
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import React from 'react';
import { Message } from 'semantic-ui-react';
import EditPersonForm from '../../Panel/pages/EditPersonPage/EditPersonForm';
import useSaveAction from '../../../lib/hooks/useSaveAction';
import { actionUrls } from '../../../lib/requests/routes.js.erb';
import { ticketStatuses } from '../../../lib/wca-data.js.erb';
import Loading from '../../Requests/Loading';
import useLoadedData from '../../../lib/hooks/useLoadedData';
import Errored from '../../Requests/Errored';

function EditPersonValidations({ ticketDetails }) {
const { ticket } = ticketDetails;
const {
data: validators, loading, error,
} = useLoadedData(actionUrls.tickets.editPersonValidators(ticket.id));

if (loading) return <Loading />;
if (error) return <Errored />;

return validators.dob.map((validator) => (
<Message warning>{validator.message}</Message>
));
}

function EditPersonTicketWorkbenchForWrt({ ticketDetails, actingStakeholderId, sync }) {
const { ticket } = ticketDetails;
Expand All @@ -24,10 +41,15 @@ function EditPersonTicketWorkbenchForWrt({ ticketDetails, actingStakeholderId, s
if (saving) return <Loading />;

return (
<EditPersonForm
wcaId={ticket.metadata.wca_id}
onSuccess={closeTicket}
/>
<>
<EditPersonValidations
ticketDetails={ticketDetails}
/>
<EditPersonForm
wcaId={ticket.metadata.wca_id}
onSuccess={closeTicket}
/>
</>
);
}

Expand Down
1 change: 1 addition & 0 deletions app/webpacker/lib/requests/routes.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export const actionUrls = {
tickets: {
show: (ticketId) => `<%= CGI.unescape(Rails.application.routes.url_helpers.ticket_path("${ticketId}", format: "json")) %>`,
updateStatus: (ticketId) => `<%= CGI.unescape(Rails.application.routes.url_helpers.ticket_update_status_path("${ticketId}")) %>`,
editPersonValidators: (ticketId) => `<%= CGI.unescape(Rails.application.routes.url_helpers.ticket_edit_person_validators_path("${ticketId}")) %>`,
},
}

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
get 'panel/redirect/:panel_page' => 'panel#redirect', as: :panel_redirect
resources :tickets, only: [:show] do
post 'update_status' => 'tickets#update_status', as: :update_status
get 'edit_person_validators' => 'tickets#edit_person_validators', as: :edit_person_validators
end
resources :notifications, only: [:index]

Expand Down
43 changes: 28 additions & 15 deletions lib/results_validators/persons_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ def include_persons?
true
end

def self.dob_validations(dob, competition_id = nil, name = nil)
validation_issues = []

# Check if DOB is January 1
if dob.month == 1 && dob.day == 1
validation_issues << ValidationWarning.new(:persons, competition_id, DOB_0101_WARNING, name: name)
end

# Check if DOB is very young, competitor less than 3 years old are extremely rare, so we'd better check these birthdate are correct.
if dob.year >= Time.now.year - 3
validation_issues << ValidationWarning.new(:persons, competition_id, VERY_YOUNG_PERSON_WARNING, name: name)
end

# Check if DOB is not so young
if dob.year <= Time.now.year - 100
validation_issues << ValidationWarning.new(:persons, competition_id, NOT_SO_YOUNG_PERSON_WARNING, name: name)
end

validation_issues
end

def run_validation(validator_data)
validator_data.each do |competition_data|
competition = competition_data.competition
Expand Down Expand Up @@ -74,26 +95,18 @@ def run_validation(validator_data)
end
duplicate_newcomer_names = []
without_wca_id.each do |p|
if p.dob.month == 1 && p.dob.day == 1
@warnings << ValidationWarning.new(:persons, competition.id,
DOB_0101_WARNING,
name: p.name)
end
if p.gender.blank?
@warnings << ValidationWarning.new(:persons, competition.id,
EMPTY_GENDER_WARNING,
name: p.name)
end
# Competitor less than 3 years old are extremely rare, so we'd better check these birthdate are correct.
if p.dob.year >= Time.now.year - 3
@warnings << ValidationWarning.new(:persons, competition.id,
VERY_YOUNG_PERSON_WARNING,
name: p.name)
end
if p.dob.year <= Time.now.year - 100
@warnings << ValidationWarning.new(:persons, competition.id,
NOT_SO_YOUNG_PERSON_WARNING,
name: p.name)

PersonsValidator.dob_validations(p.dob, competition.id, p.name).each do |validation|
if validation.is_a?(ValidationError)
@errors << validation
elsif validation.is_a?(ValidationWarning)
@warnings << validation
end
end
# Look for double whitespaces or leading/trailing whitespaces.
unless p.name.squeeze(" ").strip == p.name
Expand Down

0 comments on commit f86e5d0

Please sign in to comment.