Skip to content

Commit

Permalink
feat: add clicks and opens to listboss results (#1)
Browse files Browse the repository at this point in the history
* feat: add clicks and opens to listboss results

* feat: styling

* feat: remove client sort

* feat: move sorting to server

* style: formatting

* fix: fallback for no sorting

* fix: changed key name

* fix: changed direction to asc/desc

* fix: fix soring

* fix: fixed sorting bug

* chore: remove CodeStorm

* feat: pagination

* feat: add search

---------

Co-authored-by: René <[email protected]>
  • Loading branch information
nfmerijn and 64knl authored Mar 19, 2024
1 parent fcd21f7 commit 435f688
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/Helpers/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ private function call(string $method = 'GET', string $endPoint = '', array $para
return null;
}

$queryOrJson = $method === 'GET' ? 'query' : 'json';
$client = new Client();
$endPoint = 'job/'.$endPoint;
$newJob = $client->request($method, config('listboss.endpoint').$endPoint, [
'json' => $params,
$queryOrJson => $params,
'headers' => [
'Authorization' => 'Bearer '.config('listboss.api_key'),
'Content-Type' => 'application/json',
Expand Down
19 changes: 17 additions & 2 deletions src/Helpers/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,24 @@ public function start(array $list)
return $this->call(method: 'POST', endPoint: $this->id.'/start', params: $parameters, updateSelf: true);
}

public function result(): object
public function result(string $sort = null, int $page = 1, string $direction = 'desc', string $query = null): object
{
return $this->call(endPoint: $this->id.'/result', updateSelf: true);
$params = [];
if ($sort !== null) {
$params['sort'] = $sort;
$params['direction'] = $direction;
$params['page'] = $page;
}

if ($query !== null) {
$params['query'] = $query;
}

return $this->call(
endPoint: $this->id.'/result',
params: $params,
updateSelf: true
);
}

public function status(): ?JobStatus
Expand Down
58 changes: 42 additions & 16 deletions src/Http/Controllers/ListBossController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use NotFound\Framework\Http\Controllers\Controller;
use NotFound\Framework\Http\Requests\FormDataRequest;
use NotFound\Layout\Elements\LayoutBar;
use NotFound\Layout\Elements\LayoutPager;
use NotFound\Layout\Elements\LayoutSearchBox;
use NotFound\Layout\Elements\LayoutText;
use NotFound\Layout\Elements\Table\LayoutTable;
use NotFound\Layout\Elements\Table\LayoutTableColumn;
Expand All @@ -22,6 +25,7 @@ public function index()
}

$widget = new LayoutWidgetHelper('E-mails', 'Verzendingen');
$widget->widget->noPadding();
$widget->widget->addTable($this->selectJob());

return $widget->response();
Expand All @@ -33,28 +37,59 @@ public function status(FormDataRequest $request, int $list)
abort(404);
}

// Create job
$job = new Job($list);

$widget = new LayoutWidgetHelper('E-mails', 'Status');
// Get results
$validated = $request->validate([
'sort' => 'string|in:opens,clicks,send_status',
'asc' => 'string|in:true,false',
'page' => 'integer|min:1',
'search' => 'string|nullable',
]);

$jobResults = $job->result(
sort: $validated['sort'] ?? 'opens',
page: $validated['page'] ?? 1,
query: $validated['search'] ?? null,
direction: (isset($validated['asc']) && $validated['asc'] === 'true') ? 'asc' : 'desc',
);

$widget = new LayoutWidgetHelper('E-mails', 'Status: '.$jobResults->message);
$widget->widget->noPadding();
$widget->addBreadcrumb('Verzendingen', '/app/listboss/');
$widget->widget->addText(new LayoutText('Status: '.$job->status()->getReadableName()));

$widget->widget->addText($this->statusText($job->statusInfo()));
$bar = new LayoutBar();
$widget->widget->addText(new LayoutText('Aantal ontvangers: '.$jobResults->recipients));
$widget->widget->addText(new LayoutText('Aantal afgeleverd: '.$jobResults->delivered));
$widget->widget->addText(new LayoutText('Aantal fouten: '.$jobResults->failed));
$widget->widget->addText(new LayoutText('Aantal ontvangers geopend: '.$jobResults->opens));
$widget->widget->addText(new LayoutText('Aantal ontvangers geklikt: '.$jobResults->clicks));

$pager = new LayoutPager($jobResults->recipients, 100);
$bar->addPager($pager);
$search = new LayoutSearchBox('Zoek e-mailadres');
$bar->addSearchBox($search);

$widget->widget->addBar($bar);

$table = new LayoutTable(sort: false, delete: false, create: false);

$table->addHeader(new LayoutTableHeader('E-mailadres', 'email'));
$table->addHeader(new LayoutTableHeader('Ontvangen', 'received'));
$table->addHeader(new LayoutTableHeader('Geklikt', 'clicks'));
$table->addHeader(new LayoutTableHeader('Geopend', 'opens'));
$table->addHeader((new LayoutTableHeader('Status', 'send_status'))->sortable());
$table->addHeader((new LayoutTableHeader('Geopend', 'opens'))->sortable());
$table->addHeader((new LayoutTableHeader('Geklikt', 'clicks'))->sortable());

$rowId = 1;
foreach ($job->result()->results as $result) {

foreach ($jobResults->results as $result) {
$row = new LayoutTableRow($rowId++, '/app/listboss/'.$job->id().'/'.$result->id);
$row->addColumn(new LayoutTableColumn($result->email));
$row->addColumn(new LayoutTableColumn(\Sb::formatDate($result->delivered_at)));
$row->addColumn(new LayoutTableColumn($result->clicks));
$row->addColumn(new LayoutTableColumn($result->send_status ?? '-'));
$row->addColumn(new LayoutTableColumn($result->opens));
$row->addColumn(new LayoutTableColumn($result->clicks));
$table->addRow($row);
}

Expand All @@ -63,15 +98,6 @@ public function status(FormDataRequest $request, int $list)
return $widget->response();
}

private function statusText(object $statusInfo): LayoutText
{
$text = '<p>Aantal ontvangers: '.($statusInfo->recipients ?? '-').
'<p>Voortgang: <strong>'.($statusInfo->progress ?? '0').'%</strong>'.
'<p>Fouten: '.($statusInfo->errors ?? '0');

return new LayoutText($text);
}

private function selectJob(): LayoutTable
{
$listBoss = new ListBoss();
Expand Down

0 comments on commit 435f688

Please sign in to comment.