@cocreate/crud-server
Advanced tools
Comparing version 1.1.6 to 1.1.7
@@ -0,1 +1,8 @@ | ||
## [1.1.7](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.1.6...v1.1.7) (2021-07-23) | ||
### Bug Fixes | ||
* industry creates in org defined in config ([5cf9139](https://github.com/CoCreate-app/CoCreate-crud-server/commit/5cf91399a34b800ae3253f4fcd2785eb52f3cd9b)) | ||
## [1.1.6](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.1.5...v1.1.6) (2021-07-22) | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "@cocreate/crud-server", | ||
"version": "1.1.6", | ||
"version": "1.1.7", | ||
"description": "CoCreate-crud-server", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
const CoCreateBase = require("./base"); | ||
const {ObjectID, Binary} = require("mongodb"); | ||
// const {ObjectID, Binary} = require("mongodb"); | ||
@@ -14,3 +14,2 @@ class CoCreateOrganization extends CoCreateBase { | ||
this.wsManager.on('createOrg', (socket, data, roomInfo) => this.createOrg(socket, data)); | ||
this.wsManager.on('createUser', (socket, data, roomInfo) => this.createUser(socket, data)); | ||
this.wsManager.on('deleteOrg', (socket, data, roomInfo) => this.deleteOrg(socket, data)); | ||
@@ -26,2 +25,3 @@ } | ||
const collection = this.getCollection(data); | ||
// create new org in config db organization collection | ||
collection.insertOne({ ...data.data, organization_id: data.organization_id }, function(error, result) { | ||
@@ -31,2 +31,3 @@ if(!error && result){ | ||
const anotherCollection = self.getDB(orgId).collection(data['collection']); | ||
// Create new org db and insert organization | ||
anotherCollection.insertOne({...result.ops[0], organization_id : orgId}); | ||
@@ -43,31 +44,6 @@ | ||
} | ||
}); | ||
}catch(error){ | ||
console.log('createDocument error', error); | ||
} | ||
} | ||
async createUser(socket, data) { | ||
const self = this; | ||
if(!data.data) return; | ||
try{ | ||
const collection = this.getCollection(data); | ||
collection.insertOne(data.data, function(error, result) { | ||
if(!error && result){ | ||
const copyDB = data.copyDB; | ||
if (copyDB) { | ||
const anotherCollection = self.getDB(copyDB).collection(data['collection']); | ||
anotherCollection.insertOne(result.ops[0]); | ||
} | ||
// add new org to masterDb | ||
const masterOrgDb = self.getDB(data.mdb).collection(data['collection']); | ||
masterOrgDb.insertOne({...result.ops[0], organization_id : data['mdb']}); | ||
const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0]} | ||
self.wsManager.send(socket, 'createUser', response, data['organization_id']); | ||
// if (data.room) { | ||
// self.wsManager.broadcast(socket, data.namespace || data['organization_id'] , data.room, 'createDocument', response, true); | ||
// } else { | ||
// self.wsManager.broadcast(socket, data.namespace || data['organization_id'], null, 'createDocument', response) | ||
// } | ||
} | ||
}); | ||
@@ -79,2 +55,3 @@ }catch(error){ | ||
async deleteOrg(socket, data) { | ||
@@ -81,0 +58,0 @@ const self = this; |
@@ -13,2 +13,3 @@ | ||
if (this.wsManager) { | ||
this.wsManager.on('createUser', (socket, data, roomInfo) => this.createUser(socket, data)); | ||
this.wsManager.on('checkUnique', (socket, data, roomInfo) => this.checkUnique(socket, data, roomInfo)); | ||
@@ -22,2 +23,35 @@ this.wsManager.on('login', (socket, data, roomInfo) => this.login(socket, data, roomInfo)) | ||
async createUser(socket, data) { | ||
const self = this; | ||
if(!data.data) return; | ||
try{ | ||
const collection = this.getCollection(data); | ||
// Create new user in config db users collection | ||
collection.insertOne(data.data, function(error, result) { | ||
if(!error && result){ | ||
const orgDB = data.orgDB; | ||
// if new orgDb Create new user in new org db users collection | ||
// if (orgDB == data.organization_id) { | ||
if (orgDB) { | ||
const anotherCollection = self.getDB(orgDB).collection(data['collection']); | ||
anotherCollection.insertOne({...result.ops[0], organization_id : orgDB}); | ||
} | ||
const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0]} | ||
self.wsManager.send(socket, 'createUser', response, data['organization_id']); | ||
// if (data.room) { | ||
// self.wsManager.broadcast(socket, data.namespace || data['organization_id'] , data.room, 'createDocument', response, true); | ||
// } else { | ||
// self.wsManager.broadcast(socket, data.namespace || data['organization_id'], null, 'createDocument', response) | ||
// } | ||
} | ||
}); | ||
}catch(error){ | ||
console.log('createDocument error', error); | ||
} | ||
} | ||
/** | ||
@@ -92,4 +126,4 @@ data = { | ||
try { | ||
const {organization_id, db} = req_data | ||
const selectedDB = db || organization_id; | ||
const {organization_id} = req_data | ||
const selectedDB = organization_id; | ||
const collection = self.getDB(selectedDB).collection(req_data["data-collection"]); | ||
@@ -110,4 +144,4 @@ const query = new Object(); | ||
success: false, | ||
message: "We can't login", | ||
status: "failure" | ||
message: "Login failed", | ||
status: "failed" | ||
} | ||
@@ -118,9 +152,11 @@ if (!error && result && result.length > 0) { | ||
token = await self.wsManager.authInstance.generateToken({user_id: result[0]['_id']}); | ||
} | ||
} | ||
response = { ...response, | ||
success: true, | ||
id: result[0]['_id'], | ||
// collection: collection, | ||
document_id: result[0]['_id'], | ||
current_org: result[0]['current_org'], | ||
message: "Login successfuly", | ||
message: "Login successful", | ||
status: "success", | ||
@@ -130,6 +166,8 @@ token | ||
} | ||
console.log('before socket', response); | ||
self.wsManager.send(socket, 'login', response, req_data['organization_id']) | ||
console.log('success socket', req_data['organization_id']); | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
console.log('login failed', error); | ||
} | ||
@@ -136,0 +174,0 @@ } |
99858
1539