From 9388b873c6ce55c20c7fc2958e845e5785bebe57 Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 22 Nov 2023 15:18:18 +0100 Subject: [PATCH] refs #5835 refactor basicData --- .../InvoiceIn/Card/InvoiceInBasicData.vue | 83 ++++++++----------- .../InvoiceIn/InvoiceInBasicData.spec.js | 20 +---- 2 files changed, 35 insertions(+), 68 deletions(-) diff --git a/src/pages/InvoiceIn/Card/InvoiceInBasicData.vue b/src/pages/InvoiceIn/Card/InvoiceInBasicData.vue index 0aee913a4..9f794b91a 100644 --- a/src/pages/InvoiceIn/Card/InvoiceInBasicData.vue +++ b/src/pages/InvoiceIn/Card/InvoiceInBasicData.vue @@ -50,14 +50,10 @@ async function checkFileExists(dmsId) { async function setEditDms(dmsId) { const { data } = await axios.get(`Dms/${dmsId}`); dms.value = { - id: data.id, warehouseId: data.warehouseFk, companyId: data.companyFk, dmsTypeId: data.dmsTypeFk, - reference: data.reference, - description: data.description, - hasFile: data.hasFile, - hasFileAttached: data.hasFileAttached, + ...data, }; if (!allowedContentTypes.value.length) await allowTypesRef.value.fetch(); @@ -83,12 +79,21 @@ async function setCreateDms() { createDmsRef.value.show(); } -async function edit() { +async function upsert() { try { - if (!dms.value.companyId) throw Error(t(`The company can't be empty`)); - if (!dms.value.warehouseId) throw Error(t(`The warehouse can't be empty`)); - if (!dms.value.dmsTypeId) throw Error(t(`The DMS Type can't be empty`)); - if (!dms.value.description) throw Error(t(`The description can't be empty`)); + const isEdit = !!dms.value.id; + const errors = { + companyId: `The company can't be empty`, + warehouseId: `The warehouse can't be empty`, + dmsTypeId: `The DMS Type can't be empty`, + description: `The description can't be empty`, + }; + + Object.keys(errors).forEach((key) => { + if (!dms.value[key]) throw Error(t(errors[key])); + }); + + if (!isEdit && !dms.value.files) throw Error(t(`The files can't be empty`)); const formData = new FormData(); @@ -98,47 +103,25 @@ async function edit() { dms.value.hasFileAttached = true; } - const { data } = await axios.post(`dms/${dms.value.id}/updateFile`, formData, { - params: dms.value, - }); + if (!isEdit) { + const { data } = await axios.post('Dms/uploadFile', formData, { + params: dms.value, + }); + if (data.length) invoiceIn.value.dmsFk = data[0].id; + createDmsRef.value.hide(); + } else if (isEdit) { + const { data } = await axios.post( + `dms/${dms.value.id}/updateFile`, + formData, + { + params: dms.value, + } + ); - if (data.length) invoiceIn.value.dmsFk = data[0].id; - editDmsRef.value.hide(); - - quasar.notify({ - message: t('globals.dataSaved'), - type: 'positive', - }); - } catch (error) { - quasar.notify({ - message: t(`${error.message}`), - type: 'negative', - }); - } -} - -async function create() { - try { - if (!dms.value.companyId) throw Error(t(`The company can't be empty`)); - if (!dms.value.warehouseId) throw Error(t(`The warehouse can't be empty`)); - if (!dms.value.dmsTypeId) throw Error(t(`The DMS Type can't be empty`)); - if (!dms.value.description) throw Error(t(`The description can't be empty`)); - if (!dms.value.files) throw Error(t(`The files can't be empty`)); - - const formData = new FormData(); - - if (dms.value.files) { - for (let i = 0; i < dms.value.files.length; i++) - formData.append(dms.value.files[i].name, dms.value.files[i]); - dms.value.hasFileAttached = true; + if (data.length) invoiceIn.value.dmsFk = data[0].id; + editDmsRef.value.hide(); } - const { data } = await axios.post('Dms/uploadFile', formData, { - params: dms.value, - }); - if (data.length) invoiceIn.value.dmsFk = data[0].id; - editDmsRef.value.hide(); - quasar.notify({ message: t('globals.dataSaved'), type: 'positive', @@ -558,7 +541,7 @@ async function create() { - + @@ -670,7 +653,7 @@ async function create() { - + diff --git a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInBasicData.spec.js b/test/vitest/__tests__/pages/InvoiceIn/InvoiceInBasicData.spec.js index 3e6d8f892..a3c383f74 100644 --- a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInBasicData.spec.js +++ b/test/vitest/__tests__/pages/InvoiceIn/InvoiceInBasicData.spec.js @@ -16,28 +16,12 @@ describe('InvoiceInBasicData', () => { }).vm; }); - describe('edit()', () => { + describe('upsert()', () => { it('should throw an error when data is empty', async () => { vi.spyOn(axios, 'post').mockResolvedValue({ data: [] }); vi.spyOn(vm.quasar, 'notify'); - await vm.edit(); - - expect(vm.quasar.notify).toHaveBeenCalledWith( - expect.objectContaining({ - message: `The company can't be empty`, - type: 'negative', - }) - ); - }); - }); - - describe('create()', () => { - it('should throw an error when data is empty', async () => { - vi.spyOn(axios, 'post').mockResolvedValue({ data: [] }); - vi.spyOn(vm.quasar, 'notify'); - - await vm.create(); + await vm.upsert(); expect(vm.quasar.notify).toHaveBeenCalledWith( expect.objectContaining({