From ac676ccc28f11fc8e1bfa8b2087baf49595da993 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Mon, 24 Jun 2024 13:34:50 +0800 Subject: [PATCH] Laravel Vapor - Export pdf file through S3 bucket (sync code for bug solved at shipping portal) --- .../ImportedInvoiceMappedComponent.vue | 19 ++++++---- .../ImportedReceiptMappedComponent.vue | 19 ++++++---- .../sections/TransactionsMappingComponent.vue | 38 +++++++++++-------- ...omerTransactionHistorySectionComponent.vue | 19 ++++++---- .../elements/UploadDebtorExcelComponent.vue | 19 ++++++---- .../forms/SupplierPlaceOrderFormComponent.vue | 19 ++++++---- .../CustomerTransactionSectionComponent.vue | 19 ++++++---- .../CustomerWalletTransactionComponent.vue | 19 ++++++---- .../assets/vue/vuex/modules/crudRequest.js | 17 ++++++++- .../assets/vue/vuex/modules/crudRequestV2.js | 17 ++++++++- 10 files changed, 131 insertions(+), 74 deletions(-) diff --git a/resources/assets/vue/components/accounting/sections/ImportedInvoiceMappedComponent.vue b/resources/assets/vue/components/accounting/sections/ImportedInvoiceMappedComponent.vue index a76464d7..01194163 100644 --- a/resources/assets/vue/components/accounting/sections/ImportedInvoiceMappedComponent.vue +++ b/resources/assets/vue/components/accounting/sections/ImportedInvoiceMappedComponent.vue @@ -128,15 +128,18 @@ const url = this.route('importedInvoiceMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName; if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(url, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: url, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(url, '_blank'); diff --git a/resources/assets/vue/components/accounting/sections/ImportedReceiptMappedComponent.vue b/resources/assets/vue/components/accounting/sections/ImportedReceiptMappedComponent.vue index 2dd7770b..c81532c8 100644 --- a/resources/assets/vue/components/accounting/sections/ImportedReceiptMappedComponent.vue +++ b/resources/assets/vue/components/accounting/sections/ImportedReceiptMappedComponent.vue @@ -132,15 +132,18 @@ const url = this.route('importedReceiptMapped.export')+'?date='+arrDateTime[0]+'&time='+arrDateTime[1]+'&fileName='+fileName; if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(url, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: url, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(url, '_blank'); diff --git a/resources/assets/vue/components/accounting/sections/TransactionsMappingComponent.vue b/resources/assets/vue/components/accounting/sections/TransactionsMappingComponent.vue index 65f5b0b1..b94148ad 100644 --- a/resources/assets/vue/components/accounting/sections/TransactionsMappingComponent.vue +++ b/resources/assets/vue/components/accounting/sections/TransactionsMappingComponent.vue @@ -274,15 +274,18 @@ export default { } if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(route, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: route, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(route, '_blank'); @@ -299,15 +302,18 @@ export default { } if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(route, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: url, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(route, '_blank'); diff --git a/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue b/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue index d238b2be..0a0b06bf 100644 --- a/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue +++ b/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue @@ -123,15 +123,18 @@ export default { downloadTransaction(paramItem) { let url = this.route('transaction.credit_note.download', paramItem.id); if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(url, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: url, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(url, '_blank'); diff --git a/resources/assets/vue/components/bookings/elements/UploadDebtorExcelComponent.vue b/resources/assets/vue/components/bookings/elements/UploadDebtorExcelComponent.vue index cc53475b..0e48240e 100644 --- a/resources/assets/vue/components/bookings/elements/UploadDebtorExcelComponent.vue +++ b/resources/assets/vue/components/bookings/elements/UploadDebtorExcelComponent.vue @@ -74,15 +74,18 @@ downloadReport(){ let route = route('newDebtor.export'); if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(route, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: route, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(route, '_blank'); diff --git a/resources/assets/vue/components/bookings/forms/SupplierPlaceOrderFormComponent.vue b/resources/assets/vue/components/bookings/forms/SupplierPlaceOrderFormComponent.vue index 2eed5c90..8fdd1ce4 100644 --- a/resources/assets/vue/components/bookings/forms/SupplierPlaceOrderFormComponent.vue +++ b/resources/assets/vue/components/bookings/forms/SupplierPlaceOrderFormComponent.vue @@ -152,15 +152,18 @@ let url = this.route('whiteForm.mockUp', this.supplier.id)+'?rate='+this.parameters.rate+'&payments='+JSON.stringify(paymentIds); if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(url, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: url, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(url, '_blank'); diff --git a/resources/assets/vue/components/wallets/elements/CustomerTransactionSectionComponent.vue b/resources/assets/vue/components/wallets/elements/CustomerTransactionSectionComponent.vue index 73616dd9..41bd4175 100644 --- a/resources/assets/vue/components/wallets/elements/CustomerTransactionSectionComponent.vue +++ b/resources/assets/vue/components/wallets/elements/CustomerTransactionSectionComponent.vue @@ -126,15 +126,18 @@ export default { download(company, paramShowingPreciseAmount) { let url = this.route('wallet.details-export', company.reference, paramShowingPreciseAmount); if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(url, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: url, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(url, '_blank'); diff --git a/resources/assets/vue/components/wallets/elements/CustomerWalletTransactionComponent.vue b/resources/assets/vue/components/wallets/elements/CustomerWalletTransactionComponent.vue index 40e812bc..f9f8498e 100644 --- a/resources/assets/vue/components/wallets/elements/CustomerWalletTransactionComponent.vue +++ b/resources/assets/vue/components/wallets/elements/CustomerWalletTransactionComponent.vue @@ -32,15 +32,18 @@ downloadTransaction(paramItem) { let url = this.route('transaction.credit_note.download', paramItem.id); if(window.LARAVEL_VAPOR_ENABLED){ - return fetch(url, { - method: 'GET', - responseType: 'json', - }).then(response => response.json()) - .then(response => { - if (response.src) { - window.location.href = response.src; + this.$store.dispatch('crudRequest', {endpoint: url, method: 'get'}) + .then(response => + { + let success = response.ok; + response.json().then(response => { + if(!success){return;} + if (response.src) { + window.location.href = response.src; + } + }); } - }); + ); } else{ window.open(url, '_blank'); diff --git a/resources/assets/vue/vuex/modules/crudRequest.js b/resources/assets/vue/vuex/modules/crudRequest.js index 6f1b6ee0..832d833e 100644 --- a/resources/assets/vue/vuex/modules/crudRequest.js +++ b/resources/assets/vue/vuex/modules/crudRequest.js @@ -6,10 +6,12 @@ export default { return dispatch('ensureReCaptchaIsSet').then(function () { let combinedAbsoluteUrl = endpoint; if(window.LARAVEL_VAPOR_ENABLED){ - const queryDomain = endpoint.split('?')[0]; + let queryDomain = endpoint.split('?')[0]; let encodedParams = endpoint.split('?')[1]; let decodedParams = fullyDecodeURI(encodedParams); const queryParams = encodeURIComponent(decodedParams); + queryDomain = encodeUrlWithoutQueryParameter(queryDomain) + console.log("queryDomain: " + queryDomain); encodedParams = queryParams.toString(); let filteredEncodedParams = encodedParams.replace(/%3D/g,'='); filteredEncodedParams = filteredEncodedParams.replace(/%26/g,'&'); @@ -53,3 +55,16 @@ function fullyDecodeURI(uri){ } return uri; } + +function encodeUrlWithoutQueryParameter(url){ + let regex = /\[\d+\]/; + let match = url.match(regex); + + if (match) { + let encodedPattern = encodeURIComponent(match[0]); + let encodedUrl = url.replace(match[0], encodedPattern); + return encodedUrl; + } + + return url; +} diff --git a/resources/assets/vue/vuex/modules/crudRequestV2.js b/resources/assets/vue/vuex/modules/crudRequestV2.js index 355b1edb..9d91c4ee 100644 --- a/resources/assets/vue/vuex/modules/crudRequestV2.js +++ b/resources/assets/vue/vuex/modules/crudRequestV2.js @@ -2,10 +2,12 @@ export default { actions: { crudRequestV2({getters, dispatch}, {endpoint, method, parameters}){ return dispatch('ensureReCaptchaIsSet').then(function () { - const queryDomain = endpoint.split('?')[0]; + let queryDomain = endpoint.split('?')[0]; let encodedParams = endpoint.split('?')[1]; let decodedParams = fullyDecodeURI(encodedParams); const queryParams = encodeURIComponent(decodedParams); + queryDomain = encodeUrlWithoutQueryParameter(queryDomain) + console.log("queryDomain: " + queryDomain); encodedParams = queryParams.toString(); let filteredEncodedParams = encodedParams.replace(/%3D/g,'='); filteredEncodedParams = filteredEncodedParams.replace(/%26/g,'&'); @@ -49,3 +51,16 @@ function fullyDecodeURI(uri){ } return uri; } + +function encodeUrlWithoutQueryParameter(url){ + let regex = /\[\d+\]/; + let match = url.match(regex); + + if (match) { + let encodedPattern = encodeURIComponent(match[0]); + let encodedUrl = url.replace(match[0], encodedPattern); + return encodedUrl; + } + + return url; +}