@@ -4,8 +4,8 @@ import { MatSnackBar } from '@angular/material/snack-bar';
44import { ActivatedRoute } from '@angular/router' ;
55import { TranslocoService } from '@ngneat/transloco' ;
66import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ;
7- import { combineLatest , of } from 'rxjs' ;
8- import { catchError , concatMap , first , map , tap } from 'rxjs/operators' ;
7+ import { combineLatest , forkJoin , of } from 'rxjs' ;
8+ import { catchError , concatMap , first , map , take , tap } from 'rxjs/operators' ;
99import { ActionsDialogComponent } from '../../../../shared/actions/actions-dialog/actions-dialog.component' ;
1010import {
1111 Action ,
@@ -14,13 +14,17 @@ import {
1414import { OrderHistoryService } from '../../../../shared/actions/service/order-history.service' ;
1515import { BlockingActionService } from '../../../../shared/blocking-action/blocking-action.service' ;
1616import { DiaBackendAuthService } from '../../../../shared/dia-backend/auth/dia-backend-auth.service' ;
17+ import { DiaBackendSeriesRepository } from '../../../../shared/dia-backend/series/dia-backend-series-repository.service' ;
1718import {
1819 DiaBackendStoreService ,
1920 NetworkAppOrder ,
2021} from '../../../../shared/dia-backend/store/dia-backend-store.service' ;
2122import { ErrorService } from '../../../../shared/error/error.service' ;
2223import { OrderDetailDialogComponent } from '../../../../shared/order-detail-dialog/order-detail-dialog.component' ;
23- import { isNonNullable } from '../../../../utils/rx-operators/rx-operators' ;
24+ import {
25+ isNonNullable ,
26+ VOID$ ,
27+ } from '../../../../utils/rx-operators/rx-operators' ;
2428
2529@UntilDestroy ( )
2630@Component ( {
@@ -34,7 +38,8 @@ export class ActionsPage {
3438 . pipe ( catchError ( ( err : unknown ) => this . errorService . toastError$ ( err ) ) ) ;
3539
3640 private readonly id$ = this . route . paramMap . pipe (
37- map ( params => params . get ( 'id' ) )
41+ map ( params => params . get ( 'id' ) ) ,
42+ isNonNullable ( )
3843 ) ;
3944
4045 constructor (
@@ -47,9 +52,71 @@ export class ActionsPage {
4752 private readonly snackBar : MatSnackBar ,
4853 private readonly dialog : MatDialog ,
4954 private readonly storeService : DiaBackendStoreService ,
50- private readonly orderHistoryService : OrderHistoryService
55+ private readonly orderHistoryService : OrderHistoryService ,
56+ private readonly diaBackendStoreService : DiaBackendStoreService ,
57+ private readonly diaBackendSeriesRepository : DiaBackendSeriesRepository
5158 ) { }
5259
60+ canPerformAction$ ( action : Action ) {
61+ if ( action . title_text === 'List in CaptureClub' ) {
62+ /*
63+ Workaround:
64+ Currently there isn't a simple way to check whether an asset is listed in
65+ CaptureClub or not. So I first query List all Products API with
66+ associated_id parameter set to the assets cid. And then use list series
67+ API and check through all nested collections. See discussion here
68+ https://app.asana.com/0/0/1201558520076805/1201995911008176/f
69+ */
70+ return this . id$ . pipe (
71+ concatMap ( cid =>
72+ forkJoin ( [
73+ this . diaBackendStoreService . listAllProducts$ ( {
74+ associated_id : cid ,
75+ service_name : 'CaptureClub' ,
76+ } ) ,
77+ of ( cid ) ,
78+ ] )
79+ ) ,
80+ concatMap ( ( [ response , cid ] ) => {
81+ if ( response . count > 0 ) {
82+ throw new Error (
83+ this . translocoService . translate ( 'message.hasListedInCaptureClub' )
84+ ) ;
85+ }
86+ return of ( cid ) ;
87+ } ) ,
88+ concatMap ( async cid => {
89+ let currentOffset = 0 ;
90+ const limit = 100 ;
91+ while ( true ) {
92+ const response = await this . diaBackendSeriesRepository
93+ . fetchAll$ ( { offset : currentOffset , limit } )
94+ . toPromise ( ) ;
95+ const listedAsSeries = response . results . some ( serie =>
96+ serie . collections . some ( collection =>
97+ collection . assets . some ( asset => asset . cid === cid )
98+ )
99+ ) ;
100+ if ( listedAsSeries ) {
101+ throw new Error (
102+ this . translocoService . translate (
103+ 'message.hasListedInCaptureClub'
104+ )
105+ ) ;
106+ }
107+ if ( response . next == null ) {
108+ break ;
109+ }
110+ currentOffset += response . results . length ;
111+ }
112+ return VOID$ ;
113+ } ) ,
114+ take ( 1 )
115+ ) ;
116+ }
117+ return VOID$ ;
118+ }
119+
53120 openActionDialog$ ( action : Action ) {
54121 return combineLatest ( [
55122 this . actionsService . getParams$ ( action . params_list_custom_param1 ) ,
@@ -128,8 +195,13 @@ export class ActionsPage {
128195 }
129196
130197 doAction ( action : Action ) {
131- this . openActionDialog$ ( action )
198+ this . blockingActionService
199+ . run$ ( this . canPerformAction$ ( action ) )
132200 . pipe (
201+ catchError ( ( err : unknown ) => {
202+ return this . errorService . toastError$ ( err ) ;
203+ } ) ,
204+ concatMap ( ( ) => this . openActionDialog$ ( action ) ) ,
133205 concatMap ( createOrderInput =>
134206 this . blockingActionService . run$ (
135207 this . createOrder$ (
0 commit comments