Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More bulk actions: bulk delete + bulk archive multiple records at once from list #2154

Merged
merged 16 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
</p>

<p i18n>
This record is archived and will be be hidden from lists and select
options by default.
This record is archived and will be hidden from lists and select options
by default.
</p>
</mat-card-content>

<mat-card-actions *ngIf="!entity.anonymized">
<button
mat-stroked-button
color="accent"
(click)="entityRemoveService.undoArchive(entity)"
(click)="entityActionsService.undoArchive(entity)"
>
Reactivate
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import { EntityActionsService } from "../../entity/entity-actions/entity-actions
export class EntityArchivedInfoComponent {
@Input() entity: Entity;

constructor(public entityRemoveService: EntityActionsService) {}
constructor(public entityActionsService: EntityActionsService) {}
}
33 changes: 33 additions & 0 deletions src/app/core/entity-list/entity-list/entity-list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,39 @@ <h2>{{ title }}</h2>
<div>Actions on selected records:</div>

<div class="flex-row gap-small bulk-action-button">
<button
mat-raised-button
(click)="archiveRecords()"
[disabled]="selectedRows.length === 0"
matTooltip="Select rows to archive"
i18n-matTooltip
color="accent"
i18n="bulk action button"
>
Archive
</button>
<button
mat-raised-button
(click)="anonymizeRecords()"
[disabled]="selectedRows.length === 0"
matTooltip="Select rows to anonymize"
i18n-matTooltip
color="accent"
i18n="bulk action button"
>
Anonymize
</button>
<button
mat-raised-button
(click)="deleteRecords()"
[disabled]="selectedRows.length === 0"
matTooltip="Select rows to delete"
i18n-matTooltip
color="accent"
i18n="bulk action button"
>
Delete
</button>
<button
mat-raised-button
(click)="duplicateRecords()"
Expand Down
17 changes: 17 additions & 0 deletions src/app/core/entity-list/entity-list/entity-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { MatTooltipModule } from "@angular/material/tooltip";
import { Sort } from "@angular/material/sort";
import { ExportColumnConfig } from "../../export/data-transformation-service/export-column-config";
import { RouteTarget } from "../../../route-target";
import { EntityActionsService } from "app/core/entity/entity-actions/entity-actions.service";
import { EntitiesTableComponent } from "../../common-components/entities-table/entities-table.component";
import { applyUpdate } from "../../entity/model/entity-update";
import { Subscription } from "rxjs";
Expand Down Expand Up @@ -160,6 +161,7 @@ export class EntityListComponent<T extends Entity>
private entities: EntityRegistry,
private dialog: MatDialog,
private duplicateRecord: DuplicateRecordService,
private entityActionsService: EntityActionsService,
) {
this.screenWidthObserver
.platform()
Expand Down Expand Up @@ -294,6 +296,21 @@ export class EntityListComponent<T extends Entity>
this.selectedRows = undefined;
}

async deleteRecords() {
await this.entityActionsService.delete(this.selectedRows);
this.selectedRows = undefined;
}

async archiveRecords() {
await this.entityActionsService.archive(this.selectedRows);
this.selectedRows = undefined;
}

async anonymizeRecords() {
await this.entityActionsService.anonymize(this.selectedRows);
this.selectedRows = undefined;
}

onRowClick(row: T) {
this.elementClick.emit(row);
}
Expand Down
151 changes: 139 additions & 12 deletions src/app/core/entity/entity-actions/entity-actions.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ describe("EntityActionsService", () => {
let mockConfirmationDialog: jasmine.SpyObj<ConfirmationDialogService>;
let mockRouter;
let mockedEntityDeleteService: jasmine.SpyObj<EntityDeleteService>;
let mockedEntityAnonymizeService: jasmine.SpyObj<EntityAnonymizeService>;

let primaryEntity: Entity;
let testEntities: Entity[] = [];

beforeEach(() => {
primaryEntity = new Entity();
testEntities[0] = new Entity();
testEntities[1] = new Entity();
testEntities[2] = new Entity();

mockedEntityDeleteService = jasmine.createSpyObj(["deleteEntity"]);
mockedEntityDeleteService.deleteEntity.and.resolveTo(
new CascadingActionResult([primaryEntity]),
);
mockedEntityAnonymizeService = jasmine.createSpyObj(["anonymizeEntity"]);
mockedEntityAnonymizeService.anonymizeEntity.and.resolveTo(
new CascadingActionResult([primaryEntity]),
);
mockedEntityMapper = jasmine.createSpyObj(["save", "saveAll"]);

snackBarSpy = jasmine.createSpyObj(["open"]);
Expand All @@ -54,7 +63,10 @@ describe("EntityActionsService", () => {
providers: [
EntityActionsService,
{ provide: EntityDeleteService, useValue: mockedEntityDeleteService },
{ provide: EntityAnonymizeService, useValue: null },
{
provide: EntityAnonymizeService,
useValue: mockedEntityAnonymizeService,
},
{ provide: EntityMapperService, useValue: mockedEntityMapper },
{ provide: MatSnackBar, useValue: snackBarSpy },
Router,
Expand All @@ -80,19 +92,42 @@ describe("EntityActionsService", () => {
expect(mockedEntityDeleteService.deleteEntity).not.toHaveBeenCalled();
});

it("should delete entity, show snackbar confirmation and navigate back", async () => {
it("should delete a single entity, show snackbar confirmation and navigate back", async () => {
// onAction is never called
mockSnackBarRef.onAction.and.returnValues(NEVER);
mockSnackBarRef.afterDismissed.and.returnValue(of(undefined));

const result = await service.delete(new Entity(), true);
const result = await service.delete(primaryEntity, true);

expect(result).toBe(true);
expect(snackBarSpy.open).toHaveBeenCalled();
expect(mockedEntityDeleteService.deleteEntity).toHaveBeenCalled();
expect(mockedEntityDeleteService.deleteEntity).toHaveBeenCalledWith(
primaryEntity,
);
expect(mockRouter.navigate).toHaveBeenCalled();
});

it("should delete several entities and show snackbar confirmation", async () => {
// onAction is never called
mockSnackBarRef.onAction.and.returnValues(NEVER);
mockSnackBarRef.afterDismissed.and.returnValue(of(undefined));

const result = await service.delete(testEntities);

expect(result).toBe(true);
expect(snackBarSpy.open).toHaveBeenCalled();
expect(mockedEntityDeleteService.deleteEntity).toHaveBeenCalledTimes(3);
expect(mockedEntityDeleteService.deleteEntity).toHaveBeenCalledWith(
testEntities[0],
);
expect(mockedEntityDeleteService.deleteEntity).toHaveBeenCalledWith(
testEntities[1],
);
expect(mockedEntityDeleteService.deleteEntity).toHaveBeenCalledWith(
testEntities[2],
);
});

it("should re-save all affected entities and navigate back to entity on undo", fakeAsync(() => {
const anotherAffectedEntity = new Entity();
mockedEntityDeleteService.deleteEntity.and.resolveTo(
Expand Down Expand Up @@ -121,21 +156,113 @@ describe("EntityActionsService", () => {
expect(mockRouter.navigate).toHaveBeenCalled();
}));

it("should archive and save entity", async () => {
await service.archive(primaryEntity);
it("should anonymize and save a single entity", async () => {
// onAction is never called
mockSnackBarRef.onAction.and.returnValues(NEVER);
mockSnackBarRef.afterDismissed.and.returnValue(of(undefined));

expect(primaryEntity.isActive).toBeFalse();
expect(mockedEntityMapper.save).toHaveBeenCalledWith(primaryEntity);
// const testEntity = new Entity();
const result = await service.anonymize(primaryEntity);

expect(result).toBe(true);
expect(snackBarSpy.open).toHaveBeenCalled();
expect(mockedEntityAnonymizeService.anonymizeEntity).toHaveBeenCalledWith(
primaryEntity,
);
});

it("should archiveUndo and save entity", async () => {
it("should anonymize and save several entities and show snackbar confirmation", async () => {
// onAction is never called
mockSnackBarRef.onAction.and.returnValues(NEVER);
mockSnackBarRef.afterDismissed.and.returnValue(of(undefined));

const result = await service.anonymize(testEntities);

expect(result).toBe(true);
expect(snackBarSpy.open).toHaveBeenCalled();
expect(mockedEntityAnonymizeService.anonymizeEntity).toHaveBeenCalledTimes(
3,
);
expect(mockedEntityAnonymizeService.anonymizeEntity).toHaveBeenCalledWith(
testEntities[0],
);
expect(mockedEntityAnonymizeService.anonymizeEntity).toHaveBeenCalledWith(
testEntities[1],
);
expect(mockedEntityAnonymizeService.anonymizeEntity).toHaveBeenCalledWith(
testEntities[2],
);
});

it("should archive and save a single entity and show snackbar confirmation", async () => {
// onAction is never called
mockSnackBarRef.onAction.and.returnValues(NEVER);
mockSnackBarRef.afterDismissed.and.returnValue(of(undefined));

let expectedSavedEntity = primaryEntity.copy();
expectedSavedEntity.inactive = true;

const result = await service.archive(primaryEntity);
expect(result).toBe(true);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(expectedSavedEntity);
expect(snackBarSpy.open).toHaveBeenCalled();
});

it("should archive and save several entities and show snackbar confirmation", async () => {
// onAction is never called
mockSnackBarRef.onAction.and.returnValues(NEVER);
mockSnackBarRef.afterDismissed.and.returnValue(of(undefined));

let expectedSavedEntities = testEntities.map((e) => e.copy());
expectedSavedEntities.forEach((e) => (e.inactive = true));

const result = await service.archive(testEntities);
expect(result).toBe(true);
expect(mockedEntityMapper.save).toHaveBeenCalledTimes(3);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(
expectedSavedEntities[0],
);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(
expectedSavedEntities[1],
);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(
expectedSavedEntities[2],
);
expect(snackBarSpy.open).toHaveBeenCalled();
});

it("should archiveUndo and save a single entity", async () => {
let expectedSavedEntity = primaryEntity.copy();
expectedSavedEntity.inactive = true;

await service.archive(primaryEntity);
expect(primaryEntity.isActive).toBeFalse();
expect(mockedEntityMapper.save).toHaveBeenCalledWith(expectedSavedEntity);
mockedEntityMapper.save.calls.reset();

await service.undoArchive(primaryEntity);

expect(primaryEntity.isActive).toBeTrue();
expect(mockedEntityMapper.save).toHaveBeenCalledWith(primaryEntity);
});

it("should archiveUndo and save several entities", async () => {
let expectedSavedEntities = testEntities.map((e) => e.copy());
expectedSavedEntities.forEach((e) => (e.inactive = true));

await service.archive(testEntities);
expect(mockedEntityMapper.save).toHaveBeenCalledTimes(3);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(
expectedSavedEntities[0],
);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(
expectedSavedEntities[1],
);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(
expectedSavedEntities[2],
);
mockedEntityMapper.save.calls.reset();

await service.undoArchive(testEntities);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(testEntities[0]);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(testEntities[1]);
expect(mockedEntityMapper.save).toHaveBeenCalledWith(testEntities[2]);
});
});
Loading
Loading