0

I am currently designing a sales application (Angular 6 + Bootstrap) to be a responsive application for one of the telecom operators and my system users are more than 10K user who is accessing the system daily for sales activities on the same time and i have a lot of business rules which needs to run sometimes sequential and sometimes parallel and sometimes its client side business rules and sometimes run based on web-services calls REST.

in another application (HTML5 & JQUERY) we used an action chain concept which is designed manually and each action in the chain takes the HTML element as an input and start applying the logic and then forward to next action or fail and end the chain.

my question is how to apply the business rules in Angular application in chain concept considering that the whole backend is RESTful web services.?

Mohamed
  • 31
  • 1
  • 4
  • Angular uses RxJS which supports chaining observables, that's probably where you start looking at. You can learn some of it from [learn-rxjs](https://www.learnrxjs.io/), thought it might have some out-of-date information. – Joshua Chan May 12 '18 at 16:29
  • let us have an example from what i need to do in my application to be more clear: let us say i have a text field which will have the customer mobile number, i need to apply the following business rules on this mobile number: 1- check the mobile number status in the backend system (external system) through rest web service to make sure its valid. 2- check the mobile number in another system that the owner is the same as the current customer using the identity number. 3- check if the customer doesn’t have any outstanding amount of money on his account on the billing system – Mohamed May 12 '18 at 16:58

2 Answers2

2

Thanks for your support ..

I have developed it manually like OOP for my actions/validations and Angular controller will manage running the list of actions if exist.

Mohamed
  • 31
  • 1
  • 4
-1

One way to implement the checking as you've said in your example scenario:

First, we assume that rules 1, 2 and 3 are independent and should be checked parallel:

Note that the proper way to call API in Angular is through a service.

let checkNumber1 = this.http.post('https://example.com/api/checkNumber', { mobileNo: '0123920802' });
let checkNumber2 = this.http.post('https://example2.com/api/checkNumber', { mobileNo: '0123920802' });
let checkOutstanding = this.http.post('https://example.com/api/checkOutstanding', { userId: 23910231 });

forkJoin([checkNumber1, checkNumber2, checkOutstanding]).subscribe(results => {
    // results[0] is our result from checkNumber1
    // results[1] is our result from checkNumber2
    // results[2] is our result from checkOutstanding
    if (results[0] && results[1] && results[2]) {
        // if all checks return true 
        proceed();
    } else {
        error();
    }
});

What if you want to do the checking sequentially, one possible way would be:

checkNumber1.subscribe((result) => {
    if (result) {
        checkNumber2.subscribe((result) => {
            if (result) {
                checkOutstanding.subscribe((result) => {
                    if (result) {
                        proceed();
                    }
                });
            }
        });
    }
});
Joshua Chan
  • 1,797
  • 8
  • 16
  • you comment is valid but the validators will not be re-usable and how can this be triggered on value change ?? and how can i use the same validation (any of them) with another UI component ?? – Mohamed May 12 '18 at 18:01
  • Typically you would want your API to be encapsulated in a service, then just call a service called validator `this.validator.checkNumber(number)`. Using thing such as onChanges or Angular reactive form's valueChanges you can do things like: `this.form.valueChanges.pipe((x) => this.validator.checkNumber(x), (x) => this.validator.checkOutstanding(x))` – Joshua Chan May 12 '18 at 18:14
  • You shouldn't have 'subscribe' within a 'subscribe'. Use mergeMap or other operators. – Stefdelec May 27 '18 at 09:59
  • Technically, there isn't any difference between multiple subscribe and using subscribe -> map -> subscribe -> map when you're dealing with Http calls. mergeMap and concatMap or any other operators all give different behavior and I am in no way saying that this is the only way to do it. – Joshua Chan May 27 '18 at 13:04