diff --git a/app/Export/CSV/ClientExport.php b/app/Export/CSV/ClientExport.php index 0335be61c467..caf90402d34f 100644 --- a/app/Export/CSV/ClientExport.php +++ b/app/Export/CSV/ClientExport.php @@ -17,6 +17,7 @@ use App\Models\Company; use App\Transformers\ClientContactTransformer; use App\Transformers\ClientTransformer; use App\Utils\Ninja; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\App; use League\Csv\Writer; @@ -87,7 +88,27 @@ class ClientExport extends BaseExport $this->contact_transformer = new ClientContactTransformer(); } - public function run() + public function returnJson() + { + $query = $this->init(); + + $headerdisplay = $this->buildHeader(); + + $header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){ + return ['identifier' => $value, 'display_value' => $headerdisplay[$value]]; + })->toArray(); + + $report = $query->cursor() + ->map(function ($client) { + return $this->buildRow($client); + })->toArray(); + + return array_merge(['columns' => $header], $report); + } + + + + public function init(): Builder { MultiDB::setDb($this->company->db); App::forgetInstance('translator'); @@ -95,15 +116,9 @@ class ClientExport extends BaseExport $t = app('translator'); $t->replace(Ninja::transformTranslations($this->company->settings)); - //load the CSV document from a string - $this->csv = Writer::createFromString(); - if (count($this->input['report_keys']) == 0) { - $this->input['report_keys'] = array_values($this->entity_keys); + $this->input['report_keys'] = array_values($this->client_report_keys); } - - //insert the header - $this->csv->insertOne($this->buildHeader()); $query = Client::query()->with('contacts') ->withTrashed() @@ -112,6 +127,20 @@ class ClientExport extends BaseExport $query = $this->addDateRange($query); + return $query; + + } + + public function run() + { + $query = $this->init(); + + //load the CSV document from a string + $this->csv = Writer::createFromString(); + + //insert the header + $this->csv->insertOne($this->buildHeader()); + $query->cursor() ->each(function ($client) { $this->csv->insertOne($this->buildRow($client)); diff --git a/app/Http/Controllers/Reports/ClientReportController.php b/app/Http/Controllers/Reports/ClientReportController.php index 462b090046c0..bcbc2c02d98d 100644 --- a/app/Http/Controllers/Reports/ClientReportController.php +++ b/app/Http/Controllers/Reports/ClientReportController.php @@ -11,13 +11,14 @@ namespace App\Http\Controllers\Reports; +use App\Models\Client; +use Illuminate\Http\Response; +use App\Utils\Traits\MakesHash; use App\Export\CSV\ClientExport; +use App\Jobs\Report\SendToAdmin; +use App\Jobs\Report\PreviewReport; use App\Http\Controllers\BaseController; use App\Http\Requests\Report\GenericReportRequest; -use App\Jobs\Report\SendToAdmin; -use App\Models\Client; -use App\Utils\Traits\MakesHash; -use Illuminate\Http\Response; class ClientReportController extends BaseController { @@ -63,14 +64,26 @@ class ClientReportController extends BaseController */ public function __invoke(GenericReportRequest $request) { + /** @var \App\Models\User $user */ + $user = auth()->user(); + if ($request->has('send_email') && $request->get('send_email')) { - SendToAdmin::dispatch(auth()->user()->company(), $request->all(), ClientExport::class, $this->filename); + SendToAdmin::dispatch($user->company(), $request->all(), ClientExport::class, $this->filename); return response()->json(['message' => 'working...'], 200); } - // expect a list of visible fields, or use the default - $export = new ClientExport(auth()->user()->company(), $request->all()); + // expect a list of visible fields, or use the default + if($request->has('output') && $request->input('output') == 'json') { + + $hash = \Illuminate\Support\Str::uuid(); + + PreviewReport::dispatch($user->company(), $request->all(), ClientExport::class, $hash); + + return response()->json(['message' => $hash], 200); + } + + $export = new ClientExport($user->company(), $request->all()); $csv = $export->run(); diff --git a/tests/Feature/Export/ReportApiTest.php b/tests/Feature/Export/ReportApiTest.php index a6f45b825e3a..6b91468241e9 100644 --- a/tests/Feature/Export/ReportApiTest.php +++ b/tests/Feature/Export/ReportApiTest.php @@ -59,21 +59,7 @@ class ReportApiTest extends TestCase } - public function testActivityCSVExportJson() - { - $data = [ - 'send_email' => false, - 'date_range' => 'all', - 'report_keys' => [], - ]; - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->postJson('/api/v1/reports/activities?output=json', $data) - ->assertStatus(200); - - } public function testUserSalesReportApiRoute() { diff --git a/tests/Feature/Export/ReportPreviewTest.php b/tests/Feature/Export/ReportPreviewTest.php index 2f029a125cd7..eb08b560c550 100644 --- a/tests/Feature/Export/ReportPreviewTest.php +++ b/tests/Feature/Export/ReportPreviewTest.php @@ -44,6 +44,39 @@ class ReportPreviewTest extends TestCase } + + public function testClientExportJson() + { + $data = [ + 'send_email' => false, + 'date_range' => 'all', + 'report_keys' => [], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/reports/clients?output=json', $data) + ->assertStatus(200); + + } + + public function testActivityCSVExportJson() + { + $data = [ + 'send_email' => false, + 'date_range' => 'all', + 'report_keys' => [], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/reports/activities?output=json', $data) + ->assertStatus(200); + + } + public function testCreditExportPreview() {