let models = require('../');
let userModels = require('../../user');



//create roles

async function getOrCreateSystemAdmin() {
  let role = await models.Role.findOne({
    permissions: ['*'],
  })

  if (role) return role;

  role = models.Role({
    name: '系统管理员',
    permissions: ['*'],
  })
  role = await role.save();
  return role;
}


async function authAdmin() {
  let adminRole = await getOrCreateSystemAdmin();
  let user = await userModels.User.findOne({
    username: 'chengen'
  });
  let userRole = models.UserRole({
    user: user,
    role: adminRole,
  })
  return await userRole.save();
}


async function start() {
  await authAdmin().catch(console.error);
}



if (require.main == module) {
  start()
    .catch(console.error)
    .then(() => {
      require('process').exit()
    })
}
