From 7639a10d821acb27a1d6bd20e1aa9627e21d0288 Mon Sep 17 00:00:00 2001
From: viktorstrate <viktorstrate@gmail.com>
Date: Tue, 30 Jul 2019 00:41:41 +0200
Subject: [PATCH] Start on initial wizard

---
 api/src/index.js              | 16 +++++++-
 api/src/resolvers/siteInfo.js | 71 +++++++++++++++++++++++++++++++++++
 api/src/scanner/scanUser.js   |  2 +
 api/src/schema.graphql        |  9 ++++-
 4 files changed, 95 insertions(+), 3 deletions(-)
 create mode 100644 api/src/resolvers/siteInfo.js

diff --git a/api/src/index.js b/api/src/index.js
index af3fd1b..34342d0 100644
--- a/api/src/index.js
+++ b/api/src/index.js
@@ -38,9 +38,15 @@ const typeDefs = fs
 import usersResolver from './resolvers/users'
 import scannerResolver from './resolvers/scanner'
 import photosResolver from './resolvers/photos'
+import siteInfoResolver from './resolvers/siteInfo'
 import { isRawImage, getImageCachePath } from './scanner/utils'
 
-const resolvers = [usersResolver, scannerResolver, photosResolver]
+const resolvers = [
+  usersResolver,
+  scannerResolver,
+  photosResolver,
+  siteInfoResolver,
+]
 
 const schema = makeAugmentedSchema({
   typeDefs,
@@ -52,7 +58,13 @@ const schema = makeAugmentedSchema({
     mutation: false,
     query: true,
     query: {
-      exclude: ['ScannerResult', 'AuthorizeResult', 'Subscription', 'PhotoURL'],
+      exclude: [
+        'ScannerResult',
+        'AuthorizeResult',
+        'Subscription',
+        'PhotoURL',
+        'SiteInfo',
+      ],
     },
   },
   resolvers: resolvers.reduce((prev, curr) => _.merge(prev, curr), {}),
diff --git a/api/src/resolvers/siteInfo.js b/api/src/resolvers/siteInfo.js
new file mode 100644
index 0000000..bd398d5
--- /dev/null
+++ b/api/src/resolvers/siteInfo.js
@@ -0,0 +1,71 @@
+import { neo4jgraphql } from 'neo4j-graphql-js'
+
+async function initialSetup(driver) {
+  const session = driver.session()
+
+  await session.run(
+    `MERGE (info:SiteInfo) ON CREATE SET info = {initialSettings}`,
+    {
+      initialSettings: {
+        initialSetup: true,
+        signupEnabled: false,
+        defaultRoot: '/tmp',
+      },
+    }
+  )
+
+  session.close()
+}
+
+const Query = {
+  async siteInfo(root, args, ctx, info) {
+    await initialSetup(ctx.driver)
+
+    return neo4jgraphql(root, args, ctx, info)
+  },
+}
+
+const Mutation = {
+  async initialSetupWizard(root, args, ctx, info) {
+    await initialSetup(ctx.driver)
+
+    const session = ctx.driver.session()
+
+    const result = await session.run(`MATCH (i:SiteInfo) RETURN i`)
+
+    const siteInfo = result.records[0].get('i').properties
+
+    if (siteInfo.initialSetup == false) {
+      return {
+        success: false,
+        status: 'Has already been setup',
+        token: null,
+      }
+    }
+
+    session.close()
+
+    await session.run(
+      `MERGE (info:SiteInfo) ON CREATE SET info = {initialSettings}`,
+      {
+        initialSettings: {
+          initialSetup: true,
+          signupEnabled: false,
+        },
+      }
+    )
+
+    session.close()
+
+    return {
+      success: true,
+      status: 'Setup successful',
+      token: null,
+    }
+  },
+}
+
+export default {
+  Query,
+  Mutation,
+}
diff --git a/api/src/scanner/scanUser.js b/api/src/scanner/scanUser.js
index 3152f0b..7dfef16 100644
--- a/api/src/scanner/scanUser.js
+++ b/api/src/scanner/scanUser.js
@@ -124,6 +124,8 @@ export default async function scanUser({ driver, scanAlbum }, user) {
     return { foundImage: foundImageOrAlbum, newAlbums }
   }
 
+  await fs.mkdirp(user.rootPath)
+
   await scanPath(user.rootPath)
 
   const session = driver.session()
diff --git a/api/src/schema.graphql b/api/src/schema.graphql
index 4892eb0..ca9e859 100644
--- a/api/src/schema.graphql
+++ b/api/src/schema.graphql
@@ -62,6 +62,7 @@ type Photo {
 type SiteInfo {
   signupEnabled: Boolean!
   initialSetup: Boolean!
+  defaultRoot: String! @hasRole(roles: [Admin])
 }
 
 type AuthorizeResult {
@@ -72,7 +73,7 @@ type AuthorizeResult {
 
 type ScannerResult {
   finished: Boolean!
-  error: Boolean!
+  success: Boolean!
   errorMessage: String
   progress: Float
 }
@@ -88,6 +89,12 @@ type Mutation {
     @neo4j_ignore
 
   scanAll: ScannerResult! @isAuthenticated @neo4j_ignore
+
+  initialSetupWizard(
+    username: String!
+    password: String!
+    defaultRoot: String!
+  ): AuthorizeResult @neo4j_ignore
 }
 
 type Query {
