Skip to content

Commit

Permalink
feat: add component @cocreate/config, improves onboarding process usi…
Browse files Browse the repository at this point in the history
…ng a combination of local configs, global configs and prompts
  • Loading branch information
frankpagan committed Jun 13, 2023
1 parent a584c73 commit 35579f6
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 128 deletions.
14 changes: 5 additions & 9 deletions CoCreate.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
module.exports = {
"organization_id": "5ff747727005da1c272740ab",
"organization_id": "",
"key": "",
"host": "",
"storage": {
"mongodb": {
"provider": "mongodb",
"url": [
"mongodb+srv://cocreate-app:[email protected]/?retryWrites=true&w=majority"
]
}
},
"directories": [
{
"entry": "./superadmin",
Expand Down Expand Up @@ -104,6 +96,10 @@ module.exports = {
'path': '../CoCreate-components/CoCreate-cli',
'repo': 'github.com/CoCreate-app/CoCreate-cli.git'
},
{
'path': '../CoCreate-components/CoCreate-config',
'repo': 'github.com/CoCreate-app/CoCreate-config.git'
},
{
'path': '../CoCreate-components/CoCreate-docs',
'repo': 'github.com/CoCreate-app/CoCreate-docs.git'
Expand Down
226 changes: 127 additions & 99 deletions installation/createDB.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,143 @@
// TODO: replace mongodb with @cocreate/crud to support multiple databases
const { MongoClient, ObjectId } = require('mongodb');
const cli = require('@cocreate/cli');
const Config = require('@cocreate/config')
const uuid = require('@cocreate/uuid');
const crudServer = require('@cocreate/crud-server')

const fs = require('fs');
const path = require("path")
let config = await cli.config([
{
key: 'storage',
prompt: 'Enter a JSON.stringify storage object'

module.exports = async function () {

let config = await Config({
'name': {
prompt: 'Enter a friendly name for the new storage: ',
variable: true
},
'storage.{{name}}.provider': {
prompt: 'Enter the storage provider, ex mongodb: '
},
'storage.{{name}}.url': {
prompt: 'Enter the storage providers url: '
}
}, false, false)

const storage = {}
if (!config.name && !config.provider && !config.url) {
console.log('Could not find a url in your storage object')
process.exit()
}
])

if (typeof config.storage === 'string')
config.storage = JSON.parse(config.storage)
let databases = Object.keys(config.storage)
let dbUrl = databases[0].url[0]
if (dbUrl)
update(dbUrl)
else {
console.log('Could not find a url in your storage object')
process.exit()
}


async function update(dbUrl) {
const dbClient = await MongoClient.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
const organization_id = config.organization_id || `${ObjectId()}`
const key = config.key || uuid.generate(32);
const user_id = config.user_id || `${ObjectId()}`;

console.log(organization_id, key, user_id)

try {
// Create organization
const organizations = dbClient.db(organization_id).collection('organizations');

let organization = {};
organization._id = ObjectId(organization_id);
organization.host = config.host
organization.storage = config.storage
organization.organization_id = organization_id;
await organizations.insertOne(organization);

// Create user
const users = dbClient.db(organization_id).collection('users');
let user = {};
user['_id'] = ObjectId(user_id);
user['firstname'] = 'Admin'
user['organization_id'] = organization_id;
await users.insertOne(user);

// Create default key
const permissions = dbClient.db(organization_id).collection('keys');
let data = {
type: "key",
key: key,
actions: {
"signIn": "",
"signUp": ""
},
default: true,
organization_id
storage[config.name] = { provider: config.provider, url: config.url }
update()

async function update() {
const organization_id = config.organization_id || `${crudServer.ObjectId()}`
const key = config.key || uuid.generate(32);
const user_id = config.user_id || `${crudServer.ObjectId()}`;

console.log(organization_id, key, user_id)

try {
// Create organization
await crudServer.createDocument({
database: organization_id,
collection: 'organizations',
document: {
_id: ObjectId(organization_id),
host: config.host,
storage: config.storage,
organization_id
},
organization_id
})

// Create user
await crudServer.createDocument({
database: organization_id,
collection: 'users',
document: {
_id: ObjectId(user_id),
firstname: 'Admin',
organization_id
},
organization_id
})


// Create default key
await crudServer.createDocument({
database: organization_id,
collection: 'keys',
document: {
type: "key",
key: key,
actions: {
"signIn": "",
"signUp": ""
},
default: true,
organization_id
},
organization_id
})

// Create admin role
let role_id = crudServer.ObjectId();
await crudServer.createDocument({
database: organization_id,
collection: 'keys',
document: {
_id: role_id,
type: "role",
name: "admin",
admin: "true",
organization_id
},
organization_id
})


// Create user key
await crudServer.createDocument({
database: organization_id,
collection: 'keys',
document: {
type: "user",
key: user_id,
roles: [role_id],
organization_id
},
organization_id
})

if (organization_id && key) {
updateConfig(organization_id, key)
return organization_id
}

} catch (error) {
console.log(error)
}
await permissions.insertOne(data);

// Create admin role
let role = {
_id: ObjectId(),
type: "role",
name: "admin",
admin: "true",
organization_id: organization_id
};
await permissions.insertOne(role);

// Create user key
let userKey = {
type: "user",
key: user_id,
roles: [role._id],
organization_id: organization_id
};
await permissions.insertOne(userKey);

if (organization_id && key)
updateConfig(organization_id, key)


} catch (error) {
console.log(error)
}
}

function updateConfig(organization_id, key) {
const ppath = './'
let configfile = path.resolve(ppath, 'CoCreate.config.js');
if (!fs.existsSync(configfile))
return console.error('path does not exist:', configfile)
function updateConfig(organization_id, key) {
const ppath = './'
let configfile = path.resolve(ppath, 'CoCreate.config.js');
if (!fs.existsSync(configfile))
return console.error('path does not exist:', configfile)

let object = require(configfile)
object.organization_id = organization_id
object.key = key
let object = require(configfile)
object.organization_id = organization_id
object.key = key

let config = `module.exports = ${JSON.stringify(object, null, 4)}`
let config = `module.exports = ${JSON.stringify(object, null, 4)}`

if (fs.existsSync(configfile))
fs.unlinkSync(configfile)
fs.writeFileSync(configfile, config)
if (fs.existsSync(configfile))
fs.unlinkSync(configfile)
fs.writeFileSync(configfile, config)

console.log('updated: ', configfile)
process.exit()
console.log('updated: ', configfile)
process.exit()

}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@cocreate/authenticate": "^1.0.16",
"@cocreate/authorize": "^1.3.12",
"@cocreate/cli": "^1.33.8",
"@cocreate/config": "^1.0.0",
"@cocreate/crud-server": "^1.23.11",
"@cocreate/file-server": "^1.6.12",
"@cocreate/industry": "^1.11.29",
Expand Down
44 changes: 24 additions & 20 deletions src/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,40 @@ const unique = require('@cocreate/unique');
const users = require('@cocreate/users');
const authenticate = require('@cocreate/authenticate')
const authorize = require("@cocreate/authorize");
const Config = require("@cocreate/config");
const createDb = require('../installation/createDB');

module.exports.init = async function (server) {
try {
let config = await cli.config([
{
key: 'organization_id',
let config = await Config({
organization_id: {
prompt: 'Enter your organization_id or continue and one will created: '
}
])
})

if (!config.organization_id)
require('@cocreate/createDB');
config.organization_id = await createDb()

const databases = {
mongodb: require('@cocreate/mongodb')
}
const wsManager = new socketServer(server, 'ws')
wsManager.authenticate = authenticate

const crud = new crudServer(wsManager, databases)
wsManager.authorize = new authorize(crud)
if (config.organization_id) {
const databases = {
mongodb: require('@cocreate/mongodb')
}
const wsManager = new socketServer(server, 'ws')
wsManager.authenticate = authenticate

new fileServer(server, crud, new serverSideRender(crud));
new messageServer(wsManager);
new industry(crud);
new metricsServer(crud);
new unique(crud);
new organizations(crud);
new users(crud);
const crud = new crudServer(wsManager, databases)
wsManager.authorize = new authorize(crud)

new fileServer(server, crud, new serverSideRender(crud));
new messageServer(wsManager);
new industry(crud);
new metricsServer(crud);
new unique(crud);
new organizations(crud);
new users(crud);
} else {
console.log('organization_id could not be found')
}
} catch (error) {
console.error(error)
return {
Expand Down

0 comments on commit 35579f6

Please sign in to comment.