From b522637d821346b1eb946d5f974ba366ba6821a1 Mon Sep 17 00:00:00 2001
From: Mihail Gorceag <mgorceag@EN410053.local>
Date: Tue, 6 Apr 2021 16:12:54 +0300
Subject: [PATCH] fix: revert back publish functionality and add json in
 dockerfile-prod

---
 Dockerfile-production                  |   1 +
 server/model-manuscript/src/graphql.js | 132 ++++++++++++-------------
 2 files changed, 67 insertions(+), 66 deletions(-)

diff --git a/Dockerfile-production b/Dockerfile-production
index 11e0249356..3df6baf98d 100644
--- a/Dockerfile-production
+++ b/Dockerfile-production
@@ -59,6 +59,7 @@ COPY --chown=node:node ./scripts ./scripts
 COPY --chown=node:node ./server ./server
 COPY --chown=node:node ./app/storage ./app/storage
 COPY --chown=node:node ./startServer.js .
+COPY --chown=node:node ./google_sheets_credentials.json .
 
 COPY --from=build /home/node/app/_build/assets ./_build
 COPY --from=build /home/node/app/production_node_modules ./node_modules
diff --git a/server/model-manuscript/src/graphql.js b/server/model-manuscript/src/graphql.js
index 2da80dfe01..74d76dbc89 100644
--- a/server/model-manuscript/src/graphql.js
+++ b/server/model-manuscript/src/graphql.js
@@ -1,7 +1,7 @@
 const { ref, raw } = require('objection')
 const axios = require('axios')
 const { GoogleSpreadsheet } = require('google-spreadsheet')
-// const credentials = require('../../../google_sheets_credentials.json')
+const credentials = require('../../../google_sheets_credentials.json')
 
 const ManuscriptResolvers = ({ isVersion }) => {
   const resolvers = {
@@ -305,71 +305,71 @@ const resolvers = {
         }
       }
 
-      // if (process.env.INSTANCE_NAME === 'ncrc') {
-      //   // eslint-disable-next-line
-      //   const submissionForm = require('../../../app/storage/forms-ncrc/submit.json')
-      //   const spreadsheetId = '1OvWJj7ZTFhniC4KbFNbskuYSNMftsG2ocKuY-i9ezVA'
-
-      //   const fieldsOrder = submissionForm.children
-      //     .filter(el => el.name)
-      //     .map(formElement => formElement.name.split('.')[1])
-
-      //   const formatSubmissionData = rawSubmissionData => {
-      //     return Object.keys(rawSubmissionData).reduce((acc, key) => {
-      //       return { ...acc, [key]: rawSubmissionData[key].toString() }
-      //     }, {})
-      //   }
-
-      //   const publishArticleInGoogleSheets = async submissionData => {
-      //     const formattedSubmissionData = formatSubmissionData(submissionData)
-
-      //     const { articleURL } = formattedSubmissionData
-      //     const doc = new GoogleSpreadsheet(spreadsheetId)
-
-      //     await doc.useServiceAccountAuth({
-      //       client_email: credentials.client_email,
-      //       private_key: credentials.private_key,
-      //     })
-
-      //     await doc.loadInfo()
-      //     const sheet = doc.sheetsByIndex[0]
-      //     const rows = await sheet.getRows()
-
-      //     const indexOfExistingArticle = rows.findIndex(
-      //       row => row.articleURL === articleURL,
-      //     )
-
-      //     if (indexOfExistingArticle !== -1) {
-      //       fieldsOrder.forEach(fieldName => {
-      //         rows[indexOfExistingArticle][fieldName] =
-      //           formattedSubmissionData[fieldName] || ''
-      //       })
-      //       await rows[indexOfExistingArticle].save()
-      //     } else {
-      //       await sheet.addRow({ ...formattedSubmissionData })
-      //     }
-      //   }
-
-      //   try {
-      //     await publishArticleInGoogleSheets(manuscript.submission)
-
-      //     const updatedManuscript = await ctx.models.Manuscript.query().updateAndFetchById(
-      //       id,
-      //       {
-      //         published: new Date(),
-      //         status: 'published',
-      //       },
-      //     )
-
-      //     return updatedManuscript
-      //   } catch (e) {
-      //     // eslint-disable-next-line
-      //     console.log('error while publishing in google spreadsheet')
-      //     // eslint-disable-next-line
-      //     console.log(e)
-      //     return null
-      //   }
-      // }
+      if (process.env.INSTANCE_NAME === 'ncrc') {
+        // eslint-disable-next-line
+        const submissionForm = require('../../../app/storage/forms-ncrc/submit.json')
+        const spreadsheetId = '1OvWJj7ZTFhniC4KbFNbskuYSNMftsG2ocKuY-i9ezVA'
+
+        const fieldsOrder = submissionForm.children
+          .filter(el => el.name)
+          .map(formElement => formElement.name.split('.')[1])
+
+        const formatSubmissionData = rawSubmissionData => {
+          return Object.keys(rawSubmissionData).reduce((acc, key) => {
+            return { ...acc, [key]: rawSubmissionData[key].toString() }
+          }, {})
+        }
+
+        const publishArticleInGoogleSheets = async submissionData => {
+          const formattedSubmissionData = formatSubmissionData(submissionData)
+
+          const { articleURL } = formattedSubmissionData
+          const doc = new GoogleSpreadsheet(spreadsheetId)
+
+          await doc.useServiceAccountAuth({
+            client_email: credentials.client_email,
+            private_key: credentials.private_key,
+          })
+
+          await doc.loadInfo()
+          const sheet = doc.sheetsByIndex[0]
+          const rows = await sheet.getRows()
+
+          const indexOfExistingArticle = rows.findIndex(
+            row => row.articleURL === articleURL,
+          )
+
+          if (indexOfExistingArticle !== -1) {
+            fieldsOrder.forEach(fieldName => {
+              rows[indexOfExistingArticle][fieldName] =
+                formattedSubmissionData[fieldName] || ''
+            })
+            await rows[indexOfExistingArticle].save()
+          } else {
+            await sheet.addRow({ ...formattedSubmissionData })
+          }
+        }
+
+        try {
+          await publishArticleInGoogleSheets(manuscript.submission)
+
+          const updatedManuscript = await ctx.models.Manuscript.query().updateAndFetchById(
+            id,
+            {
+              published: new Date(),
+              status: 'published',
+            },
+          )
+
+          return updatedManuscript
+        } catch (e) {
+          // eslint-disable-next-line
+          console.log('error while publishing in google spreadsheet')
+          // eslint-disable-next-line
+          console.log(e)
+          return null
+        }
+      }
 
       if (!manuscript.published && process.env.INSTANCE_NAME === 'aperture') {
         manuscript = ctx.models.Manuscript.query().updateAndFetchById(id, {
-- 
GitLab