-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): duplicate records from list views (#2042)
closes #1362 Co-authored-by: Sebastian <sebastian@aam-digital.com>
- Loading branch information
1 parent
106e71d
commit c8b8c18
Showing
6 changed files
with
200 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/app/core/entity-list/duplicate-records/duplicate-records.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { TestBed } from "@angular/core/testing"; | ||
import { DuplicateRecordService } from "./duplicate-records.service"; | ||
import { EntityMapperService } from "../../entity/entity-mapper/entity-mapper.service"; | ||
import { | ||
DatabaseEntity, | ||
entityRegistry, | ||
EntityRegistry, | ||
} from "../../entity/database-entity.decorator"; | ||
import { Database } from "../../database/database"; | ||
import { SessionService } from "../../session/session-service/session.service"; | ||
import { Entity } from "../../entity/model/entity"; | ||
import { DatabaseField } from "../../entity/database-field.decorator"; | ||
import { CoreModule } from "../../core.module"; | ||
import { ComponentRegistry } from "../../../dynamic-components"; | ||
import { UpdateMetadata } from "../../entity/model/update-metadata"; | ||
import { MatDialog } from "@angular/material/dialog"; | ||
import { MatSnackBar } from "@angular/material/snack-bar"; | ||
import { FileService } from "../../../features/file/file.service"; | ||
|
||
describe("DuplicateRecordsService", () => { | ||
let service: DuplicateRecordService; | ||
let entityMapperService: EntityMapperService; | ||
|
||
@DatabaseEntity("DuplicateTestEntity") | ||
class DuplicateTestEntity extends Entity { | ||
static toStringAttributes = ["name"]; | ||
@DatabaseField() name: String; | ||
@DatabaseField() boolProperty: boolean; | ||
@DatabaseField() created: UpdateMetadata; | ||
@DatabaseField() updated: UpdateMetadata; | ||
@DatabaseField() inactive: boolean; | ||
} | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [CoreModule], | ||
providers: [ | ||
DuplicateRecordService, | ||
Database, | ||
EntityMapperService, | ||
SessionService, | ||
{ provide: EntityRegistry, useValue: entityRegistry }, | ||
{ provide: MatDialog, useValue: {} }, | ||
{ provide: MatSnackBar, useValue: {} }, | ||
{ provide: FileService, useValue: {} }, | ||
ComponentRegistry, | ||
], | ||
}); | ||
service = TestBed.inject(DuplicateRecordService); | ||
entityMapperService = TestBed.inject(EntityMapperService); | ||
}); | ||
|
||
it("should be created", () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it("should transform data correctly", () => { | ||
const duplicateTest = new DuplicateTestEntity(); | ||
duplicateTest.name = "TestName"; | ||
duplicateTest.boolProperty = true; | ||
|
||
const originalData = [duplicateTest]; | ||
const transformedData = service.clone(originalData); | ||
|
||
expect(transformedData[0]).toBeInstanceOf(Entity); | ||
expect(transformedData[0]._id).toBeDefined(); | ||
expect(transformedData[0]._id).not.toBe(duplicateTest["_id"]); | ||
expect(transformedData[0].name).toMatch(/^Copy of /); | ||
expect(transformedData[0].boolProperty).toBe(true); | ||
}); | ||
|
||
it("should save duplicate record", async () => { | ||
const duplicateTestEntity = new DuplicateTestEntity(); | ||
duplicateTestEntity.name = "TestName"; | ||
duplicateTestEntity.boolProperty = true; | ||
duplicateTestEntity.inactive = false; | ||
|
||
const originalData = [duplicateTestEntity]; | ||
const cloneSpy = spyOn(service, "clone").and.callThrough(); | ||
const saveAllSpy = spyOn(entityMapperService, "saveAll"); | ||
|
||
await service.duplicateRecord(originalData); | ||
|
||
expect(cloneSpy).toHaveBeenCalledWith(originalData); | ||
expect(saveAllSpy).toHaveBeenCalled(); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
src/app/core/entity-list/duplicate-records/duplicate-records.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { EntityMapperService } from "../../entity/entity-mapper/entity-mapper.service"; | ||
import { EntityRegistry } from "../../entity/database-entity.decorator"; | ||
import { EntitySchemaService } from "../../entity/schema/entity-schema.service"; | ||
import { Entity } from "../../entity/model/entity"; | ||
|
||
@Injectable({ | ||
providedIn: "root", | ||
}) | ||
export class DuplicateRecordService { | ||
get: jasmine.Spy<jasmine.Func>; | ||
constructor( | ||
private entitymapperservice: EntityMapperService, | ||
private entityTypes: EntityRegistry, | ||
private entityService: EntitySchemaService, | ||
) {} | ||
|
||
async duplicateRecord(sourceData: Entity[]) { | ||
const duplicateData = this.clone(sourceData); | ||
return await this.entitymapperservice.saveAll(duplicateData); | ||
} | ||
|
||
clone(sourceData: Entity[]): any { | ||
const duplicateData = []; | ||
|
||
sourceData.map((item: Entity) => { | ||
const entityConstructor = item.getConstructor(); | ||
const keys = [...entityConstructor.schema.keys()].filter( | ||
(key) => key !== "_id" && key !== "_rev", | ||
); | ||
const dbEntity = this.entityService.transformEntityToDatabaseFormat(item); | ||
const entityformat = this.entityService.transformDatabaseToEntityFormat( | ||
dbEntity, | ||
entityConstructor.schema, | ||
); | ||
const entity = new entityConstructor(); | ||
const nameAttribute = entityConstructor.toStringAttributes[0]; | ||
for (const key of keys) { | ||
if (nameAttribute === key && nameAttribute !== "entityId") { | ||
entityformat[key] = `Copy of ${entityformat[key]}`; | ||
} | ||
entity[key] = entityformat[key]; | ||
} | ||
duplicateData.push(entity); | ||
}); | ||
return duplicateData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters