abstract-service-ability.php
3 months ago
create-service.php
3 months ago
delete-service.php
3 months ago
disable-service.php
3 months ago
duplicate-service.php
3 months ago
enable-service.php
3 months ago
get-service-agents.php
3 months ago
get-service-bookings.php
3 months ago
get-service.php
3 months ago
get-services.php
3 months ago
list-service-categories.php
3 months ago
update-service.php
3 months ago
get-service-agents.php
71 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityGetServiceAgents extends LatePointAbstractServiceAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/get-service-agents'; |
| 10 | $this->label = __( 'Get service agents', 'latepoint' ); |
| 11 | $this->description = __( 'Returns agents (staff) assigned to a service.', 'latepoint' ); |
| 12 | $this->permission = 'service__view'; |
| 13 | $this->read_only = true; |
| 14 | } |
| 15 | |
| 16 | public function get_input_schema(): array { |
| 17 | return [ |
| 18 | 'type' => 'object', |
| 19 | 'properties' => [ |
| 20 | 'service_id' => [ |
| 21 | 'type' => 'integer', |
| 22 | 'description' => __( 'Service ID.', 'latepoint' ), |
| 23 | ], |
| 24 | ], |
| 25 | 'required' => [ 'service_id' ], |
| 26 | ]; |
| 27 | } |
| 28 | |
| 29 | public function get_output_schema(): array { |
| 30 | return [ |
| 31 | 'type' => 'object', |
| 32 | 'properties' => [ |
| 33 | 'agents' => [ |
| 34 | 'type' => 'array', |
| 35 | 'items' => [ 'type' => 'object' ], |
| 36 | ], |
| 37 | ], |
| 38 | ]; |
| 39 | } |
| 40 | |
| 41 | public function execute( array $args ) { |
| 42 | $service = new OsServiceModel( (int) $args['service_id'] ); |
| 43 | if ( $service->is_new_record() ) { |
| 44 | return new WP_Error( 'not_found', __( 'Service not found.', 'latepoint' ), [ 'status' => 404 ] ); |
| 45 | } |
| 46 | |
| 47 | $agents = ( new OsAgentModel() ) |
| 48 | ->join( LATEPOINT_TABLE_AGENTS_SERVICES, [ 'agent_id' => LATEPOINT_TABLE_AGENTS . '.id' ] ) |
| 49 | ->where( [ LATEPOINT_TABLE_AGENTS_SERVICES . '.service_id' => $service->id ] ) |
| 50 | ->get_results_as_models(); |
| 51 | |
| 52 | return [ 'agents' => array_map( [ $this, 'serialize_agent' ], $agents ) ]; |
| 53 | } |
| 54 | |
| 55 | private function serialize_agent( OsAgentModel $a ): array { |
| 56 | return [ |
| 57 | 'id' => (int) $a->id, |
| 58 | 'first_name' => $a->first_name ?? '', |
| 59 | 'last_name' => $a->last_name ?? '', |
| 60 | 'full_name' => trim( ( $a->first_name ?? '' ) . ' ' . ( $a->last_name ?? '' ) ), |
| 61 | 'email' => $a->email ?? '', |
| 62 | 'phone' => $a->phone ?? '', |
| 63 | 'status' => $a->status ?? '', |
| 64 | 'bio' => $a->bio ?? '', |
| 65 | 'wp_user_id' => (int) ( $a->wp_user_id ?? 0 ), |
| 66 | 'created_at' => ! empty( $a->created_at ) ? date( 'c', strtotime( $a->created_at ) ) : '', |
| 67 | 'updated_at' => ! empty( $a->updated_at ) ? date( 'c', strtotime( $a->updated_at ) ) : '', |
| 68 | ]; |
| 69 | } |
| 70 | } |
| 71 |