From f0ccf5565bc4847eac893e4417e352de16d4ba22 Mon Sep 17 00:00:00 2001 From: "Schild, Paul" Date: Thu, 25 Nov 2021 15:00:40 +0100 Subject: [PATCH 1/4] fixes #753 'Search broken for imported data'. --- src/app/features/data-import/data-import.service.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/app/features/data-import/data-import.service.ts b/src/app/features/data-import/data-import.service.ts index 971f1c5b56..492c89b8bb 100644 --- a/src/app/features/data-import/data-import.service.ts +++ b/src/app/features/data-import/data-import.service.ts @@ -6,6 +6,8 @@ import { BackupService } from "../../core/admin/services/backup.service"; import { ConfirmationDialogService } from "../../core/confirmation-dialog/confirmation-dialog.service"; import { MatSnackBar } from "@angular/material/snack-bar"; import { readFile } from "../../utils/utils"; +import { Entity } from "../../core/entity/model/entity"; +import { EntityMapperService } from "../../core/entity/entity-mapper.service"; @Injectable() @UntilDestroy() @@ -32,7 +34,9 @@ export class DataImportService { delete record[propertyName]; } } - + if (record["name"] !== undefined) { + record["searchIndices"] = record["name"].split(" "); + } await this.db.put(record, true); } } From 58a4caf5680b08ee1f8a90437b2f0109a293cdbf Mon Sep 17 00:00:00 2001 From: "Schild, Paul" Date: Mon, 6 Dec 2021 21:55:31 +0100 Subject: [PATCH 2/4] use dynamicEntityService for setting the searchIndices --- .../data-import/data-import.service.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/app/features/data-import/data-import.service.ts b/src/app/features/data-import/data-import.service.ts index 492c89b8bb..9642b03d9a 100644 --- a/src/app/features/data-import/data-import.service.ts +++ b/src/app/features/data-import/data-import.service.ts @@ -6,8 +6,7 @@ import { BackupService } from "../../core/admin/services/backup.service"; import { ConfirmationDialogService } from "../../core/confirmation-dialog/confirmation-dialog.service"; import { MatSnackBar } from "@angular/material/snack-bar"; import { readFile } from "../../utils/utils"; -import { Entity } from "../../core/entity/model/entity"; -import { EntityMapperService } from "../../core/entity/entity-mapper.service"; +import { DynamicEntityService } from "../../core/entity/dynamic-entity.service"; @Injectable() @UntilDestroy() @@ -17,8 +16,10 @@ export class DataImportService { private papa: Papa, private backupService: BackupService, private confirmationDialog: ConfirmationDialogService, - private snackBar: MatSnackBar - ) {} + private snackBar: MatSnackBar, + private dynamicEntityService: DynamicEntityService + ) { + } async importCsvContentToDB(csv: string): Promise { const parsedCsv = this.papa.parse(csv, { @@ -34,8 +35,14 @@ export class DataImportService { delete record[propertyName]; } } - if (record["name"] !== undefined) { - record["searchIndices"] = record["name"].split(" "); + let ctor = undefined; + if (record["_id"] !== undefined) { + const entityType = record["_id"].split(":")[0]; + ctor = this.dynamicEntityService.getEntityConstructor(entityType); + record["searchIndices"] = Object.assign( + new ctor(), + record + ).searchIndices; } await this.db.put(record, true); } From 262817e75337a62c98954843c2d9164a49447ab5 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 7 Dec 2021 10:17:30 +0100 Subject: [PATCH 3/4] small cleanup --- src/app/features/data-import/data-import.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/features/data-import/data-import.service.ts b/src/app/features/data-import/data-import.service.ts index 9642b03d9a..563dbd95ea 100644 --- a/src/app/features/data-import/data-import.service.ts +++ b/src/app/features/data-import/data-import.service.ts @@ -35,10 +35,10 @@ export class DataImportService { delete record[propertyName]; } } - let ctor = undefined; + if (record["_id"] !== undefined) { const entityType = record["_id"].split(":")[0]; - ctor = this.dynamicEntityService.getEntityConstructor(entityType); + const ctor = this.dynamicEntityService.getEntityConstructor(entityType); record["searchIndices"] = Object.assign( new ctor(), record From 5495e5f0dc576fc03b384a3687c7ecb7771d65b9 Mon Sep 17 00:00:00 2001 From: Sebastian Leidig Date: Fri, 17 Dec 2021 17:52:05 +0100 Subject: [PATCH 4/4] add unit test and fix code style --- .../data-import/data-import.service.spec.ts | 20 ++++++++++++++++--- .../data-import/data-import.service.ts | 3 +-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/app/features/data-import/data-import.service.spec.ts b/src/app/features/data-import/data-import.service.spec.ts index 7ec4769026..4c5e14b7ac 100644 --- a/src/app/features/data-import/data-import.service.spec.ts +++ b/src/app/features/data-import/data-import.service.spec.ts @@ -7,6 +7,8 @@ import { ConfirmationDialogService } from "../../core/confirmation-dialog/confir import { MatSnackBar, MatSnackBarRef } from "@angular/material/snack-bar"; import { MatDialogRef } from "@angular/material/dialog"; import { of } from "rxjs"; +import { EntityMapperService } from "../../core/entity/entity-mapper.service"; +import { EntitySchemaService } from "../../core/entity/schema/entity-schema.service"; describe("DataImportService", () => { let db: PouchDatabase; @@ -85,10 +87,11 @@ describe("DataImportService", () => { provide: MatSnackBar, useValue: mockSnackBar, }, + EntityMapperService, + EntitySchemaService, ], }); service = TestBed.inject(DataImportService); - spyOn(service, "importCsvContentToDB"); spyOn(db, "put"); }); @@ -105,6 +108,7 @@ describe("DataImportService", () => { mockBackupService.getJsonExport.and.resolveTo(null); createDialogMock(true); createSnackBarMock(false); + spyOn(service, "importCsvContentToDB"); service.handleCsvImport(null); @@ -120,6 +124,7 @@ describe("DataImportService", () => { const mockFileReader = createFileReaderMock(); mockBackupService.getJsonExport.and.resolveTo(null); createDialogMock(false); + spyOn(service, "importCsvContentToDB"); service.handleCsvImport(null); @@ -149,7 +154,16 @@ describe("DataImportService", () => { flush(); })); - it("should put csv into db", async () => { - // Todo, missing importCsv Function + it("should import csv file and generate searchIndices", async () => { + const csvString = "_id,name,projectNumber\n" + 'Child:1,"John Doe",123'; + + await service.importCsvContentToDB(csvString); + + expect(db.put).toHaveBeenCalledWith( + jasmine.objectContaining({ + searchIndices: ["John", "Doe", 123], + }), + jasmine.anything() + ); }); }); diff --git a/src/app/features/data-import/data-import.service.ts b/src/app/features/data-import/data-import.service.ts index 563dbd95ea..63a6bb895f 100644 --- a/src/app/features/data-import/data-import.service.ts +++ b/src/app/features/data-import/data-import.service.ts @@ -18,8 +18,7 @@ export class DataImportService { private confirmationDialog: ConfirmationDialogService, private snackBar: MatSnackBar, private dynamicEntityService: DynamicEntityService - ) { - } + ) {} async importCsvContentToDB(csv: string): Promise { const parsedCsv = this.papa.parse(csv, {