-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.js
70 lines (61 loc) · 2.16 KB
/
requests.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
const fetch = require("node-fetch");
const { restEndpoints } = require("./config");
/**
* Get last jobId found in server
* @param {*} deviceName
*/
const fetchLastJobId = deviceName =>
new Promise(async resolve => {
const postData = {
device_name: deviceName,
};
const res = await fetch(restEndpoints.lastJobId, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, *cors, same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json",
},
redirect: "follow", // manual, *follow, error
referrerPolicy: "no-referrer", // no-referrer, *client
body: JSON.stringify(postData), // body data type must match "Content-Type" header
});
const data = await res.json();
return resolve(data.jobId);
});
/**
* Push all jobs to server
* @param {*} jobs
*/
const pushJobsToServer = jobs =>
new Promise(async resolve => {
// Jobs sorting by timestamp
jobs.sort((a, b) => a.jobTimestamp - b.jobTimestamp);
for (const job of jobs) {
const postData = {
device_name: job.device,
job_type: job.jobType,
job_id: job.jobId,
job_pages: job.jobTotalPages,
job_completed_at: job.jobTimeCompleted,
user_name: job.jobUser,
};
const res = await fetch(restEndpoints.addJob, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, *cors, same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json",
},
redirect: "follow", // manual, *follow, error
referrerPolicy: "no-referrer", // no-referrer, *client
body: JSON.stringify(postData), // body data type must match "Content-Type" header
});
const data = await res.json();
console.log(data);
}
return resolve("Completed.");
});
module.exports = { fetchLastJobId, pushJobsToServer };