refs #5835 migrateInvoiceIn #110

Merged
jorgep merged 29 commits from 5835-migrateInvoiceIn into dev 2023-12-13 10:25:07 +00:00
2 changed files with 35 additions and 68 deletions
Showing only changes of commit 9388b873c6 - Show all commits

View File

@ -50,14 +50,10 @@ async function checkFileExists(dmsId) {
async function setEditDms(dmsId) {
const { data } = await axios.get(`Dms/${dmsId}`);
dms.value = {
jorgep marked this conversation as resolved
Review

lo miramos

lo miramos
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`,
jorgep marked this conversation as resolved Outdated

Incluso te diría que en vez de tantos ifs, haría un objeto de validaciones, donde la key es el campo y el value es el mensaje a mostrar.

const validations = {
  companyId:'The company can\'t be empty'
}

Object.entries(validations).forEach(([key, value])=>{
  if(!dms.value[key])
    throw Error(t(value))
});

Para la que aplica en edit, usuaria la bandera que hemos comentado

Incluso te diría que en vez de tantos ifs, haría un objeto de validaciones, donde la key es el campo y el value es el mensaje a mostrar. ``` const validations = { companyId:'The company can\'t be empty' } Object.entries(validations).forEach(([key, value])=>{ if(!dms.value[key]) throw Error(t(value)) }); ``` Para la que aplica en edit, usuaria la bandera que hemos comentado
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,
}
);
jorgep marked this conversation as resolved Outdated

Yo movería la lógica de create y save a funciones constantes porque estás duplicando código que hacen lo mismo

Yo movería la lógica de create y save a funciones constantes porque estás duplicando código que hacen lo mismo
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() {
</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({