diff --git a/src/stores/quotations/index.ts b/src/stores/quotations/index.ts index f17dc4de..e4718017 100644 --- a/src/stores/quotations/index.ts +++ b/src/stores/quotations/index.ts @@ -9,11 +9,15 @@ import { QuotationPayload, QuotationPaymentData, QuotationStats, + PaymentPayload, } from './types'; import { PaginationResult } from 'src/types'; -import { AxiosInstance } from 'axios'; +import { AxiosInstance, AxiosProgressEvent } from 'axios'; + +import { baseUrl, manageAttachment } from '../utils'; export const useQuotationStore = defineStore('quotation-store', () => { + const base = ref(''); const data = ref([]); const page = ref(1); const pageMax = ref(1); @@ -129,61 +133,6 @@ export const useQuotationStore = defineStore('quotation-store', () => { }; }); -function managePaymentFile(api: AxiosInstance) { - return { - listFile: async (opts: { quotationId: string; paymentId: string }) => { - const res = await api.get(`/${base}/${opts.quotationId}/file`); - if (res.status >= 400) return false; - return res.data; - }, - getFile: async (opts: { - group: T; - parentId: string; - fileId: string; - download?: boolean; - }) => { - const url = `/${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`; - const res = await api.get(url); - if (opts.download) { - fetch(res.data) - .then(async (res) => await res.blob()) - .then((blob) => { - const a = document.createElement('a'); - a.download = opts.fileId; - a.href = window.URL.createObjectURL(blob); - a.click(); - a.remove(); - }); - } - return res.data; - }, - putFile: async (opts: { - group: T; - parentId: string; - fileId: string; - file: File; - }) => { - const res = await api.put( - `/${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`, - opts.file, - { - headers: { 'Content-Type': opts.file.type }, - onUploadProgress: (e) => console.log(e), - }, - ); - if (res.status < 400) return true; - return false; - }, - delFile: async (opts: { group: T; parentId: string; fileId: string }) => { - const res = await api.delete( - `/${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`, - ); - if (res.status < 400) return true; - return false; - }, - }; -} - export const useQuotationPayment = defineStore('quotation-payment', () => { async function getQuotationPayment(quotationId: string) { const res = await api.get< @@ -195,7 +144,32 @@ export const useQuotationPayment = defineStore('quotation-payment', () => { return null; } + async function updateQuotationPayment( + quotationId: string, + paymentId: string, + payload: PaymentPayload, + ) { + const res = await api.put( + `/quotation/${quotationId}/payment/${paymentId}`, + payload, + ); + if (res.status < 400) { + return res.data; + } + return null; + } + + function createPaymentFileManager( + api: AxiosInstance, + quotationId: string, + opts?: { onUploadProgress: (e: AxiosProgressEvent) => void }, + ) { + return manageAttachment(api, `/quotation/${quotationId}/payment`, opts); + } + return { getQuotationPayment, + createPaymentFileManager, + updateQuotationPayment, }; }); diff --git a/src/stores/utils/index.ts b/src/stores/utils/index.ts index 25dbb240..da415c64 100644 --- a/src/stores/utils/index.ts +++ b/src/stores/utils/index.ts @@ -5,7 +5,7 @@ import { ComposerTranslation } from 'vue-i18n'; import { defineStore } from 'pinia'; import { Ref, ref } from 'vue'; import { getRole } from 'src/services/keycloak'; -import { AxiosInstance } from 'axios'; +import { AxiosInstance, AxiosProgressEvent } from 'axios'; export const baseUrl = import.meta.env.VITE_API_BASE_URL; @@ -251,7 +251,11 @@ export function resetScrollBar(elementId: string) { } } -export function manageAttachment(api: AxiosInstance, base: string) { +export function manageAttachment( + api: AxiosInstance, + base: string, + option?: { onUploadProgress?: (e: AxiosProgressEvent) => void }, +) { return { listAttachment: async (opts: { parentId: string }) => { const res = await api.get( @@ -290,7 +294,9 @@ export function manageAttachment(api: AxiosInstance, base: string) { opts.file, { headers: { 'Content-Type': opts.file.type }, - onUploadProgress: (e) => console.log(e), + onUploadProgress: option?.onUploadProgress + ? option.onUploadProgress + : (e) => console.log(e), }, ); if (res.status < 400) return true; @@ -306,7 +312,11 @@ export function manageAttachment(api: AxiosInstance, base: string) { }; } -export function manageFile(api: AxiosInstance, base: string) { +export function manageFile( + api: AxiosInstance, + base: string, + option?: { onUploadProgress?: (e: AxiosProgressEvent) => void }, +) { return { listFile: async (opts: { group: T; parentId: string }) => { const res = await api.get( @@ -347,7 +357,9 @@ export function manageFile(api: AxiosInstance, base: string) { opts.file, { headers: { 'Content-Type': opts.file.type }, - onUploadProgress: (e) => console.log(e), + onUploadProgress: option?.onUploadProgress + ? option.onUploadProgress + : (e) => console.log(e), }, ); if (res.status < 400) return true;