Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Dec 23, 2024
1 parent 0d6dee4 commit 0d44481
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/Jobs/ExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(
$this->fileName = $params['fileName'];
$this->offset = $params['offset'];
$this->limit = $params['limit'];
$this->filtered = $params['filtered'];
$this->filters = (array) Crypt::decrypt($params['filters']);
$this->properties = (array) Crypt::decrypt($params['parameters']);

Expand All @@ -41,8 +42,6 @@ public function __construct(

public function handle(): void
{
$exportable = new $this->exportableClass();

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

Expand All @@ -53,7 +52,7 @@ public function handle(): void
}, $this->componentTable->columns());

/** @phpstan-ignore-next-line */
$exportable
(new $this->exportableClass())
->fileName($this->getFilename())
->setData($columnsWithHiddenState, $this->prepareToExport($this->properties))
->download([]);
Expand Down
14 changes: 7 additions & 7 deletions src/Traits/ExportableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ trait ExportableJob

private array $filters;

private array $filtered;

private function getFilename(): Stringable
{
return Str::of($this->fileName)
Expand All @@ -35,18 +37,16 @@ private function getFilename(): Stringable

private function prepareToExport(array $properties = []): Eloquent\Collection|Collection
{
/** @phpstan-ignore-next-line */
$this->componentTable->filters = $this->filters ?? [];
$this->componentTable->filters = $this->filters ?? [];
$this->componentTable->filtered = $this->filtered ?? [];

/** @phpstan-ignore-next-line */
$processDataSource = tap(
ProcessDataSource::make($this->componentTable, $properties),
fn ($datasource) => $datasource->get()
);

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

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

$sortField = Str::of($processDataSource->component->sortField)->contains('.') ? $processDataSource->component->sortField : $currentTable . '.' . $processDataSource->component->sortField;
Expand All @@ -57,8 +57,8 @@ private function prepareToExport(array $properties = []): Eloquent\Collection|Co
->filterContains()
->filter()
)
->when($inClause, function ($query, $inClause) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $inClause);
->when($filtered, function ($query, $filtered) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $filtered);
})
->offset($this->offset)
->limit($this->limit)
Expand Down
17 changes: 9 additions & 8 deletions src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension):

$this->exportedFiles = [];
$filters = $processDataSource?->component?->filters ?? [];
$filtered = $processDataSource?->component?->filtered ?? [];
$queues = collect([]);
$queueCount = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1;
$perPage = $this->total > $queueCount ? ($this->total / $queueCount) : 1;
Expand All @@ -120,6 +121,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension):
'.' . $fileExtension;

$params = [
'filtered' => $filtered,
'exportableClass' => $exportableClass,
'fileName' => $fileName,
'offset' => $offset,
Expand Down Expand Up @@ -166,16 +168,16 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
{
$processDataSource = tap(ProcessDataSource::make($this), fn ($datasource) => $datasource->get());

$inClause = $processDataSource->component->filtered;
$filtered = $processDataSource->component->filtered;

if ($selected && filled($processDataSource->component->checkboxValues)) {
$inClause = $processDataSource->component->checkboxValues;
$filtered = $processDataSource->component->checkboxValues;
}

if ($processDataSource->component->datasource() instanceof Collection) {
if ($inClause) {
if ($filtered) {
$results = $processDataSource->get(isExport: true)
->whereIn($this->primaryKey, $inClause);
->whereIn($this->primaryKey, $filtered);

return DataSourceBase::transform($results, $this);
}
Expand All @@ -194,8 +196,8 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
->filterContains()
->filter()
)
->when($inClause, function ($query, $inClause) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $inClause);
->when($filtered, function ($query, $filtered) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $filtered);
})
->orderBy($sortField, $processDataSource->component->sortDirection)
->get();
Expand Down Expand Up @@ -244,10 +246,9 @@ private function export(string $exportType, bool $selected): BinaryFileResponse|
/** @var string $fileName */
$fileName = data_get($this->setUp, 'exportable.fileName');
$exportable
->fileName($fileName) /** @phpstan-ignore-next-line */
->fileName($fileName)
->setData($columnsWithHiddenState, $this->prepareToExport($selected));

/** @phpstan-ignore-next-line */
return $exportable->download(
exportOptions: $this->setUp['exportable']
);
Expand Down

0 comments on commit 0d44481

Please sign in to comment.