Skip to content
Snippets Groups Projects
Commit cb78d00d authored by Audrey Hamelers's avatar Audrey Hamelers
Browse files

Use transactions in grant link check

parent ee689fea
No related branches found
No related tags found
1 merge request!393Dev
...@@ -4,6 +4,7 @@ const Manuscript = require('../xpub-model/entities/manuscript/data-access') ...@@ -4,6 +4,7 @@ const Manuscript = require('../xpub-model/entities/manuscript/data-access')
const { const {
gManuscript, gManuscript,
} = require('../xpub-model/entities/manuscript/helpers/transform') } = require('../xpub-model/entities/manuscript/helpers/transform')
const { transaction } = require('objection')
const { errorDevEmail } = require('../email') const { errorDevEmail } = require('../email')
const { createGrantLinks } = require('./linkGrants') const { createGrantLinks } = require('./linkGrants')
...@@ -45,6 +46,7 @@ process ...@@ -45,6 +46,7 @@ process
}) })
async function grantLinkCheck() { async function grantLinkCheck() {
const trx = await transaction.start(Manuscript.knex())
try { try {
const linkingManuscripts = await Manuscript.query() const linkingManuscripts = await Manuscript.query()
.where('ncbiState', 'linking grants') .where('ncbiState', 'linking grants')
...@@ -56,18 +58,20 @@ async function grantLinkCheck() { ...@@ -56,18 +58,20 @@ async function grantLinkCheck() {
if (!grantLinkDone.success) { if (!grantLinkDone.success) {
throw new Error(grantLinkDone.message) throw new Error(grantLinkDone.message)
} }
await Promise.all( await linkingManuscripts.reduce(async (promise, m) => {
linkingManuscripts.map(async m => { await promise
m.ncbiState = 'success' m.ncbiState = 'success'
await m.save() await m.saveWithTrx(trx)
return true return Promise.resolve()
}), }, Promise.resolve())
) trx.commit()
return true return true
} }
logger.info('No grant links to send.') logger.info('No grant links to send.')
if (trx) await trx.rollback()
return true return true
} catch (e) { } catch (e) {
if (trx) await trx.rollback()
await errorDevEmail('sending grants to EBI', e) await errorDevEmail('sending grants to EBI', e)
logger.error(`Unable to get or send grant links: ${e}`) logger.error(`Unable to get or send grant links: ${e}`)
throw e throw e
......
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