@@ -4,27 +4,29 @@ import { first, switchMap } from "rxjs/operators";
44
55import { selectDatasetsInBatch } from "state-management/selectors/datasets.selectors" ;
66import {
7- appendToDatasetArrayFieldAction ,
87 clearBatchAction ,
98 prefillBatchAction ,
109 removeFromBatchAction ,
1110 storeBatchAction ,
1211} from "state-management/actions/datasets.actions" ;
13- import { Message , MessageType } from "state-management/models" ;
12+ import { MessageType } from "state-management/models" ;
1413import { showMessageAction } from "state-management/actions/user.actions" ;
1514import { DialogComponent } from "shared/modules/dialog/dialog.component" ;
1615
1716import { Router } from "@angular/router" ;
1817import { ArchivingService } from "../archiving.service" ;
19- import { Observable , Subscription , combineLatest } from "rxjs" ;
18+ import { Observable , Subscription , combineLatest , firstValueFrom } from "rxjs" ;
2019import { MatDialog } from "@angular/material/dialog" ;
2120import { ShareDialogComponent } from "datasets/share-dialog/share-dialog.component" ;
2221import { AppConfigService } from "app-config.service" ;
2322import {
2423 selectIsAdmin ,
2524 selectProfile ,
2625} from "state-management/selectors/user.selectors" ;
27- import { OutputDatasetObsoleteDto } from "@scicatproject/scicat-sdk-ts-angular" ;
26+ import {
27+ DatasetsService ,
28+ OutputDatasetObsoleteDto ,
29+ } from "@scicatproject/scicat-sdk-ts-angular" ;
2830
2931@Component ( {
3032 selector : "batch-view" ,
@@ -54,6 +56,7 @@ export class BatchViewComponent implements OnInit, OnDestroy {
5456 private store : Store ,
5557 private archivingSrv : ArchivingService ,
5658 private router : Router ,
59+ private datasetService : DatasetsService ,
5760 ) { }
5861
5962 private clearBatch ( ) {
@@ -116,45 +119,75 @@ export class BatchViewComponent implements OnInit, OnDestroy {
116119 sharedUsersList,
117120 } ,
118121 } ) ;
119- dialogRef . afterClosed ( ) . subscribe ( ( result : Record < string , string [ ] > ) => {
120- if ( result && result . users ) {
121- this . datasetList . forEach ( ( dataset ) => {
122- // NOTE: If the logged in user is not an owner of the dataset or and not admin then skip sharing.
123- if (
124- ( ! this . isAdmin && dataset . ownerEmail !== this . userProfile . email ) ||
125- ( ! this . isAdmin &&
126- ! this . userProfile . accessGroups . includes ( dataset . ownerGroup ) )
127- ) {
128- return ;
122+ dialogRef
123+ . afterClosed ( )
124+ . subscribe ( async ( result : Record < string , string [ ] > ) => {
125+ if ( result && result . users ) {
126+ const successfulShares : string [ ] = [ ] ;
127+ const failedShares : string [ ] = [ ] ;
128+ // Process each dataset sharing attempt individually
129+ for ( const dataset of this . datasetList ) {
130+ if (
131+ ( ! this . isAdmin &&
132+ dataset . ownerEmail !== this . userProfile . email ) ||
133+ ( ! this . isAdmin &&
134+ ! this . userProfile . accessGroups . includes ( dataset . ownerGroup ) )
135+ ) {
136+ continue ;
137+ }
138+
139+ try {
140+ await firstValueFrom (
141+ this . datasetService . datasetsControllerFindByIdAndUpdate (
142+ dataset . pid ,
143+ {
144+ sharedWith : result . users ,
145+ } ,
146+ ) ,
147+ ) ;
148+
149+ successfulShares . push ( dataset . datasetName || dataset . pid ) ;
150+ } catch ( error ) {
151+ console . error ( error ) ;
152+
153+ failedShares . push ( dataset . datasetName || dataset . pid ) ;
154+ }
129155 }
156+ const message = {
157+ type :
158+ successfulShares . length > 0
159+ ? MessageType . Success
160+ : MessageType . Error ,
161+ content : [
162+ successfulShares . length > 0
163+ ? `Successfully shared ${ successfulShares . length } datasets`
164+ : "" ,
165+ failedShares . length > 0
166+ ? `Failed to share ${ failedShares . length } datasets`
167+ : "" ,
168+ ]
169+ . filter ( Boolean )
170+ . join ( ". " ) ,
171+ duration : 5000 ,
172+ } ;
130173
131- this . store . dispatch (
132- appendToDatasetArrayFieldAction ( {
133- pid : dataset . pid ,
134- fieldName : "sharedWith" ,
135- data : result . users ,
136- } ) ,
137- ) ;
138- } ) ;
139-
140- const datasetUpdatedBatch = this . datasetList . map ( ( item ) => ( {
141- ...item ,
142- sharedWith : result . users ,
143- } ) ) ;
144-
145- this . clearBatch ( ) ;
146- this . storeBatch ( datasetUpdatedBatch ) ;
147-
148- const message = new Message (
149- result . users . length
150- ? "Datasets successfully shared!"
151- : "Shared users successfully removed!" ,
152- MessageType . Success ,
153- 5000 ,
154- ) ;
155- this . store . dispatch ( showMessageAction ( { message } ) ) ;
156- }
157- } ) ;
174+ if ( successfulShares . length === this . datasetList . length ) {
175+ const sharedDatasets = this . datasetList . map ( ( dataset ) => ( {
176+ ...dataset ,
177+ sharedWith : result . users ,
178+ } ) ) ;
179+
180+ this . clearBatch ( ) ;
181+ this . storeBatch ( sharedDatasets ) ;
182+
183+ if ( ! result . users . length ) {
184+ message . content = "Shared users successfully removed!" ;
185+ }
186+ }
187+
188+ this . store . dispatch ( showMessageAction ( { message } ) ) ;
189+ }
190+ } ) ;
158191 }
159192
160193 onArchive ( ) {
0 commit comments