From 48797dff621b239ab161cad8f43d124ec6e7e60d Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 15 Feb 2023 19:19:17 +0530 Subject: [PATCH] fix: support option field value or label - remove address placeholder - allow more types of entries to be imported --- schemas/app/Party.json | 1 - schemas/app/PrintSettings.json | 1 - schemas/app/inventory/Location.json | 1 - schemas/tests/Party.json | 1 - src/importer.ts | 60 ++++++++++++++++++++++++++++- src/pages/ImportWizard.vue | 26 ++++++------- utils/index.ts | 4 +- 7 files changed, 74 insertions(+), 20 deletions(-) diff --git a/schemas/app/Party.json b/schemas/app/Party.json index 96ceff80..820efd1d 100644 --- a/schemas/app/Party.json +++ b/schemas/app/Party.json @@ -73,7 +73,6 @@ "label": "Address", "fieldtype": "Link", "target": "Address", - "placeholder": "Click to create", "inline": true }, { diff --git a/schemas/app/PrintSettings.json b/schemas/app/PrintSettings.json index ed88843e..1499093e 100644 --- a/schemas/app/PrintSettings.json +++ b/schemas/app/PrintSettings.json @@ -40,7 +40,6 @@ "label": "Address", "fieldtype": "Link", "target": "Address", - "placeholder": "Click to create", "inline": true }, { diff --git a/schemas/app/inventory/Location.json b/schemas/app/inventory/Location.json index 6926aaee..45161032 100644 --- a/schemas/app/inventory/Location.json +++ b/schemas/app/inventory/Location.json @@ -16,7 +16,6 @@ "label": "Address", "fieldtype": "Link", "target": "Address", - "placeholder": "Click to create", "inline": true } ], diff --git a/schemas/tests/Party.json b/schemas/tests/Party.json index 121a7065..b833ec4e 100644 --- a/schemas/tests/Party.json +++ b/schemas/tests/Party.json @@ -73,7 +73,6 @@ "label": "Address", "fieldtype": "Link", "target": "Address", - "placeholder": "Click to create", "inline": true } ], diff --git a/src/importer.ts b/src/importer.ts index 08f551e0..38de3fcc 100644 --- a/src/importer.ts +++ b/src/importer.ts @@ -8,13 +8,17 @@ import { Field, FieldType, FieldTypeEnum, + OptionField, RawValue, Schema, TargetField, } from 'schemas/types'; import { generateCSV, parseCSV } from 'utils/csvParser'; +import { getValueMapFromList } from 'utils/index'; -export type TemplateField = Field & { +export type TemplateField = Field & TemplateFieldProps; + +type TemplateFieldProps = { schemaName: string; schemaLabel: string; fieldKey: string; @@ -82,6 +86,15 @@ export class Importer { */ docs: Doc[]; + /** + * Used if an options field is imported where the import data + * provided maybe the label and not the value + */ + optionsMap: { + values: Record>; + labelValueMap: Record>; + }; + constructor(schemaName: string, fyo: Fyo) { if (!fyo.schemaMap[schemaName]) { throw new ValidationError( @@ -94,6 +107,10 @@ export class Importer { this.fyo = fyo; this.docs = []; this.valueMatrix = []; + this.optionsMap = { + values: {}, + labelValueMap: {}, + }; const templateFields = getTemplateFields(schemaName, fyo, this); this.assignedTemplateFields = templateFields.map((f) => f.fieldKey); @@ -423,6 +440,10 @@ export class Importer { return vmi; } + if ('options' in tf && typeof vmi.rawValue === 'string') { + return this.getOptionFieldVmi(vmi, tf); + } + try { vmi.value = Converter.toDocValue(rawValue, tf, this.fyo); } catch { @@ -432,6 +453,39 @@ export class Importer { return vmi; } + getOptionFieldVmi( + { rawValue }: ValueMatrixItem, + tf: OptionField & TemplateFieldProps + ): ValueMatrixItem { + if (typeof rawValue !== 'string') { + return { error: true, value: null, rawValue }; + } + + if (!tf?.options.length) { + return { value: null, rawValue }; + } + + if (!this.optionsMap.labelValueMap[tf.fieldKey]) { + const values = new Set(tf.options.map(({ value }) => value)); + const labelValueMap = getValueMapFromList(tf.options, 'label', 'value'); + + this.optionsMap.labelValueMap[tf.fieldKey] = labelValueMap; + this.optionsMap.values[tf.fieldKey] = values; + } + + const hasValue = this.optionsMap.values[tf.fieldKey].has(rawValue); + if (hasValue) { + return { value: rawValue, rawValue }; + } + + const value = this.optionsMap.labelValueMap[tf.fieldKey][rawValue]; + if (value) { + return { value, rawValue }; + } + + return { error: true, value: null, rawValue }; + } + assignTemplateFieldsFromParsedRow(row: string[]): boolean { const isKeyRow = row.some((key) => this.templateFieldsMap.has(key)); if (!isKeyRow) { @@ -550,6 +604,10 @@ function getTemplateFields( tf.readOnly = false; } + if (schema.isChild && tf.fieldname === 'name') { + tf.required = false; + } + const schemaName = schema.name; const schemaLabel = schema.label; const fieldKey = `${schema.name}.${field.fieldname}`; diff --git a/src/pages/ImportWizard.vue b/src/pages/ImportWizard.vue index 1b2e583c..084a4545 100644 --- a/src/pages/ImportWizard.vue +++ b/src/pages/ImportWizard.vue @@ -4,12 +4,12 @@