Advertisement
Guest User

Untitled

a guest
Jul 8th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. var app = new (require('express'))();
  2. var wt = require('webtask-tools');
  3. var mysql = require('promise-mysql');
  4.  
  5. app.get('/', function (req, res) {
  6. if(!req.user['https://workchat.mn/tenant_id']){
  7. res.status(403);
  8. res.json({"status": 403, "message": "Cannot get tenant ID!"});
  9. res.end();
  10. }
  11.  
  12. var tenantId = req.user['https://workchat.mn/tenant_id'];
  13.  
  14. //Now need to list other members of tenant
  15. //console.log(req.webtaskContext.data.access_token);
  16. getUsers(req,tenantId).then(function(rows){
  17. if(rows){
  18. res.status(200);
  19. res.json(
  20. {
  21. "status": 200,
  22. "users": rows
  23. }
  24. );
  25. res.end();
  26. } else {
  27. res.status(403);
  28. res.json({"status": 403, "message": "Tenant not found in DB!" });
  29. res.end();
  30. }
  31. });
  32. });
  33.  
  34. function getUsers(req,tenant_id)
  35. {
  36. return mysql.createConnection({
  37. host : 'example.com',
  38. user : 'webtask',
  39. password : req.webtaskContext.secrets.DB_PASS,
  40. database : 'workchat'
  41. }).then((conn) => {
  42. connection = conn;
  43.  
  44. let q = "";
  45. q = "select a.id, a.role, a.first_name, a.last_name, a.sip_username, a.email, a.status, a.department_id, b.name AS department_name FROM users as a LEFT JOIN " +
  46. "departments AS b ON a.department_id = b.id WHERE " +
  47. "a.tenant_id="+tenant_id+" ORDER BY b.name, a.first_name";
  48.  
  49. return conn.query(q);
  50. }).then((rows) => {
  51. var members = [];
  52.  
  53. rows.forEach(function (row){
  54. var user_phones = [];
  55.  
  56. member = {};
  57. member['id' ] = row.id;
  58. member['first_name' ] = row.first_name;
  59. member['last_name' ] = row.last_name;
  60. member['full_name' ] = row.first_name + ' ' + row.last_name;
  61. member['role' ] = row.role;
  62. member['email' ] = row.email;
  63. member['status' ] = row.status;
  64. member['department_id' ] = row.department_id;
  65. member['department_name'] = row.department_name;
  66.  
  67. connection.query("select label,phone FROM phones WHERE user="+row.id+" ORDER BY label").then(function(phones){
  68. phones.forEach(function (item){
  69. phone = {};
  70. phone['label' ] = item.label;
  71. phone['number' ] = item.phone;
  72.  
  73. user_phones.push(phone);
  74. });
  75.  
  76. member['phones' ] = user_phones;
  77. }).then(function(){
  78. members.push(member);
  79. });
  80. });
  81.  
  82. connection.end();
  83.  
  84. return members;
  85. });
  86. }
  87.  
  88. module.exports = wt.fromExpress(app).auth0();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement