Skip to content
Snippets Groups Projects
Commit 0860a5e0 authored by Yogmatee Roochun's avatar Yogmatee Roochun
Browse files

issue 378 resolved

parent df4deb75
No related branches found
No related tags found
2 merge requests!75Shared data model,!77Dev
......@@ -3,10 +3,25 @@ const logger = require('@pubsweet/logger')
const FileModel = rfr('server/xpub-model/entities/file/data-access')
module.exports.createFiles = async function createFiles(filesArr) {
for (let i = 0; i < filesArr.length; i += 1) {
/* eslint-disable no-await-in-loop */
const filedb = await new FileModel(filesArr[i]).save({
module.exports.createFiles = async function createFiles(
filesArr,
manuscriptId,
) {
const existingFiles = await FileModel.selectByManIdConvertedFiles(
manuscriptId,
)
await upsertFileUrl('tempHTML', filesArr, existingFiles)
await upsertFileUrl('PMCfinal', filesArr, existingFiles)
}
async function upsertFileUrl(fileType, filesArr, existingFiles) {
const newFile = filesArr.find(file => file.type === fileType)
const existingFile = existingFiles.find(file => file.type === fileType)
if (existingFile) {
const filedb = await FileModel.updateFileUrl(existingFile.id, newFile.url)
logger.info(filedb)
} else {
const filedb = await new FileModel(newFile).save({
insertMissing: true,
})
logger.info(filedb)
......
......@@ -132,11 +132,12 @@ module.exports.pushXML = async function pushXML(fileUrl, manuscriptId) {
)
}
const dbFilesArr = filesArr.map(obj => {
obj.url = `/download/${uuid}${obj.extension}`
delete obj.extension
return obj
})
await db.createFiles(dbFilesArr)
await db.createFiles(dbFilesArr, manuscriptId)
await Manuscript.updatePdfDepositState(manuscriptId)
await Manuscript.clearFormState(manuscriptId)
logger.info('Uploading to Minio and the database has been completed.')
......
......@@ -74,6 +74,25 @@ class File extends EpmcBaseModel {
return rows.map(rowToEntity)
}
static async selectByManIdConvertedFiles(manId) {
const rows = await runQuery(
buildQuery
.select()
.from('file')
.where({ 'file.manuscript_id': manId })
.andWhere('file.type', 'in', ['tempHTML', 'PMCfinal'])
.whereNull('file.deleted'),
)
return rows.map(rowToEntity)
}
static async updateFileUrl(id, fileurl) {
const fileUpdated = await File.query()
.patch({ url: fileurl })
.where('id', id)
return fileUpdated
}
static async selectAll() {
const rows = await runQuery(
buildQuery
......
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