-
Notifications
You must be signed in to change notification settings - Fork 8
/
offer-service.js
180 lines (161 loc) · 5.37 KB
/
offer-service.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var { getClusters, getColumns, getOffers } = require('./codebeamer.js');
const FIELD_IDS = {
MIN_AGE: 10000,
MAX_AGE: 10010,
MIN_SALES: 10011,
MAX_SALES: 10012,
MIN_EMPLOYEES: 10013,
MAX_EMPLOYEES: 10014,
MAX_TOTAL_ASSETS: 10015,
LEGAL: 1004,
};
/**
* Filter the complete list of funding programs based on the provided filterParams
* @param {FilterParams} filterParams parameters used for filtering the funding programs
*/
function filterOffers(filterParams) {
let filteredOffers = getOffers();
if (filterParams.state) {
filteredOffers = filteredOffers.filter(off => {
return off.fields
.find(field => field.fieldId === 1000).values
.find(val => val.id == filterParams.state || val.id === 17)
});
}
if (filterParams.trade) {
filteredOffers = filteredOffers.filter(off => {
const tradeField = off.fields.find(field => field.fieldId === 1001);
if (!tradeField) return true;
return !tradeField.values.find(val => val.id == filterParams.trade)
});
}
if (filterParams.age) {
filteredOffers = filteredOffers.filter(off => minMaxFilter(off.fields, FIELD_IDS.MIN_AGE, FIELD_IDS.MAX_AGE, filterParams.age));
}
if (filterParams.sales) {
filteredOffers = filteredOffers.filter(off => {
return minMaxFilter(off.fields, FIELD_IDS.MIN_SALES, FIELD_IDS.MAX_SALES, filterParams.sales) ||
maxFilter(off.fields, FIELD_IDS.MAX_TOTAL_ASSETS, filterParams.totalAssets)
});
}
const getLegalFilter = (offer) => {
return offer.fields.find(f=>f.fieldId == FIELD_IDS.LEGAL);
}
if (filterParams.legal) {
filteredOffers = filteredOffers.filter(offer=>{
let legalFilter = getLegalFilter(offer);
if(legalFilter == undefined) {
return true;
}
return !!legalFilter.values.find(value=>value.id==filterParams.legal);
});
}
if (filterParams.employees) {
filteredOffers = filteredOffers.filter(off => minMaxFilter(off.fields, FIELD_IDS.MIN_EMPLOYEES, FIELD_IDS.MAX_EMPLOYEES, filterParams.employees));
}
return filteredOffers;
}
function maxFilter(rowFields, maxId, filterValue) {
const maxField = rowFields.find(field => field.fieldId === maxId);
if (!maxField) return false;
return maxField.value >= filterValue;
}
function selectFilter(rowFields, maxId, filterValue) {
const maxField = rowFields.find(field => field.fieldId === maxId);
if (!maxField) return false;
return maxField.value >= filterValue;
}
/**
* Filter an by comparing a filter parameter integer value with the lower and upper bounds for a funding program
* @param {any[]} rowFields list of fields which includes the specified IDs
* @param {integer} minId field to be used as lower boundary
* @param {integer} maxId field to be used as upper boundary
* @param {integer} filterValue
*/
function minMaxFilter(rowFields, minId, maxId, filterValue) {
const minField = rowFields.find(field => field.fieldId === minId);
const maxField = rowFields.find(field => field.fieldId === maxId);
if (!minField && !maxField) return true;
if (!minField) return maxField.value >= filterValue;
if (!maxField) return minField.value <= filterValue;
return minField.value <= filterValue && maxField.value >= filterValue;
}
/**
* Structure programs to be uniform
* @param {FundingProgram} offer
* @returns {FundingProgramFormatted}
*/
function formatOffer(offer) {
return {
id: offer.id,
name: offer.name,
fields: {
main: filterFieldsByDisplayType(offer.fields, 'main').map(formatField),
details: filterFieldsByDisplayType(offer.fields, 'details').map(formatField)
}
};
}
/**
* Filter fields for different section granularity (as specified in the codebeamer data base)
* @param {any} fields
* @param {'main' | 'details'} type
* @returns {any[]}
*/
function filterFieldsByDisplayType(fields, type) {
return fields.filter(field => {
const displayField = getColumns().find(col => col.id === field.fieldId)
if (!displayField) return false;
return displayField.type === type;
});
}
/**
* Transform codebeamer field format into uniform application field format
* @param {any} field Semi-structured field
* @returns {FundingProgramField}
*/
function formatField(field) {
let value;
if (field.value !== undefined) {
if (typeof field.value === 'boolean') {
value = field.value ? 'Ja' : 'Nein';
} else {
value = field.value.replace(/[\\\~]/g, '');
}
} else {
value = field.values.map(val => val.name).join(', ');
}
return { id: field.fieldId, name: field.name, value }
}
/**
* Structure funding programs into clusters
* @param {FundingProgram[]} offers
* @returns {FundingProgramCluster[]}
*/
function formatOffersClustered(offers) {
return getClusters().map(clusterName => {
return {
name: clusterName,
offers: offers
.filter(off => off.fields.find(field => field.fieldId === 1002).values.map(val => val.name).includes(clusterName))
.map(off => formatOffer(off, getColumns()))
};
});
}
/**
* Process the retrieval of funding programs
* @param {FilterParams} filterParams
*/
function getOffersCtrl(filterParams) {
return formatOffersClustered(filterOffers(filterParams));
}
function getColumnsCtrl() {
return getColumns().map(col => col.name);
}
function getClustersCtrl() {
return getClusters();
}
module.exports = {
getClusters: getClustersCtrl,
getColumns: getColumnsCtrl,
getOffers: getOffersCtrl,
}