diff --git a/backend/src/db/migrate/store.js b/backend/src/db/migrate/store.js index f3f7e563f..5d39fb82c 100644 --- a/backend/src/db/migrate/store.js +++ b/backend/src/db/migrate/store.js @@ -1,11 +1,60 @@ import { getDriver, getNeode } from '../../db/neo4j' +import { hashSync } from 'bcryptjs' +import { v4 as uuid } from 'uuid' + +const defaultAdmin = { + email: 'admin@example.org', + password: hashSync('1234', 10), + name: 'admin', + id: uuid(), + slug: 'admin', +} + +const createDefaultAdminUser = async (session) => { + const readTxResultPromise = session.readTransaction(async (txc) => { + const result = await txc.run('MATCH (user:User) RETURN count(user) AS userCount') + return result.records.map((r) => r.get('userCount')) + }) + let createAdmin = false + try { + const userCount = parseInt(String(await readTxResultPromise)) + if (userCount === 0) createAdmin = true + } catch (error) { + console.log(error) // eslint-disable-line no-console + } + if (createAdmin) { + const createAdminTxResultPromise = session.writeTransaction(async (txc) => { + txc.run( + `MERGE (e:EmailAddress { + email: "${defaultAdmin.email}", + createdAt: toString(datetime()) + })-[:BELONGS_TO]->(u:User { + name: "${defaultAdmin.name}", + encryptedPassword: "${defaultAdmin.password}", + role: "admin", + id: "${defaultAdmin.id}", + slug: "${defaultAdmin.slug}", + createdAt: toString(datetime()), + deleted: false, + disabled: false + })-[:PRIMARY_EMAIL]->(e)`, + ) + }) + try { + await createAdminTxResultPromise + console.log('Successfully created default admin user') // eslint-disable-line no-console + } catch (error) { + console.log(error) // eslint-disable-line no-console + } + } +} class Store { async init(next) { const neode = getNeode() const { driver } = neode const session = driver.session() - // eslint-disable-next-line no-console + await createDefaultAdminUser(session) const writeTxResultPromise = session.writeTransaction(async (txc) => { await txc.run('CALL apoc.schema.assert({},{},true)') // drop all indices return Promise.all(