Skip to content

Commit

Permalink
Merge branch 'release/2.5.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Syno0 committed Apr 4, 2018
2 parents d6be9d8 + b117bc2 commit bb96151
Show file tree
Hide file tree
Showing 112 changed files with 18,836 additions and 2,748 deletions.
133 changes: 101 additions & 32 deletions database/component.js
Expand Up @@ -4,7 +4,7 @@
var models = require('../models/');

// Insert a new component link to an entity
exports.createNewComponentOnEntity = function(attr, callback) {
exports.createNewComponentOnEntity = function (attr, callback) {

var idModule = attr.id_module;
var options = attr.options;
Expand All @@ -16,8 +16,8 @@ exports.createNewComponentOnEntity = function(attr, callback) {
codeName: options.value,
id_module: idModule,
version: 1
}).then(function(createdComponent) {
createdComponent.addDataEntity(attr.id_data_entity).then(function(){
}).then(function (createdComponent) {
createdComponent.addDataEntity(attr.id_data_entity).then(function () {
var info = {
insertId: createdComponent.id,
message: "database.component.create.successOnEntity",
Expand All @@ -26,14 +26,14 @@ exports.createNewComponentOnEntity = function(attr, callback) {
callback(null, info);
});

}).catch(function(err) {
}).catch(function (err) {
callback(err, null);
});
}
}

// Insert a new component link to a module
exports.createNewComponentOnModule = function(attr, callback) {
exports.createNewComponentOnModule = function (attr, callback) {

var id_module = attr.id_module;
var options = attr.options;
Expand All @@ -44,76 +44,75 @@ exports.createNewComponentOnModule = function(attr, callback) {
codeName: options.value,
id_module: id_module,
version: 1
}).then(function(createdComponent) {
}).then(function (createdComponent) {
var info = {
insertId: createdComponent.id,
message: "database.component.create.success",
messageParams: [createdComponent.name, createdComponent.id]
};

callback(null, info);
}).catch(function(err) {
}).catch(function (err) {
callback(err, null);
});
}
}

// Get a component with a given name in a module
// Get a component with a given code name in a module
exports.getComponentByCodeNameInModule = function(idModule, codeName, displayName, callback) {

models.Component.findOne({
where:{
where: {
codeName: codeName,
id_module: idModule
}
}).then(function(component) {
}).then(function (component) {
if (!component) {
var err = new Error();
err.message = "database.component.notFound.notFoundedInModule";
err.messageParams = [displayName, idModule];
return callback(err, null);
}
callback(null, component);
}).catch(function(err) {
}).catch(function (err) {
callback(err, null);
});
}

// Get a component with a given name in a module
exports.getComponentByNameInModule = function(idModule, nameComponent, callback) {
exports.getComponentByNameInModule = function (idModule, nameComponent, callback) {

models.Component.findOne({
where:{
where: {
name: nameComponent,
id_module: idModule
}
}).then(function(component) {
}).then(function (component) {
if (!component) {
err = new Error();
err.message = "database.component.notFound.notFoundedInModule";
err.messageParams = [nameComponent, idModule];
return callback(err, null);
}
callback(null, component);
}).catch(function(err) {
}).catch(function (err) {
callback(err, null);
});
}

// Get a component codeName and the has many entity and check if the given ID entity is in
exports.checkIfComponentCodeNameExistOnEntity = function(codeNameComponent, idModule, idEntity, callback) {
exports.checkIfComponentCodeNameExistOnEntity = function (codeNameComponent, idModule, idEntity, callback) {

models.DataEntity.findOne({
where: {
id: idEntity,
id_module: idModule
}
}).then(function(foundEntity){
if(foundEntity){
}).then(function (foundEntity) {
if (foundEntity) {
var alreadyExist = false;
foundEntity.getComponents().then(function(components){
for(var i=0; i<components.length; i++){
if(components[i].codeName == codeNameComponent){
foundEntity.getComponents().then(function (components) {
for (var i = 0; i < components.length; i++) {
if (components[i].codeName == codeNameComponent) {
alreadyExist = true;
}
}
Expand All @@ -125,11 +124,29 @@ exports.checkIfComponentCodeNameExistOnEntity = function(codeNameComponent, idMo
err.messageParams = [codeNameComponent];
return callback(err, null);
}
}).catch(function(err) {
}).catch(function (err) {
callback(err, null);
});
}

exports.checkIfComponentCodeNameExistOnAnEntity = function (codeNameComponent,idModule, callback) {
models.Component.findOne({where: {codeName: codeNameComponent,id_module:idModule}, include: [{all: true}]}).then(function (component) {
if (component) {
if (component.DataEntities && component.DataEntities.length > 0)
callback(null, true);
else
callback(null, false);
} else {
var err = new Error();
err.message = "database.component.notFound.withThisName";
err.messageParams = [codeNameComponent];
return callback(err, null);
}
}).catch(function (e) {
callback(e, null);
});
};

// Get a component codeName and the has many entity and check if the given ID entity is in
exports.deleteComponentOnEntity = function(codeNameComponent, idModule, idEntity, callback) {

Expand All @@ -138,17 +155,17 @@ exports.deleteComponentOnEntity = function(codeNameComponent, idModule, idEntity
id: idEntity,
id_module: idModule
}
}).then(function(foundEntity){
if(foundEntity){
}).then(function (foundEntity) {
if (foundEntity) {
var destroyed = false;
foundEntity.getComponents().then(function(components){
for(var i=0; i<components.length; i++){
if(components[i].codeName == codeNameComponent){
foundEntity.getComponents().then(function (components) {
for (var i = 0; i < components.length; i++) {
if (components[i].codeName == codeNameComponent) {
destroyed = true;
components[i].destroy();
}
}
if(!destroyed){
if (!destroyed) {
var err = new Error();
err.message = "database.component.delete.error";
return callback(err, null);
Expand All @@ -158,17 +175,69 @@ exports.deleteComponentOnEntity = function(codeNameComponent, idModule, idEntity
};
callback(null, info);
});
} else{
} else {
var err = new Error();
err.message = "database.component.delete.error";
return callback(err, null);
}
}).catch(function(err) {
}).catch(function (err) {
callback(err, null);
});
}

// Delete component_data_entity entry without delete component
exports.deleteComponentAndEntityAssociation = function (codeNameComponent, idModule,idEntity, callback) {
models.Component.findOne({where: {codeName: codeNameComponent,id_module:idModule}, include: [{all: true}]}).then(function (component) {
if (component) {
if (component.DataEntities && component.DataEntities.length) {
for (var i = 0; i < component.DataEntities.length; i++) {
var dataEntity = component.DataEntities[i];
if (Array.isArray(dataEntity.component_data_entity)) {
for (var j = 0; j < dataEntity.component_data_entity.length; j++) {
var association = dataEntity.component_data_entity[j];
if (association.id_entity === idEntity) {
association.destroy();
return callback(null);
}
}
} else if (dataEntity.component_data_entity.id_entity === idEntity) {
dataEntity.component_data_entity.destroy();
return callback(null);
}
}
}
callback(null);
} else {
var err = new Error();
err.message = "database.component.notFound.withThisName";
return callback(err, null);
}
}).catch(function (e) {
callback(e, null);
});
}

exports.deleteComponentByCodeNameInModule = function (codeNameComponent, idModule, callback) {
models.Component.findOne({
where: {
codeName: codeNameComponent,
id_module: idModule
}
}).then(function (component) {
if (!component) {
err = new Error();
err.message = "database.component.notFound.notFoundedInModule";
err.messageParams = [codeNameComponent, idModule];
return callback(err, null);
} else {
component.destroy();
callback(null, component);
}
}).catch(function (err) {
callback(err, null);
});
}

// Get a component codeName and the has many entity and check if the given ID entity is in
exports.deleteComponentOnModule = function(codeNameComponent, idModule, callback) {

models.Component.findOne({
Expand Down

0 comments on commit bb96151

Please sign in to comment.