@natlibfi/melinda-rest-api-commons 2.3.2-alpha.4 → 2.3.2-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/fix-constants.js +96 -0
- package/dist/helpers/fix-constants.js.map +1 -0
- package/dist/helpers/fix-handle-tempurns.js +161 -0
- package/dist/helpers/fix-handle-tempurns.js.map +1 -0
- package/dist/helpers/fix-strip-f884s.js +91 -0
- package/dist/helpers/fix-strip-f884s.js.map +1 -0
- package/dist/helpers/fix-utils.js +88 -0
- package/dist/helpers/fix-utils.js.map +1 -0
- package/dist/helpers/fix.js +124 -352
- package/dist/helpers/fix.js.map +1 -1
- package/dist/helpers/fix.spec.js +42 -251
- package/dist/helpers/fix.spec.js.map +1 -1
- package/dist/mongoLog.js +1 -1
- package/dist/mongoLog.js.map +1 -1
- package/package.json +8 -7
package/dist/helpers/fix.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
fixRecord: true
|
|
8
|
+
};
|
|
7
9
|
exports.fixRecord = fixRecord;
|
|
8
10
|
|
|
9
11
|
var _marcRecord = require("@natlibfi/marc-record");
|
|
@@ -12,6 +14,24 @@ var _melindaBackendCommons = require("@natlibfi/melinda-backend-commons");
|
|
|
12
14
|
|
|
13
15
|
var _debug = _interopRequireDefault(require("debug"));
|
|
14
16
|
|
|
17
|
+
var _fixHandleTempurns = require("./fix-handle-tempurns");
|
|
18
|
+
|
|
19
|
+
var _fixStripF884s = require("./fix-strip-f884s");
|
|
20
|
+
|
|
21
|
+
var _fixConstants = require("./fix-constants");
|
|
22
|
+
|
|
23
|
+
Object.keys(_fixConstants).forEach(function (key) {
|
|
24
|
+
if (key === "default" || key === "__esModule") return;
|
|
25
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
26
|
+
if (key in exports && exports[key] === _fixConstants[key]) return;
|
|
27
|
+
Object.defineProperty(exports, key, {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () {
|
|
30
|
+
return _fixConstants[key];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
15
35
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
36
|
|
|
17
37
|
/**
|
|
@@ -41,381 +61,133 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
41
61
|
* for the JavaScript code in this file.
|
|
42
62
|
*
|
|
43
63
|
*/
|
|
64
|
+
const logger = (0, _melindaBackendCommons.createLogger)();
|
|
65
|
+
const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons:fixRecord');
|
|
66
|
+
const debugData = debug.extend('data');
|
|
67
|
+
|
|
44
68
|
function fixRecord(record, settings = {}) {
|
|
45
|
-
const logger = (0, _melindaBackendCommons.createLogger)();
|
|
46
|
-
const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons:fixRecord');
|
|
47
|
-
const debugData = debug.extend('data');
|
|
48
69
|
logger.verbose(`We will apply formatting to the record according to settings: ${JSON.stringify(settings)}`);
|
|
49
70
|
|
|
50
71
|
const newRecord = _marcRecord.MarcRecord.clone(record, {
|
|
51
72
|
subfieldValues: false
|
|
52
73
|
});
|
|
53
74
|
|
|
54
|
-
debugData(`settings: ${JSON.stringify(settings)}`);
|
|
55
|
-
|
|
56
|
-
generateMissingSIDs(generateMissingSIDsOptions);
|
|
57
|
-
const replacePrefixesOptions = settings.replacePrefixes || [];
|
|
58
|
-
debugData(`replacePrefixesOptions: ${JSON.stringify(replacePrefixesOptions)}`);
|
|
59
|
-
replacePrefixesOptions.forEach(options => {
|
|
60
|
-
replacePrefixes(options);
|
|
61
|
-
});
|
|
62
|
-
handleTempUrns(settings.handleTempUrns);
|
|
63
|
-
stripF884s(settings.stripF884s);
|
|
64
|
-
return newRecord.toObject(); // Replace prefix in all specified subfields
|
|
65
|
-
|
|
66
|
-
function replacePrefixes(options) {
|
|
67
|
-
const {
|
|
68
|
-
oldPrefix,
|
|
69
|
-
newPrefix,
|
|
70
|
-
prefixReplaceCodes
|
|
71
|
-
} = options;
|
|
72
|
-
debug(`Replacing ${oldPrefix} with ${newPrefix} in subfields ${prefixReplaceCodes}`);
|
|
73
|
-
const pattern = `(${oldPrefix})`;
|
|
74
|
-
const replacement = `(${newPrefix})`;
|
|
75
|
-
newRecord.getDatafields().forEach(field => {
|
|
76
|
-
field.subfields.filter(({
|
|
77
|
-
code
|
|
78
|
-
}) => prefixReplaceCodes.includes(code)).forEach(subfield => {
|
|
79
|
-
subfield.value = subfield.value.replace(pattern, replacement); // eslint-disable-line functional/immutable-data
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
}
|
|
75
|
+
debugData(`settings: ${JSON.stringify(settings)}`); // We should handle these in some prettier way ...
|
|
76
|
+
// Fix 1, generate SIDs
|
|
83
77
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const f035Filters = options && options.f035Filters ? options.f035Filters : [];
|
|
87
|
-
const fSIDFilters = options && options.fSIDFilters ? options.fSIDFilters : [];
|
|
78
|
+
const generateMissingSIDsOptions = settings.generateMissingSIDs || undefined;
|
|
79
|
+
const newRecord1 = generateMissingSIDs(newRecord, generateMissingSIDsOptions); // Fix 2, replace identifier prefixes
|
|
88
80
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
81
|
+
const replacePrefixesOptions = settings.replacePrefixes || [];
|
|
82
|
+
const newRecord2 = replaceAllPrefixes(newRecord1, replacePrefixesOptions); // Fix 3, handle temporary URNs from legal deposit record import
|
|
93
83
|
|
|
94
|
-
|
|
95
|
-
const fSIDs = sidsToSidInfo(newRecord.get(/^SID$/u));
|
|
96
|
-
const sidsToBeAdded = genNewSids(fSIDs.length === 0 ? f035s // test that SIDs are not there yet
|
|
97
|
-
: f035s.filter(f035SidInfo => fSIDs.some(fSIDInfo => f035SidInfo.SID !== fSIDInfo.SID && f035SidInfo.value !== fSIDInfo.value))); // Add new SIDs
|
|
98
|
-
|
|
99
|
-
return sidsToBeAdded.forEach(sidField => newRecord.insertField(sidField));
|
|
100
|
-
|
|
101
|
-
function genNewSids(sidsToBeAdded) {
|
|
102
|
-
return sidsToBeAdded.map(sidInfo => ({
|
|
103
|
-
tag: 'SID',
|
|
104
|
-
ind1: ' ',
|
|
105
|
-
ind2: ' ',
|
|
106
|
-
subfields: [{
|
|
107
|
-
code: 'c',
|
|
108
|
-
value: sidInfo.value
|
|
109
|
-
}, {
|
|
110
|
-
code: 'b',
|
|
111
|
-
value: sidInfo.SID
|
|
112
|
-
}]
|
|
113
|
-
}));
|
|
114
|
-
}
|
|
84
|
+
const newRecord3 = (0, _fixHandleTempurns.handleTempUrns)(newRecord2, settings.handleTempUrns); // Fix 4, strip extra field 884s
|
|
115
85
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}) => {
|
|
120
|
-
const [SID] = subfields.filter(sub => sub.code === 'b' && fSIDFilters.includes(sub.value)).map(sub => sub.value);
|
|
121
|
-
const [value] = subfields.filter(sub => sub.code === 'c' && sub.value !== undefined).map(sub => sub.value);
|
|
122
|
-
|
|
123
|
-
if (SID && value) {
|
|
124
|
-
return {
|
|
125
|
-
SID,
|
|
126
|
-
value
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return undefined;
|
|
131
|
-
}).filter(value => value !== undefined);
|
|
132
|
-
}
|
|
86
|
+
const newRecord4 = (0, _fixStripF884s.stripF884s)(newRecord3, settings.stripF884s);
|
|
87
|
+
return newRecord4.toObject();
|
|
88
|
+
}
|
|
133
89
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const [wantedSub] = subfields.filter(sub => sub.code === 'a' && f035Filters.some(regexp => regexp.test(sub.value)));
|
|
139
|
-
|
|
140
|
-
if (wantedSub) {
|
|
141
|
-
return {
|
|
142
|
-
SID: fSIDFilters[f035Filters.findIndex(regexp => regexp.test(wantedSub.value))],
|
|
143
|
-
value: wantedSub.value.slice(wantedSub.value.indexOf(')') + 1)
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return undefined;
|
|
148
|
-
}).filter(value => value !== undefined);
|
|
149
|
-
}
|
|
90
|
+
function replaceAllPrefixes(record, replacePrefixesOptions) {
|
|
91
|
+
if (replacePrefixesOptions.length < 1) {
|
|
92
|
+
debug(`NOT running replacePrefixes, no options`);
|
|
93
|
+
return record;
|
|
150
94
|
}
|
|
151
95
|
|
|
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
|
-
// NOTE: this sorts MELINDA-f884s after possible other f884s
|
|
180
|
-
|
|
181
|
-
newRecord.removeFields(f884Melindas);
|
|
182
|
-
newRecord.insertFields(uniqFields);
|
|
183
|
-
return; // Keep just first instance of each similar field, compare without subfield with subfieldCode
|
|
184
|
-
|
|
185
|
-
function uniqWithOutSubfield(fields, subfieldCode) {
|
|
186
|
-
return fields.reduce((uniq, field) => {
|
|
187
|
-
if (!uniq.some(f => _marcRecord.MarcRecord.isEqual(removeSubfield(f, subfieldCode), removeSubfield(field, subfieldCode)))) {
|
|
188
|
-
// eslint-disable-line functional/no-conditional-statement
|
|
189
|
-
uniq.push(field); // eslint-disable-line functional/immutable-data
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return uniq;
|
|
193
|
-
}, []);
|
|
194
|
-
} // sort fields by value of each fields first subfield with subfielCode
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
function sortFieldsBySubfieldValue(fields, subfieldCode) {
|
|
198
|
-
return [...fields].sort((a, b) => {
|
|
199
|
-
const a1value = getFirstSubfieldValue(a, subfieldCode);
|
|
200
|
-
const b1value = getFirstSubfieldValue(b, subfieldCode);
|
|
201
|
-
|
|
202
|
-
if (a1value && !b1value) {
|
|
203
|
-
return -1;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (!a1value && b1value) {
|
|
207
|
-
return 1;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
if (a1value > b1value) {
|
|
211
|
-
return 1;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (b1value > a1value) {
|
|
215
|
-
return -1;
|
|
216
|
-
}
|
|
96
|
+
debug(`Running replacePrefixes fixer`);
|
|
97
|
+
debugData(`replacePrefixesOptions: ${JSON.stringify(replacePrefixesOptions)}`);
|
|
98
|
+
replacePrefixesOptions.forEach(options => {
|
|
99
|
+
replacePrefix(record, options);
|
|
100
|
+
});
|
|
101
|
+
return record;
|
|
102
|
+
} // Replace prefix in all specified subfields
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
function replacePrefix(record, options) {
|
|
106
|
+
const {
|
|
107
|
+
oldPrefix,
|
|
108
|
+
newPrefix,
|
|
109
|
+
prefixReplaceCodes
|
|
110
|
+
} = options;
|
|
111
|
+
debug(`Replacing ${oldPrefix} with ${newPrefix} in subfields ${prefixReplaceCodes}`);
|
|
112
|
+
const pattern = `(${oldPrefix})`;
|
|
113
|
+
const replacement = `(${newPrefix})`;
|
|
114
|
+
record.getDatafields().forEach(field => {
|
|
115
|
+
field.subfields.filter(({
|
|
116
|
+
code
|
|
117
|
+
}) => prefixReplaceCodes.includes(code)).forEach(subfield => {
|
|
118
|
+
subfield.value = subfield.value.replace(pattern, replacement); // eslint-disable-line functional/immutable-data
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
return record;
|
|
122
|
+
}
|
|
217
123
|
|
|
218
|
-
|
|
219
|
-
|
|
124
|
+
function generateMissingSIDs(record, options) {
|
|
125
|
+
const f035Filters = options && options.f035Filters ? options.f035Filters : [];
|
|
126
|
+
const fSIDFilters = options && options.fSIDFilters ? options.fSIDFilters : [];
|
|
220
127
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
}
|
|
128
|
+
if (f035Filters.length !== fSIDFilters.length || f035Filters.length < 1 || fSIDFilters.length < 1) {
|
|
129
|
+
debug(`NOT running generateMissingsSIDs, no options`);
|
|
130
|
+
return record;
|
|
226
131
|
}
|
|
227
132
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
133
|
+
debug(`Running generateMissingSIDs fixer`);
|
|
134
|
+
debugData(`generateMissingSIDs options: ${JSON.stringify(options)}`);
|
|
135
|
+
const f035s = f035ToSidInfo(record.get(/^035$/u));
|
|
136
|
+
const fSIDs = sidsToSidInfo(record.get(/^SID$/u));
|
|
137
|
+
const sidsToBeAdded = genNewSids(fSIDs.length === 0 ? f035s // test that SIDs are not there yet
|
|
138
|
+
: f035s.filter(f035SidInfo => fSIDs.some(fSIDInfo => f035SidInfo.SID !== fSIDInfo.SID && f035SidInfo.value !== fSIDInfo.value))); // Add new SIDs
|
|
139
|
+
|
|
140
|
+
sidsToBeAdded.forEach(sidField => record.insertField(sidField));
|
|
141
|
+
return record;
|
|
142
|
+
|
|
143
|
+
function genNewSids(sidsToBeAdded) {
|
|
144
|
+
return sidsToBeAdded.map(sidInfo => ({
|
|
145
|
+
tag: 'SID',
|
|
146
|
+
ind1: ' ',
|
|
147
|
+
ind2: ' ',
|
|
148
|
+
subfields: [{
|
|
149
|
+
code: 'c',
|
|
150
|
+
value: sidInfo.value
|
|
151
|
+
}, {
|
|
152
|
+
code: 'b',
|
|
153
|
+
value: sidInfo.SID
|
|
154
|
+
}]
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
233
157
|
|
|
234
|
-
|
|
235
|
-
|
|
158
|
+
function sidsToSidInfo(SIDs) {
|
|
159
|
+
return SIDs.flatMap(({
|
|
160
|
+
subfields
|
|
161
|
+
}) => {
|
|
162
|
+
const [SID] = subfields.filter(sub => sub.code === 'b' && fSIDFilters.includes(sub.value)).map(sub => sub.value);
|
|
163
|
+
const [value] = subfields.filter(sub => sub.code === 'c' && sub.value !== undefined).map(sub => sub.value);
|
|
164
|
+
|
|
165
|
+
if (SID && value) {
|
|
166
|
+
return {
|
|
167
|
+
SID,
|
|
168
|
+
value
|
|
169
|
+
};
|
|
236
170
|
}
|
|
237
171
|
|
|
238
|
-
return
|
|
239
|
-
|
|
240
|
-
ind1: field.ind1,
|
|
241
|
-
ind2: field.ind2,
|
|
242
|
-
subfields: filteredSubfields
|
|
243
|
-
};
|
|
244
|
-
} // return controlFields as is
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
return field;
|
|
248
|
-
} // eslint-disable-next-line max-statements
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
function handleTempUrns(options) {
|
|
252
|
-
debugData(`Options for handleTempUrns: ${JSON.stringify(options)}`);
|
|
253
|
-
|
|
254
|
-
if (options !== true) {
|
|
255
|
-
return;
|
|
256
|
-
} // If we have an non-temp URN, we can delete tempURNs, otherwise we should delete the temp subfield from URN
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
const hasURN = f => f.tag === '856' && f.subfields.some(({
|
|
260
|
-
code,
|
|
261
|
-
value
|
|
262
|
-
}) => code === 'u' && /urn.fi/u.test(value));
|
|
263
|
-
|
|
264
|
-
const hasTempSubfield = f => f.subfields.some(({
|
|
265
|
-
code,
|
|
266
|
-
value
|
|
267
|
-
}) => code === '9' && /^MELINDA<TEMP>$/u.test(value));
|
|
268
|
-
|
|
269
|
-
const hasNoTempSubfield = f => !f.subfields.some(({
|
|
270
|
-
code,
|
|
271
|
-
value
|
|
272
|
-
}) => code === '9' && /^MELINDA<TEMP>$/u.test(value));
|
|
273
|
-
|
|
274
|
-
const f856sUrn = newRecord.fields.filter(hasURN);
|
|
275
|
-
const f856sUrnsWithTempSubfields = f856sUrn.filter(hasTempSubfield);
|
|
276
|
-
const f856sUrnsWithNoTempSubfields = f856sUrn.filter(hasNoTempSubfield);
|
|
277
|
-
debugData(`URN f856s: ${JSON.stringify(f856sUrn)}`);
|
|
278
|
-
debugData(`URN f856s with temp subfield: ${JSON.stringify(f856sUrnsWithTempSubfields)}`); // None of the URNs has temp subfields, we don't need to do anything
|
|
279
|
-
|
|
280
|
-
if (f856sUrnsWithTempSubfields.length < 1) {
|
|
281
|
-
debug(`No Urns with temp subfield`);
|
|
282
|
-
return;
|
|
283
|
-
} // Do we have an existing URN with legal deposit subfields, if we have, we can remove the temp URN field(s)
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const existingLegalDepositURN = validateLD(f856sUrnsWithNoTempSubfields);
|
|
287
|
-
debug(`existingLegalDepositURN: ${existingLegalDepositURN}`);
|
|
288
|
-
|
|
289
|
-
if (existingLegalDepositURN) {
|
|
290
|
-
debug(`We have an existing LD URN, we can delete temp fields (${f856sUrnsWithTempSubfields.length})`);
|
|
291
|
-
newRecord.removeFields(f856sUrnsWithTempSubfields);
|
|
292
|
-
return;
|
|
293
|
-
} // We do not have an existing legalDepositURN, we should use the tempURN and remove the temp subfields from it
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (!existingLegalDepositURN && f856sUrnsWithTempSubfields.length > 0) {
|
|
297
|
-
//debug(`All Urns (${f856sUrn.length}) have a temp subfield`);
|
|
298
|
-
debugData(`Original temp URNs: (${JSON.stringify(f856sUrnsWithTempSubfields)})`);
|
|
299
|
-
const fixedFields = f856sUrnsWithTempSubfields.map(removeTempSubfield).filter(field => field);
|
|
300
|
-
debug(`We removed temp subfields: (${JSON.stringify(fixedFields)})`);
|
|
301
|
-
newRecord.removeFields(f856sUrnsWithTempSubfields);
|
|
302
|
-
newRecord.insertFields(fixedFields);
|
|
303
|
-
return;
|
|
304
|
-
} // Should we check that the non-temp URN has 2nd ind '0' - meaning that the URN handles the actual resource itself?
|
|
305
|
-
|
|
306
|
-
} // ---- LD-functions below are same as in https://github.com/NatLibFi/marc-record-validators-melinda/blob/feature-updates-to-urn/src/urn.js
|
|
307
|
-
// we propably should develop the urn/legaldeposit -validator to use also here
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
function validateLD(f856sUrn) {
|
|
311
|
-
debug(`Validating the existence of legal deposit subfields`);
|
|
312
|
-
const LD_SUBFIELDS = [{
|
|
313
|
-
code: 'z',
|
|
314
|
-
value: 'Käytettävissä vapaakappalekirjastoissa'
|
|
315
|
-
}, {
|
|
316
|
-
code: '5',
|
|
317
|
-
value: 'FI-Vapaa'
|
|
318
|
-
}];
|
|
319
|
-
const f856sUrnWithLdSubfields = f856sUrn.filter(field => fieldHasLDSubfields(field, LD_SUBFIELDS));
|
|
320
|
-
|
|
321
|
-
if (f856sUrnWithLdSubfields.length > 0) {
|
|
322
|
-
debug(`Record has ${f856sUrnWithLdSubfields.length} URN fields with all necessary legal deposit subfields`);
|
|
323
|
-
debugData(`f856sUrnWithLdSubfields: ${JSON.stringify(f856sUrnWithLdSubfields)}`);
|
|
324
|
-
return true;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
return false;
|
|
172
|
+
return undefined;
|
|
173
|
+
}).filter(value => value !== undefined);
|
|
328
174
|
}
|
|
329
175
|
|
|
330
|
-
function
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const filteredSubfields = field.subfields.filter(sf => sf.code !== '9' && !/^MELINDA<TEMP>$/u.test(sf.value)); // Remove whole field if there are no subfields left
|
|
342
|
-
|
|
343
|
-
if (filteredSubfields.length < 1) {
|
|
344
|
-
return false;
|
|
176
|
+
function f035ToSidInfo(f035s) {
|
|
177
|
+
return f035s.flatMap(({
|
|
178
|
+
subfields
|
|
179
|
+
}) => {
|
|
180
|
+
const [wantedSub] = subfields.filter(sub => sub.code === 'a' && f035Filters.some(regexp => regexp.test(sub.value)));
|
|
181
|
+
|
|
182
|
+
if (wantedSub) {
|
|
183
|
+
return {
|
|
184
|
+
SID: fSIDFilters[f035Filters.findIndex(regexp => regexp.test(wantedSub.value))],
|
|
185
|
+
value: wantedSub.value.slice(wantedSub.value.indexOf(')') + 1)
|
|
186
|
+
};
|
|
345
187
|
}
|
|
346
188
|
|
|
347
|
-
return
|
|
348
|
-
|
|
349
|
-
ind1: field.ind1,
|
|
350
|
-
ind2: field.ind2,
|
|
351
|
-
subfields: filteredSubfields
|
|
352
|
-
};
|
|
353
|
-
} // return controlFields as is
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return field;
|
|
189
|
+
return undefined;
|
|
190
|
+
}).filter(value => value !== undefined);
|
|
357
191
|
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
const BIB_F035_TO_SID = {
|
|
362
|
-
f035Filters: [/^\(FI-BTJ\)/u, /^\(FI-TATI\)/u],
|
|
363
|
-
fSIDFilters: ['FI-BTJ', 'tati']
|
|
364
|
-
}; // There is somewhere other implementations of this same standardPrefixes to alephInternalPrefixes normalization
|
|
365
|
-
|
|
366
|
-
exports.BIB_F035_TO_SID = BIB_F035_TO_SID;
|
|
367
|
-
const REPLACE_PREFIXES = [{
|
|
368
|
-
oldPrefix: 'FI-MELINDA',
|
|
369
|
-
newPrefix: 'FIN01',
|
|
370
|
-
prefixReplaceCodes: ['w']
|
|
371
|
-
}, {
|
|
372
|
-
oldPrefix: 'FI-ASTERI-S',
|
|
373
|
-
newPrefix: 'FIN10',
|
|
374
|
-
prefixReplaceCodes: ['0']
|
|
375
|
-
}, {
|
|
376
|
-
oldPrefix: 'FI-ASTERI-N',
|
|
377
|
-
newPrefix: 'FIN11',
|
|
378
|
-
prefixReplaceCodes: ['0']
|
|
379
|
-
}, {
|
|
380
|
-
oldPrefix: 'FI-ASTERI-A',
|
|
381
|
-
newPrefix: 'FIN12',
|
|
382
|
-
prefixReplaceCodes: ['0']
|
|
383
|
-
}, {
|
|
384
|
-
oldPrefix: 'FI-ASTERI-W',
|
|
385
|
-
newPrefix: 'FIN13',
|
|
386
|
-
prefixReplaceCodes: ['0']
|
|
387
|
-
}]; // If placed in config.js testing needs envs
|
|
388
|
-
// BIB_FORMAT_SETTINGS is outdated, use other settings instead
|
|
389
|
-
|
|
390
|
-
exports.REPLACE_PREFIXES = REPLACE_PREFIXES;
|
|
391
|
-
const BIB_FORMAT_SETTINGS = {
|
|
392
|
-
replacePrefixes: REPLACE_PREFIXES,
|
|
393
|
-
generateMissingSIDs: BIB_F035_TO_SID
|
|
394
|
-
};
|
|
395
|
-
exports.BIB_FORMAT_SETTINGS = BIB_FORMAT_SETTINGS;
|
|
396
|
-
const BIB_PREVALIDATION_FIX_SETTINGS = {
|
|
397
|
-
generateMissingSIDs: BIB_F035_TO_SID
|
|
398
|
-
};
|
|
399
|
-
exports.BIB_PREVALIDATION_FIX_SETTINGS = BIB_PREVALIDATION_FIX_SETTINGS;
|
|
400
|
-
const BIB_POSTMERGE_FIX_SETTINGS = {
|
|
401
|
-
handleTempUrns: true,
|
|
402
|
-
stripF884s: true
|
|
403
|
-
};
|
|
404
|
-
exports.BIB_POSTMERGE_FIX_SETTINGS = BIB_POSTMERGE_FIX_SETTINGS;
|
|
405
|
-
const BIB_PREIMPORT_FIX_SETTINGS = {
|
|
406
|
-
replacePrefixes: REPLACE_PREFIXES
|
|
407
|
-
};
|
|
408
|
-
exports.BIB_PREIMPORT_FIX_SETTINGS = BIB_PREIMPORT_FIX_SETTINGS;
|
|
409
|
-
const AUTNAME_PREIMPORT_FIX_SETTINGS = {
|
|
410
|
-
replacePrefixes: REPLACE_PREFIXES
|
|
411
|
-
};
|
|
412
|
-
exports.AUTNAME_PREIMPORT_FIX_SETTINGS = AUTNAME_PREIMPORT_FIX_SETTINGS;
|
|
413
|
-
const BIB_HANDLE_TEMP_URNS_SETTINGS = {
|
|
414
|
-
handleTempUrns: true
|
|
415
|
-
};
|
|
416
|
-
exports.BIB_HANDLE_TEMP_URNS_SETTINGS = BIB_HANDLE_TEMP_URNS_SETTINGS;
|
|
417
|
-
const BIB_STRIP_F884S_SETTINGS = {
|
|
418
|
-
stripF884s: true
|
|
419
|
-
};
|
|
420
|
-
exports.BIB_STRIP_F884S_SETTINGS = BIB_STRIP_F884S_SETTINGS;
|
|
192
|
+
}
|
|
421
193
|
//# sourceMappingURL=fix.js.map
|
package/dist/helpers/fix.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fix.js","names":["fixRecord","record","settings","logger","createLogger","debug","createDebugLogger","debugData","extend","verbose","JSON","stringify","newRecord","MarcRecord","clone","subfieldValues","generateMissingSIDsOptions","generateMissingSIDs","undefined","replacePrefixesOptions","replacePrefixes","forEach","options","handleTempUrns","stripF884s","toObject","oldPrefix","newPrefix","prefixReplaceCodes","pattern","replacement","getDatafields","field","subfields","filter","code","includes","subfield","value","replace","f035Filters","fSIDFilters","length","f035s","f035ToSidInfo","get","fSIDs","sidsToSidInfo","sidsToBeAdded","genNewSids","f035SidInfo","some","fSIDInfo","SID","sidField","insertField","map","sidInfo","tag","ind1","ind2","SIDs","flatMap","sub","wantedSub","regexp","test","findIndex","slice","indexOf","isMelindaf884","f","f884Melindas","fields","sortedFields","sortFieldsBySubfieldValue","uniqFields","uniqWithOutSubfield","removeFields","insertFields","subfieldCode","reduce","uniq","isEqual","removeSubfield","push","sort","a","b","a1value","getFirstSubfieldValue","b1value","subs","subf","isNaN","parseInt","filteredSubfields","sf","hasURN","hasTempSubfield","hasNoTempSubfield","f856sUrn","f856sUrnsWithTempSubfields","f856sUrnsWithNoTempSubfields","existingLegalDepositURN","validateLD","fixedFields","removeTempSubfield","LD_SUBFIELDS","f856sUrnWithLdSubfields","fieldHasLDSubfields","ldSubfields","every","ldsf","BIB_F035_TO_SID","REPLACE_PREFIXES","BIB_FORMAT_SETTINGS","BIB_PREVALIDATION_FIX_SETTINGS","BIB_POSTMERGE_FIX_SETTINGS","BIB_PREIMPORT_FIX_SETTINGS","AUTNAME_PREIMPORT_FIX_SETTINGS","BIB_HANDLE_TEMP_URNS_SETTINGS","BIB_STRIP_F884S_SETTINGS"],"sources":["../../src/helpers/fix.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport createDebugLogger from 'debug';\n\nexport function fixRecord(record, settings = {}) {\n const logger = createLogger();\n const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:fixRecord');\n const debugData = debug.extend('data');\n\n logger.verbose(`We will apply formatting to the record according to settings: ${JSON.stringify(settings)}`);\n const newRecord = MarcRecord.clone(record, {subfieldValues: false});\n debugData(`settings: ${JSON.stringify(settings)}`);\n\n const generateMissingSIDsOptions = settings.generateMissingSIDs || undefined;\n generateMissingSIDs(generateMissingSIDsOptions);\n\n const replacePrefixesOptions = settings.replacePrefixes || [];\n debugData(`replacePrefixesOptions: ${JSON.stringify(replacePrefixesOptions)}`);\n\n replacePrefixesOptions.forEach(options => {\n replacePrefixes(options);\n });\n\n handleTempUrns(settings.handleTempUrns);\n stripF884s(settings.stripF884s);\n\n return newRecord.toObject();\n\n // Replace prefix in all specified subfields\n function replacePrefixes(options) {\n const {oldPrefix, newPrefix, prefixReplaceCodes} = options;\n debug(`Replacing ${oldPrefix} with ${newPrefix} in subfields ${prefixReplaceCodes}`);\n const pattern = `(${oldPrefix})`;\n const replacement = `(${newPrefix})`;\n newRecord.getDatafields()\n .forEach(field => {\n field.subfields\n .filter(({code}) => prefixReplaceCodes.includes(code))\n .forEach(subfield => {\n subfield.value = subfield.value.replace(pattern, replacement); // eslint-disable-line functional/immutable-data\n });\n });\n }\n\n\n function generateMissingSIDs(options) {\n debugData(`generateMissingSIDs options: ${JSON.stringify(options)}`);\n\n const f035Filters = options && options.f035Filters ? options.f035Filters : [];\n const fSIDFilters = options && options.fSIDFilters ? options.fSIDFilters : [];\n\n if (f035Filters.length !== fSIDFilters.length || f035Filters.length < 1 || fSIDFilters.length < 1) {\n debug(`No sane options for generating missing SIDs`);\n return;\n }\n\n const f035s = f035ToSidInfo(newRecord.get(/^035$/u));\n const fSIDs = sidsToSidInfo(newRecord.get(/^SID$/u));\n\n const sidsToBeAdded = genNewSids(fSIDs.length === 0\n ? f035s\n // test that SIDs are not there yet\n : f035s.filter(f035SidInfo => fSIDs.some(fSIDInfo => f035SidInfo.SID !== fSIDInfo.SID && f035SidInfo.value !== fSIDInfo.value)));\n\n // Add new SIDs\n return sidsToBeAdded.forEach(sidField => newRecord.insertField(sidField));\n\n function genNewSids(sidsToBeAdded) {\n return sidsToBeAdded.map(sidInfo => ({tag: 'SID', ind1: ' ', ind2: ' ', subfields: [{code: 'c', value: sidInfo.value}, {code: 'b', value: sidInfo.SID}]}));\n }\n\n function sidsToSidInfo(SIDs) {\n return SIDs.flatMap(({subfields}) => {\n const [SID] = subfields.filter(sub => sub.code === 'b' && fSIDFilters.includes(sub.value)).map(sub => sub.value);\n const [value] = subfields.filter(sub => sub.code === 'c' && sub.value !== undefined).map(sub => sub.value);\n\n if (SID && value) {\n return {\n SID,\n value\n };\n }\n\n return undefined;\n }).filter(value => value !== undefined);\n }\n\n function f035ToSidInfo(f035s) {\n return f035s.flatMap(({subfields}) => {\n const [wantedSub] = subfields.filter(sub => sub.code === 'a' && f035Filters.some(regexp => regexp.test(sub.value)));\n\n if (wantedSub) {\n return {\n SID: fSIDFilters[f035Filters.findIndex(regexp => regexp.test(wantedSub.value))],\n value: wantedSub.value.slice(wantedSub.value.indexOf(')') + 1)\n };\n }\n\n return undefined;\n }).filter(value => value !== undefined);\n }\n }\n\n function stripF884s(options) {\n debugData(`Options for stripF884s: ${JSON.stringify(options)}`);\n\n if (options !== true) {\n return;\n }\n\n // Handle only f884 with $5 MELINDA - let's no remove random f884s\n const isMelindaf884 = f => f.tag === '884' && f.subfields.some(({code, value}) => code === '5' && value === 'MELINDA');\n\n const f884Melindas = newRecord.fields.filter(isMelindaf884);\n debugData(`Melinda's f884s (${f884Melindas.length}) \\n ${JSON.stringify(f884Melindas)}`);\n\n if (f884Melindas.length < 2) {\n debug(`Not enough Melinda's f884.s to filter (${f884Melindas.length})`);\n return;\n }\n\n // Sort fields by date subfield $g, so we'll keep the oldest one\n const sortedFields = sortFieldsBySubfieldValue(f884Melindas, 'g');\n debugData(`Melinda's f884s sorted by 'g' - oldest first: (${sortedFields.length}) \\n ${JSON.stringify(sortedFields)}`);\n\n // Drop fields that are similar without date subfield $g\n const uniqFields = uniqWithOutSubfield(sortedFields, 'g');\n debugData(`Melindas f884s uniqued without sf $g (${uniqFields.length}) \\n ${JSON.stringify(uniqFields)}`);\n\n // Replace original Melinda-f884s with remaining Melinda-f884s\n // NOTE: this sorts MELINDA-f884s after possible other f884s\n newRecord.removeFields(f884Melindas);\n newRecord.insertFields(uniqFields);\n return;\n\n // Keep just first instance of each similar field, compare without subfield with subfieldCode\n function uniqWithOutSubfield(fields, subfieldCode) {\n return fields.reduce((uniq, field) => {\n if (!uniq.some(f => MarcRecord.isEqual(removeSubfield(f, subfieldCode), removeSubfield(field, subfieldCode)))) { // eslint-disable-line functional/no-conditional-statement\n uniq.push(field); // eslint-disable-line functional/immutable-data\n }\n\n return uniq;\n }, []);\n }\n\n // sort fields by value of each fields first subfield with subfielCode\n function sortFieldsBySubfieldValue(fields, subfieldCode) {\n return [...fields].sort((a, b) => {\n const a1value = getFirstSubfieldValue(a, subfieldCode);\n const b1value = getFirstSubfieldValue(b, subfieldCode);\n if (a1value && !b1value) {\n return -1;\n }\n if (!a1value && b1value) {\n return 1;\n }\n if (a1value > b1value) {\n return 1;\n }\n if (b1value > a1value) {\n return -1;\n }\n return 0;\n });\n\n // get value for the for instance of subfield with subfieldCode\n function getFirstSubfieldValue(field, subfieldCode) {\n const subs = field.subfields ? field.subfields.filter(subf => subf.code === subfieldCode) : [];\n return subs.length > 0 ? subs[0].value : '';\n }\n }\n\n }\n\n function removeSubfield(field, code) {\n\n // Handle non-numeric fields, and fields with a numeric tag of 010 and greater\n // Aleph's FMT as a controlfield might be a problem\n if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {\n\n const filteredSubfields = field.subfields.filter(sf => sf.code !== code);\n\n // Remove whole field if there are no subfields left\n if (filteredSubfields.length < 1) {\n return false;\n }\n\n return {\n tag: field.tag,\n ind1: field.ind1,\n ind2: field.ind2,\n subfields: filteredSubfields\n };\n }\n // return controlFields as is\n return field;\n }\n\n\n // eslint-disable-next-line max-statements\n function handleTempUrns(options) {\n debugData(`Options for handleTempUrns: ${JSON.stringify(options)}`);\n\n if (options !== true) {\n return;\n }\n\n // If we have an non-temp URN, we can delete tempURNs, otherwise we should delete the temp subfield from URN\n\n const hasURN = f => f.tag === '856' && f.subfields.some(({code, value}) => code === 'u' && (/urn.fi/u).test(value));\n\n const hasTempSubfield = f => f.subfields.some(({code, value}) => code === '9' && (/^MELINDA<TEMP>$/u).test(value));\n const hasNoTempSubfield = f => !f.subfields.some(({code, value}) => code === '9' && (/^MELINDA<TEMP>$/u).test(value));\n\n const f856sUrn = newRecord.fields.filter(hasURN);\n const f856sUrnsWithTempSubfields = f856sUrn.filter(hasTempSubfield);\n const f856sUrnsWithNoTempSubfields = f856sUrn.filter(hasNoTempSubfield);\n\n debugData(`URN f856s: ${JSON.stringify(f856sUrn)}`);\n debugData(`URN f856s with temp subfield: ${JSON.stringify(f856sUrnsWithTempSubfields)}`);\n\n // None of the URNs has temp subfields, we don't need to do anything\n if (f856sUrnsWithTempSubfields.length < 1) {\n debug(`No Urns with temp subfield`);\n return;\n }\n\n // Do we have an existing URN with legal deposit subfields, if we have, we can remove the temp URN field(s)\n const existingLegalDepositURN = validateLD(f856sUrnsWithNoTempSubfields);\n debug(`existingLegalDepositURN: ${existingLegalDepositURN}`);\n\n if (existingLegalDepositURN) {\n debug(`We have an existing LD URN, we can delete temp fields (${f856sUrnsWithTempSubfields.length})`);\n newRecord.removeFields(f856sUrnsWithTempSubfields);\n return;\n }\n\n // We do not have an existing legalDepositURN, we should use the tempURN and remove the temp subfields from it\n if (!existingLegalDepositURN && f856sUrnsWithTempSubfields.length > 0) {\n //debug(`All Urns (${f856sUrn.length}) have a temp subfield`);\n debugData(`Original temp URNs: (${JSON.stringify(f856sUrnsWithTempSubfields)})`);\n const fixedFields = f856sUrnsWithTempSubfields.map(removeTempSubfield).filter(field => field);\n debug(`We removed temp subfields: (${JSON.stringify(fixedFields)})`);\n newRecord.removeFields(f856sUrnsWithTempSubfields);\n newRecord.insertFields(fixedFields);\n return;\n }\n\n // Should we check that the non-temp URN has 2nd ind '0' - meaning that the URN handles the actual resource itself?\n }\n\n // ---- LD-functions below are same as in https://github.com/NatLibFi/marc-record-validators-melinda/blob/feature-updates-to-urn/src/urn.js\n // we propably should develop the urn/legaldeposit -validator to use also here\n\n function validateLD(f856sUrn) {\n debug(`Validating the existence of legal deposit subfields`);\n\n const LD_SUBFIELDS = [\n {code: 'z', value: 'Käytettävissä vapaakappalekirjastoissa'},\n {code: '5', value: 'FI-Vapaa'}\n ];\n\n const f856sUrnWithLdSubfields = f856sUrn.filter(field => fieldHasLDSubfields(field, LD_SUBFIELDS));\n if (f856sUrnWithLdSubfields.length > 0) {\n debug(`Record has ${f856sUrnWithLdSubfields.length} URN fields with all necessary legal deposit subfields`);\n debugData(`f856sUrnWithLdSubfields: ${JSON.stringify(f856sUrnWithLdSubfields)}`);\n return true;\n }\n return false;\n }\n\n function fieldHasLDSubfields(field, ldSubfields) {\n if (ldSubfields.every(ldsf => field.subfields.some(sf => sf.code === ldsf.code && sf.value === ldsf.value))) {\n return true;\n }\n }\n\n // ---\n\n function removeTempSubfield(field) {\n\n // Handle non-numeric fields, and fields with a numeric tag of 010 and greater\n // Aleph's FMT as a controlfield might be a problem\n if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {\n\n const filteredSubfields = field.subfields.filter(sf => sf.code !== '9' && !(/^MELINDA<TEMP>$/u).test(sf.value));\n\n // Remove whole field if there are no subfields left\n if (filteredSubfields.length < 1) {\n return false;\n }\n\n return {\n tag: field.tag,\n ind1: field.ind1,\n ind2: field.ind2,\n subfields: filteredSubfields\n };\n }\n // return controlFields as is\n return field;\n }\n}\n\n// This could be formatted so that 035-prefix and SID contents would be better connected than by just an array index\nexport const BIB_F035_TO_SID = {\n f035Filters: [/^\\(FI-BTJ\\)/u, /^\\(FI-TATI\\)/u],\n fSIDFilters: ['FI-BTJ', 'tati']\n};\n\n// There is somewhere other implementations of this same standardPrefixes to alephInternalPrefixes normalization\nexport const REPLACE_PREFIXES = [\n {\n oldPrefix: 'FI-MELINDA',\n newPrefix: 'FIN01',\n prefixReplaceCodes: ['w']\n },\n {\n oldPrefix: 'FI-ASTERI-S',\n newPrefix: 'FIN10',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-N',\n newPrefix: 'FIN11',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-A',\n newPrefix: 'FIN12',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-W',\n newPrefix: 'FIN13',\n prefixReplaceCodes: ['0']\n }\n];\n\n// If placed in config.js testing needs envs\n// BIB_FORMAT_SETTINGS is outdated, use other settings instead\nexport const BIB_FORMAT_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES,\n generateMissingSIDs: BIB_F035_TO_SID\n};\n\nexport const BIB_PREVALIDATION_FIX_SETTINGS = {\n generateMissingSIDs: BIB_F035_TO_SID\n};\n\nexport const BIB_POSTMERGE_FIX_SETTINGS = {\n handleTempUrns: true,\n stripF884s: true\n};\n\nexport const BIB_PREIMPORT_FIX_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES\n};\n\nexport const AUTNAME_PREIMPORT_FIX_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES\n};\n\nexport const BIB_HANDLE_TEMP_URNS_SETTINGS = {\n handleTempUrns: true\n};\n\nexport const BIB_STRIP_F884S_SETTINGS = {\n stripF884s: true\n};\n"],"mappings":";;;;;;;;AA4BA;;AACA;;AACA;;;;AA9BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAMO,SAASA,SAAT,CAAmBC,MAAnB,EAA2BC,QAAQ,GAAG,EAAtC,EAA0C;EAC/C,MAAMC,MAAM,GAAG,IAAAC,mCAAA,GAAf;EACA,MAAMC,KAAK,GAAG,IAAAC,cAAA,EAAkB,8CAAlB,CAAd;EACA,MAAMC,SAAS,GAAGF,KAAK,CAACG,MAAN,CAAa,MAAb,CAAlB;EAEAL,MAAM,CAACM,OAAP,CAAgB,iEAAgEC,IAAI,CAACC,SAAL,CAAeT,QAAf,CAAyB,EAAzG;;EACA,MAAMU,SAAS,GAAGC,sBAAA,CAAWC,KAAX,CAAiBb,MAAjB,EAAyB;IAACc,cAAc,EAAE;E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AmB,EAAEiG;AAFY,CAA5B;;AAKA,MAAMG,8BAA8B,GAAG;EAC5CpG,mBAAmB,EAAEiG;AADuB,CAAvC;;AAIA,MAAMI,0BAA0B,GAAG;EACxC/F,cAAc,EAAE,IADwB;EAExCC,UAAU,EAAE;AAF4B,CAAnC;;AAKA,MAAM+F,0BAA0B,GAAG;EACxCnG,eAAe,EAAE+F;AADuB,CAAnC;;AAIA,MAAMK,8BAA8B,GAAG;EAC5CpG,eAAe,EAAE+F;AAD2B,CAAvC;;AAIA,MAAMM,6BAA6B,GAAG;EAC3ClG,cAAc,EAAE;AAD2B,CAAtC;;AAIA,MAAMmG,wBAAwB,GAAG;EACtClG,UAAU,EAAE;AAD0B,CAAjC"}
|
|
1
|
+
{"version":3,"file":"fix.js","names":["logger","createLogger","debug","createDebugLogger","debugData","extend","fixRecord","record","settings","verbose","JSON","stringify","newRecord","MarcRecord","clone","subfieldValues","generateMissingSIDsOptions","generateMissingSIDs","undefined","newRecord1","replacePrefixesOptions","replacePrefixes","newRecord2","replaceAllPrefixes","newRecord3","handleTempUrns","newRecord4","stripF884s","toObject","length","forEach","options","replacePrefix","oldPrefix","newPrefix","prefixReplaceCodes","pattern","replacement","getDatafields","field","subfields","filter","code","includes","subfield","value","replace","f035Filters","fSIDFilters","f035s","f035ToSidInfo","get","fSIDs","sidsToSidInfo","sidsToBeAdded","genNewSids","f035SidInfo","some","fSIDInfo","SID","sidField","insertField","map","sidInfo","tag","ind1","ind2","SIDs","flatMap","sub","wantedSub","regexp","test","findIndex","slice","indexOf"],"sources":["../../src/helpers/fix.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-rest-api-commons is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Affero General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport {createLogger} from '@natlibfi/melinda-backend-commons';\nimport createDebugLogger from 'debug';\n\nimport {handleTempUrns} from './fix-handle-tempurns';\nimport {stripF884s} from './fix-strip-f884s';\n\nexport * from './fix-constants';\n\nconst logger = createLogger();\nconst debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:fixRecord');\nconst debugData = debug.extend('data');\n\nexport function fixRecord(record, settings = {}) {\n\n logger.verbose(`We will apply formatting to the record according to settings: ${JSON.stringify(settings)}`);\n const newRecord = MarcRecord.clone(record, {subfieldValues: false});\n debugData(`settings: ${JSON.stringify(settings)}`);\n\n // We should handle these in some prettier way ...\n\n // Fix 1, generate SIDs\n const generateMissingSIDsOptions = settings.generateMissingSIDs || undefined;\n const newRecord1 = generateMissingSIDs(newRecord, generateMissingSIDsOptions);\n\n // Fix 2, replace identifier prefixes\n const replacePrefixesOptions = settings.replacePrefixes || [];\n const newRecord2 = replaceAllPrefixes(newRecord1, replacePrefixesOptions);\n\n // Fix 3, handle temporary URNs from legal deposit record import\n const newRecord3 = handleTempUrns(newRecord2, settings.handleTempUrns);\n\n // Fix 4, strip extra field 884s\n const newRecord4 = stripF884s(newRecord3, settings.stripF884s);\n\n return newRecord4.toObject();\n\n}\n\n\nfunction replaceAllPrefixes(record, replacePrefixesOptions) {\n if (replacePrefixesOptions.length < 1) {\n debug(`NOT running replacePrefixes, no options`);\n return record;\n }\n\n debug(`Running replacePrefixes fixer`);\n debugData(`replacePrefixesOptions: ${JSON.stringify(replacePrefixesOptions)}`);\n\n replacePrefixesOptions.forEach(options => {\n replacePrefix(record, options);\n });\n return record;\n}\n\n\n// Replace prefix in all specified subfields\nfunction replacePrefix(record, options) {\n const {oldPrefix, newPrefix, prefixReplaceCodes} = options;\n debug(`Replacing ${oldPrefix} with ${newPrefix} in subfields ${prefixReplaceCodes}`);\n const pattern = `(${oldPrefix})`;\n const replacement = `(${newPrefix})`;\n record.getDatafields()\n .forEach(field => {\n field.subfields\n .filter(({code}) => prefixReplaceCodes.includes(code))\n .forEach(subfield => {\n subfield.value = subfield.value.replace(pattern, replacement); // eslint-disable-line functional/immutable-data\n });\n });\n return record;\n}\n\n\nfunction generateMissingSIDs(record, options) {\n\n const f035Filters = options && options.f035Filters ? options.f035Filters : [];\n const fSIDFilters = options && options.fSIDFilters ? options.fSIDFilters : [];\n\n if (f035Filters.length !== fSIDFilters.length || f035Filters.length < 1 || fSIDFilters.length < 1) {\n debug(`NOT running generateMissingsSIDs, no options`);\n return record;\n }\n\n debug(`Running generateMissingSIDs fixer`);\n debugData(`generateMissingSIDs options: ${JSON.stringify(options)}`);\n\n const f035s = f035ToSidInfo(record.get(/^035$/u));\n const fSIDs = sidsToSidInfo(record.get(/^SID$/u));\n\n const sidsToBeAdded = genNewSids(fSIDs.length === 0\n ? f035s\n // test that SIDs are not there yet\n : f035s.filter(f035SidInfo => fSIDs.some(fSIDInfo => f035SidInfo.SID !== fSIDInfo.SID && f035SidInfo.value !== fSIDInfo.value)));\n\n // Add new SIDs\n sidsToBeAdded.forEach(sidField => record.insertField(sidField));\n return record;\n\n function genNewSids(sidsToBeAdded) {\n return sidsToBeAdded.map(sidInfo => ({tag: 'SID', ind1: ' ', ind2: ' ', subfields: [{code: 'c', value: sidInfo.value}, {code: 'b', value: sidInfo.SID}]}));\n }\n\n function sidsToSidInfo(SIDs) {\n return SIDs.flatMap(({subfields}) => {\n const [SID] = subfields.filter(sub => sub.code === 'b' && fSIDFilters.includes(sub.value)).map(sub => sub.value);\n const [value] = subfields.filter(sub => sub.code === 'c' && sub.value !== undefined).map(sub => sub.value);\n\n if (SID && value) {\n return {\n SID,\n value\n };\n }\n\n return undefined;\n }).filter(value => value !== undefined);\n }\n\n function f035ToSidInfo(f035s) {\n return f035s.flatMap(({subfields}) => {\n const [wantedSub] = subfields.filter(sub => sub.code === 'a' && f035Filters.some(regexp => regexp.test(sub.value)));\n\n if (wantedSub) {\n return {\n SID: fSIDFilters[f035Filters.findIndex(regexp => regexp.test(wantedSub.value))],\n value: wantedSub.value.slice(wantedSub.value.indexOf(')') + 1)\n };\n }\n\n return undefined;\n }).filter(value => value !== undefined);\n }\n}\n\n\n"],"mappings":";;;;;;;;;;AA4BA;;AACA;;AACA;;AAEA;;AACA;;AAEA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;;;AAnCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAWA,MAAMA,MAAM,GAAG,IAAAC,mCAAA,GAAf;AACA,MAAMC,KAAK,GAAG,IAAAC,cAAA,EAAkB,8CAAlB,CAAd;AACA,MAAMC,SAAS,GAAGF,KAAK,CAACG,MAAN,CAAa,MAAb,CAAlB;;AAEO,SAASC,SAAT,CAAmBC,MAAnB,EAA2BC,QAAQ,GAAG,EAAtC,EAA0C;EAE/CR,MAAM,CAACS,OAAP,CAAgB,iEAAgEC,IAAI,CAACC,SAAL,CAAeH,QAAf,CAAyB,EAAzG;;EACA,MAAMI,SAAS,GAAGC,sBAAA,CAAWC,KAAX,CAAiBP,MAAjB,EAAyB;IAACQ,cAAc,EAAE;EAAjB,CAAzB,CAAlB;;EACAX,SAAS,CAAE,aAAYM,IAAI,CAACC,SAAL,CAAeH,QAAf,CAAyB,EAAvC,CAAT,CAJ+C,CAM/C;EAEA;;EACA,MAAMQ,0BAA0B,GAAGR,QAAQ,CAACS,m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