This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.js
116 lines (106 loc) · 3.75 KB
/
api.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
108
109
110
111
112
113
114
115
116
const express = require("express");
const router = express.Router();
const util = require("util");
const cron = require("../scheduling/cron.js");
const logger = new (require("../logger/logger.js"))();
const elasticsearchCommunicator = new (require('../communication/elasticsearchcommunicator'))();
const fetchScheduler = new (require("../scheduling/fetchscheduler.js"))(elasticsearchCommunicator);
const librisCommunicator = new (require("../communication/libriscommunicator.js"))();
const dataConverter = new (require("../data/dataconverter.js"))();
const folioCommunicator = new (require("../communication/foliocommunicator.js"))(logger);
const librisFolioDataMover = new (require("../librisfoliodatamover"))(
fetchScheduler,
librisCommunicator,
dataConverter,
folioCommunicator
);
router.post("/InstancesAndHoldingsWithId", async function(req, res) {
try {
const id = req.query.id;
await librisFolioDataMover.moveDataById(id);
res.send(
"Instances and holdings in FOLIO has been updated with data from Libris."
);
} catch (error) {
await logger.error('InstancesAndHoldingsById', error);
res.status(500).send(error.message);
}
});
router.post("/InstancesAndHoldingsWithTimestamps", async function(req, res) {
try {
const from = req.query.from;
const until = req.query.until;
await librisFolioDataMover.moveDataByTimestamps(from, until, false);
res.send(
"Instances and holdings in FOLIO has been updated with data from Libris."
);
} catch (error) {
await logger.error('InstancesAndHoldings', error);
res.status(500).send(error);
}
});
router.post("/InstancesAndHoldings", async function(req, res) {
const id = req.query.id;
const from = req.query.from;
const until = req.query.until;
try {
if (id) {
await librisFolioDataMover.moveDataById(id);
} else {
await librisFolioDataMover.moveDataByTimestamps(from, until, false);
}
res.send(
"Instances and holdings in FOLIO has been updated with data from Libris."
);
} catch (error) {
await logger.error('InstancesAndHoldings', error);
res.status(500).send(error.message);
}
});
router.get("/FetchTimes", async function(req, res) {
try {
const timestamp = await fetchScheduler.getLatestFetchTimestamps();
return res.json(timestamp);
} catch (error) {
await logger.error('FetchTimes:', error);
res.status(500).send(error);
}
});
router.get("/Bookmarklet", function(req, res) {
res.send(
`<a href="javascript:(function(){window.s0=document.createElement('script');window.s0.setAttribute('type','text/javascript');window.s0.setAttribute('src','${process.env.serverurl}/bookmarklet.js?t='+Date.now());document.getElementsByTagName('body')[0].appendChild(window.s0);})();">LIBRIS->FOLIO</a>`
);
});
router.get("/PostAndRedirect", async (req, res) => {
try {
await folioCommunicator.acquireTokenFromFolio();
let librisId = req.query.id;
await librisFolioDataMover.moveDataById(librisId);
let value = await folioCommunicator.instanceExists(librisId);
let i = 0;
while (!value.exists && i < 4) {
i++;
value = await folioCommunicator.instanceExists(librisId);
}
let uriTemp =
"https://chalmers.folio.ebsco.com/inventory/view/%s?sort=title";
let uri = util.format(uriTemp, value.folioId);
if (value.exists) {
res.redirect(uri);
} else {
throw new Error("Record was not found in FOLIO");
}
} catch (error) {
await logger.error('PostAndRedirect:', error);
res.status(500).send(error);
}
});
router.get("/NextDailyRunTime", async (req, res) => {
try {
res.json(cron.nextDailyRun());
} catch (error) {
await logger.error('NextDailyRunTime:', error);
res.status(500).send(error);
}
});
module.exports = router;