@@ -10,6 +10,9 @@ class TicketsController < ApplicationController
1010 before_action -> { check_ticket_errors ( TicketLog . action_types [ :metadata_action ] , TicketsEditPerson ::ACTION_TYPE [ :approve_edit_person_request ] ) } , only : [ :approve_edit_person_request ]
1111 before_action -> { check_ticket_errors ( TicketLog . action_types [ :metadata_action ] , TicketsEditPerson ::ACTION_TYPE [ :reject_edit_person_request ] ) } , only : [ :reject_edit_person_request ]
1212 before_action -> { check_ticket_errors ( TicketLog . action_types [ :metadata_action ] , TicketsEditPerson ::ACTION_TYPE [ :sync_edit_person_request ] ) } , only : [ :sync_edit_person_request ]
13+ before_action -> { check_ticket_errors ( TicketLog . action_types [ :metadata_action ] , TicketsClaimWcaId ::ACTION_TYPE [ :approve_claim ] ) } , only : [ :approve_claim_wca_id ]
14+ before_action -> { check_ticket_errors ( TicketLog . action_types [ :metadata_action ] , TicketsClaimWcaId ::ACTION_TYPE [ :reject_claim ] ) } , only : [ :reject_claim_wca_id ]
15+ before_action -> { check_ticket_errors ( TicketLog . action_types [ :metadata_action ] , TicketsClaimWcaId ::ACTION_TYPE [ :transfer_claim ] ) } , only : [ :transfer_claim_wca_id ]
1316 before_action -> { redirect_to_root_unless_user ( :can_admin_results? ) } , only : %i[ delete_inbox_persons ]
1417
1518 SORT_WEIGHT_LAMBDAS = {
@@ -36,6 +39,39 @@ class TicketsController < ApplicationController
3639 return if metadata_action . nil?
3740
3841 render status : :unauthorized , json : { error : "You are not allowed to perform this metadata action." } unless @acting_stakeholder . metadata_actions_allowed . include? ( @metadata_action )
42+
43+ case @ticket . metadata_type
44+ when Ticket ::TICKET_TYPES [ :claim_wca_id ]
45+ if @metadata_action == TicketsClaimWcaId ::ACTION_TYPE [ :approve_claim ]
46+ @user = @ticket . metadata . user
47+ if @user . wca_id . present?
48+ render status : :unprocessable_content , json : {
49+ error : "This user already has a WCA ID assigned." ,
50+ }
51+ end
52+
53+ if @user . unconfirmed_wca_id . nil?
54+ render status : :unprocessable_content , json : {
55+ error : "This user does not have an unconfirmed WCA ID to approve." ,
56+ }
57+ end
58+
59+ unless @acting_stakeholder . assigned?
60+ render status : :unauthorized , json : {
61+ error : "Only the assigned delegate can approve/reject." ,
62+ }
63+ end
64+ elsif @metadata_action == TicketsClaimWcaId ::ACTION_TYPE [ :transfer_claim ]
65+ new_delegate_id = params . require ( :new_delegate_id )
66+ @new_delegate = User . find ( new_delegate_id )
67+
68+ unless @new_delegate . any_kind_of_delegate?
69+ render status : :unprocessable_content , json : {
70+ error : "The selected user is not a delegate." ,
71+ }
72+ end
73+ end
74+ end
3975 end
4076
4177 def index
@@ -394,4 +430,83 @@ def join_as_bcc_stakeholder
394430
395431 render status : :ok , json : { success : true }
396432 end
433+
434+ def approve_claim_wca_id
435+ ticket_status = TicketsClaimWcaId . statuses [ :closed ]
436+
437+ ActiveRecord ::Base . transaction do
438+ user . update! ( wca_id : @user . unconfirmed_wca_id )
439+ @ticket . metadata . update! ( status : ticket_status )
440+ ticket_log = @ticket . ticket_logs . create! (
441+ action_type : @action_type ,
442+ acting_user_id : current_user . id ,
443+ acting_stakeholder_id : @acting_stakeholder . id ,
444+ metadata_action : @metadata_action ,
445+ )
446+ ticket_log . ticket_log_changes . create! (
447+ field_name : TicketLogChange . field_names [ :status ] ,
448+ field_value : ticket_status ,
449+ )
450+ end
451+ render status : :ok , json : { success : true }
452+ end
453+
454+ def reject_claim_wca_id
455+ ticket_status = TicketsClaimWcaId . statuses [ :closed ]
456+
457+ ActiveRecord ::Base . transaction do
458+ @ticket . metadata . update! ( status : ticket_status )
459+ ticket_log = @ticket . ticket_logs . create! (
460+ action_type : @action_type ,
461+ acting_user_id : current_user . id ,
462+ acting_stakeholder_id : @acting_stakeholder . id ,
463+ metadata_action : @metadata_action ,
464+ )
465+ ticket_log . ticket_log_changes . create! (
466+ field_name : TicketLogChange . field_names [ :status ] ,
467+ field_value : ticket_status ,
468+ )
469+ end
470+ render status : :ok , json : { success : true }
471+ end
472+
473+ def transfer_claim_wca_id
474+ user = @ticket . metadata . user
475+
476+ ActiveRecord ::Base . transaction do
477+ user . update! ( delegate_to_handle_wca_id_claim : @new_delegate )
478+
479+ old_stakeholder = @ticket . ticket_stakeholders . find_by (
480+ stakeholder_type : "User" ,
481+ stakeholder_role : TicketStakeholder . stakeholder_roles [ :actioner ] ,
482+ connection : TicketStakeholder . connections [ :assigned ] ,
483+ )
484+ old_stakeholder . update! ( connection : TicketStakeholder . connections [ :cc ] )
485+
486+ existing_new = @ticket . ticket_stakeholders . find_by (
487+ stakeholder_type : "User" ,
488+ stakeholder_id : @new_delegate . id ,
489+ )
490+
491+ if existing_new
492+ existing_new . update! ( connection : TicketStakeholder . connections [ :assigned ] )
493+ else
494+ @ticket . ticket_stakeholders . create! (
495+ stakeholder : @new_delegate ,
496+ connection : TicketStakeholder . connections [ :assigned ] ,
497+ stakeholder_role : TicketStakeholder . stakeholder_roles [ :actioner ] ,
498+ is_active : true ,
499+ )
500+ end
501+
502+ @ticket . ticket_logs . create! (
503+ action_type : @action_type ,
504+ acting_user_id : current_user . id ,
505+ acting_stakeholder_id : @acting_stakeholder . id ,
506+ metadata_action : @metadata_action ,
507+ )
508+ end
509+
510+ render status : :ok , json : @ticket
511+ end
397512end
0 commit comments