-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple-SAP-API-Hub-client.js
106 lines (94 loc) · 3.01 KB
/
simple-SAP-API-Hub-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
let {
BusinessPartner,
BpContactToAddress
} = require('@sap/cloud-sdk-vdm-business-partner-service');
let {
SalesOrder,
SalesOrderItem,
} = require('@sap/cloud-sdk-vdm-sales-order-service');
/**
* Wrapperclass for the SAP API Hub.
* Fallback solution in case the SAP Graph API does not work.
*/
class SimpleSAPAPIHubClient {
async getCustomerBySalesOrder(salesOrder) {
let businessPartners = await BusinessPartner.requestBuilder()
.getAll()
.select(
BusinessPartner.BUSINESS_PARTNER,
BusinessPartner.BUSINESS_PARTNER_NAME,
BusinessPartner.ORGANIZATION_BP_NAME_1,
BusinessPartner.TO_BUSINESS_PARTNER_ADDRESS
)
.filter(BusinessPartner.BUSINESS_PARTNER.equals(salesOrder.customer.id))
.top(1)
.addCustomHeaders({ APIKey: process.env.SAPAPIHubKey })
.execute({
url: process.env.SAPAPIHubDestination,
});
return this.mapToSAPGraphCustomer(businessPartners[0]);
}
async getOrderByOrderNumber(orderNumber) {
let salesOrders = await SalesOrder.requestBuilder()
.getAll()
.select(SalesOrder.ALL_FIELDS, SalesOrder.TO_ITEM)
.filter(SalesOrder.SALES_ORDER.equals(orderNumber))
.top(5)
.addCustomHeaders({ APIKey: process.env.SAPAPIHubKey })
.execute({
url: process.env.SAPAPIHubDestination,
});
return [this.mapToSAPGraphSalesOrder(salesOrders[0])];
}
mapToSAPGraphSalesOrder(salesOrder){
//ToDo read process status text
let graphSalesOrder = {
"displayId":salesOrder.salesOrder,
"customer":{
"id": salesOrder.soldToParty
},
"distributionChannel":{
"name":salesOrder.distributionChannel
},
"division":{
"name":salesOrder.organizationDivision
},
"orderDate": salesOrder.salesOrderDate._d.toISOString().substr(0,19) + 'Z',
"netAmount":salesOrder.totalNetAmount,
"currency":{
"code":salesOrder.transactionCurrency
},
"processingStatus":{
"code": salesOrder.overallSdProcessStatus,
"name":"Open"
},
"items":[]
}
salesOrder.toItem.forEach(element => {
graphSalesOrder.items.push({"text": element.salesOrderItemText,"netAmount": element.netAmount.c[0], "quantity": element.requestedQuantity.c[0]});
});
return graphSalesOrder;
}
mapToSAPGraphCustomer(businessPartner){
//ToDo select phoneNumber
//BpContactToAddress.TO_PHONE_NUMBER
let phone = '112233';
let graphCustomer = {
"displayId":businessPartner.businessPartner,
"addressData":[{
"phoneNumbers":[
{"number":phone}
]
}],
"organization":{
"nameDetails":{
"formattedOrgNameLine1":businessPartner.organizationBpName1
}
}
}
return graphCustomer;
}
}
exports.SimpleSAPAPIHubClient = SimpleSAPAPIHubClient;