Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = new (require('express'))();
- var wt = require('webtask-tools');
- var mysql = require('promise-mysql');
- app.get('/', function (req, res) {
- if(!req.user['https://workchat.mn/tenant_id']){
- res.status(403);
- res.json({"status": 403, "message": "Cannot get tenant ID!"});
- res.end();
- }
- var tenantId = req.user['https://workchat.mn/tenant_id'];
- //Now need to list other members of tenant
- //console.log(req.webtaskContext.data.access_token);
- getUsers(req,tenantId).then(function(rows){
- if(rows){
- res.status(200);
- res.json(
- {
- "status": 200,
- "users": rows
- }
- );
- res.end();
- } else {
- res.status(403);
- res.json({"status": 403, "message": "Tenant not found in DB!" });
- res.end();
- }
- });
- });
- function getUsers(req,tenant_id)
- {
- return mysql.createConnection({
- host : 'example.com',
- user : 'webtask',
- password : req.webtaskContext.secrets.DB_PASS,
- database : 'workchat'
- }).then((conn) => {
- connection = conn;
- let q = "";
- 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 " +
- "departments AS b ON a.department_id = b.id WHERE " +
- "a.tenant_id="+tenant_id+" ORDER BY b.name, a.first_name";
- return conn.query(q);
- }).then((rows) => {
- var members = [];
- rows.forEach(function (row){
- var user_phones = [];
- member = {};
- member['id' ] = row.id;
- member['first_name' ] = row.first_name;
- member['last_name' ] = row.last_name;
- member['full_name' ] = row.first_name + ' ' + row.last_name;
- member['role' ] = row.role;
- member['email' ] = row.email;
- member['status' ] = row.status;
- member['department_id' ] = row.department_id;
- member['department_name'] = row.department_name;
- connection.query("select label,phone FROM phones WHERE user="+row.id+" ORDER BY label").then(function(phones){
- phones.forEach(function (item){
- phone = {};
- phone['label' ] = item.label;
- phone['number' ] = item.phone;
- user_phones.push(phone);
- });
- member['phones' ] = user_phones;
- }).then(function(){
- members.push(member);
- });
- });
- connection.end();
- return members;
- });
- }
- module.exports = wt.fromExpress(app).auth0();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement