Skip to content

Commit

Permalink
Add filters on export queues (#1205)
Browse files Browse the repository at this point in the history
* Fix where clause is ambiguous

Fix "where clause is ambiguous"

* Fix filtering when no filter input text is not needed

When no filter input text is not needed  and field has dot notation:.

This filtering applies to :
- is_empty
- is_not_empty
- is_null
- is_not_null
- is_blank
- is_not_blank

* Fix braces

* Add filters on export queues

Missing filers on exporting on queues

* Remove lines, not needed

* Remove and clean code

* Remove collection

* FIx code

* Remove ignore line

* Remove unused import

* Pin fixes

* Add ignore line

* fix export count logic

* pint

---------

Co-authored-by: luanfreitasdev <[email protected]>
  • Loading branch information
mariusdatakrag and luanfreitasdev authored Oct 14, 2023
1 parent 2eed22c commit a93e0b0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
21 changes: 12 additions & 9 deletions src/Jobs/ExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Bus\{Batchable, Queueable};
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\{InteractsWithQueue, SerializesModels};
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
Expand Down Expand Up @@ -35,25 +34,29 @@ public function __construct(
$this->fileName = $params['fileName'];
$this->offset = $params['offset'];
$this->limit = $params['limit'];
$this->filters = $params['filters'];

/** @var PowerGridComponent $componentTable */
$this->componentTable = new $componentTable();
}

public function handle(): void
{
/** @var Builder $query */
$query = $this->componentTable->datasource();
$exportable = new $this->exportableClass();

$query = $query->offset($this->offset)
->limit($this->limit)
->get();
$currentHiddenStates = collect($this->columns)
->mapWithKeys(fn ($column) => [data_get($column, 'field') => data_get($column, 'hidden')]);

$exportable = new $this->exportableClass();
$columnsWithHiddenState = array_map(function ($column) use ($currentHiddenStates) {
$column->hidden = $currentHiddenStates[$column->field];

return $column;
}, $this->componentTable->columns());

/** @phpstan-ignore-next-line */
$exportable->fileName($this->getFilename())
->setData($this->columns, $this->transform($query))
$exportable
->fileName($this->getFilename())
->setData($columnsWithHiddenState, $this->prepareToExport())
->download([]);
}
}
42 changes: 31 additions & 11 deletions src/Traits/ExportableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace PowerComponents\LivewirePowerGrid\Traits;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\{Str, Stringable};
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Illuminate\Database\Eloquent as Eloquent;
use Illuminate\Support\{Collection, Str, Stringable};
use PowerComponents\LivewirePowerGrid\DataSource\Builder;
use PowerComponents\LivewirePowerGrid\{PowerGridComponent, ProcessDataSource};

/** @codeCoverageIgnore */
trait ExportableJob
Expand All @@ -21,23 +22,42 @@ trait ExportableJob

private int $limit;

private array $filters;

private function getFilename(): Stringable
{
return Str::of($this->fileName)
->replace('.xlsx', '')
->replace('.csv', '');
}

private function transform(Collection $collection): Collection
private function prepareToExport(): Eloquent\Collection|Collection
{
return $collection->transform(function ($row) {
$columns = $this->componentTable->addColumns()->columns;
/** @phpstan-ignore-next-line */
$processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn ($datasource) => $datasource->get());

$inClause = $processDataSource->component->filtered ?? [];

/** @phpstan-ignore-next-line */
$this->componentTable->filters = $this->filters ?? [];

/** @phpstan-ignore-next-line */
$currentTable = $processDataSource->component->currentTable;

$sortField = Str::of($processDataSource->component->sortField)->contains('.') ? $processDataSource->component->sortField : $currentTable . '.' . $processDataSource->component->sortField;

foreach ($columns as $key => $column) {
$row->{$key} = $column($row);
}
$results = $processDataSource->prepareDataSource()
->where(
fn ($query) => Builder::make($query, $this->componentTable)
->filterContains()
->filter()
)
->when($inClause, function ($query, $inClause) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $inClause);
})
->orderBy($sortField, $processDataSource->component->sortDirection)
->get();

return $row;
});
return $processDataSource->transform($results);
}
}
8 changes: 6 additions & 2 deletions src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ public function runOnQueue(string $exportFileType, string $exportType): bool

private function putQueuesToBus(string $exportableClass, string $fileExtension): Collection
{
$processDataSource = tap(ProcessDataSource::fillData($this), fn ($datasource) => $datasource->get());

$this->exportedFiles = [];
$filters = $processDataSource?->component?->filters ?? [];
$queues = collect([]);
$countQueue = $this->getQueuesCount();
$perPage = $this->total / $countQueue;
$countQueue = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1;
$perPage = $this->total > $countQueue ? ($this->total / $countQueue) : 1;
$offset = 0;
$limit = $perPage;

Expand All @@ -117,6 +120,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension):
'fileName' => $fileName,
'offset' => $offset,
'limit' => $limit,
'filters' => $filters,
];

$queues->push(new $this->exportableJobClass(
Expand Down

0 comments on commit a93e0b0

Please sign in to comment.