Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. router.post('/Users',jsonParser, function(req,res,next){
  2.  
  3. var newData = req.body;
  4. if(newData.username){
  5. newData.username=null;
  6. }
  7. if(newData.password||newData.password!==""){
  8. newData.password = crypto.createHash('md5').update(newData.password).digest('hex');
  9. }else{
  10. delete newData.password;
  11. }
  12. models[req.domainURL].sequelize.transaction(function (t) {
  13. var created_userID;
  14. return DB.createUser(req.domainURL, newData).then(function(results){
  15. created_userID=results.ID;
  16. var userbranchIds=newData.branchIDs.map(function(branchID){
  17. var userbranch={
  18. userID:created_userID,
  19. branchID:branchID
  20. };
  21. return userbranch;
  22.  
  23. });
  24.  
  25. return DB.addUserBranches(req.domainURL,userbranchIds);
  26. }).then(function(results){
  27. var usersettings = newData.userSettings.map(function(userSetting){
  28.  
  29. var usersetting={
  30. settingID : userSetting.settingID,
  31. settingValue: userSetting.value,
  32. userID:created_userID
  33.  
  34. };
  35.  
  36.  
  37. return usersetting;
  38.  
  39. });
  40. return DB.addUserUserSettings(req.domainURL,usersettings);
  41.  
  42. }).then(function(results){
  43. return DB.addUserRole(req.domainURL,newData.RoleId,created_userID);
  44.  
  45.  
  46. }).then(function(results){
  47.  
  48. ///here there is the problem
  49. return DB.addCompanyUser(req.domainURL,req.decoded.companyId,created_userID);
  50. });
  51. }).then(function(results){
  52. res.send();
  53.  
  54.  
  55. }).catch(function(error){
  56.  
  57. Errors.handleError(new Errors.SequelizeErrorWrapper(error,'User'),res);
  58. });
  59.  
  60.  
  61. });
  62.  
  63.  
  64.  
  65. var CompanyUsers = sequelize.define('CompanyUsers',{
  66. companyID:
  67. {
  68. type: DataTypes.INTEGER(10).UNSIGNED, primaryKey: true, allowNull: false, field: 'company_id'
  69. },
  70. userID:
  71. {
  72. type: DataTypes.INTEGER(10).UNSIGNED, primaryKey: true, allowNull: false, field: 'user_id'
  73. }
  74. },
  75. {
  76. classMethods: {
  77. associate: function(models) {
  78. CompanyUsers.belongsTo(models.Companies, {foreignKey: 'companyID', targetKey: 'ID'});
  79. CompanyUsers.belongsTo(models.Users, {foreignKey: 'userID', targetKey: 'ID'});
  80. }
  81. },
  82. timestamps:false,
  83. tableName: 'company_users',
  84. freezeTableName: true
  85. });
  86. };
  87.  
  88.  
  89. addCompanyUser: function(domain,company_id,created_user_id){
  90. var data={
  91. companyID:company_id,
  92. userID:userid
  93. };
  94.  
  95. return models[domain].CompanyUsers.create(data);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement