-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcodebeamer.js
134 lines (116 loc) · 3.48 KB
/
codebeamer.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var fetch = require('node-fetch')
var BASE_PATH = 'https://liquidebleiben.codebeamer.com/api/v3'
var HEADERS = {
'Authorization': `Basic ${process.env.CB_BASIC_AUTH}`,
'Content-Type': 'application/json'
};
const SHOW_STATUS_ID = 8;
const DETAILS_STATUS_ID = 10;
/** @type {FundingProgram[]} */
let offers = [];
/** @type {string[]} */
let clusters = [];
/**
* List of columns which are to be shown to the end-user
* @type {{ id: number, name: string, type: 'main' | 'details' }[]}
*/
let displayedColumns = [];
const wikiIndizes = [3136, 3140, 3144, 3146, 3149, 3153, 3192, 3240]
const wikiPages = []
async function retrieveWikiNameAndText(id) {
return await fetch(`${BASE_PATH}/wikipages/${id}`, {
method: 'GET',
headers: HEADERS,
})
.then(res => res.json())
.then(async wikiEntry => ({ name: wikiEntry.name, html: await retrieveWikiAsHtml(id, 'WIKI', wikiEntry.version, wikiEntry.markup)}))
}
async function retrieveWikiAsHtml(id, type, version, value) {
return await fetch(`${BASE_PATH}/projects/2/wiki2html`, {
method: 'POST',
body: JSON.stringify({
contextId: id,
contextVersion: version,
renderingContextType: type,
markup: value
}),
headers: HEADERS,
})
.then(res => res.text());
}
async function retrieveOffers() {
const cbOffers = await fetch(`${BASE_PATH}/trackers/2221/reports/3017/items?page=1&pageSize=500`, {
method: 'GET',
headers: HEADERS,
})
.then(res => res.json())
offers = cbOffers.items.map(item => ({
id: item.item.id,
version: item.item.version,
name: item.item.name,
fields: item.item.customFields
}));
offers.forEach(offer => {
offer.fields.forEach(async field => {
if(field.type === "WikiTextFieldValue") {
field.value = await retrieveWikiAsHtml(offer.id, 'TRACKER_ITEM', offer.version, field.value);
}
})
});
}
async function retrieveColumnsAndClusters() {
const cbSchema = await fetch(`${BASE_PATH}/trackers/2221/schema`, {
method: 'GET',
headers: HEADERS,
})
.then(res => res.json())
displayedColumns = cbSchema
.filter(col => col.mandatoryInStatuses.findIndex(status => [SHOW_STATUS_ID, DETAILS_STATUS_ID].includes(status.id)) > -1)
.map(col => ({ id: col.id, name: col.name, type: col.mandatoryInStatuses.find(status => status.id === SHOW_STATUS_ID) ? 'main' : 'details' }));
clusters = cbSchema.find(col => col.id === 1002).options.map(opt => opt.name).splice(1);
}
async function retrieveDropdownOptions(fieldId) {
const dropdownValues = await fetch(`${BASE_PATH}/trackers/2221/fields/${fieldId}`, {
method: 'GET',
headers: HEADERS,
}).then(res => res.json());
return dropdownValues.options.splice(1).map(opt => ({ id: opt.id.toString(), name: opt.name }));
}
/**
* Return the full list of offers stored in the cache
* @returns {FundingProgram[]}
*/
function getOffers() {
return offers;
}
function getColumns() {
return displayedColumns;
}
function getClusters() {
return clusters;
}
function getDescriptions() {
return wikiPages;
}
/**
* Query for all data which is stored in codebeamer to be saved in the in in-memory cache
*/
function refreshData() {
Promise.all([
retrieveOffers(),
retrieveColumnsAndClusters()
]).catch(err=> {
console.log('err');
})
Promise.all(wikiIndizes.map(async (wp, idx) => {
wikiPages[idx] = await retrieveWikiNameAndText(wp);
}))
}
setInterval(refreshData, 1800000);
refreshData();
module.exports = {
getClusters,
getColumns,
getDescriptions,
getOffers
}