forked from verdnatura/salix-front
refs #5835 refactor basicData
This commit is contained in:
parent
7e7154525d
commit
9388b873c6
|
@ -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,12 +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`,
|
||||||
|
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();
|
const formData = new FormData();
|
||||||
|
|
||||||
|
@ -98,47 +103,25 @@ async function edit() {
|
||||||
dms.value.hasFileAttached = true;
|
dms.value.hasFileAttached = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data } = await axios.post(`dms/${dms.value.id}/updateFile`, formData, {
|
if (!isEdit) {
|
||||||
params: dms.value,
|
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;
|
if (data.length) invoiceIn.value.dmsFk = data[0].id;
|
||||||
editDmsRef.value.hide();
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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({
|
quasar.notify({
|
||||||
message: t('globals.dataSaved'),
|
message: t('globals.dataSaved'),
|
||||||
type: 'positive',
|
type: 'positive',
|
||||||
|
@ -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>
|
||||||
|
|
|
@ -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({
|
||||||
|
|
Loading…
Reference in New Issue