Skip to content

Commit

Permalink
Merge branch 'next-12824/docuemtn-preview-needs-authentication' into …
Browse files Browse the repository at this point in the history
…'master'

NEXT-12824 - get document preview with headers

See merge request shopware/6/product/platform!4274
  • Loading branch information
ssltg committed Feb 2, 2021
2 parents c8dd739 + 44fc8a9 commit 962331d
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 29 deletions.
@@ -0,0 +1,7 @@
---
title: Fix pagination setting snippet listing
issue: NEXT-13252
---
# Administration
* Deprecated method `generateDocumentPreviewLink` in `Administration/Resources/app/administration/src/core/service/api/document.api.service.js` use `getDocumentPreview` instead.
* Deprecated method `generateDocumentLink` in `Administration/Resources/app/administration/src/core/service/api/document.api.service.js` use `getDocument` instead.
Expand Up @@ -50,11 +50,41 @@ class DocumentApiService extends ApiService {
});
}

/**
* @deprecated tag:v6.4.0 use getDocumentPreview to get the preview blob instead
*/
generateDocumentPreviewLink(orderId, orderDeepLink, documentTypeName, config, context) {
// eslint-disable-next-line max-len
return `${context.apiPath}/v${this.getApiVersion()}/_action/order/${orderId}/${orderDeepLink}/document/${documentTypeName}/preview?config=${config}`;
}

getDocumentPreview(orderId, orderDeepLink, documentTypeName, params) {
const config = JSON.stringify(params);

return this.httpClient
.get(
`/_action/order/${orderId}/${orderDeepLink}/document/${documentTypeName}/preview?config=${config}`,
{
responseType: 'blob',
headers: this.getBasicHeaders()
}
);
}

getDocument(documentId, documentDeepLink, context, download = false) {
return this.httpClient
.get(
`/_action/document/${documentId}/${documentDeepLink}${download ? '?download=1' : ''}`,
{
responseType: 'blob',
headers: this.getBasicHeaders()
}
);
}

/**
* @deprecated tag:v6.4.0 use getDocument to get the document blob instead
*/
generateDocumentLink(documentId, documentDeepLink, context, download = false) {
// eslint-disable-next-line max-len
return `${context.apiPath}/v${this.getApiVersion()}/_action/document/${documentId}/${documentDeepLink}${download ? '?download=1' : ''}`;
Expand Down
Expand Up @@ -221,6 +221,24 @@ Component.register('sw-order-document-card', {
this.showModal = true;
},

downloadDocument(documentId, documentDeepLink) {
this.documentService.getDocument(
documentId,
documentDeepLink,
Shopware.Context.api,
true
).then((response) => {
if (response.data) {
const filename = response.headers['content-disposition'].split('filename=')[1];
const link = document.createElement('a');
link.href = URL.createObjectURL(response.data);
link.download = filename;
link.dispatchEvent(new MouseEvent('click'));
link.parentNode.removeChild(link);
}
});
},

onCreateDocument(params, additionalAction, referencedDocumentId = null, file = null) {
this.showModal = false;
this.$nextTick().then(() => {
Expand All @@ -236,35 +254,31 @@ Component.register('sw-order-document-card', {
this.$emit('document-save');

if (additionalAction === 'download') {
window.open(
this.documentService.generateDocumentLink(
response.data.documentId,
response.data.documentDeepLink,
Shopware.Context.api,
true
),
'_blank'
);
this.downloadDocument(response.data.documentId, response.data.documentDeepLink);
}
});
},

onPreview(params) {
const config = JSON.stringify(params);
window.open(
this.documentService.generateDocumentPreviewLink(
this.order.id,
this.order.deepLinkCode,
this.currentDocumentType.technicalName,
config,
Shopware.Context.api
),
'_blank'
);
this.documentService.getDocumentPreview(
this.order.id,
this.order.deepLinkCode,
this.currentDocumentType.technicalName,
params
).then((response) => {
if (response.data) {
const filename = response.headers['content-disposition'].split('filename=')[1];
const link = document.createElement('a');
link.href = URL.createObjectURL(response.data);
link.download = filename;
link.dispatchEvent(new MouseEvent('click'));
link.parentNode.removeChild(link);
}
});
},

onDownload(id, deepLink) {
window.open(this.documentService.generateDocumentLink(id, deepLink, Shopware.Context.api, false), '_blank');
this.downloadDocument(id, deepLink);
}
}
});
Expand Up @@ -45,7 +45,7 @@
{% block sw_order_document_settings_modal_form_document_footer %}
<template #modal-footer>
{% block sw_order_document_settings_modal_form_document_footer_preview %}
<sw-button size="small" @click="onPreview">
<sw-button id="sw-order-document-settings-modal__preview-button" size="small" @click="onPreview">
{{ $tc('sw-order.documentModal.labelPreview') }}
</sw-button>
{% endblock %}
Expand Down
Expand Up @@ -36,6 +36,11 @@ describe('Order: Create credit note', () => {
method: 'post'
}).as('orderRecalculateCall');

cy.route({
url: `${Cypress.env('apiPath')}/_action/order/**/document/invoice/preview*`,
method: 'get'
}).as('onPreview');

cy.route({
url: `${Cypress.env('apiPath')}/_action/order/*/document/invoice`,
method: 'post'
Expand Down Expand Up @@ -83,13 +88,22 @@ describe('Order: Create credit note', () => {
'Invoice'
);

// Generate preview
cy.get('.sw-order-document-settings-modal__settings-modal').should('be.visible');
cy.get('#sw-field--documentConfig-documentComment').type('New invoice');
cy.get('#sw-order-document-settings-modal__preview-button').click();

cy.wait('@onPreview').then((xhr) => {
expect(xhr.status).equal(200);
});

// Generate invoice
cy.get('.sw-order-document-settings-modal__settings-modal').should('be.visible');
cy.get('#sw-field--documentConfig-documentComment').type('New invoice');
cy.get('.sw-order-document-settings-modal__settings-modal .sw-button--primary').click();

cy.wait('@createInvoice').then((xhr) => {
expect(xhr).to.have.property('status', 200);
expect(xhr.status).equal(200);
});

// Open create edit note modal
Expand Down
Expand Up @@ -82,12 +82,27 @@ describe('Order: Test order state', () => {
const documentId = xhr.response.body.documentId;
const documentDeepLink = xhr.response.body.documentDeepLink;

return cy.request(`/api/v*/_action/document/${documentId}/${documentDeepLink}`);
}).then((xhr) => {
expect(xhr).to.have.property('status', 200);
expect(xhr.headers).to.have.property('content-type', 'application/pdf');
cy.getCookie('bearerAuth').then((bearerAuth) => {
return JSON.parse(bearerAuth.value).access;
}).then((authToken) => {
return cy.request(
{
headers: {
Accept: 'application/vnd.api+json',
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json'
},
method: 'GET',
url: `/api/v*/_action/document/${documentId}/${documentDeepLink}`
}
);
}).then((xhr) => {
expect(xhr).to.have.property('status', 200);
expect(xhr.headers).to.have.property('content-type', 'application/pdf');
});
});


cy.wait('@findDocumentCall').then((xhr) => {
cy.log(`metal.total${xhr.responseBody.meta.total}`);
expect(xhr).to.have.property('status', 200);
Expand Down
3 changes: 1 addition & 2 deletions src/Core/Checkout/Document/Controller/DocumentController.php
Expand Up @@ -42,7 +42,7 @@ public function __construct(DocumentService $documentService, EntityRepositoryIn

/**
* @Since("6.0.0.0")
* @Route("/api/v{version}/_action/document/{documentId}/{deepLinkCode}", defaults={"auth_required"=false}, name="api.action.download.document", methods={"GET"})
* @Route("/api/v{version}/_action/document/{documentId}/{deepLinkCode}", name="api.action.download.document", methods={"GET"})
*/
public function downloadDocument(Request $request, string $documentId, string $deepLinkCode, Context $context): Response
{
Expand Down Expand Up @@ -76,7 +76,6 @@ public function downloadDocument(Request $request, string $documentId, string $d
* @Since("6.0.0.0")
* @Route(
* "/api/v{version}/_action/order/{orderId}/{deepLinkCode}/document/{documentTypeName}/preview",
* defaults={"auth_required"=false},
* name="api.action.document.preview",
* methods={"GET"}
* )
Expand Down

0 comments on commit 962331d

Please sign in to comment.