164 lines
6.0 KiB
JavaScript
164 lines
6.0 KiB
JavaScript
|
|
const db = require('mantra-db-models');
|
||
|
|
const prompt = require("prompt")
|
||
|
|
|
||
|
|
db.init().then(() => {
|
||
|
|
// We could probably run the things in the seeders...
|
||
|
|
db.Role.findAll()
|
||
|
|
.then(roles => {
|
||
|
|
if(roles.length == 0) {
|
||
|
|
return db.Role.bulkCreate([
|
||
|
|
{
|
||
|
|
type: "user",
|
||
|
|
description: "All users should have this role..."
|
||
|
|
},
|
||
|
|
{
|
||
|
|
type: "admin",
|
||
|
|
description: "This role takes care of the things in the club..."
|
||
|
|
},
|
||
|
|
{
|
||
|
|
type: "superadmin",
|
||
|
|
description: "The user that can do all the things on the system..."
|
||
|
|
}
|
||
|
|
]);
|
||
|
|
} else {
|
||
|
|
return roles;
|
||
|
|
}
|
||
|
|
})
|
||
|
|
.then(roles => {
|
||
|
|
// Roles have been made and all...
|
||
|
|
console.log()
|
||
|
|
// Now we can create the admin account...
|
||
|
|
var schema = {
|
||
|
|
properties: {
|
||
|
|
email: {
|
||
|
|
description: 'Enter admin email', // Prompt displayed to the user. If not supplied name will be used.
|
||
|
|
type: 'string',
|
||
|
|
message: 'Please enter admin email',
|
||
|
|
required: true
|
||
|
|
},
|
||
|
|
displayName: {
|
||
|
|
description: 'Enter admin displayName', // Prompt displayed to the user. If not supplied name will be used.
|
||
|
|
type: 'string',
|
||
|
|
pattern: /^[a-zA-Z\s\-]+$/,
|
||
|
|
message: 'Name must be only letters, spaces, or dashes',
|
||
|
|
required: true
|
||
|
|
},
|
||
|
|
// phoneNumber: {
|
||
|
|
// description: `Enter admin phonenumber (please use country calling code)`, // Prompt displayed to the user. If not supplied name will be used.
|
||
|
|
// type: 'string',
|
||
|
|
// pattern: /^[1-9][0-9]*$/,
|
||
|
|
// message: 'Please enter phone number with the country code',
|
||
|
|
// required: true
|
||
|
|
// },
|
||
|
|
password: {
|
||
|
|
description: 'Enter admin password', // Prompt displayed to the user. If not supplied name will be used.
|
||
|
|
type: 'string',
|
||
|
|
message: 'Please enter admin password',
|
||
|
|
hidden: true,
|
||
|
|
replace: '*',
|
||
|
|
required: true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
prompt.get(schema, async function (err, result) {
|
||
|
|
if (err) {
|
||
|
|
console.error(err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
db.User.create({
|
||
|
|
userEmails: [
|
||
|
|
{
|
||
|
|
email: {
|
||
|
|
address: result.email
|
||
|
|
}
|
||
|
|
}
|
||
|
|
],
|
||
|
|
activeEmail: result.email,
|
||
|
|
displayName: result.displayName,
|
||
|
|
// userPhoneNumbers: [
|
||
|
|
// {
|
||
|
|
// phoneNumber: {
|
||
|
|
// number: result.phoneNumber,
|
||
|
|
// confirmedOn: new Date()
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
// ],
|
||
|
|
passwords: [{
|
||
|
|
password: result.password
|
||
|
|
}],
|
||
|
|
emailConfirmedOn: Date.now(),
|
||
|
|
activatedOn: Date.now()
|
||
|
|
}, {
|
||
|
|
include: [
|
||
|
|
{
|
||
|
|
association: db.User.Passwords
|
||
|
|
},
|
||
|
|
{
|
||
|
|
association: db.User.UserEmails,
|
||
|
|
include: [
|
||
|
|
{
|
||
|
|
association: db.UserEmail.Email
|
||
|
|
}
|
||
|
|
]
|
||
|
|
}
|
||
|
|
// {
|
||
|
|
// association: db.User.UserPhoneNumbers,
|
||
|
|
// include: [
|
||
|
|
// {
|
||
|
|
// association: db.UserPhoneNumber.PhoneNumber
|
||
|
|
// }
|
||
|
|
// ]
|
||
|
|
// }
|
||
|
|
]
|
||
|
|
})
|
||
|
|
.then(async (user) => {
|
||
|
|
const individualEntity = await db.IndividualEntityUser.create({
|
||
|
|
userId: user.id,
|
||
|
|
entityUser: {
|
||
|
|
userId: user.id,
|
||
|
|
type: "admin",
|
||
|
|
entity: {
|
||
|
|
name: result.displayName,
|
||
|
|
type: "individual"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
include: [
|
||
|
|
{
|
||
|
|
association: db.IndividualEntityUser.EntityUser,
|
||
|
|
include: [
|
||
|
|
{
|
||
|
|
association: db.EntityUser.Entity
|
||
|
|
}
|
||
|
|
]
|
||
|
|
}
|
||
|
|
]
|
||
|
|
})
|
||
|
|
console.log("Created: ", user.firstName);
|
||
|
|
console.log("Created: ", individualEntity.entityUser.entity.name);
|
||
|
|
console.log("Updated User Password: " + user.passwords[0]);
|
||
|
|
|
||
|
|
// Create this Users Roles...
|
||
|
|
var userRoles = [];
|
||
|
|
for(var i in roles) {
|
||
|
|
var role = roles[i];
|
||
|
|
userRoles.push({
|
||
|
|
userId: user.id,
|
||
|
|
roleId: role.id
|
||
|
|
})
|
||
|
|
};
|
||
|
|
db.UserRole.bulkCreate(userRoles)
|
||
|
|
.then(dbUserRoles => {
|
||
|
|
console.log("Created User Roles: "+ JSON.stringify(dbUserRoles))
|
||
|
|
}).catch(error => {
|
||
|
|
console.error("User Role Error: ", error)
|
||
|
|
})
|
||
|
|
})
|
||
|
|
.catch(error => {
|
||
|
|
console.error("Hanlde error: " + error);
|
||
|
|
})
|
||
|
|
});
|
||
|
|
})
|
||
|
|
})
|