From 3f7a12060069005cd1218c2d4ec6ef7026fdbe26 Mon Sep 17 00:00:00 2001 From: kngako Date: Sun, 19 Jun 2022 01:35:10 +0200 Subject: [PATCH] Add scripts --- scripts/create-super-admin.js | 163 ++++++++++++++++++++++++++++++++++ scripts/init-dialects.js | 88 ++++++++++++++++++ scripts/init-license.js | 20 +++++ 3 files changed, 271 insertions(+) create mode 100644 scripts/create-super-admin.js create mode 100644 scripts/init-dialects.js create mode 100644 scripts/init-license.js diff --git a/scripts/create-super-admin.js b/scripts/create-super-admin.js new file mode 100644 index 0000000..8afb98b --- /dev/null +++ b/scripts/create-super-admin.js @@ -0,0 +1,163 @@ +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); + }) + }); + }) +}) diff --git a/scripts/init-dialects.js b/scripts/init-dialects.js new file mode 100644 index 0000000..b5ff49d --- /dev/null +++ b/scripts/init-dialects.js @@ -0,0 +1,88 @@ +const db = require('mantra-db-models'); +const prompt = require("prompt") + +db.init().then(() => { + // We could probably run the things in the seeders... + db.Language.findAll() + .then(languages => { + if (languages.length == 0) { + // TODO: Create languages + return db.Language.bulkCreate([ + { + id: "en", + name: "English", + displayName: "English", + }, + { + id: "st", + name: "Sesotho", + displayName: "Sesotho", + }, + { + id: "zu", + name: "Isizulu", + displayName: "Isizulu", + } + ]) + } else { + return languages + } + }).then(languages => { + return db.Continent.findAll() + }).then(continents => { + if (continents.length == 0) { + return db.Continent.bulkCreate([ + { + id: "afr", + name: "Africa" + } + ]) + } else { + return continents + } + }).then(continents => { + return db.Country.findAll() + }).then(countries => { + if (countries.length == 0) { + return db.Country.bulkCreate([ + { + id: "int", + name: "International" + }, + { + id: "zaf", + name: "South Africa" + } + ]) + } else { + return countries + } + }).then(countries => { + return db.Dialect.findAll() + }).then(dialects => { + if (dialects.length == 0) { + // TODO: Create dialects + return db.Dialect.bulkCreate([ + { + name: "English", + countryId: "int", + languageId: "en" + }, + { + name: "Sesotho", + countryId: "zaf", + languageId: "st" + }, + { + name: "Zulu", + countryId: "zaf", + languageId: "zu" + } + ]) + } else { + return dialects + } + }).catch(error => { + console.error("Error: ", error) + }) +}) diff --git a/scripts/init-license.js b/scripts/init-license.js new file mode 100644 index 0000000..7a4cb96 --- /dev/null +++ b/scripts/init-license.js @@ -0,0 +1,20 @@ +const db = require('mantra-db-models'); +const prompt = require("prompt") + +db.init().then(() => { + // We could probably run the things in the seeders... + db.License.findAll() + .then(licenses => { + if (licenses.length == 0) { + // TODO: Create languages + return db.License.create({ + id: "copyright", + name: "Copyright" + }) + } else { + return licenses + } + }).catch(error => { + console.error("Error: ", error) + }) +})