Skip to content

Commit 09653f7

Browse files
authored
Merge pull request #7 from ProjectPODER/main
Main
2 parents d9d5331 + 1ac0471 commit 09653f7

7 files changed

Lines changed: 1627 additions & 634 deletions

File tree

LICENSE

Lines changed: 646 additions & 629 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ The implementation of the recordDecompiler function is up to you. In general, th
4444

4545
## Streaming the output
4646

47-
Use a tool such as http://gitlab.rindecuentas.org/equipo-qqw/stream2db/ to stream the decompiled values back into Mongo, or [Logstash](https://www.elastic.co/products/logstash) to stream into ElasticSearch.
47+
Use a tool such as [stream2db](http://gitlab.rindecuentas.org/equipo-qqw/stream2db/) to stream the decompiled values into Mongo or ElasticSearch.

lib/record_types/contractRecord.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
function recordDecompiler(ocid, record) {
2+
return pieces = getContractsFromRecord(record.compiledRelease, record.releases);
3+
}
4+
5+
function getIDFieldName() {
6+
return 'ocid';
7+
}
8+
9+
function getCorrectContractDetails(releases, contract) {
10+
let contract_id = contract.id;
11+
let buyer_id = null;
12+
let details = {
13+
buyer_id: null,
14+
source: [],
15+
sourceRun: []
16+
}
17+
18+
releases.map( (r) => {
19+
let real_contract = r.contracts.filter( (c) => c.id == contract_id );
20+
if(real_contract.length > 0) {
21+
r.parties.map( (p) => {
22+
if(p.roles[0] == 'buyer') details.buyer_id = p.id
23+
} );
24+
25+
let sourceFound = false;
26+
r.source.map( (s) => {
27+
details.source.map( (ds) => {
28+
if(ds.id == s.id) sourceFound = true;
29+
} );
30+
} );
31+
32+
if(!sourceFound) {
33+
details.source.push(...r.source);
34+
details.sourceRun.push(...r.sourceRun);
35+
}
36+
}
37+
} );
38+
39+
return details;
40+
}
41+
42+
function getContractsFromRecord(record, releases) {
43+
let contracts = [];
44+
45+
record.contracts.map( (contract) => {
46+
let details = getCorrectContractDetails(releases, contract);
47+
let buyer_id = details.buyer_id;
48+
let buyer_party = record.parties.filter( (party) => party.id == buyer_id )[0];
49+
50+
let award_id = contract.awardID;
51+
let award = record.awards.filter( (award) => award.id == award_id )[0];
52+
53+
let supplier_ids = [];
54+
award.suppliers.map( (supplier) => supplier_ids.push(supplier.id) );
55+
let supplier_parties = record.parties.filter( (party) => supplier_ids.indexOf(party.id) >= 0 );
56+
57+
let funder_party = null;
58+
let funder_arr = record.parties.filter( (party) => party.roles[0] == "funder" );
59+
if(funder_arr.length > 0) funder_party = funder_arr[0];
60+
61+
let computed_contract = {};
62+
for( var x in record ) {
63+
switch(x) {
64+
case 'parties':
65+
computed_contract.parties = [];
66+
if(buyer_party)
67+
computed_contract.parties.push(buyer_party);
68+
if(supplier_parties.length > 0)
69+
supplier_parties.map( (supplier) => { computed_contract.parties.push(supplier) } );
70+
if(funder_party) {
71+
if(funder_party.name.indexOf(';')) {
72+
let funder_names = funder_party.name.split(';');
73+
let funder_ids = funder_party.id.split(';');
74+
funder_names.map( (f, i) => {
75+
let f_party = JSON.parse(JSON.stringify(funder_party));
76+
f_party.name = f;
77+
f_party.id = funder_ids[i];
78+
computed_contract.parties.push(f_party);
79+
} );
80+
}
81+
else
82+
computed_contract.parties.push(funder_party);
83+
}
84+
break;
85+
case 'buyer':
86+
computed_contract.buyer = { id:buyer_id, name: (buyer_party)? buyer_party.name : null }
87+
break;
88+
case 'awards':
89+
computed_contract.awards = award;
90+
break;
91+
case 'contracts':
92+
computed_contract.contracts = contract;
93+
break;
94+
case 'total_amount':
95+
break;
96+
case 'source':
97+
computed_contract.source = details.source;
98+
break;
99+
case 'sourceRun':
100+
computed_contract.sourceRun = details.sourceRun;
101+
break;
102+
default:
103+
computed_contract[x] = record[x];
104+
break;
105+
}
106+
}
107+
contracts.push(computed_contract);
108+
} );
109+
110+
return contracts;
111+
}
112+
113+
function simplifyObject(obj) {
114+
if(obj == null) return '';
115+
// - Si es un valor, copiarlo
116+
// - Si es objeto, recursar
117+
// - Si es array
118+
// - Si solo tiene un elemento, volver objeto
119+
// - Si tiene más de un elemento
120+
// - Si son objetos, objeto indexado
121+
// - Si son valores, dejarlo como array
122+
let objType = getType(obj);
123+
let tempObj = {};
124+
125+
switch(objType) {
126+
case 'array':
127+
if(obj.length == 1) {
128+
return obj[0];
129+
}
130+
else {
131+
obj.map((item, index) => {
132+
if(getType(item) == 'object') {
133+
tempObj[index] = simplifyObject(item);
134+
}
135+
else {
136+
tempObj[index] = item;
137+
}
138+
});
139+
return tempObj;
140+
}
141+
break;
142+
case 'object':
143+
Object.keys(obj).map((key) => {
144+
tempObj[key] = simplifyObject(obj[key]);
145+
});
146+
return tempObj
147+
default:
148+
return obj;
149+
}
150+
}
151+
152+
// Returns if a value is a string
153+
function isString (value) {
154+
return typeof value === 'string' || value instanceof String;
155+
}
156+
157+
// Returns if a value is an array
158+
function isArray (value) {
159+
return value && typeof value === 'object' && value.constructor === Array;
160+
}
161+
162+
// Returns if a value is an object
163+
function isObject (value) {
164+
return value && typeof value === 'object' && value.constructor === Object;
165+
}
166+
167+
// Return the type of an object using above functions
168+
function getType(obj) {
169+
if( isArray(obj) ) return 'array';
170+
else if( isObject(obj) ) return 'object';
171+
else return isString(obj)? 'string' : typeof obj;
172+
}
173+
174+
module.exports = { recordDecompiler, getIDFieldName };

0 commit comments

Comments
 (0)