aboutsummaryrefslogtreecommitdiffstats
path: root/lib/auth.js.map
blob: e539bafdf4b2c9d19153c02635c72a6b15644bb8 (plain) (blame)
1
{"version":3,"file":"auth.js","sourceRoot":"","sources":["auth.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+DAAgD;AAEhD,oCAA2C;AAC3C,oDAA6B;AAC7B,8DAA+B;AAElB,QAAA,KAAK,GAAG;IACnB,aAAa,EAAE,kBAAe,CAAC,YAAY,CACzC,KAAK,EACL,EAAE,OAAO,EAAE,KAAK,EAAE,EAClB,UAAU,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI;QACjC,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,OAAO,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAC3D,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;IAClB,CAAC,CACF;CACF,CAAC;AAEW,QAAA,QAAQ,GAAG;IACtB,aAAa,EAAE,kBAAe,CAAC,YAAY,CACzC,KAAK,EACL,EAAE,OAAO,EAAE,KAAK,EAAE,EAClB,UAAU,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI;QACjC,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;IAClB,CAAC,CACF;CACF,CAAC;AAEW,QAAA,IAAI,GAAG;IAClB,aAAa,EAAE,kBAAe,CAAC,YAAY,CAAC,KAAK,EAAE;QACjD,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,KAAK;KACf,CAAC;CACH,CAAC;AAEK,IAAM,UAAU,GAAG,iEAAO,EAAc;QAAZ,IAAI,UAAA,EAAE,IAAI,UAAA;;QAC3C,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,CAAC;YAAE,IAAA,YAAI,EAAC,aAAa,CAAC,CAAC;;;KACvE,CAAC;AAFW,QAAA,UAAU,cAErB;AAEW,QAAA,WAAW,GAAG,IAAI,sBAAG,CAAC,QAAQ,CACzC;IACE,cAAc,EAAE,sBAAG,CAAC,UAAU,CAAC,cAAc,CAAC;QAC5C,sBAAG,CAAC,UAAU,CAAC,2BAA2B,EAAE;QAC5C,UAAC,GAAG,IAAK,OAAA,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,EAAxC,CAAwC;KAClD,CAAC;IACF,WAAW,EAAE,iBAAM,CAAC,GAAG;CACxB,EACD,UAAO,OAAO,EAAE,IAAI;;;;oBACP,qBAAM,IAAA,eAAO,EAAC,OAAO,CAAC,EAAE,CAAC,EAAA;;gBAAhC,IAAI,GAAG,SAAyB;gBACpC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;;;;KAClB,CACF,CAAC"}
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
import express from "express";
var router = express.Router();

import * as parseInput from "../scripts/apiParse";
import Sequelize from "@sequelize/core";
const Op = Sequelize.Op;

/**
 * @api {get} /users Retrieve user list
 * @apiName getUsers
 * @apiGroup Admin
 *
 * @apiPermission admin
 *
 * @apiSuccess {Object} users List of users
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "users" : [
 *         { "username": 'John Doe', "email": '[email protected]', ...},
 *         { "username": 'Jane Doe', "email": '[email protected]', ...}
 *        ]
 *     }
 */
router.get(
  "/users",
  // auth,
  async (req, res) => {
    var { search, starts_with, contains } = req.query;
    var users = knex
      .select(
        "username",
        "email",
        "phone",
        "verified",
        knex.raw("LEFT(createdAt,10) as created_at")
      )
      .from("users");

    var timeCondition = parseInput.addTimeSearch(req.query, "users.createdAt");
    var userCondition = parseInput.addUserSearch(req.query);

    if (timeCondition) users = users.whereRaw(timeCondition);
    if (userCondition) users = users.whereRaw(userCondition);

    // Alternative Sequelize query syntax
    //   await db.User.findAll({
    //   attributes: ['username', 'email', 'phone', 'verified', 'createdAt']
    // })

    const found = await users;
    return res.send({ users: found });
  }
);

/**
 * @api {get} /referrals Retrieve list of referral tokens
 * @apiName referrals
 * @apiGroup Admin
 *
 * @apiPermission admin
 *
 * @apiSuccess returns list of referral tokens
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "referrals" : [
 *         { "token": '***', "status": 'pending', sponsor: 'Adam', user: 'newUsername1', expiry: null },
 *         { "token": '***', "status": 'pending', sponsor: 'Adam', user: 'newUsername2', expiry: null },
 *        ]
 *     }
 */
router.get("/referrals", auth, async (req, res) => {
  var referrals = knex
    .select(
      "token",
      "users.username as user",
      "sponsor.username as sponsor",
      knex.raw("LEFT(created_at, 10) as created_at"),
      "referrals.status"
    )
    .from("referrals")
    .leftJoin("users", "users.id", "referrals.user_id")
    .leftJoin("users as sponsor", "sponsor.id", "referrals.sponsor_id");

  // Alternative Sequelize query syntax (returns sub-tables as hash)
  // var referrals = await db.Referral.findAll({
  //   include: [
  //     {model: db.User, as: 'user', attributes: [['username', 'user']]},
  //     {model: db.User, as: 'sponsor', attributes: [['username', 'sponsor']]}
  //   ]
  // })

  var timeCondition = parseInput.addTimeSearch(
    req.query,
    "referrals.updated_at"
  );
  var userCondition = parseInput.addUserSearch(req.query, "sponsor");

  if (userCondition) {
    referrals = referrals.whereRaw(userCondition);
  }
  if (timeCondition) {
    referrals = referrals.whereRaw(timeCondition);
  }

  var found = await referrals;
  return res.send({ referrals: found });
});

/**
 * @api {get} /waiting_list Retrieve waiting_list
 * @apiName waiting_list
 * @apiGroup Admin
 *
 * @apiPermission admin
 *
 * @apiSuccess returns list of referral tokens
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "waiting_list" : [
 *         { email: "[email protected]", phone: '604 123-4567 },
 *         { email: "[email protected]", phone: '604 246-8910 },
 *        ]
 *     }
 */
router.get(
  "/waiting_list",
  // auth,
  async (req, res) => {
    var { search, starts_with, contains } = req.query;

    var queue = knex
      .select(
        "waiting_list.email",
        "waiting_list.phone",
        "waiting_list.created_at as requested",
        "users.id as current_user_id"
      )
      .from("waiting_list")
      .leftJoin("users", "users.email", "waiting_list.email")
      .where("waiting_list.id", ">", 0);

    var timeCondition = parseInput.addTimeSearch(
      req.query,
      "waiting_list.updated_at"
    );
    var userCondition = parseInput.addUserSearch(req.query);

    if (timeCondition) queue = queue.whereRaw(timeCondition);
    if (userCondition) queue = queue.whereRaw(userCondition);

    const found = await queue;

    return res.send({ queue: found });
  }
);

/**
 * @api {get} /accounts Retrieve list of accounts
 * @apiName accounts
 * @apiGroup Admin
 *
 * @apiPermission admin
 *
 * @apiSuccess returns list of accounts
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "accounts" : [
 *         { "user": 'Adam', "account_id": '123', balance: 500 ... },
 *          ...
 *        ]
 *     }
 */
router.get("/accounts", auth, async (req, res) => {
  var { nonZero, search, starts_with, contains } = req.query;

  var accounts = knex
    .select(
      "username",
      "accounts.id as account_id",
      "ticker",
      "balance",
      knex.raw("LEFT(accounts.createdAt, 10) as created"),
      knex.raw("LEFT(accounts.updatedAt, 10) as updated")
    )
    .from("accounts")
    .leftJoin("users", "users.id", "accounts.user_id");

  if (nonZero) {
    accounts = accounts.where("balance", ">", 0);
  }

  var timeCondition = parseInput.addTimeSearch(req.query, "accounts.updatedAt");
  var userCondition = parseInput.addUserSearch(req.query);

  if (timeCondition) accounts = accounts.whereRaw(timeCondition);
  if (userCondition) accounts = accounts.whereRaw(userCondition);

  const found = await accounts;

  return res.send({ accounts: found });
});

/**
 * @api {get} /transactions Retrieve summary of user transactions
 * 
 * @apiName user_transactions
 * @apiGroup Admin
 *
 * @apiPermission admin
 * 
 * @apiSuccess returns list of invoices, payments, orders for users
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "transactions" : [
 *         { "username": 'Adam', "email": '[email protected]', invoices: 1, orders: 0, payments: 2 },
         ...
 *        ]
 *     }
 */
router.get(
  "/transactions",
  // auth,
  async (req, res) => {
    var { type, since, search, starts_with, contains } = req.query;

    var transactions = knex
      .select(
        "users.username",
        "users.email"
        // knex.raw('LEFT(created_at, 10) as created')
        // knex.raw('LEFT(updatedAt, 10) as updated')
        // knex.raw('COUNT(invoices.id) as invoices'),
        // knex.raw('COUNT(payments.id) as payments'),
        // knex.raw('COUNT(orders.id) as orders')
      )
      .from("users");

    const types = ["deposits", "withdrawals", "orders", "payments", "invoices"];
    for (var i = 0; i < types.length; i++) {
      var stamp = types[i].replace(/s$/, "") + "_date";

      transactions = transactions
        .select(
          knex.raw("COUNT(DISTINCT " + types[i] + ".id) as " + types[i]),
          knex.raw("LEFT(MAX(" + types[i] + ".updatedAt), 10) as " + stamp)
        )
        .leftJoin(types[i], "users.id", types[i] + ".user_id");
      // .havingRaw(types[i] + ' > ?', [0])

      if (since) {
        transactions = transactions.havingRaw(
          "(" + stamp + " >= ? OR " + stamp + " IS NULL)",
          [since]
        );
      }
    }

    if (since)
      transactions = transactions.havingRaw(
        "invoices + payments + deposits + orders > ?",
        [0]
      );

    var userCondition = parseInput.addUserSearch(req.query);
    if (userCondition) transactions = transactions.whereRaw(userCondition);

    transactions = transactions.groupBy("users.id");

    const found = await transactions;

    return res.send({ transactions: found });
  }
);

/**
 * @api {get} /orders Retrieve summary of orders
 * 
 * @apiName orders
 * @apiGroup Admin
 *
 * @apiPermission admin
 * 
 * @apiSuccess returns list of orders
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "orders" : [
 *         { "seller": 'Adam', "buyer": 'bob' ... },
         ...
 *        ]
 *     }
 */
router.get("/orders", auth, async (req, res) => {
  var { type, since, search, starts_with, contains } = req.query;

  var orders = knex
    .select(
      "u1.username as seller",
      "u2.username as buyer",
      "v1 as sell_amount",
      "v2 as buy_amount",
      "rate",
      "a1.name as from",
      "a2.name as to",
      knex.raw("Left(completedAt,16) as completed")
    )
    .from("orders")
    .leftJoin("accounts as a1", "a1.id", "orders.a1_id")
    .leftJoin("accounts as a2", "a2.id", "orders.a2_id")
    .leftJoin("users as u1", "a1.user_id", "u1.id")
    .leftJoin("users as u2", "a2.user_id", "u2.id");

  var timeCondition = parseInput.addTimeSearch(req.query, "orders.updatedAt");
  if (timeCondition) orders = orders.whereRaw(timeCondition);

  var userCondition1 = parseInput.addUserSearch(req.query, "u1");
  var userCondition2 = parseInput.addUserSearch(req.query, "u2");

  if (userCondition1)
    orders = orders.where(function() {
      this.whereRaw(userCondition1);
      this.orWhere(knex.raw(userCondition2));
    });

  var found = await orders;
  return res.send({ orders: found });
});

/**
 * @api {get} /invoices Retrieve summary of invoices
 * 
 * @apiName invoices
 * @apiGroup Admin
 *
 * @apiPermission admin
 * 
 * @apiSuccess returns list of invoices
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "invoices" : [
 *         { "seller": 'Adam', "buyer": 'bob' ... },
         ...
 *        ]
 *     }
 */
router.get("/invoices", auth, async (req, res) => {
  var { type, since, search, starts_with, contains } = req.query;

  var invoices = knex
    .select(
      "username",
      "users.email",
      "invoices.currency",
      "invoices.amount",
      "accounts.network",
      knex.raw("Left(invoices.updatedAt,16) as updated")
    )
    .from("invoices")
    .leftJoin("accounts", "invoices.account_id", "accounts.id")
    .leftJoin("users", "accounts.user_id", "users.id");

  var timeCondition = parseInput.addTimeSearch(req.query, "invoices.updatedAt");
  var userCondition = parseInput.addUserSearch(req.query);

  if (timeCondition) invoices = invoices.whereRaw(timeCondition);
  if (userCondition) invoices = invoices.whereRaw(userCondition);

  var found = await invoices;
  return res.send({ invoices: found });
});

/**
 * @api {get} /deposits Retrieve summary of deposits
 * 
 * @apiName deposits
 * @apiGroup Admin
 *
 * @apiPermission admin
 * 
 * @apiSuccess returns list of deposits
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "deposits" : [
 *         { "seller": 'Adam', "buyer": 'bob' ... },
         ...
 *        ]
 *     }
 */
router.get("/deposits", auth, async (req, res) => {
  var { type, since, search, starts_with, contains } = req.query;

  var deposits = knex
    .select(
      "username",
      "users.email",
      "amount",
      "credited",
      knex.raw("Left(deposits.updatedAt,16) as deposited")
    )
    .from("deposits")
    .leftJoin("users", "deposits.user_id", "users.id");

  var timeCondition = parseInput.addTimeSearch(req.query, "deposits.updatedAt");
  var userCondition = parseInput.addUserSearch(req.query);

  if (timeCondition) deposits = deposits.whereRaw(timeCondition);
  if (userCondition) deposits = deposits.whereRaw(userCondition);

  var found = await deposits;
  return res.send({ deposits: found });
});

/**
 * @api {get} /withdrawals Retrieve summary of withdrawals
 * 
 * @apiName withdrawals
 * @apiGroup Admin
 *
 * @apiPermission admin
 * 
 * @apiSuccess returns list of withdrawals
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "withdrawals" : [
 *         { "seller": 'Adam', "buyer": 'bob' ... },
         ...
 *        ]
 *     }
 */
router.get("/withdrawals", auth, async (req, res) => {
  var { type, since, search, starts_with, contains } = req.query;

  var withdrawals = knex
    .select(
      "username",
      "users.email",
      "amount",
      "notes",
      knex.raw("Left(withdrawals.updatedAt,16) as withdrawn")
    )
    .from("withdrawals")
    .leftJoin("users", "withdrawals.user_id", "users.id");

  var timeCondition = parseInput.addTimeSearch(
    req.query,
    "withdrawals.updatedAt"
  );
  var userCondition = parseInput.addUserSearch(req.query);

  if (timeCondition) withdrawals = withdrawals.whereRaw(timeCondition);
  if (userCondition) withdrawals = withdrawals.whereRaw(userCondition);

  var found = await withdrawals;
  return res.send({ withdrawals: found });
});

/**
 * @api {get} /payments Retrieve summary of payments
 * 
 * @apiName payments
 * @apiGroup Admin
 *
 * @apiPermission admin
 * 
 * @apiSuccess returns list of payments
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "payments" : [
 *         { "seller": 'Adam', "buyer": 'bob' ... },
         ...
 *        ]
 *     }
**/
router.get("/payments", auth, async (req, res) => {
  var { type, since, search, starts_with, contains } = req.query;

  var payments = knex
    .select(
      "username",
      "users.email",
      "amount",
      "payments.network",
      knex.raw("LEFT(hash,20) as hash"),
      knex.raw("Left(payments.updatedAt,16) as deposited")
    )
    .from("payments")
    .leftJoin("accounts", "payments.account_id", "accounts.id")
    .leftJoin("users", "accounts.user_id", "users.id");

  var timeCondition = parseInput.addTimeSearch(req.query, "payments.updatedAt");
  var userCondition = parseInput.addUserSearch(req.query);

  if (timeCondition) payments = payments.whereRaw(timeCondition);
  if (userCondition) payments = payments.whereRaw(userCondition);

  var found = await payments;
  return res.send({ payments: found });
});

/**
 * @api {get} /user_kyc Retrieve user kyc details
 * 
 * @apiName user_kyc
 * @apiGroup Admin
 *
 * @apiPermission admin
 * 
 * @apiSuccess returns list of kyc details for each user
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "users" : [
 *         { "username": 'Adam', "email": '[email protected]', ... kyc_data... },
         ...
 *        ]
 *     }
 */
router.get("/kyc_required", auth, async (req, res) => {
  var { since, search, starts_with, contains } = req.query;

  var threshold = "0.2";

  var kyc = knex
    .select(
      knex.raw("CONCAT( FORMAT(Max(amount)/1000000, 1), ' M') as max"),
      "username",
      "users.email",
      "users.verified as kyc_verified",
      // 'payments.currency',
      // 'payments.network',
      // knex.raw('LEFT(hash,20) as hash'),
      // knex.raw('Left(payments.updatedAt,16) as transferred')
      // knex.raw( parseInput.readableDate('Max(payments.updatedAt)', 'last_changed') )
      knex.raw(
        "DATE_FORMAT(Max(payments.updatedAt), ?) AS last_changed",
        "%M %d, %Y"
      )
    )
    .from("payments")
    .leftJoin("accounts", "payments.account_id", "accounts.id")
    .leftJoin("users", "payments.user_id", "users.id") // should really point to accounts
    .groupBy("users.id");
  // .groupBy('payments.currency')
  // .groupBy('payments.network')

  var timeCondition = parseInput.addTimeSearch(req.query, "payments.updatedAt");
  var userCondition = parseInput.addUserSearch(req.query);

  if (timeCondition) kyc = kyc.whereRaw(timeCondition);
  if (userCondition) kyc = kyc.whereRaw(userCondition);

  var found = await kyc.having("max", ">=", threshold);

  return res.send({ found: found });
});

export default router;