I have a unique use case that requires me to verify certain conditions. If one condition fails, I should not proceed to the next one. Instead, based on the failed condition, I need to display a dialog with a title and description explaining what went wrong. Assume I have a service (pseudo code) // verify-condition.service.ts
public validateConditions():Observable<ValidationData> {
public validateConditions(): Observable<ValidationModal> {
const hasContract$: Observable<ContractStatus> = this.getContractSignStatus();
const hasTUTSigned$: Observable<boolean> = this.hasTUTSigned();
const doData$: Observable<DoModel> = this.getDoData();
return combineLatest([hasContract$, hasTUTSigned$, doData$]).pipe(
map(([hasContract, hasTUTSigned, doData,]) => {
const validationConditions: ValidationModal = {
conditions: {
hasContract: hasContract === ContractStatus.SIGNED,
hasTUTSigned,
wasSigned: doData.wasSigned,
valid: doData.valid,
signDate: doData.signDate,
}
};
return validationConditions;
})
);
}
}
and in the component where it is being used
public verifyProcess(): void {
verifyConditionService.validateConditions().pipe(take(1)).subscribe((validationData:ValidationData=> {
if (validationData) {
this.modalValidationService.openModal(validationData);
} else {
this.beginProcess();
}
})
}
The challenge here is that in the service, all conditions are checked at once using combineLatest. However, I want to check them one by one. If the first condition fails, I should not proceed to the next one but instead throw an error or indicate that it fails and provide the data necessary to display the dialog.
public validateConditionsInOrder():Observable<ValidationData|boolean> {
return this.getContractSignStatus().pipe(
map((signStatus:ContractStatus)=>{
if(signStatus !== ContractStatus.SIGNED){
return {
hasContract:signStatus
}
} else {
return true;
}
}),
switchMap((previousCondition)=>{
if(!previousCondition){ // if previous condition failed, stop verification and return data from the failed condition
} else {
this.hasTUTSigned(); // if previous condition passes, move on to the next one. Need to know what went wrong for displaying dialog.
}
})
)
}