From ba27dacb54765832fd14f1bd5d12ab5cf6f3dfe3 Mon Sep 17 00:00:00 2001
From: Nikos Marinos <nmarinos@ebi.ac.uk>
Date: Thu, 14 Mar 2019 13:07:39 +0000
Subject: [PATCH] Fixes to xsweet converter with Koosum

---
 package.json                                  |  2 +-
 server/ftp-integration/api.js                 | 23 ++++++++++++-------
 .../xpub-model/entities/file/data-access.js   |  2 +-
 server/xsweet-conversion/index.js             | 23 +++++++++++--------
 4 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/package.json b/package.json
index 1a688482d..a6fd3edc3 100755
--- a/package.json
+++ b/package.json
@@ -164,7 +164,7 @@
     "reset": "pubsweet setupdb --clobber",
     "start": "docker-compose -f ./docker-compose.yml -f ./docker-compose.dev.yml up --build",
     "start:production": "docker-compose -f ./docker-compose.yml -f ./docker-compose.prod.yml up --build",
-    "start:services": "docker-compose up postgres ftpd_server minio",
+    "start:services": "docker-compose up postgres ftpd_server minio xsweet",
     "start:services:dev": "docker-compose -f ./docker-compose.yml -f ./docker-compose.dev.yml up postgres ftpd_server minio",
     "ftp:listener": "./runFtpMonitor.sh &",
     "server": "crontab crontab && pubsweet server",
diff --git a/server/ftp-integration/api.js b/server/ftp-integration/api.js
index 6beb16d7c..1c78f65ae 100644
--- a/server/ftp-integration/api.js
+++ b/server/ftp-integration/api.js
@@ -73,7 +73,11 @@ async function tidyUp(filePath, tmpPath, manuscript, isError) {
           // node couldn't execute the command
           reject(err)
         }
-        logger.info(`Finished Bulk import process for ${filePath}`)
+        logger.info(
+          `Finished Bulk import process for ${filePath} as manuscript ${
+            manuscript.id
+          }.`,
+        )
         resolve(true)
       })
     })
@@ -516,6 +520,15 @@ function uploadFile(
   })
 }
 
+async function submitManuscript(manuscriptdb, submitter) {
+  const input = {
+    id: manuscriptdb.id,
+    status: 'submitted',
+  }
+  const { userId } = submitter
+  await ManuscriptManager.submit(input, userId)
+}
+
 async function createManuscript(
   pubsweetServer,
   xmlObj,
@@ -690,13 +703,7 @@ async function createManuscript(
       )[0].fileURI
 
       await xsweetConverter.xsweetConvert(manuscriptFileURI, manuscriptdb.id)
-
-      const input = {
-        id: manuscriptdb.id,
-        status: 'submitted',
-      }
-      const { userId } = submitter
-      await ManuscriptManager.submit(input, userId)
+      await submitManuscript(manuscriptdb, submitter)
     })
   return manuscriptdb
 }
diff --git a/server/xpub-model/entities/file/data-access.js b/server/xpub-model/entities/file/data-access.js
index 437ece15b..ef8b75055 100644
--- a/server/xpub-model/entities/file/data-access.js
+++ b/server/xpub-model/entities/file/data-access.js
@@ -28,7 +28,7 @@ class File extends EpmcBaseModel {
         type: { type: 'string' },
         size: { type: 'int' },
         url: { type: 'string' },
-        label: { type: 'string' },
+        label: { type: ['string', 'null'] },
         deleted: { type: 'timestamp' },
         updatedBy: { type: 'uuid' },
       },
diff --git a/server/xsweet-conversion/index.js b/server/xsweet-conversion/index.js
index bf89c5dc3..38bfed8a7 100644
--- a/server/xsweet-conversion/index.js
+++ b/server/xsweet-conversion/index.js
@@ -1,3 +1,4 @@
+const config = require('config')
 const fs = require('fs')
 const rp = require('request-promise')
 const tar = require('../utils/unTar.js')
@@ -9,6 +10,8 @@ const path = require('path')
 const files = require('../utils/files.js')
 const db = require('../utils/db.js')
 
+const pubsweetServer = config.get('pubsweet-server.baseUrl')
+const label = 'xsweet conversion'
 /*
 xsweetConvert(
   '/home/yogmatee/projects/xmlValidation_files/xweet_files/tiana.docx',
@@ -16,14 +19,15 @@ xsweetConvert(
 )
 */
 
-module.exports.xsweetConvert = function xsweetConvert(fileUrl, manuscriptId) {
+module.exports.xsweetConvert = async function xsweetConvert(
+  fileUrl,
+  manuscriptId,
+) {
   const options = {
     method: 'POST',
-    uri: 'http://localhost:3000/convertDocxToHTML',
+    uri: `${pubsweetServer}/convertDocxToHTML`,
     formData: {
-      docx: fs.createReadStream(
-        '/home/yogmatee/projects/xmlValidation_files/xweet_files/tiana.docx',
-      ),
+      docx: fs.createReadStream(fileUrl),
     },
     headers: {
       /* 'content-type': 'multipart/form-data' */
@@ -31,7 +35,7 @@ module.exports.xsweetConvert = function xsweetConvert(fileUrl, manuscriptId) {
     },
   }
 
-  rp(options)
+  await rp(options)
     .then(async body => {
       try {
         const tmpPath = await tar.createTempDir()
@@ -42,6 +46,7 @@ module.exports.xsweetConvert = function xsweetConvert(fileUrl, manuscriptId) {
           `${tmpPath}/${manuscriptId}.html`,
           manuscriptId,
           user,
+          label,
         )
         const uuid = uuidv4()
 
@@ -58,7 +63,7 @@ module.exports.xsweetConvert = function xsweetConvert(fileUrl, manuscriptId) {
         delete fileInfo.extension
 
         await db.upsertHtmlPrevFile(fileInfo, manuscriptId)
-        logger.info('file has been uploaded to Minio and to db')
+        logger.info('HTML converted file has been uploaded to Minio and to db')
       } catch (err) {
         throw err
       }
@@ -68,7 +73,7 @@ module.exports.xsweetConvert = function xsweetConvert(fileUrl, manuscriptId) {
     })
 }
 
-function getFileInfo(filepath, manuscriptId, user) {
+function getFileInfo(filepath, manuscriptId, user, label) {
   const filename = filepath.substring(filepath.lastIndexOf('/') + 1)
   const fileExt = path.extname(filepath)
   const fileSize = fs.statSync(filepath).size
@@ -77,7 +82,7 @@ function getFileInfo(filepath, manuscriptId, user) {
     url: filepath,
     filename,
     type: 'source',
-    label: '',
+    label,
     size: fileSize,
     extension: fileExt,
     mimeType: `${mime.contentType(fileExt)}`,
-- 
GitLab