Skip to content

Commit

Permalink
fix: createDb functions
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed May 7, 2023
1 parent d63d20c commit 088fed7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 46 deletions.
2 changes: 2 additions & 0 deletions CoCreate.config.js.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module.exports = {
"organization": {
"_id": "<If Known or leave empty>",
"name": "<organization, company or project name>",
"hosts": [
"<CoCreate subdomain or custom domain pointing to CoCreateWS server>",
]
},
"user": {
"_id": "<If Known or leave empty>",
"name": "admin",
"email": "<email to use for loggin>",
"password": "<password to use for loggin>"
Expand Down
28 changes: 0 additions & 28 deletions installation/CoCreate.config.js

This file was deleted.

40 changes: 23 additions & 17 deletions installation/createDB.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
const { ObjectId } = require("mongodb");
const { mongoClient } = require("../src/db")
const config = require('./CoCreate.config');
// const { ObjectId } = require("mongodb");
const { MongoClient, ObjectId } = require('mongodb');
const config = require('../CoCreate.config');
const CoCreateUUID = require('@cocreate/uuid');
// const { updateConfig } = require('./updateConfig');

const fs = require('fs');
const path = require("path")
const prettier = require("prettier");

mongoClient().then(dbClient => {
update(dbClient)
});

async function update(dbClient){
const organization_id = `${ObjectId()}`
const apiKey = CoCreateUUID.generate(32);
let user_id = '';
let dbUrl = config.database.url[0]
if (dbUrl)
update(dbUrl)
async function update(dbUrl) {
let dbClient = await MongoClient.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
const organization_id = config.config.organization_id || `${ObjectId()}`
const apiKey = config.config.apiKey || CoCreateUUID.generate(32);
let user_id = config.user._id;
console.log(organization_id, apiKey, user_id)

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

let organization = config.organization;
organization.organization_id = organization_id;
organization._id = ObjectId(organization_id);
organization.apiKey = apiKey;
organization.databases = {
mongodb: {name: config.database.name, url: config.database.url}
}
organization.organization_id = organization_id;

await organizations.insertOne(organization);

// Create apiKey permission
Expand Down Expand Up @@ -55,7 +61,7 @@ async function update(dbClient){
"runIndustry": "",
"sendgrid": ["sendEmail"]
},
"admin": "false"
"admin": "true"
}
await permissions.insertOne(data);
}
Expand All @@ -64,6 +70,7 @@ async function update(dbClient){
if (organization_id) {
const users = dbClient.db(organization_id).collection('users');
let user = config.user;
user['_id'] = ObjectId(user._id);
user['password'] = encryptPassword(user.password);
user['connected_orgs'] = [organization_id];
user['current_org'] = organization_id;
Expand Down Expand Up @@ -134,9 +141,8 @@ function updateConfig(organization_id, apiKey) {
Object.assign(object.config, {apiKey})
delete object.organization
delete object.user
let str = JSON.stringify(object)
let formated = prettier.format(str, { semi: false, parser: "json" });
let config = `module.exports = ${formated}`

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

if (fs.existsSync(configfile))
fs.unlinkSync(configfile)
Expand Down
1 change: 0 additions & 1 deletion installation/updateConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

const fs = require('fs');
const path = require("path")
const prettier = require("prettier");

function updateConfig(organization_id, apiKey) {
const ppath = './'
Expand Down

0 comments on commit 088fed7

Please sign in to comment.