Skip to content
115 changes: 74 additions & 41 deletions src/app/datasets/batch-view/batch-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ import { first, switchMap } from "rxjs/operators";

import { selectDatasetsInBatch } from "state-management/selectors/datasets.selectors";
import {
appendToDatasetArrayFieldAction,
clearBatchAction,
prefillBatchAction,
removeFromBatchAction,
storeBatchAction,
} from "state-management/actions/datasets.actions";
import { Message, MessageType } from "state-management/models";
import { MessageType } from "state-management/models";
import { showMessageAction } from "state-management/actions/user.actions";
import { DialogComponent } from "shared/modules/dialog/dialog.component";

import { Router } from "@angular/router";
import { ArchivingService } from "../archiving.service";
import { Observable, Subscription, combineLatest } from "rxjs";
import { Observable, Subscription, combineLatest, firstValueFrom } from "rxjs";
import { MatDialog } from "@angular/material/dialog";
import { ShareDialogComponent } from "datasets/share-dialog/share-dialog.component";
import { AppConfigService } from "app-config.service";
import {
selectIsAdmin,
selectProfile,
} from "state-management/selectors/user.selectors";
import { OutputDatasetObsoleteDto } from "@scicatproject/scicat-sdk-ts-angular";
import {
DatasetsService,
OutputDatasetObsoleteDto,
} from "@scicatproject/scicat-sdk-ts-angular";

@Component({
selector: "batch-view",
Expand Down Expand Up @@ -54,6 +56,7 @@ export class BatchViewComponent implements OnInit, OnDestroy {
private store: Store,
private archivingSrv: ArchivingService,
private router: Router,
private datasetService: DatasetsService,
) {}

private clearBatch() {
Expand Down Expand Up @@ -116,45 +119,75 @@ export class BatchViewComponent implements OnInit, OnDestroy {
sharedUsersList,
},
});
dialogRef.afterClosed().subscribe((result: Record<string, string[]>) => {
if (result && result.users) {
this.datasetList.forEach((dataset) => {
// NOTE: If the logged in user is not an owner of the dataset or and not admin then skip sharing.
if (
(!this.isAdmin && dataset.ownerEmail !== this.userProfile.email) ||
(!this.isAdmin &&
!this.userProfile.accessGroups.includes(dataset.ownerGroup))
) {
return;
dialogRef
.afterClosed()
.subscribe(async (result: Record<string, string[]>) => {
if (result && result.users) {
const successfulShares: string[] = [];
const failedShares: string[] = [];
// Process each dataset sharing attempt individually
for (const dataset of this.datasetList) {
if (
(!this.isAdmin &&
dataset.ownerEmail !== this.userProfile.email) ||
(!this.isAdmin &&
!this.userProfile.accessGroups.includes(dataset.ownerGroup))
) {
continue;
}

try {
await firstValueFrom(
this.datasetService.datasetsControllerFindByIdAndUpdate(
dataset.pid,
{
sharedWith: result.users,
},
),
);

successfulShares.push(dataset.datasetName || dataset.pid);
} catch (error) {
console.error(error);

failedShares.push(dataset.datasetName || dataset.pid);
}
}
const message = {
type:
successfulShares.length > 0
? MessageType.Success
: MessageType.Error,
content: [
successfulShares.length > 0
? `Successfully shared ${successfulShares.length} datasets`
: "",
failedShares.length > 0
? `Failed to share ${failedShares.length} datasets`
: "",
]
.filter(Boolean)
.join(". "),
duration: 5000,
};

this.store.dispatch(
appendToDatasetArrayFieldAction({
pid: dataset.pid,
fieldName: "sharedWith",
data: result.users,
}),
);
});

const datasetUpdatedBatch = this.datasetList.map((item) => ({
...item,
sharedWith: result.users,
}));

this.clearBatch();
this.storeBatch(datasetUpdatedBatch);

const message = new Message(
result.users.length
? "Datasets successfully shared!"
: "Shared users successfully removed!",
MessageType.Success,
5000,
);
this.store.dispatch(showMessageAction({ message }));
}
});
if (successfulShares.length === this.datasetList.length) {
const sharedDatasets = this.datasetList.map((dataset) => ({
...dataset,
sharedWith: result.users,
}));

this.clearBatch();
this.storeBatch(sharedDatasets);

if (!result.users.length) {
message.content = "Shared users successfully removed!";
}
}

this.store.dispatch(showMessageAction({ message }));
}
});
}

onArchive() {
Expand Down
36 changes: 0 additions & 36 deletions src/app/state-management/actions/datasets.actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,42 +465,6 @@ describe("Dataset Actions", () => {
});
});

describe("appendToDatasetArrayFieldAction", () => {
it("should create an action", () => {
const pid = "string";
const fieldName = "test";
const data: string[] = ["string"];
const action = fromActions.appendToDatasetArrayFieldAction({
pid,
fieldName,
data,
});
expect({ ...action }).toEqual({
type: "[Dataset] Append To Array Field",
pid,
fieldName,
data,
});
});
});

describe("updateDatasetAccessGrappendToDatasetArrayFieldCompleteActionoupsCompleteAction", () => {
it("should create an action", () => {
const action = fromActions.appendToDatasetArrayFieldCompleteAction();
expect({ ...action }).toEqual({
type: "[Dataset] Append To Array Field Complete",
});
});
});
describe("appendToDatasetArrayFieldFailedAction", () => {
it("should create an action", () => {
const action = fromActions.appendToDatasetArrayFieldFailedAction();
expect({ ...action }).toEqual({
type: "[Dataset] Append To Array Field Failed",
});
});
});

describe("selectDatasetAction", () => {
it("should create an action", () => {
const action = fromActions.selectDatasetAction({ dataset });
Expand Down
11 changes: 0 additions & 11 deletions src/app/state-management/actions/datasets.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,6 @@ export const reduceDatasetFailedAction = createAction(
"[Dataset] Reduce Dataset Failed",
);

export const appendToDatasetArrayFieldAction = createAction(
"[Dataset] Append To Array Field",
props<{ pid: string; fieldName: string; data: any[] }>(),
);
export const appendToDatasetArrayFieldCompleteAction = createAction(
"[Dataset] Append To Array Field Complete",
);
export const appendToDatasetArrayFieldFailedAction = createAction(
"[Dataset] Append To Array Field Failed",
);

// === Dataset Table Selection ===

export const selectDatasetAction = createAction(
Expand Down
41 changes: 0 additions & 41 deletions src/app/state-management/effects/datasets.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ describe("DatasetEffects", () => {
"datasetsControllerCreateAttachment",
"datasetsControllerFindOneAttachmentAndUpdate",
"datasetsControllerFindOneAttachmentAndRemove",
"datasetsControllerAppendToArrayField",
"datasetsControllerCount",
]),
},
Expand Down Expand Up @@ -549,46 +548,6 @@ describe("DatasetEffects", () => {
});
});

describe("appendToArrayField$", () => {
it("should result in a appendToDatasetArrayFieldCompleteAction", () => {
const pid = "string";
const fieldName = "test";
const data = ["string"];
const action = fromActions.appendToDatasetArrayFieldAction({
pid,
fieldName,
data,
});
const outcome = fromActions.appendToDatasetArrayFieldCompleteAction();

actions = hot("-a", { a: action });
const response = cold("-a|", {});
datasetApi.datasetsControllerAppendToArrayField.and.returnValue(response);

const expected = cold("--b", { b: outcome });
expect(effects.appendToArrayField$).toBeObservable(expected);
});

it("should result in a appendToDatasetArrayFieldFailedAction", () => {
const pid = "string";
const fieldName = "test";
const data = ["string"];
const action = fromActions.appendToDatasetArrayFieldAction({
pid,
fieldName,
data,
});
const outcome = fromActions.appendToDatasetArrayFieldFailedAction();

actions = hot("-a", { a: action });
const response = cold("-#", {});
datasetApi.datasetsControllerAppendToArrayField.and.returnValue(response);

const expected = cold("--b", { b: outcome });
expect(effects.appendToArrayField$).toBeObservable(expected);
});
});

describe("loading$", () => {
describe("ofType fetchDatasetsAction", () => {
it("should dispatch a loadingAction", () => {
Expand Down
16 changes: 0 additions & 16 deletions src/app/state-management/effects/datasets.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,22 +367,6 @@ export class DatasetEffects {
);
});

appendToArrayField$ = createEffect(() => {
return this.actions$.pipe(
ofType(fromActions.appendToDatasetArrayFieldAction),
mergeMap(({ pid, fieldName, data }) =>
this.datasetsService
.datasetsControllerAppendToArrayField(pid, fieldName, data)
.pipe(
map(() => fromActions.appendToDatasetArrayFieldCompleteAction()),
catchError(() =>
of(fromActions.appendToDatasetArrayFieldFailedAction()),
),
),
),
);
});

loading$ = createEffect(() => {
return this.actions$.pipe(
ofType(
Expand Down
Loading