mirror of
https://github.com/frappe/books.git
synced 2024-12-23 11:29:03 +00:00
incr: update validations
- display errors if present
This commit is contained in:
parent
8e8e88d49f
commit
3e5f0b6f83
@ -168,6 +168,36 @@ export class Importer {
|
|||||||
return doesNotExist;
|
return doesNotExist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkCellErrors() {
|
||||||
|
const assigned = this.assignedTemplateFields
|
||||||
|
.map((key, index) => ({
|
||||||
|
key,
|
||||||
|
index,
|
||||||
|
tf: this.templateFieldsMap.get(key ?? ''),
|
||||||
|
}))
|
||||||
|
.filter(({ key, tf }) => !!key && !!tf) as {
|
||||||
|
key: string;
|
||||||
|
index: number;
|
||||||
|
tf: TemplateField;
|
||||||
|
}[];
|
||||||
|
|
||||||
|
const cellErrors = [];
|
||||||
|
for (const i in this.valueMatrix) {
|
||||||
|
const row = this.valueMatrix[i];
|
||||||
|
for (const { tf, index } of assigned) {
|
||||||
|
if (!row[index]?.error) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rowLabel = this.fyo.t`Row ${i + 1}`;
|
||||||
|
const columnLabel = getColumnLabel(tf);
|
||||||
|
cellErrors.push(`(${rowLabel}, ${columnLabel})`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cellErrors;
|
||||||
|
}
|
||||||
|
|
||||||
populateDocs() {
|
populateDocs() {
|
||||||
const { dataMap, childTableMap } =
|
const { dataMap, childTableMap } =
|
||||||
this.getDataAndChildTableMapFromValueMatrix();
|
this.getDataAndChildTableMapFromValueMatrix();
|
||||||
@ -536,3 +566,11 @@ function getTemplateFields(
|
|||||||
|
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getColumnLabel(field: TemplateField): string {
|
||||||
|
if (field.parentSchemaChildField) {
|
||||||
|
return `${field.label} (${field.parentSchemaChildField.label})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return field.label;
|
||||||
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
type="primary"
|
type="primary"
|
||||||
class="text-sm"
|
class="text-sm"
|
||||||
@click="importData"
|
@click="importData"
|
||||||
|
:disabled="errorMessage.length > 0"
|
||||||
>
|
>
|
||||||
{{ t`Import Data` }}
|
{{ t`Import Data` }}
|
||||||
</Button>
|
</Button>
|
||||||
@ -60,14 +61,18 @@
|
|||||||
@change="setImportType"
|
@change="setImportType"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<p v-if="errorMessage.length > 0" class="text-base ms-2 text-red-500">
|
||||||
|
{{ errorMessage }}
|
||||||
|
</p>
|
||||||
<p
|
<p
|
||||||
|
v-else
|
||||||
class="text-base ms-2"
|
class="text-base ms-2"
|
||||||
:class="fileName ? 'text-gray-900 font-semibold' : 'text-gray-700'"
|
:class="fileName ? 'text-gray-900 font-semibold' : 'text-gray-700'"
|
||||||
>
|
>
|
||||||
<span v-if="fileName" class="font-normal"
|
<span v-if="fileName" class="font-normal"
|
||||||
>{{ t`Selected file` }}
|
>{{ t`Selected file` }}
|
||||||
</span>
|
</span>
|
||||||
{{ helperText }}{{ fileName ? ',' : '' }}
|
{{ helperMessage }}{{ fileName ? ',' : '' }}
|
||||||
<span v-if="fileName" class="font-normal">
|
<span v-if="fileName" class="font-normal">
|
||||||
{{ t`verify the imported data and click on` }} </span
|
{{ t`verify the imported data and click on` }} </span
|
||||||
>{{ ' ' }}<span v-if="fileName">{{ t`Import Data` }}</span>
|
>{{ ' ' }}<span v-if="fileName">{{ t`Import Data` }}</span>
|
||||||
@ -158,6 +163,7 @@
|
|||||||
<!-- FormControl Field if Column is Assigned -->
|
<!-- FormControl Field if Column is Assigned -->
|
||||||
<FormControl
|
<FormControl
|
||||||
v-else
|
v-else
|
||||||
|
:class="val.error ? 'border border-red-300 rounded-md' : ''"
|
||||||
:title="getFieldTitle(val)"
|
:title="getFieldTitle(val)"
|
||||||
:df="
|
:df="
|
||||||
importer.templateFieldsMap.get(
|
importer.templateFieldsMap.get(
|
||||||
@ -343,7 +349,7 @@ import FormHeader from 'src/components/FormHeader.vue';
|
|||||||
import HowTo from 'src/components/HowTo.vue';
|
import HowTo from 'src/components/HowTo.vue';
|
||||||
import Modal from 'src/components/Modal.vue';
|
import Modal from 'src/components/Modal.vue';
|
||||||
import PageHeader from 'src/components/PageHeader.vue';
|
import PageHeader from 'src/components/PageHeader.vue';
|
||||||
import { Importer, TemplateField } from 'src/importer';
|
import { getColumnLabel, Importer, TemplateField } from 'src/importer';
|
||||||
import { fyo } from 'src/initFyo';
|
import { fyo } from 'src/initFyo';
|
||||||
import { getSavePath, saveData, selectFile } from 'src/utils/ipcCalls';
|
import { getSavePath, saveData, selectFile } from 'src/utils/ipcCalls';
|
||||||
import { docsPathMap } from 'src/utils/misc';
|
import { docsPathMap } from 'src/utils/misc';
|
||||||
@ -407,6 +413,53 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
duplicates(): string[] {
|
||||||
|
if (!this.hasImporter) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const dupes = new Set<string>();
|
||||||
|
const assignedSet = new Set<string>();
|
||||||
|
|
||||||
|
for (const key of this.importer.assignedTemplateFields) {
|
||||||
|
if (!key) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tf = this.importer.templateFieldsMap.get(key);
|
||||||
|
if (assignedSet.has(key) && tf) {
|
||||||
|
dupes.add(getColumnLabel(tf));
|
||||||
|
}
|
||||||
|
|
||||||
|
assignedSet.add(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(dupes);
|
||||||
|
},
|
||||||
|
requiredNotSelected(): string[] {
|
||||||
|
if (!this.hasImporter) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const assigned = new Set(this.importer.assignedTemplateFields);
|
||||||
|
return [...this.importer.templateFieldsMap.values()]
|
||||||
|
.filter((f) => f.required && !assigned.has(f.fieldKey))
|
||||||
|
.map((f) => getColumnLabel(f));
|
||||||
|
},
|
||||||
|
errorMessage(): string {
|
||||||
|
if (this.duplicates.length) {
|
||||||
|
return this.t`Duplicate columns found: ${this.duplicates.join(', ')}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.requiredNotSelected.length) {
|
||||||
|
return this
|
||||||
|
.t`Required fields not selected: ${this.requiredNotSelected.join(
|
||||||
|
', '
|
||||||
|
)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
},
|
||||||
canImportData(): boolean {
|
canImportData(): boolean {
|
||||||
if (!this.hasImporter) {
|
if (!this.hasImporter) {
|
||||||
return false;
|
return false;
|
||||||
@ -459,9 +512,6 @@ export default defineComponent({
|
|||||||
|
|
||||||
return map;
|
return map;
|
||||||
},
|
},
|
||||||
requiredColumnsNotPicked(): string[] {
|
|
||||||
return [];
|
|
||||||
},
|
|
||||||
importer(): Importer {
|
importer(): Importer {
|
||||||
if (!this.nullOrImporter) {
|
if (!this.nullOrImporter) {
|
||||||
throw new ValidationError(this.t`Importer not set, reload tool`, false);
|
throw new ValidationError(this.t`Importer not set, reload tool`, false);
|
||||||
@ -532,7 +582,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
return this.file.name;
|
return this.file.name;
|
||||||
},
|
},
|
||||||
helperText(): string {
|
helperMessage(): string {
|
||||||
if (!this.importType) {
|
if (!this.importType) {
|
||||||
return this.t`Set an Import Type`;
|
return this.t`Set an Import Type`;
|
||||||
} else if (!this.fileName) {
|
} else if (!this.fileName) {
|
||||||
@ -553,10 +603,7 @@ export default defineComponent({
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let label = field.label;
|
const label = getColumnLabel(field);
|
||||||
if (field.parentSchemaChildField) {
|
|
||||||
label = `${label} (${field.parentSchemaChildField.label})`;
|
|
||||||
}
|
|
||||||
|
|
||||||
options.push({ value, label });
|
options.push({ value, label });
|
||||||
}
|
}
|
||||||
@ -585,12 +632,11 @@ export default defineComponent({
|
|||||||
this.clear();
|
this.clear();
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
log: console.log,
|
|
||||||
getFieldTitle(vmi: {
|
getFieldTitle(vmi: {
|
||||||
value?: DocValue;
|
value?: DocValue;
|
||||||
rawValue?: RawValue;
|
rawValue?: RawValue;
|
||||||
error?: boolean;
|
error?: boolean;
|
||||||
}) {
|
}): string {
|
||||||
const title: string[] = [];
|
const title: string[] = [];
|
||||||
if (vmi.value != null) {
|
if (vmi.value != null) {
|
||||||
title.push(this.t`Value: ${String(vmi.value)}`);
|
title.push(this.t`Value: ${String(vmi.value)}`);
|
||||||
@ -605,8 +651,9 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!title.length) {
|
if (!title.length) {
|
||||||
return '';
|
return this.t`No Value`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return title.join(', ');
|
return title.join(', ');
|
||||||
},
|
},
|
||||||
pickColumn(fieldKey: string, value: boolean): void {
|
pickColumn(fieldKey: string, value: boolean): void {
|
||||||
@ -624,7 +671,7 @@ export default defineComponent({
|
|||||||
this.reassignTemplateFields();
|
this.reassignTemplateFields();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
reassignTemplateFields() {
|
reassignTemplateFields(): void {
|
||||||
if (this.importer.valueMatrix.length) {
|
if (this.importer.valueMatrix.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -671,8 +718,41 @@ export default defineComponent({
|
|||||||
|
|
||||||
await saveData(template, filePath);
|
await saveData(template, filePath);
|
||||||
},
|
},
|
||||||
|
async preImportValidations(): Promise<boolean> {
|
||||||
|
const message = this.t`Cannot Import`;
|
||||||
|
if (this.errorMessage.length) {
|
||||||
|
await showMessageDialog({
|
||||||
|
message,
|
||||||
|
detail: this.errorMessage,
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cellErrors = this.importer.checkCellErrors();
|
||||||
|
if (cellErrors.length) {
|
||||||
|
await showMessageDialog({
|
||||||
|
message,
|
||||||
|
detail: this.t`Following cells have errors: ${cellErrors.join(', ')}`,
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const absentLinks = await this.importer.checkLinks();
|
||||||
|
if (absentLinks.length) {
|
||||||
|
await showMessageDialog({
|
||||||
|
message,
|
||||||
|
detail: this.t`Following links do not exist: ${absentLinks
|
||||||
|
.map((l) => `(${l.schemaLabel}, ${l.name})`)
|
||||||
|
.join(', ')}`,
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
async importData(): Promise<void> {
|
async importData(): Promise<void> {
|
||||||
if (this.isMakingEntries || this.complete) {
|
const isValid = await this.preImportValidations();
|
||||||
|
if (!isValid || this.isMakingEntries || this.complete) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user