I have an angular project and I have some api that creates a line item and then loops through and calls the api to add each modification to the line item and finally it fires the print request. Right now I have to do this using multiple subscribes and was thinking if there was away to modify the service itself so it runs the api's in order and returns the response so it shortens the code as well. The loops make it a bit complicated for exhaustmap or mergemap.
The function in my component that calls the api's in order are:
addLineItems() {
this.lineItems = this.lineItems.filter(function (item) {
delete item.image;
return true;
});
console.log('In Line items' + this.orderId);
this.storedMerchantId = this.currentUser.merchantId
this.storedCloverToken = this.currentUser.cloverToken;
console.log('line Item Is: ' + JSON.stringify(this.lineItems));
this.lineItems.forEach(lineItem => {
console.log(JSON.stringify(lineItem));
this.cloverOrderService
.postNewLineItem(
this.orderId,
lineItem,
this.storedMerchantId
)
.subscribe(cloverItem => {
lineItem.id = cloverItem[0].id;
console.log(JSON.stringify(lineItem));
lineItem.newModifications.forEach(modification => {
this.cloverOrderService
.postNewModification(
this.orderId,
lineItem.id,
modification,
this.storedMerchantId
)
.pipe(first())
.subscribe(itemModification => {
console.log(
'modification added:' +
JSON.stringify(itemModification)
);
},
error => {
this.alertService.error(error);
this.loading = false;
});
});
console.log(JSON.stringify(lineItem));
this.cartService.SetOrderHistoryToCart();
this.cartService.removeFromCart(lineItem);
});
});
this.orderPlaced();
}
The code for the service functions for the api calls are:
postNewLineItem(id: string, lineItem: LineItem, merchantId: string) {
this.cloverMerchantIdAndTokenSet();
const body = {
'merchant_id': this.merchant_id,
'token': this.cloverToken,
'order_id': id,
'line_item': lineItem
};
const newLineItem = this.http.post<CloverOrder>(`${environment.apiUrl}/cloverOrders/createLineItem`, body);
const printOrder = this.fireOrder(id, merchantId);
//return forkJoin(newLineItem, printOrder);
return this.http.post<any>(`${environment.apiUrl}/cloverOrders/createLineItem`, body);
}
fireOrder(id: string, merchantId: string) {
this.cloverMerchantIdAndTokenSet();
const body = {
'merchant_id': this.merchant_id,
'token': this.cloverToken,
'printItem': new PrinterObject('PrintOrder', id),
'order_id': id
};
return this.http.post<any>(`${environment.apiUrl}/cloverOrders/firePrint`, body);
}
postNewModification(id: string, lineItemID: string, modification: Modification, merchantID: string) {
this.cloverMerchantIdAndTokenSet();
const body = {
// 'merchant_id': localStorage.getItem('merchant_id'),
'merchant_id': this.merchant_id,
'token': this.cloverToken,
'order_id': id,
'lineitem_id': lineItemID,
'modification': modification
};
return this.http.post<any>(`${environment.apiUrl}/cloverOrders/createItemModification`, body);
}