Skip to content

Commit

Permalink
review cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal committed Jul 18, 2023
1 parent 2d6bbdb commit 8e1b861
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import { HelpButtonComponent } from "../../../core/common-components/help-button
export class ImportAdditionalActionsComponent implements OnChanges {
@Input() entityType: string;

@Output() importActionsChange = new EventEmitter<AdditionalImportAction[]>();
@Input() importActions: AdditionalImportAction[] = [];
@Output() importActionsChange = new EventEmitter<AdditionalImportAction[]>();

linkableEntityTypes: string[] = [];
typeToString = (val) => this.entityTypeLabelPipe.transform(val);
Expand Down Expand Up @@ -106,10 +106,7 @@ export class ImportAdditionalActionsComponent implements OnChanges {
}

addAction() {
const newAction: AdditionalImportAction = {
type: this.linkEntityForm.get("type").value,
id: this.linkEntityForm.get("id").value,
};
const newAction = this.linkEntityForm.getRawValue();
this.importActions = [...(this.importActions ?? []), newAction];
this.linkEntityForm.reset();
this.importActionsChange.emit(this.importActions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class DateValueMappingComponent {
this.format.setErrors({ parsingError: true });
}
});
// Sort unparsed dates to front
this.values.sort((v1, v2) =>
v1.parsed && !v2.parsed ? 1 : !v1.parsed && v2.parsed ? -1 : 0
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export class ImportColumnMappingComponent implements OnChanges {
@Output() columnMappingChange = new EventEmitter<ColumnMapping[]>();

@Input() set entityType(value: string) {
this.entityCtr = this.entities.get(value);
this.entityCtor = this.entities.get(value);
this.mappingCmp = {};
this.allProps = [...this.entityCtr.schema.entries()]
this.allProps = [...this.entityCtor.schema.entries()]
.filter(([_, schema]) => schema.label)
.map(([name, schema]) => {
const cmp = this.importService.getMappingComponent(schema);
Expand All @@ -57,13 +57,13 @@ export class ImportColumnMappingComponent implements OnChanges {
});
}

private entityCtr: EntityConstructor;
private entityCtor: EntityConstructor;
// entity properties that have a label
allProps: string[] = [];
// properties that need further adjustments through a component
mappingCmp: { [key: string]: ComponentType<any> };

labelMapper = (name: string) => this.entityCtr.schema.get(name).label;
labelMapper = (name: string) => this.entityCtor.schema.get(name).label;
isUsed = (option: string) =>
this.columnMapping.some(({ propertyName }) => propertyName === option);

Expand Down Expand Up @@ -99,7 +99,7 @@ export class ImportColumnMappingComponent implements OnChanges {
data: {
col: col,
values: [...uniqueValues],
entityType: this.entityCtr,
entityType: this.entityCtor,
},
disableClose: true,
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/features/import/import-file/import-file.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export class ImportFileComponent {
data: ParsedData<any>;
@ViewChild(InputFileComponent) inputFileField: InputFileComponent;

onFileLoad($event: ParsedData<any>) {
this.dataLoaded.emit($event);
this.data = $event;
onFileLoad(parsedData: ParsedData<any>) {
this.dataLoaded.emit(parsedData);
this.data = parsedData;
}

public reset() {
Expand Down
19 changes: 17 additions & 2 deletions src/app/features/import/import.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ describe("ImportService", () => {
const rawData: any[] = [
{ x: "John", y: "111" },
{ x: "Jane" },
{ x: "broken date", y: "foo" }, // date column ("y") ignored
{ x: "broken date", y: "foo" }, // date column; ("y") ignored
{ x: "with broken mapping column", brokenMapping: "foo" }, // column mapped to non-existing property ignored
{ x: "", onlyUnmappedColumn: "1" }, // only empty or unmapped columns => row skipped
{ x: "with zero", y: "0" }, // "" value ignored; 0 value mapped
{ x: "with zero", y: "0" }, // 0 value mapped
{ x: "custom mapping fn", z: "30.01.2023" },
];
const entityType: string = "HealthCheck";
Expand Down Expand Up @@ -159,4 +159,19 @@ describe("ImportService", () => {
entityMapper.load(ImportMetadata, importMeta.getId())
).toBeRejected();
});

it("should not fail undo if some entities have already been removed", async () => {
const importMeta = new ImportMetadata();
importMeta.config = { entityType: "Child", columnMapping: undefined };
importMeta.ids = ["Child:1", "Child:2"];
const children = ["1", "2", "3"].map((id) => new Child(id));
const entityMapper = TestBed.inject(EntityMapperService);
await entityMapper.saveAll([...children, importMeta]);

await entityMapper.remove(children[1]);

await service.undoImport(importMeta);

await expectEntitiesToBeInDatabase([children[2]], false, true);
});
});
36 changes: 18 additions & 18 deletions src/app/features/import/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ImportService {
private linkableEntities: {
[key: string]: {
[key: string]: {
create: (e: any[], link: string) => Promise<any>;
create: (entities: Entity[], id: string) => Promise<any>;
undo: (importMeta: ImportMetadata, id: string) => Promise<any>;
};
};
Expand Down Expand Up @@ -120,19 +120,19 @@ export class ImportService {
return importMeta;
}

private linkEntities(entities: any[], settings: ImportSettings) {
private linkEntities(entities: Entity[], settings: ImportSettings) {
return Promise.all(
settings.additionalActions?.map(({ type, id }) =>
this.linkableEntities[settings.entityType][type].create(entities, id)
) ?? []
);
}

private linkToSchool(entities: any[], link: string) {
private linkToSchool(entities: Entity[], id: string) {
const relations = entities.map((entity) => {
const relation = new ChildSchoolRelation();
relation.childId = Entity.extractEntityIdFromId(entity._id);
relation.schoolId = link;
relation.childId = entity.getId();
relation.schoolId = id;
return relation;
});
return this.entityMapper.saveAll(relations);
Expand All @@ -148,9 +148,9 @@ export class ImportService {
return Promise.all(imported.map((rel) => this.entityMapper.remove(rel)));
}

private async linkToActivity(entities: any[], link: string) {
const activity = await this.entityMapper.load(RecurringActivity, link);
const ids = entities.map((e) => Entity.extractEntityIdFromId(e._id));
private async linkToActivity(entities: Entity[], id: string) {
const activity = await this.entityMapper.load(RecurringActivity, id);
const ids = entities.map((e) => e.getId());
activity.participants.push(...ids);
return this.entityMapper.save(activity);
}
Expand All @@ -164,16 +164,16 @@ export class ImportService {
}

undoImport(item: ImportMetadata) {
const removes = item.ids.map(
(id) =>
this.entityMapper
.load(item.config.entityType, id)
.then((e) => this.entityMapper.remove(e))
.catch(() => undefined) // TODO test
);
const undoes = item.config.additionalActions.map(({ type, id }) =>
this.linkableEntities[item.config.entityType][type].undo(item, id)
const removes = item.ids.map((id) =>
this.entityMapper
.load(item.config.entityType, id)
.then((e) => this.entityMapper.remove(e))
.catch(() => undefined)
);
const undoes =
item.config.additionalActions?.map(({ type, id }) =>
this.linkableEntities[item.config.entityType][type].undo(item, id)
) ?? [];

// Or should the ImportMetadata still be kept indicating that it has been undone?
return Promise.all([...removes, ...undoes, this.entityMapper.remove(item)]);
Expand Down Expand Up @@ -206,7 +206,7 @@ export class ImportService {

const parsed = this.parseRow(row[col], mapping, entity);

// ignoring empty strings or un-parseable values for import
// ignoring falsy values except 0 (=> null, undefined, empty string)
if (!!parsed || parsed === 0) {
entity[mapping.propertyName] = parsed;
hasMappedProperty = true;
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/import/import/import.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class ImportComponent {
this.stepper.reset();
}

onDataLoaded(data: ParsedData<any>) {
onDataLoaded(data: ParsedData) {
this.rawData = data.data;

if (this.columnMapping) {
Expand Down

0 comments on commit 8e1b861

Please sign in to comment.