Download purchase order PDF from API

This commit is contained in:
David Bomba 2023-02-01 20:00:55 +11:00
parent 83def1f297
commit a69eae2f66
3 changed files with 91 additions and 6 deletions

View File

@ -762,4 +762,74 @@ class PurchaseOrderController extends BaseController
}
/**
* @OA\Get(
* path="/api/v1/purchase_order/{invitation_key}/download",
* operationId="downloadPurchaseOrder",
* tags={"purchase_orders"},
* summary="Download a specific purchase order by invitation key",
* description="Downloads a specific purchase order",
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="invitation_key",
* in="path",
* description="The Purchase Order Invitation Key",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Purchase Order pdf",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
* @param $invitation_key
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function downloadPdf($invitation_key)
{
$invitation = $this->purchase_order_repository->getInvitationByKey($invitation_key);
if (! $invitation) {
return response()->json(['message' => 'no record found'], 400);
}
$purchase_order = $invitation->purchase_order;
$file = $purchase_order->service()->getPurchaseOrderPdf();
$headers = ['Content-Type' => 'application/pdf'];
if (request()->input('inline') == 'true') {
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
}
return response()->streamDownload(function () use ($file) {
echo Storage::get($file);
}, basename($file), $headers);
}
}

View File

@ -242,6 +242,12 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
Route::post('projects/bulk', [ProjectController::class, 'bulk'])->name('projects.bulk');
Route::put('projects/{project}/upload', [ProjectController::class, 'upload'])->name('projects.upload');
Route::resource('purchase_orders', PurchaseOrderController::class);
Route::post('purchase_orders/bulk', [PurchaseOrderController::class, 'bulk'])->name('purchase_orders.bulk');
Route::put('purchase_orders/{purchase_order}/upload', [PurchaseOrderController::class, 'upload']);
Route::get('purchase_orders/{purchase_order}/{action}', [PurchaseOrderController::class, 'action'])->name('purchase_orders.action');
Route::get('purchase_order/{invitation_key}/download', [PurchaseOrderController::class, 'downloadPdf'])->name('purchase_orders.downloadPdf');
Route::resource('quotes', QuoteController::class); // name = (quotes. index / create / show / update / destroy / edit
Route::get('quotes/{quote}/{action}', [QuoteController::class, 'action'])->name('quotes.action');
Route::post('quotes/bulk', [QuoteController::class, 'bulk'])->name('quotes.bulk');
@ -316,12 +322,6 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
Route::post('vendors/bulk', [VendorController::class, 'bulk'])->name('vendors.bulk');
Route::put('vendors/{vendor}/upload', [VendorController::class, 'upload']);
Route::resource('purchase_orders', PurchaseOrderController::class);
Route::post('purchase_orders/bulk', [PurchaseOrderController::class, 'bulk'])->name('purchase_orders.bulk');
Route::put('purchase_orders/{purchase_order}/upload', [PurchaseOrderController::class, 'upload']);
Route::get('purchase_orders/{purchase_order}/{action}', [PurchaseOrderController::class, 'action'])->name('purchase_orders.action');
Route::get('users', [UserController::class, 'index']);
Route::get('users/create', [UserController::class, 'create'])->middleware('password_protected');
Route::get('users/{user}', [UserController::class, 'show'])->middleware('password_protected');

View File

@ -40,6 +40,21 @@ class PurchaseOrderTest extends TestCase
$this->makeTestData();
}
public function testPurchaseOrderDownloadPDF()
{
$i = $this->purchase_order->invitations->first();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get("/api/v1/purchase_order/{$i->key}/download");
$response->assertStatus(200);
$this->assertTrue($response->headers->get('content-type') == 'application/pdf');
}
public function testPurchaseOrderGetWithClientStatus()
{
$response = $this->withHeaders([