Commit e0cdcecd authored by Audrey Hamelers's avatar Audrey Hamelers
Browse files
parent 6f8ea86c
Pipeline #12805 passed with stages
in 5 minutes and 34 seconds
......@@ -18,13 +18,13 @@ let c = new Client()
;(async () => {
const beforeUpdate = Date.now()
const updatedManuscripts = await fromNcbi().catch(err => logger.error(err))
await Promise.all(
updatedManuscripts
.filter(m => m.ncbiState === 'success')
.map(async m => {
await createGrantLinks(m)
}),
const successManuscripts = updatedManuscripts.filter(
m => m.ncbiState === 'success',
)
if (successManuscripts.length > 0) {
logger.info('Sending grant data')
await createGrantLinks(successManuscripts)
}
logger.info(`from ncbi check was finished in ${Date.now() - beforeUpdate} ms`)
})()
......
......@@ -6,47 +6,65 @@ const Client = require('ftp')
const { Readable } = require('stream')
module.exports.createGrantLinks = async function createGrantLinks(
manuscript,
manuscripts,
removeFundingGroup,
) {
try {
const filename = `grants-${manuscript.id}.${moment().format(
'YYYY_MM_DD-HH-mm-SS',
)}.xml`
const articleIds =
manuscript.meta.articleIds &&
manuscript.meta.articleIds
.filter(a => ['pmcid', 'pmid'].includes(a.pubIdType))
.sort((a, b) => (a.pubIdType > b.pubIdType ? 1 : -1))
const fundingGroup =
manuscript.meta.fundingGroup &&
manuscript.meta.fundingGroup.length > 0 &&
manuscript.meta.fundingGroup
if (
articleIds &&
articleIds.length > 0 &&
(fundingGroup || removeFundingGroup)
) {
const contents = await createGrantXML(
filename,
articleIds,
fundingGroup,
removeFundingGroup,
const filename = `grants${
manuscripts.length === 1 ? `-${manuscripts[0].id}` : ''
}.${moment().format('YYYY_MM_DD-HH-mm-SS')}.xml`
const sendManuscripts = manuscripts.reduce((list, manuscript) => {
const articleIds =
manuscript.meta.articleIds &&
manuscript.meta.articleIds
.filter(a => ['pmcid', 'pmid'].includes(a.pubIdType))
.sort((a, b) => (a.pubIdType > b.pubIdType ? 1 : -1))
const fundingGroup =
manuscript.meta.fundingGroup &&
manuscript.meta.fundingGroup.length > 0 &&
manuscript.meta.fundingGroup
if (
articleIds &&
articleIds.length > 0 &&
(fundingGroup || removeFundingGroup)
) {
list.push({
id: manuscript.id,
articleIds,
fundingGroup,
removeFundingGroup,
})
} else {
logger.info(`${manuscript.id} No grants to link or no ID to link to`)
}
return list
}, [])
if (sendManuscripts.length > 0) {
let contents = `<?xml version="1.0" encoding="UTF-8"?>
<grants file="${filename}">`
await Promise.all(
sendManuscripts.map(async each => {
contents += await createGrantXML(
each.articleIds,
each.fundingGroup,
each.removeFundingGroup,
)
logger.info(`${each.id} Grant list created`)
}),
)
contents += '</grants>'
await sendXML(contents, filename)
logger.info(`${manuscript.id} Grant list sent to NCBI`)
logger.info(`Grant list sent to NCBI`)
return { success: true, message: 'Grant list sent to NCBI' }
}
logger.info(`${manuscript.id} No grants to link or no ID to link to`)
return { success: true, message: 'No grants to link or no ID to link to' }
} catch (err) {
logger.error(`${manuscript.id} Unable to link grants: ${err}`)
logger.error(`Unable to link grants: ${err}`)
return { success: false, message: `Unable to link grants: ${err}` }
}
}
async function createGrantXML(
filename,
articleIds = [],
fundingGroup = [],
removeFundingGroup = [],
......@@ -62,18 +80,16 @@ async function createGrantXML(
const listIDs = articleIds
.map(a => `${a.pubIdType}="${a.id.replace('PMC', '')}"`)
.join(' ')
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<grants file="${filename}">
${removeFundingGroup
.map(
g =>
` <grant cmd="remove" ${listIDs} grant-name="${funderInfoList.find(
f => f.name === g.fundingSource,
) && funderInfoList.find(f => f.name === g.fundingSource).grantPrefix}${
g.awardId
}" authority="WT"/>`,
)
.join('\n')}
const xml = `${removeFundingGroup
.map(
g =>
` <grant cmd="remove" ${listIDs} grant-name="${funderInfoList.find(
f => f.name === g.fundingSource,
) && funderInfoList.find(f => f.name === g.fundingSource).grantPrefix}${
g.awardId
}" authority="WT"/>`,
)
.join('\n')}
${fundingGroup
.map(
g =>
......@@ -83,8 +99,7 @@ ${fundingGroup
g.awardId
}" authority="WT"/>`,
)
.join('\n')}
</grants>`
.join('\n')}`
return xml
}
......
......@@ -225,7 +225,7 @@ const Manuscript = {
try {
const manuscript = await ManuscriptAccess.selectById(id)
if (manuscript) {
const result = await createGrantLinks(gManuscript(manuscript))
const result = await createGrantLinks([gManuscript(manuscript)])
if (result.success) {
const teams = await Team.selectByManuscriptId(id)
const subTeam = teams.find(t => t.roleName === 'submitter')
......@@ -325,7 +325,7 @@ const Manuscript = {
const updatedMan = await ManuscriptAccess.selectById(input.id, true)
const manuscript = gManuscript(updatedMan)
if (send) {
await createGrantLinks(manuscript, removeGroup)
await createGrantLinks([manuscript], removeGroup)
}
return manuscript
},
......@@ -364,7 +364,7 @@ const Manuscript = {
try {
await keepMan.save()
if (send) {
await createGrantLinks(gManuscript(keepMan), removeGroup)
await createGrantLinks([gManuscript(keepMan)], removeGroup)
}
await ManuscriptAccess.delete(throwId, userId)
await removeDuplicateEmail(user, throwMan, keepMan)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment