From d22a0eea9feb65ae83aa4944a21e82d6bf196287 Mon Sep 17 00:00:00 2001
From: Ben Whitmore <ben.whitmore0@gmail.com>
Date: Fri, 19 Mar 2021 15:05:55 +1300
Subject: [PATCH] refactor(form-builder): rationalise form query

---
 .../component-submit/src/components/SubmitPage.js    |  8 ++++----
 config/permissions.js                                |  2 +-
 server/formbuilder/src/resolvers.js                  | 12 ++----------
 server/formbuilder/src/typeDefs.js                   |  3 +--
 4 files changed, 8 insertions(+), 17 deletions(-)

diff --git a/app/components/component-submit/src/components/SubmitPage.js b/app/components/component-submit/src/components/SubmitPage.js
index d58e5e020b..68f4a5caaa 100644
--- a/app/components/component-submit/src/components/SubmitPage.js
+++ b/app/components/component-submit/src/components/SubmitPage.js
@@ -121,7 +121,7 @@ const fragmentFields = `
 `
 
 const query = gql`
-  query($id: ID!, $form: String!) {
+  query($id: ID!, $formId: String!) {
     currentUser {
       id
       username
@@ -141,7 +141,7 @@ const query = gql`
       }
     }
 
-    getFile(form: $form)
+    getForm(formId: $formId)
   }
 `
 
@@ -182,7 +182,7 @@ const SubmitPage = ({ match, history }) => {
   }
 
   const { data, loading, error } = useQuery(query, {
-    variables: { id: match.params.version, form: 'submit' },
+    variables: { id: match.params.version, formId: 'submit' },
     partialRefetch: true,
   })
 
@@ -194,7 +194,7 @@ const SubmitPage = ({ match, history }) => {
   if (error) return JSON.stringify(error)
 
   const manuscript = data?.manuscript
-  const form = data?.getFile
+  const form = data?.getForm
 
   const updateManuscript = (versionId, manuscriptDelta) => {
     update({
diff --git a/config/permissions.js b/config/permissions.js
index 0738a32331..5b3c9f521b 100644
--- a/config/permissions.js
+++ b/config/permissions.js
@@ -304,7 +304,7 @@ const permissions = {
     manuscripts: allow,
     manuscript: allow,
     messages: allow,
-    getFile: allow, // this is a query that gets the form
+    getForm: allow,
     user: allow,
   },
   Mutation: {
diff --git a/server/formbuilder/src/resolvers.js b/server/formbuilder/src/resolvers.js
index 5fb84808e2..0b0d71e47c 100644
--- a/server/formbuilder/src/resolvers.js
+++ b/server/formbuilder/src/resolvers.js
@@ -1,6 +1,5 @@
 const config = require('config')
 const fs = require('fs')
-const path = require('path')
 const { readFiles, mkdirp } = require('./util')
 
 const writeJson = (filePath, object) =>
@@ -137,13 +136,6 @@ const resolvers = {
     },
   },
   Query: {
-    async getFile() {
-      return JSON.parse(
-        fs.readFileSync(
-          path.join(__dirname, '../../../app/storage/forms/submit.json'),
-        ),
-      )
-    },
     async getForms() {
       try {
         const folderPath = `${config.get(
@@ -164,9 +156,9 @@ const resolvers = {
         )}/`
 
         const filePath = `${folderPath}${formId}.json`
-        const forms = JSON.parse(fs.readFileSync(filePath, 'utf8'))
+        const form = JSON.parse(fs.readFileSync(filePath, 'utf8'))
 
-        return forms
+        return form
       } catch (err) {
         throw new Error(err)
       }
diff --git a/server/formbuilder/src/typeDefs.js b/server/formbuilder/src/typeDefs.js
index ab4ee97d73..a7e95d05b2 100644
--- a/server/formbuilder/src/typeDefs.js
+++ b/server/formbuilder/src/typeDefs.js
@@ -1,7 +1,6 @@
 const typeDefs = `
   extend type Query {
-    getFile(form: String!): Form
-    getForm(form: String!): Form
+    getForm(formId: String!): Form
     getForms: Form
   }
 
-- 
GitLab