@@ -4,27 +4,29 @@ import { first, switchMap } from "rxjs/operators";
4
4
5
5
import { selectDatasetsInBatch } from "state-management/selectors/datasets.selectors" ;
6
6
import {
7
- appendToDatasetArrayFieldAction ,
8
7
clearBatchAction ,
9
8
prefillBatchAction ,
10
9
removeFromBatchAction ,
11
10
storeBatchAction ,
12
11
} from "state-management/actions/datasets.actions" ;
13
- import { Message , MessageType } from "state-management/models" ;
12
+ import { MessageType } from "state-management/models" ;
14
13
import { showMessageAction } from "state-management/actions/user.actions" ;
15
14
import { DialogComponent } from "shared/modules/dialog/dialog.component" ;
16
15
17
16
import { Router } from "@angular/router" ;
18
17
import { ArchivingService } from "../archiving.service" ;
19
- import { Observable , Subscription , combineLatest } from "rxjs" ;
18
+ import { Observable , Subscription , combineLatest , firstValueFrom } from "rxjs" ;
20
19
import { MatDialog } from "@angular/material/dialog" ;
21
20
import { ShareDialogComponent } from "datasets/share-dialog/share-dialog.component" ;
22
21
import { AppConfigService } from "app-config.service" ;
23
22
import {
24
23
selectIsAdmin ,
25
24
selectProfile ,
26
25
} 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" ;
28
30
29
31
@Component ( {
30
32
selector : "batch-view" ,
@@ -54,6 +56,7 @@ export class BatchViewComponent implements OnInit, OnDestroy {
54
56
private store : Store ,
55
57
private archivingSrv : ArchivingService ,
56
58
private router : Router ,
59
+ private datasetService : DatasetsService ,
57
60
) { }
58
61
59
62
private clearBatch ( ) {
@@ -116,45 +119,75 @@ export class BatchViewComponent implements OnInit, OnDestroy {
116
119
sharedUsersList,
117
120
} ,
118
121
} ) ;
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
+ }
129
155
}
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
+ } ;
130
173
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
+ } ) ;
158
191
}
159
192
160
193
onArchive ( ) {
0 commit comments