Skip to content

Commit

Permalink
Fix CSV import for staff & campaign fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tkleinke committed Feb 28, 2025
1 parent 06f010a commit cbaceb0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
35 changes: 29 additions & 6 deletions desktop/src/app/components/import/import/process/merge-resource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Associative, cond, detach, dropRightWhile, filter, flow, forEach, includedIn, is, isArray,
isAssociative, isEmpty, isNot, isnt, isObject, Map, update, values, clone } from 'tsfun';
import { Relation, typeOf, NewResource, Resource } from 'idai-field-core';
import { Relation, typeOf, NewResource, Resource, EditableValue } from 'idai-field-core';
import { hasEmptyAssociatives } from '../../util';
import { ImportErrors } from '../import-errors';

Expand Down Expand Up @@ -117,11 +117,11 @@ function assertNoEmptyAssociatives(resource: Resource|NewResource) {
}


function isObjectArray(as: Array<any>|any) {
function isObjectArray(fieldContent: any[]|any): boolean {

if (!isArray(as)) return false;
if (!isArray(fieldContent)) return false;

const arrayType = as
const arrayType = fieldContent
.map(typeOf)
// typeof null -> 'object', typeof undefined -> 'undefined'
.map(cond(is('undefined'), 'object'))
Expand All @@ -132,6 +132,12 @@ function isObjectArray(as: Array<any>|any) {
}


function isEditableValueArray(target: Resource, fieldName: string): boolean {

return target.category === 'Project' && (fieldName === 'staff' || fieldName == 'campaigns');
}


/**
* Iterates over all fields of source, except those specified by exclusions
* and either copies them from source to target
Expand All @@ -149,8 +155,11 @@ function overwriteOrDeleteProperties(target: Map<any>|undefined, source: Map<any
return Object.keys(source)
.filter(isNot(includedIn(exclusions)))
.reduce((target: any, property: string|number) => {
if (source[property] === null) delete target[property];
else if (isObjectArray(source[property])) {
if (source[property] === null) {
delete target[property];
} else if (isEditableValueArray(target, property as string)) {
target[property] = mergeEditableValues(source[property], target[property]);
} else if (isObjectArray(source[property])) {
if (!target[property]) target[property] = [];
target[property] = expandObjectArray(target[property], source[property]);
if (target[property].length === 0) delete target[property];
Expand Down Expand Up @@ -199,3 +208,17 @@ function expandObjectArray(target: Array<any>, source: Array<any>) {
if (result.includes(null)) throw [ImportErrors.EMPTY_SLOTS_IN_ARRAYS_FORBIDDEN];
return result;
}


function mergeEditableValues(source: string[], target: Array<EditableValue>): Array<EditableValue> {

if (!target) target = [];

return source.map(value => {
const existingElement: EditableValue = target.find(element => element.value === value);
return {
value,
selectable: !existingElement || existingElement.selectable
};
});
}
3 changes: 2 additions & 1 deletion desktop/src/app/components/import/parser/convert-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function convertTypeDependent(container: any, fieldName: string, inputType: Inpu
if (inputType === InputType.INT || inputType === InputType.UNSIGNEDINT) convertInt(container, fieldName);
if (inputType === InputType.FLOAT || inputType === InputType.UNSIGNEDFLOAT) convertFloat(container, fieldName);
if (inputType === InputType.COMPOSITE) convertComposite(container, fieldName, field);
if (inputType === InputType.CHECKBOXES || inputType === InputType.SIMPLE_MULTIINPUT) {
if (inputType === InputType.CHECKBOXES || inputType === InputType.SIMPLE_MULTIINPUT
|| inputType === InputType.VALUELIST_MULTIINPUT) {
convertStringArray(container, fieldName)
};
}
Expand Down
38 changes: 38 additions & 0 deletions desktop/test/unit/components/import/import/merge-resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,44 @@ describe('mergeResource', () => {
});


test('merge staff', () => {

target.category = 'Project';
source.category = 'Project';

target['staff'] = [
{ value: 'Person A', selectable: true },
{ value: 'Person B', selectable: false }
];
source['staff'] = ['Person B', 'Person C'];

const result = mergeResource(target, source);
expect(result['staff']).toEqual([
{ value: 'Person B', selectable: false },
{ value: 'Person C', selectable: true }
]);
});


test('merge campaigns', () => {

target.category = 'Project';
source.category = 'Project';

target['campaigns'] = [
{ value: 'Campaign A', selectable: true },
{ value: 'Campaign B', selectable: false }
];
source['campaigns'] = ['Campaign B', 'Campaign C'];

const result = mergeResource(target, source);
expect(result['campaigns']).toEqual([
{ value: 'Campaign B', selectable: false },
{ value: 'Campaign C', selectable: true }
]);
});


test('overwrite, do not merge geometry', () => {

target[GEOMETRY] = { a: 1 };
Expand Down

0 comments on commit cbaceb0

Please sign in to comment.