Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dctap-dancer
1 change: 1 addition & 0 deletions ldpjs
Submodule ldpjs added at d7aee3
58 changes: 57 additions & 1 deletion util-service/routes/marc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* MARC Preview Routes
* MARC Routes
*
* Handles MARC preview generation:
* - POST /marcpreview/:type - Generate MARC preview from RDF
* - POST /marcformat - Generate MARC of 1 format from another format
*/

const express = require('express');
Expand Down Expand Up @@ -88,6 +89,40 @@ function marcRecordHtmlify(data) {
return formattedMarcRecord.join('\r\n');
}

/**
*
* @param {*} marcxml MARC record as XML that will be changed
* @param {string} sourceType Type of the incoming MARC, type record is {'leader': <....>, 'fields': [...]}
* @param {string} targetType Type of the outgoing MARC
* @returns
*/
function marcChangeFormat(marc, sourceType, targetType){
let sourceTypeList = [ 'iso2709', 'marcxml', 'mij', 'record']
let targetTypeList = [ 'iso2709', 'marcxml', 'mij', 'text', 'json', 'html']

if (!sourceTypeList.includes(sourceType)){
return [false, 'source', sourceTypeList]
}
if (!targetTypeList.includes(targetType)){
return [false, 'target', targetTypeList]
}

let formatted
if (targetType != 'html'){
const record = Marc.parse(marc, sourceType);
formatted = Marc.format(record, targetType)
} else {
if (sourceType == 'record'){
formatted = marcRecordHtmlify(marc)
} else {
formatted = Marc.parse(marc, 'marcxml')
formatted = marcRecordHtmlify(formatted)
}
}

return [true, true, formatted]
}

/**
* Create MARC routes
* @returns {Router} Express router
Expand Down Expand Up @@ -148,6 +183,27 @@ function createMarcRoutes() {
res.json(results);
});

/**
* POST /marcformat - Generate MARC of 1 format from another format
*/
router.post('/marcformat', async (req, res) => {
let marc = req.body.mrc;
const sourceType = req.body.sourceType;
const targetType = req.body.targetType;

if (sourceType != 'record'){
marc = marc.replaceAll('marcxml:', '')
}
let recordFormatted = marcChangeFormat(marc, sourceType, targetType)
if (!recordFormatted[0]){
let message = 'Failed to match ' + recordFormatted[1] + ' format. Available formats: ' + recordFormatted[2].join(", ")

return res.status(500).json({ msg: 'Error: ' + message });
} else {
return res.status(200).json({ result: recordFormatted[2] });
}
});

return router;
}

Expand Down