Skip to content

Commit

Permalink
Merge pull request #392 from cultuurnet/feature/WID-513
Browse files Browse the repository at this point in the history
WID-513 - Use UiTPAS promotions from new UiTPAS API
  • Loading branch information
brampauwelyn authored Feb 28, 2024
2 parents 69adcda + 83ebab6 commit 103fcdf
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 19 deletions.
15 changes: 15 additions & 0 deletions app/ApplicationBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CultuurNet\ProjectAanvraag\Project\ProjectProvider;
use CultuurNet\ProjectAanvraag\SearchAPI\SearchAPIServiceProvider;
use CultuurNet\ProjectAanvraag\CuratorenAPI\CuratorenAPIServiceProvider;
use CultuurNet\ProjectAanvraag\UitpasAPI\UitpasAPIServiceProvider;
use CultuurNet\ProjectAanvraag\ArticleLinkerAPI\ArticleLinkerAPIServiceProvider;
use CultuurNet\ProjectAanvraag\User\UserRoleServiceProvider;
use CultuurNet\ProjectAanvraag\User\UserServiceProvider;
Expand Down Expand Up @@ -181,6 +182,20 @@ function ($translator, $app) {
]
);

// Uitpas API
$this->register(
new UitpasAPIServiceProvider(),
[
'uitpas_api.base_url' => $this['config']['uitpas_api']['live']['base_url'],
'uitpas_api.x_client_id' => $this['config']['uitpas_api']['live']['x_client_id'],
'uitpas_api_test.base_url' => $this['config']['uitpas_api']['test']['base_url'],
'uitpas_api_test.x_client_id' => $this['config']['uitpas_api']['test']['x_client_id'],
'uitpas_api.cache.enabled' => $this['config']['uitpas_api']['cache']['enabled'],
'uitpas_api.cache.backend' => $this['config']['uitpas_api']['cache']['backend'],
'uitpas_api.cache.ttl' => $this['config']['uitpas_api']['cache']['ttl'],
]
);

// Curatoren API
$this->register(
new CuratorenAPIServiceProvider(),
Expand Down
36 changes: 36 additions & 0 deletions app/UitpasAPI/UitpasAPIServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace CultuurNet\ProjectAanvraag\UitpasAPI;

use CultuurNet\ProjectAanvraag\APIServiceProviderBase;
use CultuurNet\ProjectAanvraag\Uitpas\UitpasClient;
use GuzzleHttp\Client;
use Pimple\Container;

class UitpasAPIServiceProvider extends APIServiceProviderBase
{

public function register(Container $pimple)
{
$pimple['uitpas_api'] = function (Container $pimple) {
return new UitpasClient(new Client($this->getConfig($pimple, false)));
};

$pimple['uitpas_api_test'] = function (Container $pimple) {
return new UitpasClient(new Client($this->getConfig($pimple, true)));
};
}

private function getConfig(Container $pimple, bool $test): array
{
return [
'base_uri' => $test ? $pimple['uitpas_api_test.base_url'] : $pimple['uitpas_api.base_url'],
'headers' => [
'Content-type' => 'application/json; charset=utf-8',
'Accept' => 'application/ld+json',
'x-client-id' => $test ? $pimple['uitpas_api_test.x_client_id'] : $pimple['uitpas_api.x_client_id'],
],
'handler' => $this->getHandlerStack('uitpas_api', $pimple),
];
}
}
3 changes: 2 additions & 1 deletion app/Widget/WidgetServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public function register(Container $pimple)
$pimple['translator']
),
$pimple['translator'],
$pimple['curatoren_api']
$pimple['curatoren_api'],
$pimple['uitpas_api']
);
};

Expand Down
14 changes: 14 additions & 0 deletions config.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ search_api:
backend: filesystem # supported cache: filesystem or redis
ttl: 3600

# Uitpas API Settings
uitpas_api:
test:
base_url: https://api-test.uitpas.be
x_client_id: ''
live:
base_url: https://api.uitpas.be
x_client_id: ''
cache:
enabled: true
backend: redis
ttl: 3600


# Curatoren API settings
curatoren_api:
test:
Expand Down
53 changes: 53 additions & 0 deletions src/Uitpas/UitpasClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace CultuurNet\ProjectAanvraag\Uitpas;

use GuzzleHttp\ClientInterface;

/**
* Default client to perform searches on the uitpas api.
*/
class UitpasClient implements UitpasClientInterface
{

/**
* @var ClientInterface
*/
protected $client;

/**
* UitpasClient constructor.
* @param ClientInterface $client
*/
public function __construct(ClientInterface $client)
{
$this->client = $client;
}

public function setClient(ClientInterface $client)
{
$this->client = $client;
}

public function getClient()
{
return $this->client;
}

public function searchRewards(String $organizerId, Int $limit)
{
$options = [
'query' => [
'organizerId' => $organizerId,
'status' => 'ACTIVE',
'type' => 'POINTS',
'sort[creationDate]' => 'desc',
'limit' => $limit,
],
];

$result = $this->client->request('GET', 'rewards', $options);

return json_decode($result->getBody(true), true);
}
}
20 changes: 20 additions & 0 deletions src/Uitpas/UitpasClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CultuurNet\ProjectAanvraag\Uitpas;

use GuzzleHttp\ClientInterface;

/**
* Provides an interface for search clients on the uitpas api
*/
interface UitpasClientInterface
{
/**
* Perform a search on uitpas rewards.
*
* @param String $organizerdId
* @param Int $limit
* @return Array
*/
public function searchRewards(String $organizerId, Int $limit);
}
37 changes: 22 additions & 15 deletions src/Widget/Twig/TwigPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use CultuurNet\ProjectAanvraag\Widget\Translation\Service\TranslateTerm;
use CultuurNet\ProjectAanvraag\Widget\Translation\Service\FilterForKeyWithFallback;
use CultuurNet\ProjectAanvraag\Curatoren\CuratorenClient;
use CultuurNet\ProjectAanvraag\Uitpas\UitpasClient;
use CultuurNet\SearchV3\ValueObjects\CalendarSummaryFormat;
use CultuurNet\SearchV3\ValueObjects\CalendarSummaryLanguage;
use CultuurNet\SearchV3\ValueObjects\Event;
Expand Down Expand Up @@ -69,6 +70,12 @@ class TwigPreprocessor
*/
protected $curatorenClient;

/**
* @var UitpasClient
*/
protected $uitpasClient;


/**
* @var array
*/
Expand All @@ -87,7 +94,8 @@ public function __construct(
FilterForKeyWithFallback $translateWithFallback,
TranslateTerm $translateTerm,
TranslatorInterface $translator,
CuratorenClient $curatorenClient
CuratorenClient $curatorenClient,
UitpasClient $uitpasClient
) {
$this->twig = $twig;
$this->request = $requestStack->getCurrentRequest();
Expand All @@ -97,6 +105,7 @@ public function __construct(
$this->translateTerm = $translateTerm;
$this->translator = $translator;
$this->curatorenClient = $curatorenClient;
$this->uitpasClient = $uitpasClient;
$this->fallbackImages = [];
}

Expand Down Expand Up @@ -344,23 +353,20 @@ public function preprocessEventDetail(Event $event, string $langcode, array $set
}

$variables['uitpas_promotions'] = '';
// Load Uitpas promotions via culturefeed.
if ($variables['uitpas'] && !empty($settings['uitpas_benefits']) && $event->getOrganizer()) {
$promotionsQuery = new \CultureFeed_Uitpas_Passholder_Query_SearchPromotionPointsOptions();
$promotionsQuery->max = 4;
$promotionsQuery->balieConsumerKey = $event->getOrganizer()->getCdbid();
$promotionsQuery->unexpired = true;
$organizerName = $this->translateOrganizerName($event, $langcode);

$organizerId = $event->getOrganizer()->getCdbid();

try {
$uitpasPromotions = $this->cultureFeed->uitpas()->getPromotionPoints($promotionsQuery);
$uitpasPromotions = $this->uitpasClient->searchRewards($organizerId, 4);
$variables['uitpas_promotions'] = $this->twig->render(
'widgets/search-results-widget/uitpas-promotions.html.twig',
[
'promotions' => $this->preprocessUitpasPromotions($uitpasPromotions),
'promotions' => $this->preprocessUitpasPromotions($uitpasPromotions['member']),
'organizerName' => $organizerName,
'organizerUrlName' => $this->formatOrganizerUrlName($organizerName),
'organizerId' => $promotionsQuery->balieConsumerKey,
'organizerId' => $organizerId,
]
);
} catch (\Exception $e) {
Expand Down Expand Up @@ -519,16 +525,17 @@ public function formatPublisherLabel($publisher)

/**
* Preprocess the uitpas promotions.
* @param \CultureFeed_ResultSet $resultSet
* @param array $rawPromotions
*/
public function preprocessUitpasPromotions(\CultureFeed_ResultSet $resultSet)
public function preprocessUitpasPromotions($rawPromotions)
{
$promotions = [];
/** @var \CultureFeed_Uitpas_Passholder_PointsPromotion $object */
foreach ($resultSet->objects as $object) {

foreach ($rawPromotions as $promotion) {
$promotions[] = [
'title' => $object->title,
'points' => $object->points,
'title' => $promotion['title'],
'points' => $promotion['points'],
'benefitURL' => 'https://www.uitpas.be/voordelen-zoeken#/voordelen/' . $promotion['id'],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div class="cnw_uitpas-promotions">

<p class="cnw_uitpas-promotions-block-title">Voordelen bij {{ organizerName }}</p>
<p class="cnw_uitpas-promotions-block-title">Voordelen bij &nbsp;{{ organizerName }}</p>

<ul>
{% for promotion in promotions %}
<li>{{ promotion.title }}</li>
<li><span class="cnw_uitpas-promotions-points-badge">{{ promotion.points }} &nbsp; {{ promotion.points == 1 ? 'punt' : 'punten' }}</span> <a href="{{ promotion.benefitURL }}" target="_blank">{{ promotion.title }}</a></li>
{% endfor %}
</ul>

Expand Down
32 changes: 31 additions & 1 deletion web/assets/css/cn_widget_styling.css

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions widgets-static/app/styles/01_components/_detail-page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@
margin: 0 0 10px 0;
padding: 0;
list-style: none;

li {
display: flex;
}

li a {
color: #40404a;
margin-left: 12px;
}

li:not(:first-child) {
margin-top: 6px;
}
}

&-points-badge {
background-color:#40404a;
border-radius:3px;
color:#fff;
display:inline-block;
padding:1px .325rem;
display: flex;
justify-content: center;
min-width: 5rem;
}
}

Expand Down

0 comments on commit 103fcdf

Please sign in to comment.