Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SL-150 #3130

Merged
merged 17 commits into from
Jul 31, 2024
Merged

SL-150 #3130

Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion common
Submodule common updated 0 files
229 changes: 210 additions & 19 deletions src/Tickets/Seating/Admin/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ class Ajax extends Controller_Contract {
*/
public const ACTION_FETCH_ATTENDEES = 'tec_tickets_seating_fetch_attendees';

/**
* The action to update a set of seat types.
*
* @since TBD
*
* @var string
*/
public const ACTION_SEAT_TYPES_UPDATED = 'tec_tickets_seating_seat_types_updated';

/**
* The action to update a set of reservations following a seat type update.
*
* @since TBD
*
* @var string
*/
public const ACTION_RESERVATIONS_UPDATED_FROM_SEAT_TYPES = 'tec_tickets_seating_reservations_updated_from_seat_types';

/**
* A reference to the Seat Types service object.
*
Expand Down Expand Up @@ -208,8 +226,10 @@ public function __construct(
protected function do_register(): void {
$this->register_assets();
add_action( 'wp_ajax_' . self::ACTION_GET_SEAT_TYPES_BY_LAYOUT_ID, [ $this, 'fetch_seat_types_by_layout_id' ] );
add_action( 'wp_ajax_' . self::ACTION_INVALIDATE_MAPS_LAYOUTS_CACHE,
[ $this, 'invalidate_maps_layouts_cache' ] );
add_action(
'wp_ajax_' . self::ACTION_INVALIDATE_MAPS_LAYOUTS_CACHE,
[ $this, 'invalidate_maps_layouts_cache' ]
);
add_action( 'wp_ajax_' . self::ACTION_INVALIDATE_LAYOUTS_CACHE, [ $this, 'invalidate_layouts_cache' ] );
add_action( 'wp_ajax_' . self::ACTION_DELETE_MAP, [ $this, 'delete_map_from_service' ] );
add_action( 'wp_ajax_' . self::ACTION_DELETE_LAYOUT, [ $this, 'delete_layout_from_service' ] );
Expand All @@ -218,6 +238,11 @@ protected function do_register(): void {
add_action( 'wp_ajax_' . self::ACTION_CLEAR_RESERVATIONS, [ $this, 'clear_reservations' ] );
add_action( 'wp_ajax_nopriv_' . self::ACTION_CLEAR_RESERVATIONS, [ $this, 'clear_reservations' ] );
add_action( 'wp_ajax_' . self::ACTION_DELETE_RESERVATIONS, [ $this, 'delete_reservations' ] );
add_action( 'wp_ajax_' . self::ACTION_SEAT_TYPES_UPDATED, [ $this, 'update_seat_types' ] );
add_action(
'wp_ajax_' . self::ACTION_RESERVATIONS_UPDATED_FROM_SEAT_TYPES,
[ $this, 'update_reservations_from_seat_types' ]
);

add_action( 'tec_tickets_seating_session_interrupt', [ $this, 'clear_commerce_cart_cookie' ] );
}
Expand All @@ -244,6 +269,11 @@ public function unregister(): void {
remove_action( 'wp_ajax_nopriv_' . self::ACTION_CLEAR_RESERVATIONS, [ $this, 'clear_reservations' ] );
remove_action( 'tec_tickets_seating_session_interrupt', [ $this, 'clear_commerce_cart_cookie' ] );
remove_action( 'wp_ajax_' . self::ACTION_DELETE_RESERVATIONS, [ $this, 'delete_reservations' ] );
remove_action( 'wp_ajax_' . self::ACTION_SEAT_TYPES_UPDATED, [ $this, 'update_seat_types' ] );
remove_action(
'wp_ajax_' . self::ACTION_RESERVATIONS_UPDATED_FROM_SEAT_TYPES,
[ $this, 'update_reservations_from_seat_types' ]
);
}

/**
Expand All @@ -255,17 +285,19 @@ public function unregister(): void {
*/
public function get_ajax_data(): array {
return [
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'ajaxNonce' => wp_create_nonce( self::NONCE_ACTION ),
'ACTION_INVALIDATE_MAPS_LAYOUTS_CACHE' => self::ACTION_INVALIDATE_MAPS_LAYOUTS_CACHE,
'ACTION_INVALIDATE_LAYOUTS_CACHE' => self::ACTION_INVALIDATE_LAYOUTS_CACHE,
'ACTION_DELETE_MAP' => self::ACTION_DELETE_MAP,
'ACTION_DELETE_LAYOUT' => self::ACTION_DELETE_LAYOUT,
'ACTION_POST_RESERVATIONS' => self::ACTION_POST_RESERVATIONS,
'ACTION_CLEAR_RESERVATIONS' => self::ACTION_CLEAR_RESERVATIONS,
'ACTION_DELETE_RESERVATIONS' => self::ACTION_DELETE_RESERVATIONS,
'ACTION_FETCH_ATTENDEES' => self::ACTION_FETCH_ATTENDEES,
'ACTION_GET_SEAT_TYPES_BY_LAYOUT_ID' => self::ACTION_GET_SEAT_TYPES_BY_LAYOUT_ID,
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'ajaxNonce' => wp_create_nonce( self::NONCE_ACTION ),
'ACTION_INVALIDATE_MAPS_LAYOUTS_CACHE' => self::ACTION_INVALIDATE_MAPS_LAYOUTS_CACHE,
'ACTION_INVALIDATE_LAYOUTS_CACHE' => self::ACTION_INVALIDATE_LAYOUTS_CACHE,
'ACTION_DELETE_MAP' => self::ACTION_DELETE_MAP,
'ACTION_DELETE_LAYOUT' => self::ACTION_DELETE_LAYOUT,
'ACTION_POST_RESERVATIONS' => self::ACTION_POST_RESERVATIONS,
'ACTION_CLEAR_RESERVATIONS' => self::ACTION_CLEAR_RESERVATIONS,
'ACTION_DELETE_RESERVATIONS' => self::ACTION_DELETE_RESERVATIONS,
'ACTION_FETCH_ATTENDEES' => self::ACTION_FETCH_ATTENDEES,
'ACTION_GET_SEAT_TYPES_BY_LAYOUT_ID' => self::ACTION_GET_SEAT_TYPES_BY_LAYOUT_ID,
'ACTION_SEAT_TYPES_UPDATED' => self::ACTION_SEAT_TYPES_UPDATED,
'ACTION_RESERVATIONS_UPDATED_FROM_SEAT_TYPES' => self::ACTION_RESERVATIONS_UPDATED_FROM_SEAT_TYPES,
];
}

Expand All @@ -280,9 +312,9 @@ private function register_assets(): void {
$this->built_asset_url( 'ajax.js' ),
Tickets_Main::VERSION
)
->add_localize_script( 'tec.tickets.seating.ajax', [ $this, 'get_ajax_data' ] )
->add_to_group( 'tec-tickets-seating' )
->register();
->add_localize_script( 'tec.tickets.seating.ajax', [ $this, 'get_ajax_data' ] )
->add_to_group( 'tec-tickets-seating' )
->register();
}

/**
Expand Down Expand Up @@ -468,7 +500,7 @@ public function update_reservations() {
return;
}

$body = $this->get_request_body();
$body = $this->get_request_body();
$decoded = json_decode( $body, true );

if ( ! (
Expand Down Expand Up @@ -551,12 +583,12 @@ public function update_reservations() {
* @return void The JSON response is sent to the client.
*/
public function clear_reservations(): void {
if ( ! $this->check_current_ajax_user_can( 'exist') ) {
if ( ! $this->check_current_ajax_user_can( 'exist' ) ) {
return;
}

$post_id = (int) tribe_get_request_var( 'postId', 0 );
$token = tribe_get_request_var( 'token' );
$token = tribe_get_request_var( 'token' );

if ( ! ( $post_id && $token && is_string( $token ) ) ) {
wp_send_json_error(
Expand Down Expand Up @@ -683,4 +715,163 @@ public function delete_reservations(): void {

wp_send_json_success( [ 'numberDeleted' => $deleted ] );
}

/**
* Handles the update of seat types from the service.
*
* @since TBD
*
* @return void The function does not return a value but will echo the JSON response.
*/
public function update_seat_types(): void {
if ( ! $this->check_current_ajax_user_can( 'manage_options' ) ) {
wp_send_json_error(
[
'error' => 'Nonce verification failed',
],
403
);

return;
}

$body = $this->get_request_body();
$decoded = json_decode( $body, true );

if ( ! ( $decoded && is_array( $decoded ) ) ) {
wp_send_json_error(
[
'error' => 'Invalid request body',
],
400
);

return;
}

$valid = array_filter(
$decoded,
static function ( $updated_seat_type ) {
return is_array( $updated_seat_type )
&& isset(
$updated_seat_type['id'],
$updated_seat_type['name'],
$updated_seat_type['mapId'],
$updated_seat_type['layoutId'],
$updated_seat_type['description'],
$updated_seat_type['seatsCount'],
);
}
);

if ( empty( $valid ) ) {
wp_send_json_error(
[
'error' => 'Invalid request body',
],
400
);

return;
}

$updated_seat_types = $this->seat_types->update_from_service( $valid );

if ( false === $updated_seat_types ) {
wp_send_json_error(
[
'error' => 'Failed to update the seat types from the service.',
],
500
);

return;
}

$seat_type_to_capacity_map = array_reduce(
$valid,
static function ( array $carry, array $seat_type ): array {
return $carry + [ $seat_type['id'] => $seat_type['seatsCount'] ];
},
[]
);

$updated_tickets = $this->seat_types->update_tickets_capacity( $seat_type_to_capacity_map );

$layout_to_seats_map = array_reduce(
$valid,
static function ( array $carry, array $seat_type ): array {
$layout_id = $seat_type['layoutId'];
if ( isset( $carry[ $layout_id ] ) ) {
$carry[ $layout_id ] += $seat_type['seatsCount'];
} else {
$carry[ $layout_id ] = $seat_type['seatsCount'];
}

return $carry;
},
[]
);

$updated_posts = $this->layouts->update_posts_capacity( $layout_to_seats_map );

wp_send_json_success(
[
'updatedSeatTypes' => $updated_seat_types,
'updatedTickets' => $updated_tickets,
'updatedPosts' => $updated_posts,
]
);
}

/**
* Updates the seat type of Attendees following based on their reservation.
*
* @since TBD
*
* @return void The function does not return a value but will send the JSON response.
*/
public function update_reservations_from_seat_types(): void {
if ( ! $this->check_current_ajax_user_can( 'manage_options' ) ) {
wp_send_json_error(
[
'error' => 'Nonce verification failed',
],
403
);

return;
}

$body = $this->get_request_body();
$decoded = json_decode( $body, true );

if ( ! ( $decoded && is_array( $decoded ) ) ) {
wp_send_json_error(
[
'error' => 'Invalid request body',
],
400
);

return;
}

$valid = array_filter( $decoded, 'is_array' );

if ( empty( $valid ) ) {
wp_send_json_error(
[
'error' => 'Invalid request body',
],
400
);

return;
}

$updated = $this->reservations->update_attendees_seat_type( $valid );

wp_send_json_success( [ 'updatedAttendees' => $updated ] );
}
}
Loading
Loading