/* eslint-disable no-console */

import prompt from 'prompt';
import bcrypt from 'bcryptjs';

import config from '../src/utils/config';
import knex from '../src/utils/db';

const saltRounds = config.auth.saltRounds;
const schema = {
  properties: {
    email: {
      required: true,
    },
    password: {
      required: true,
      hidden: true,
    },
  },
};

console.log('Enter credentials of new admin user:');
prompt.start();

prompt.get(schema, (err, result) => {
  if (err) {
    process.exit(1);
  }

  bcrypt.genSalt(saltRounds, (saltErr, salt) => {
    if (saltErr) {
      console.log(saltErr);
      process.exit(1);
    }

    bcrypt.hash(result.password, salt, (hashErr, hash) => {
      if (hashErr) {
        console.log(hashErr);
        process.exit(1);
      } else {
        knex('users')
          .insert({
            email: result.email,
            password: hash,
            scope: 'admin',
          })
          .then(() => {
            console.log('Successfully created new admin user.');
            process.exit(0);
          });
      }
    });
  });
});

@@ -670,16 +670,22 @@
 d, salt,
+ async
  (hashEr
@@ -797,19 +797,85 @@
         
-kne
+await knex.transaction(async trx =%3E %7B%0A          const user = await tr
 x('users
@@ -877,16 +877,18 @@
 users')%0A
+  
         
@@ -899,16 +899,18 @@
 nsert(%7B%0A
+  
         
@@ -950,93 +950,265 @@
     
-password: hash,%0A            scope: 'admin',%0A          %7D)%0A          .then(() =%3E %7B%0A    
+  scope: 'admin',%0A            %7D)%0A            .returning('*')%0A            .then(results =%3E results%5B0%5D); // return only first result%0A%0A          await trx('secrets').insert(%7B%0A            ownerId: user.id,%0A            password: hash,%0A          %7D);%0A        %7D);%0A%0A
     
@@ -1268,28 +1268,24 @@
 ');%0A        
-    
 process.exit
@@ -1289,30 +1289,16 @@
 xit(0);%0A
-          %7D);%0A
       %7D%0A

Fix register_admin.js to work with users/secrets tables