This repository was archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathb1ServiceLayer.js
107 lines (91 loc) · 3.11 KB
/
b1ServiceLayer.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
107
/* Service Layer module to interact with B1 Data */
/* Server Configuration and User Credentials set in environment variables */
/** Environment Variables Required:
* SL_SERVER ->http://12.12.12.21:50001/b1s/v1
*
* Optional Environment variables which have default values:
* B1_USER: -> manager
* B1_PASS -> 1234
* B1_COMP -> SBODEMOUS
*
* */
//Load Node Modules
var req = require('request') // HTTP Client
//Load Local configuration file
const SL_SERVER = process.env.SL_SERVER || "http://<YOUR SERVER>:50001/b1s/v1";
module.exports = {
PostMessage: function (callback) {
return (PostMessage(callback));
}
}
function Connect(callback) {
var uri = SL_SERVER + "/Login"
var resp = {}
//B1 Login Credentials
var data = {
UserName: process.env.B1_USER || "manager",
Password: process.env.B1_PASS || "1234",
CompanyDB: process.env.B1_COMP || "SBODEMOUS"
};
//Set HTTP Request Options
var options = {
uri: uri,
body: JSON.stringify(data)
}
console.log("Connecting to SL on " + uri);
//Make Request
req.post(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
console.log("Connection to SL Successfully \n" + body)
resp.cookie = response.headers['set-cookie']
callback(null, resp);
} else {
callback(response.statusMessage, response);
}
});
}
function SLPost(options, endpoint, callback) {
options.uri = SL_SERVER + endpoint
console.log("Posting " + endpoint + " to " + options.uri)
req.post(options, function (error, response, body) {
if (!error && response.statusCode == 201) {
body = JSON.parse(body);
delete body["odata.metadata"];
callback(null, response, body);
} else {
callback(response.statusMessage, response, null);
}
});
}
function PostMessage(options, callback) {
Connect(function (error, resp) {
if (error) {
console.error("Can't Connect to Service Layer");
console.error(error);
} else {
console.log("Connected Successfully, lets send a message");
var options = {
headers: {
'Cookie': resp.cookie
}
};
options.body = JSON.stringify({
RecipientCollection: [{
SendInternal: "tYES",
UserCode: process.env.B1_USER_ENV || "manager"
}],
Subject: "Dash Button Pressed",
Text: "This is a message from your dash button"
})
//Make Request
SLPost(options, "/Messages", function (error, response, bodymess) {
if (error) {
console.error("Error Sending SL Message \n" + error );
} else {
console.log("Message Sent successfully!")
}
})
}
});
}