Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- router.post('/Users',jsonParser, function(req,res,next){
- var newData = req.body;
- if(newData.username){
- newData.username=null;
- }
- if(newData.password||newData.password!==""){
- newData.password = crypto.createHash('md5').update(newData.password).digest('hex');
- }else{
- delete newData.password;
- }
- models[req.domainURL].sequelize.transaction(function (t) {
- var created_userID;
- return DB.createUser(req.domainURL, newData).then(function(results){
- created_userID=results.ID;
- var userbranchIds=newData.branchIDs.map(function(branchID){
- var userbranch={
- userID:created_userID,
- branchID:branchID
- };
- return userbranch;
- });
- return DB.addUserBranches(req.domainURL,userbranchIds);
- }).then(function(results){
- var usersettings = newData.userSettings.map(function(userSetting){
- var usersetting={
- settingID : userSetting.settingID,
- settingValue: userSetting.value,
- userID:created_userID
- };
- return usersetting;
- });
- return DB.addUserUserSettings(req.domainURL,usersettings);
- }).then(function(results){
- return DB.addUserRole(req.domainURL,newData.RoleId,created_userID);
- }).then(function(results){
- ///here there is the problem
- return DB.addCompanyUser(req.domainURL,req.decoded.companyId,created_userID);
- });
- }).then(function(results){
- res.send();
- }).catch(function(error){
- Errors.handleError(new Errors.SequelizeErrorWrapper(error,'User'),res);
- });
- });
- var CompanyUsers = sequelize.define('CompanyUsers',{
- companyID:
- {
- type: DataTypes.INTEGER(10).UNSIGNED, primaryKey: true, allowNull: false, field: 'company_id'
- },
- userID:
- {
- type: DataTypes.INTEGER(10).UNSIGNED, primaryKey: true, allowNull: false, field: 'user_id'
- }
- },
- {
- classMethods: {
- associate: function(models) {
- CompanyUsers.belongsTo(models.Companies, {foreignKey: 'companyID', targetKey: 'ID'});
- CompanyUsers.belongsTo(models.Users, {foreignKey: 'userID', targetKey: 'ID'});
- }
- },
- timestamps:false,
- tableName: 'company_users',
- freezeTableName: true
- });
- };
- addCompanyUser: function(domain,company_id,created_user_id){
- var data={
- companyID:company_id,
- userID:userid
- };
- return models[domain].CompanyUsers.create(data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement