Newer
Older
const { set, get, reduce } = require('lodash')
const PackageManager = require('./PackageManager')
const {
defaultConfig,
defaultParseXmlOptions,
defaultS3Config,
constructor(
config = defaultConfig,
options = defaultParseXmlOptions,
s3Config = defaultS3Config,
this.config = config
this.options = options
this.jsonTemplate = mts.getJsonTemplate(config)
createFileName(id = Date.now()) {
return `${this.config.prefix}${id}`
}
parseHtml(content = '') {
if (/<\/?[^>]*>/.test(content)) {
return convert.xml2js(content, this.options)
}
return content
}
convertToXML(json = {}) {
const content = convert.json2xml(json, this.options)
const customId = get(
json,
'article.front.article-meta.article-id[0]._text',
this.createFileName(),
)
const name = `${customId}.xml`
return {
name,
content,
}
}
const fileName = this.createFileName(metadata.customId)
'article-title': this.parseHtml(metadata.title),
const articleId = [
{
_attributes: {
'pub-id-type': 'publisher-id',
},
_text: fileName,
},
{
_attributes: {
'pub-id-type': 'manuscript',
},
_text: fileName,
const articleType = {
'subj-group': [
{
_attributes: {
'subj-group-type': 'Article Type',
},
subject: {
_text: metadata.type,
},
},
],
}
set(jsonTemplate, 'article.front.article-meta.title-group', titleGroup)
set(jsonTemplate, 'article.front.article-meta.article-id', articleId)
set(
jsonTemplate,
'article.front.article-meta.article-categories',
articleType,
)
set(jsonTemplate, 'article.front.article-meta.abstract', metadata.abstract)
return jsonTemplate
}
static setHistory(submitted, jsonTemplate) {
const date = new Date(submitted)
const parsedDate = {
date: {
_attributes: {
'date-type': 'received',
},
day: {
},
year: {
_text: date.getFullYear(),
},
},
}
set(jsonTemplate, 'article.front.article-meta.history', parsedDate)
return jsonTemplate
}
static setFiles(files, jsonTemplate) {
const jsonFiles = reduce(
files,
(result, value, key) => {
value.map(v =>
result.push({
item_type: {
_text: key,
},
item_description: {
},
item_name: {
_text: v.name,
},
}),
)
return result
[],
)
set(jsonTemplate, 'article.front.files.file', jsonFiles)
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
return jsonTemplate
}
static setContributors(authors = [], jsonTemplate) {
const contrib = authors.map((a, i) => ({
_attributes: {
'contrib-type': 'author',
corresp: a.isCorresponding ? 'yes' : 'no',
},
role: {
_attributes: {
'content-type': '1',
},
},
name: {
surname: {
_text: a.firstName,
},
'given-names': {
_text: a.lastName,
},
prefix: {
_text: a.title || 'Dr.',
},
},
email: {
_text: a.email,
},
xref: {
_attributes: {
'ref-type': 'aff',
rid: `aff${i + 1}`,
},
},
}))
const aff = authors.map((a, i) => ({
_attributes: {
id: `aff${i + 1}`,
},
'addr-line': {
_text: a.affiliation || '',
},
}))
set(
jsonTemplate,
'article.front.article-meta.contrib-group.contrib',
contrib,
)
set(jsonTemplate, 'article.front.article-meta.contrib-group.aff', aff)
return jsonTemplate
}
composeJson(fragment = {}) {
const {
authors = [],
metadata = { title: 'untitled', abstract: '' },
submitted = new Date(),
} = fragment
return {
...this.jsonTemplate,
...this.setMetadata(metadata, this.jsonTemplate),
...this.constructor.setContributors(authors, this.jsonTemplate),
...this.constructor.setHistory(submitted, this.jsonTemplate),
...this.constructor.setFiles(files, this.jsonTemplate),
convertFragmentToXML(fragment = {}) {
return this.convertToXML(this.composeJson(fragment))
}
sendPackage(fragment = {}) {
const xmlFile = this.convertFragmentToXML(fragment)
return PackageManager.createFilesPackage(this.s3Config)({
}).then(() => {
const manuscriptName = get(xmlFile, 'name', '').replace('.xml', '')
const filename = `${manuscriptName}.zip`
return PackageManager.uploadFiles({
filename,
s3Config: this.s3Config,
config: this.ftpConfig,
})