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) {
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,47 +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 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;
}
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 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();
@ -133,11 +103,24 @@ async function create() {
dms.value.hasFileAttached = true;
}
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'),
@ -558,7 +541,7 @@ async function create() {
</QCardSection>
<QCardActions class="justify-end">
<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>
</QCard>
</QDialog>
@ -670,7 +653,7 @@ async function create() {
</QCardSection>
<QCardActions align="right">
<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>
</QCard>
</QDialog>

View File

@ -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({