0
0
Fork 0

refs #5835 refactor basicData

This commit is contained in:
Jorge Penadés 2023-11-22 15:18:18 +01:00
parent 7e7154525d
commit 9388b873c6
2 changed files with 35 additions and 68 deletions

View File

@ -50,14 +50,10 @@ async function checkFileExists(dmsId) {
async function setEditDms(dmsId) { async function setEditDms(dmsId) {
const { data } = await axios.get(`Dms/${dmsId}`); const { data } = await axios.get(`Dms/${dmsId}`);
dms.value = { dms.value = {
id: data.id,
warehouseId: data.warehouseFk, warehouseId: data.warehouseFk,
companyId: data.companyFk, companyId: data.companyFk,
dmsTypeId: data.dmsTypeFk, dmsTypeId: data.dmsTypeFk,
reference: data.reference, ...data,
description: data.description,
hasFile: data.hasFile,
hasFileAttached: data.hasFileAttached,
}; };
if (!allowedContentTypes.value.length) await allowTypesRef.value.fetch(); if (!allowedContentTypes.value.length) await allowTypesRef.value.fetch();
@ -83,47 +79,21 @@ async function setCreateDms() {
createDmsRef.value.show(); createDmsRef.value.show();
} }
async function edit() { async function upsert() {
try { try {
if (!dms.value.companyId) throw Error(t(`The company can't be empty`)); const isEdit = !!dms.value.id;
if (!dms.value.warehouseId) throw Error(t(`The warehouse can't be empty`)); const errors = {
if (!dms.value.dmsTypeId) throw Error(t(`The DMS Type can't be empty`)); companyId: `The company can't be empty`,
if (!dms.value.description) throw Error(t(`The description can't be empty`)); warehouseId: `The warehouse can't be empty`,
dmsTypeId: `The DMS Type can't be empty`,
const formData = new FormData(); description: `The description can't be empty`,
};
if (dms.value.files) {
for (let i = 0; i < dms.value.files.length; i++) Object.keys(errors).forEach((key) => {
formData.append(dms.value.files[i].name, dms.value.files[i]); if (!dms.value[key]) throw Error(t(errors[key]));
dms.value.hasFileAttached = true; });
}
if (!isEdit && !dms.value.files) throw Error(t(`The files can't be empty`));
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(); const formData = new FormData();
@ -133,11 +103,24 @@ async function create() {
dms.value.hasFileAttached = true; dms.value.hasFileAttached = true;
} }
if (!isEdit) {
const { data } = await axios.post('Dms/uploadFile', formData, { const { data } = await axios.post('Dms/uploadFile', formData, {
params: dms.value, 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; if (data.length) invoiceIn.value.dmsFk = data[0].id;
editDmsRef.value.hide(); editDmsRef.value.hide();
}
quasar.notify({ quasar.notify({
message: t('globals.dataSaved'), message: t('globals.dataSaved'),
@ -558,7 +541,7 @@ async function create() {
</QCardSection> </QCardSection>
<QCardActions class="justify-end"> <QCardActions class="justify-end">
<QBtn flat :label="t('globals.close')" color="primary" v-close-popup /> <QBtn flat :label="t('globals.close')" color="primary" v-close-popup />
<QBtn :label="t('globals.save')" color="primary" @click="edit" /> <QBtn :label="t('globals.save')" color="primary" @click="upsert" />
</QCardActions> </QCardActions>
</QCard> </QCard>
</QDialog> </QDialog>
@ -670,7 +653,7 @@ async function create() {
</QCardSection> </QCardSection>
<QCardActions align="right"> <QCardActions align="right">
<QBtn flat :label="t('globals.close')" color="primary" v-close-popup /> <QBtn flat :label="t('globals.close')" color="primary" v-close-popup />
<QBtn :label="t('globals.save')" color="primary" @click="create()" /> <QBtn :label="t('globals.save')" color="primary" @click="upsert" />
</QCardActions> </QCardActions>
</QCard> </QCard>
</QDialog> </QDialog>

View File

@ -16,28 +16,12 @@ describe('InvoiceInBasicData', () => {
}).vm; }).vm;
}); });
describe('edit()', () => { describe('upsert()', () => {
it('should throw an error when data is empty', async () => { it('should throw an error when data is empty', async () => {
vi.spyOn(axios, 'post').mockResolvedValue({ data: [] }); vi.spyOn(axios, 'post').mockResolvedValue({ data: [] });
vi.spyOn(vm.quasar, 'notify'); vi.spyOn(vm.quasar, 'notify');
await vm.edit(); await vm.upsert();
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();
expect(vm.quasar.notify).toHaveBeenCalledWith( expect(vm.quasar.notify).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({