-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathedges.js
213 lines (191 loc) · 6.56 KB
/
edges.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { compareVersions } from 'compare-versions';
import { monotoneX } from '../../utils/curve.js';
import interestTypesCodelist from '../../codelists/interestTypes.js';
// This sets the style and shape of the edges using D3 parameters
const edgeConfig = {
style: 'fill: none; stroke: #000; stroke-width: 5px;',
curve: monotoneX,
};
const defaultStroke = 5;
const getInterests = (interests) => {
return interests.reduce((acc, interest) => {
const { type, share, endDate } = interest;
const typeKey = type;
const typeCategory = interestTypesCodelist[type]?.category || '';
const transformedInterest = {
type: typeKey,
share,
category: typeCategory,
};
acc.push(transformedInterest);
return acc;
}, []);
};
const getStroke = (shareValues) => {
const { exact, minimum, exclusiveMinimum, maximum, exclusiveMaximum } = shareValues?.share || {};
if (exact === undefined) {
if (
(minimum !== undefined || exclusiveMinimum !== undefined) &&
(maximum !== undefined || exclusiveMaximum !== undefined)
) {
return ((minimum || exclusiveMinimum) + (maximum || exclusiveMaximum)) / 2 / 10;
} else {
return defaultStroke;
}
} else {
return exact / 10;
}
};
const getText = (shareValues, type) => {
const { exact, minimum, exclusiveMinimum, maximum, exclusiveMaximum } = shareValues?.share || {};
if (exact === undefined) {
if (
(minimum !== undefined || exclusiveMinimum !== undefined) &&
(maximum !== undefined || exclusiveMaximum !== undefined)
) {
return `${type} ${minimum || exclusiveMinimum} - ${maximum || exclusiveMaximum}%`;
} else {
return `${type}`;
}
} else {
return `${type} ${exact}%`;
}
};
export const checkInterests = (interestRelationship) => {
return interestRelationship === 'indirect' || interestRelationship === '' ? true : false;
};
export const getOwnershipEdges = (bodsData) => {
const version = bodsData[0]?.publicationDetails?.bodsVersion || '0.4';
const filteredData = bodsData.filter((statement) => {
if (compareVersions(version, '0.4') >= 0) {
return statement.recordType === 'relationship';
} else {
return statement.statementType === 'ownershipOrControlStatement';
}
});
const mappedData = filteredData.map((statement) => {
const {
statementID = null,
statementId = null,
statementDate = null,
recordId = null,
recordStatus,
recordDetails = null,
subject,
interestedParty,
interests,
} = statement;
const replaces = statement.replacesStatements ? statement.replacesStatements : [];
const interestsData = recordDetails?.interests || interests || [];
const { interestLevel, directOrIndirect } = interestsData
? interestsData[0] || { interestLevel: 'unknown' }
: { interestLevel: 'unknown' };
let interestRelationship, source, target;
if (compareVersions(version, '0.4') >= 0) {
if (directOrIndirect) {
interestRelationship = directOrIndirect;
} else {
if (!interestsData.length) {
interestRelationship = '';
} else {
interestRelationship = 'unknown';
}
}
source = typeof recordDetails.interestedParty === 'string' ? recordDetails.interestedParty : 'unknown';
target = typeof recordDetails.subject === 'string' ? recordDetails.subject : 'unknown';
} else {
if (directOrIndirect) {
interestRelationship = directOrIndirect;
} else if (interestLevel) {
interestRelationship = interestLevel;
} else {
if (!interestsData.length) {
interestRelationship = '';
} else {
interestRelationship = 'unknown';
}
}
source =
interestedParty?.describedByPersonStatement ||
interestedParty?.describedByEntityStatement ||
'unknown';
target = subject.describedByPersonStatement
? subject.describedByPersonStatement
: subject.describedByEntityStatement;
}
const mappedInterests = getInterests(interestsData);
// work out the ownership stroke and text
const unknownStroke = getStroke(mappedInterests.find((item) => item.category === ''));
const shareStroke = getStroke(mappedInterests.find((item) => item.category === 'ownership'));
const shareText = getText(
mappedInterests.find((item) => item.category === 'ownership'),
'Owns'
);
const controlStroke = getStroke(mappedInterests.find((item) => item.category === 'control'));
const controlText = getText(
mappedInterests.find((item) => item.category === 'control'),
'Controls'
);
if (mappedInterests.some((item) => item.category === 'ownership')) {
const arrowheadColour = shareStroke === 0 ? 'black' : '';
const arrowheadShape = `${arrowheadColour}${
mappedInterests.some((item) => item.category === 'control') ? 'Half' : 'Full'
}`;
const strokeValue = shareStroke === 0 ? '#000' : '#652eb1';
const positiveStroke = shareStroke === 0 ? 1 : shareStroke;
edgeConfig.share = {
arrowheadShape,
strokeValue,
positiveStroke,
};
}
if (mappedInterests.some((item) => item.category === 'control')) {
const arrowheadColour = controlStroke === 0 ? 'black' : '';
const arrowheadShape = `${arrowheadColour}${
mappedInterests.some((item) => item.category === 'ownership') ? 'Half' : 'Full'
}`;
const strokeValue = controlStroke === 0 ? '#000' : '#349aee';
const positiveStroke = controlStroke === 0 ? 1 : controlStroke;
edgeConfig.control = {
arrowheadShape,
strokeValue,
positiveStroke,
};
}
if (!mappedInterests.length || mappedInterests.some((item) => item.category === '')) {
edgeConfig.unknown = {
arrowheadShape: 'blackFull',
strokeValue: '#000',
positiveStroke: unknownStroke,
};
}
return {
id: statementId || statementID,
statementDate,
recordId,
recordStatus,
interests: mappedInterests,
interestRelationship,
controlStroke,
controlText,
shareText,
shareStroke,
unknownStroke,
source,
target,
config: { ...edgeConfig },
replaces: replaces,
fullDescription: statement,
description: {
statementDate,
recordId,
interests: recordDetails?.interests || interests || [],
},
};
});
return mappedData;
};
export const getEdges = (data) => {
const ownershipEdges = getOwnershipEdges(data);
return { edges: [...ownershipEdges] };
};