Added endpoint to seed default ticket types
from eventbrite POST /api/v1/summits/{id}/ticket-types/seed-defaults Change-Id: Ie8f625ef092f319084710641c9ae5e6d5d051692
This commit is contained in:
parent
5395330f27
commit
75d969b81a
|
@ -406,9 +406,42 @@ final class OAuth2SummitsTicketTypesApiController extends OAuth2ProtectedControl
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $summit_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function seedDefaultTicketTypesBySummit($summit_id){
|
||||
try {
|
||||
|
||||
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
||||
if (is_null($summit)) return $this->error404();
|
||||
|
||||
$ticket_types = $this->ticket_type_service->seedSummitTicketTypesFromEventBrite($summit);
|
||||
|
||||
$response = new PagingResponse
|
||||
(
|
||||
count($ticket_types),
|
||||
count($ticket_types),
|
||||
1,
|
||||
1,
|
||||
$ticket_types
|
||||
);
|
||||
|
||||
return $this->created($response->toArray());
|
||||
}
|
||||
catch (ValidationException $ex1) {
|
||||
Log::warning($ex1);
|
||||
return $this->error412(array($ex1->getMessage()));
|
||||
}
|
||||
catch(EntityNotFoundException $ex2)
|
||||
{
|
||||
Log::warning($ex2);
|
||||
return $this->error404(array('message'=> $ex2->getMessage()));
|
||||
}
|
||||
catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
return $this->error500($ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -15,6 +15,7 @@ use GuzzleHttp\Client;
|
|||
use Exception;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use models\summit\Summit;
|
||||
/**
|
||||
* Class EventbriteAPI
|
||||
* @package services\apis
|
||||
|
@ -48,26 +49,28 @@ final class EventbriteAPI implements IEventbriteAPI
|
|||
|
||||
$client = new Client();
|
||||
|
||||
$query = array
|
||||
(
|
||||
$query = [
|
||||
|
||||
'token' => $this->auth_info['token']
|
||||
);
|
||||
];
|
||||
|
||||
foreach ($params as $param => $value) {
|
||||
$query[$param] = $value;
|
||||
}
|
||||
|
||||
$response = $client->get($api_url, array
|
||||
(
|
||||
$response = $client->get($api_url, [
|
||||
'query' => $query
|
||||
)
|
||||
]
|
||||
);
|
||||
|
||||
if ($response->getStatusCode() !== 200)
|
||||
throw new Exception('invalid status code!');
|
||||
|
||||
$content_type = $response->getHeaderLine('content-type');
|
||||
|
||||
if (empty($content_type))
|
||||
throw new Exception('invalid content type!');
|
||||
|
||||
if ($content_type !== 'application/json')
|
||||
throw new Exception('invalid content type!');
|
||||
|
||||
|
@ -88,6 +91,16 @@ final class EventbriteAPI implements IEventbriteAPI
|
|||
{
|
||||
$order_id = intval($order_id);
|
||||
$url = sprintf('%s/orders/%s', self::BaseUrl, $order_id);
|
||||
return $this->getEntity($url, array('expand' => 'attendees'));
|
||||
return $this->getEntity($url, ['expand' => 'attendees']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Summit $summit
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTicketTypes(Summit $summit){
|
||||
$event_id = $summit->getExternalSummitId();
|
||||
$url = sprintf('%s/events/%s', self::BaseUrl, $event_id);
|
||||
return $this->getEntity($url, ['expand' => 'ticket_classes']);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
namespace services\apis;
|
||||
<?php namespace services\apis;
|
||||
/**
|
||||
* Copyright 2016 OpenStack Foundation
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
@ -12,6 +11,11 @@ namespace services\apis;
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
use models\summit\Summit;
|
||||
/**
|
||||
* Interface IEventbriteAPI
|
||||
* @package services\apis
|
||||
*/
|
||||
interface IEventbriteAPI
|
||||
{
|
||||
/**
|
||||
|
@ -25,4 +29,10 @@ interface IEventbriteAPI
|
|||
* @return mixed
|
||||
*/
|
||||
public function getOrder($order_id);
|
||||
|
||||
/**
|
||||
* @param Summit $summit
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTicketTypes(Summit $summit);
|
||||
}
|
|
@ -48,4 +48,11 @@ interface ISummitTicketTypeService
|
|||
* @throws ValidationException
|
||||
*/
|
||||
public function deleteTicketType(Summit $summit, $ticket_type_id);
|
||||
|
||||
/**
|
||||
* @param Summit $summit
|
||||
* @return SummitTicketType[]
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function seedSummitTicketTypesFromEventBrite(Summit $summit);
|
||||
}
|
|
@ -18,6 +18,8 @@ use models\exceptions\ValidationException;
|
|||
use models\summit\ISummitTicketTypeRepository;
|
||||
use models\summit\Summit;
|
||||
use models\summit\SummitTicketType;
|
||||
use services\apis\IEventbriteAPI;
|
||||
|
||||
/**
|
||||
* Class SummitTicketTypeService
|
||||
* @package App\Services\Model
|
||||
|
@ -32,19 +34,27 @@ final class SummitTicketTypeService
|
|||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var IEventbriteAPI
|
||||
*/
|
||||
private $eventbrite_api;
|
||||
|
||||
/**
|
||||
* SummitTicketTypeService constructor.
|
||||
* @param ISummitTicketTypeRepository $repository
|
||||
* @param IEventbriteAPI $eventbrite_api
|
||||
* @param ITransactionService $tx_service
|
||||
*/
|
||||
public function __construct
|
||||
(
|
||||
ISummitTicketTypeRepository $repository,
|
||||
IEventbriteAPI $eventbrite_api,
|
||||
ITransactionService $tx_service
|
||||
)
|
||||
{
|
||||
parent::__construct($tx_service);
|
||||
$this->repository = $repository;
|
||||
$this->repository = $repository;
|
||||
$this->eventbrite_api = $eventbrite_api;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -195,4 +205,58 @@ final class SummitTicketTypeService
|
|||
$summit->removeTicketType($ticket_type);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Summit $summit
|
||||
* @return SummitTicketType[]
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function seedSummitTicketTypesFromEventBrite(Summit $summit){
|
||||
|
||||
return $this->tx_service->transaction(function() use($summit){
|
||||
$res = [];
|
||||
$external_summit_id = $summit->getExternalSummitId();
|
||||
|
||||
if(empty($external_summit_id)){
|
||||
throw new ValidationException
|
||||
(
|
||||
trans
|
||||
(
|
||||
'validation_errors.SummitTicketTypeService.seedSummitTicketTypesFromEventBrite.MissingExternalId',
|
||||
[
|
||||
'summit_id' => $summit->getId()
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$response = $this->eventbrite_api->getTicketTypes($summit);
|
||||
|
||||
if (!isset($response['ticket_classes'])) return $res;
|
||||
|
||||
$ticket_classes = $response['ticket_classes'];
|
||||
|
||||
foreach ($ticket_classes as $ticket_class) {
|
||||
|
||||
$id = $ticket_class['id'];
|
||||
$old_ticket_type = $summit->getTicketTypeByExternalId($id);
|
||||
|
||||
if (!is_null($old_ticket_type)) {
|
||||
|
||||
$old_ticket_type->setName(trim($ticket_class['name']));
|
||||
$old_ticket_type->setDescription(isset($ticket_class['description']) ? trim($ticket_class['description']) : '');
|
||||
continue;
|
||||
}
|
||||
|
||||
$new_ticket_type = new SummitTicketType();
|
||||
$new_ticket_type->setExternalId($id);
|
||||
$new_ticket_type->setName($ticket_class['name']);
|
||||
$new_ticket_type->setDescription(isset($ticket_class['description']) ? trim($ticket_class['description']) : '');
|
||||
$summit->addTicketType($new_ticket_type);
|
||||
$res[] = $new_ticket_type;
|
||||
}
|
||||
|
||||
return $res;
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1132,6 +1132,15 @@ class ApiEndpointsSeeder extends Seeder
|
|||
sprintf(SummitScopes::WriteSummitData, $current_realm)
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'seed-default-ticket-types',
|
||||
'route' => '/api/v1/summits/{id}/ticket-types/seed-defaults',
|
||||
'http_method' => 'POST',
|
||||
'scopes' => [
|
||||
sprintf(SummitScopes::WriteTicketTypeData, $current_realm),
|
||||
sprintf(SummitScopes::WriteSummitData, $current_realm)
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'update-ticket-type',
|
||||
'route' => '/api/v1/summits/{id}/ticket-types/{ticket_type_id}',
|
||||
|
|
|
@ -59,4 +59,5 @@ return [
|
|||
'SummitTicketTypeService.addTicketType.ExternalIdAlreadyExists' => 'ticket external id :external_id already exists on summit :summit_id',
|
||||
'SummitTicketTypeService.updateTicketType.NameAlreadyExists' => 'ticket name :name already exists on summit :summit_id',
|
||||
'SummitTicketTypeService.updateTicketType.ExternalIdAlreadyExists' => 'ticket external id :external_id already exists on summit :summit_id',
|
||||
'SummitTicketTypeService.seedSummitTicketTypesFromEventBrite.MissingExternalId' => 'summit :summit_is has not set external id (eventbrite)',
|
||||
];
|
|
@ -165,4 +165,35 @@ final class OAuth2TicketTypesApiTest extends ProtectedApiTest
|
|||
$this->assertTrue($ticket_type->description == 'test description');
|
||||
return $ticket_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $summit_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function testSeedDefaultTicketTypes($summit_id = 24){
|
||||
$params = [
|
||||
'id' => $summit_id,
|
||||
];
|
||||
|
||||
$headers = [
|
||||
"HTTP_Authorization" => " Bearer " . $this->access_token,
|
||||
"CONTENT_TYPE" => "application/json"
|
||||
];
|
||||
|
||||
$response = $this->action(
|
||||
"POST",
|
||||
"OAuth2SummitsTicketTypesApiController@seedDefaultTicketTypesBySummit",
|
||||
$params,
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
$headers
|
||||
);
|
||||
|
||||
$content = $response->getContent();
|
||||
$this->assertResponseStatus(201);
|
||||
$ticket_types = json_decode($content);
|
||||
$this->assertTrue(!is_null($ticket_types));
|
||||
return $ticket_types;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue