From 01ecc22d5f81ee51cdeedf75a22a60d7a7f178b5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 27 Apr 2022 15:17:45 +1000 Subject: [PATCH] Exports --- app/Export/CSV/BaseExport.php | 2 +- app/Export/CSV/ClientExport.php | 6 +- app/Export/CSV/ContactExport.php | 33 ++++---- .../OpenAPI/ClientReportSchema.php | 21 +++++ app/Http/Controllers/OpenAPI/ClientSchema.php | 1 - .../Reports/ClientContactReportController.php | 84 +++++++++++++++++++ .../Reports/ClientReportController.php | 21 +++-- .../Report/ClientContactReportRequest.php | 38 +++++++++ .../Requests/Report/ClientReportRequest.php | 11 +++ routes/api.php | 1 + tests/Feature/Export/ExportCsvTest.php | 5 +- 11 files changed, 193 insertions(+), 30 deletions(-) create mode 100644 app/Http/Controllers/OpenAPI/ClientReportSchema.php create mode 100644 app/Http/Controllers/Reports/ClientContactReportController.php create mode 100644 app/Http/Requests/Report/ClientContactReportRequest.php diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 79d108f1da40..d9283ac3d38f 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -16,7 +16,7 @@ use Illuminate\Support\Carbon; class BaseExport { - private function addDateRange($query) + protected function addDateRange($query) { $date_range = $this->input['date_range']; diff --git a/app/Export/CSV/ClientExport.php b/app/Export/CSV/ClientExport.php index e493ca109a5d..8894f5f2a351 100644 --- a/app/Export/CSV/ClientExport.php +++ b/app/Export/CSV/ClientExport.php @@ -21,11 +21,11 @@ use Illuminate\Support\Facades\App; use League\Csv\Writer; use Illuminate\Support\Carbon; -class ClientExport +class ClientExport extends BaseExport { private $company; - private $input; + protected $input; private $client_transformer; @@ -35,7 +35,7 @@ class ClientExport private string $end_date; - private string $date_key = 'created_at'; + protected string $date_key = 'created_at'; private array $entity_keys = [ 'address1' => 'client.address1', diff --git a/app/Export/CSV/ContactExport.php b/app/Export/CSV/ContactExport.php index 4e6871f49587..e9dba9704135 100644 --- a/app/Export/CSV/ContactExport.php +++ b/app/Export/CSV/ContactExport.php @@ -21,15 +21,17 @@ use App\Utils\Ninja; use Illuminate\Support\Facades\App; use League\Csv\Writer; -class ContactExport +class ContactExport extends BaseExport { - private $company; + private Company $company; - private $report_keys; + protected array $input; - private $client_transformer; + private ClientTransformer $client_transformer; - private $contact_transformer; + private ClientContactTransformer $contact_transformer; + + protected string $date_key = 'created_at'; private array $entity_keys = [ 'address1' => 'client.address1', @@ -79,10 +81,10 @@ class ContactExport 'client.industry', ]; - public function __construct(Company $company, array $report_keys) + public function __construct(Company $company, array $input) { $this->company = $company; - $this->report_keys = $report_keys; + $this->input = $input; $this->client_transformer = new ClientTransformer(); $this->contact_transformer = new ClientContactTransformer(); } @@ -102,13 +104,16 @@ class ContactExport //insert the header $this->csv->insertOne($this->buildHeader()); - ClientContact::where('company_id', $this->company->id) - ->cursor() - ->each(function ($contact){ + $query = ClientContact::query() + ->where('company_id', $this->company->id); - $this->csv->insertOne($this->buildRow($contact)); + $query = $this->addDateRange($query); - }); + $query->cursor()->each(function ($contact){ + + $this->csv->insertOne($this->buildRow($contact)); + + }); return $this->csv->toString(); @@ -120,7 +125,7 @@ class ContactExport $header = []; - foreach(array_keys($this->report_keys) as $key) + foreach(array_keys($this->input['report_keys']) as $key) $header[] = ctrans("texts.{$key}"); return $header; @@ -136,7 +141,7 @@ class ContactExport $entity = []; - foreach(array_values($this->report_keys) as $key){ + foreach(array_values($this->input['report_keys']) as $key){ $parts = explode(".",$key); $entity[$parts[1]] = ""; diff --git a/app/Http/Controllers/OpenAPI/ClientReportSchema.php b/app/Http/Controllers/OpenAPI/ClientReportSchema.php new file mode 100644 index 000000000000..c92e9f117cfa --- /dev/null +++ b/app/Http/Controllers/OpenAPI/ClientReportSchema.php @@ -0,0 +1,21 @@ +user()->company(), $request->all()); + + $csv = $export->run(); + + $headers = array( + 'Content-Disposition' => 'attachment', + 'Content-Type' => 'text/csv', + ); + + return response()->streamDownload(function () use ($csv) { + echo $csv; + }, $this->filename, $headers); + + } + + + +} diff --git a/app/Http/Controllers/Reports/ClientReportController.php b/app/Http/Controllers/Reports/ClientReportController.php index 324ee773feb7..0ece930625f3 100644 --- a/app/Http/Controllers/Reports/ClientReportController.php +++ b/app/Http/Controllers/Reports/ClientReportController.php @@ -36,6 +36,10 @@ class ClientReportController extends BaseController * description="Export client reports", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\RequestBody( + * required=true, + * @OA\JsonContent(ref="#/components/schemas/ClientReportSchema") + * ), * @OA\Response( * response=200, * description="success", @@ -59,16 +63,19 @@ class ClientReportController extends BaseController { // expect a list of visible fields, or use the default - // return response()->json(['message' => 'Processing'], 200); - - // [ - // 'report_keys', - // 'date_range', - // ] - $export = new ClientExport(auth()->user()->company(), $request->all()); $csv = $export->run(); + + $headers = array( + 'Content-Disposition' => 'attachment', + 'Content-Type' => 'text/csv', + ); + + return response()->streamDownload(function () use ($csv) { + echo $csv; + }, 'clients.csv', $headers); + } diff --git a/app/Http/Requests/Report/ClientContactReportRequest.php b/app/Http/Requests/Report/ClientContactReportRequest.php new file mode 100644 index 000000000000..a8c8035a1246 --- /dev/null +++ b/app/Http/Requests/Report/ClientContactReportRequest.php @@ -0,0 +1,38 @@ +user()->isAdmin(); + } + + public function rules() + { + return [ + 'start_date' => 'string|date', + 'end_date' => 'string|date', + 'date_key' => 'string', + 'date_range' => 'string', + 'report_keys' => 'sometimes|array' + ]; + } +} diff --git a/app/Http/Requests/Report/ClientReportRequest.php b/app/Http/Requests/Report/ClientReportRequest.php index 6d30fc36a9d3..8112e3be19c5 100644 --- a/app/Http/Requests/Report/ClientReportRequest.php +++ b/app/Http/Requests/Report/ClientReportRequest.php @@ -24,4 +24,15 @@ class ClientReportRequest extends Request { return auth()->user()->isAdmin(); } + + public function rules() + { + return [ + 'start_date' => 'string|date', + 'end_date' => 'string|date', + 'date_key' => 'string', + 'date_range' => 'string', + 'report_keys' => 'sometimes|array' + ]; + } } diff --git a/routes/api.php b/routes/api.php index 385cdd98aa3d..d3a56076c9af 100644 --- a/routes/api.php +++ b/routes/api.php @@ -155,6 +155,7 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale Route::post('refresh', 'Auth\LoginController@refresh'); Route::post('reports/clients', 'Reports\ClientReportController'); + Route::post('reports/contacts', 'Reports\ClientContactReportController'); Route::get('scheduler', 'SchedulerController@index'); diff --git a/tests/Feature/Export/ExportCsvTest.php b/tests/Feature/Export/ExportCsvTest.php index d1ac66b6e91f..8d50478c7e7d 100644 --- a/tests/Feature/Export/ExportCsvTest.php +++ b/tests/Feature/Export/ExportCsvTest.php @@ -34,8 +34,6 @@ class ExportCsvTest extends TestCase ThrottleRequests::class ); - // $this->faker = \Faker\Factory::create(); - $this->makeTestData(); $this->withoutExceptionHandling(); @@ -61,7 +59,6 @@ class ExportCsvTest extends TestCase // nlog(print_r($merged_keys, 1)); // nlog(print_r($merged_values, 1)); - foreach ($merged_keys as &$key) { $key = ctrans('texts.'.$key); } @@ -76,7 +73,7 @@ class ExportCsvTest extends TestCase } } - //Storage::put(base_path('invy.csv'), $csv->getContent()); + // Storage::put('invy.csv', $csv->getContent()); $this->markTestSkipped(); }