const shared_consts = require('../tools/shared_nodejs');

const express = require('express');
const router = express.Router();

const tools = require('../tools/general');

var server_constants = require('../tools/server_constants');

var {authenticate, auth_default} = require('../middleware/authenticate');

var mongoose = require('mongoose').set('debug', false);
const Subscription = mongoose.model('subscribers');

const _ = require('lodash');
const {MyBacheca} = require('../models/mybacheca');
var {User} = require('../models/user');

const globalTables = require('../tools/globalTables');

const {ObjectID} = require('mongodb');

//GET orders
router.post('/page', authenticate, function(req, res, next) {

  //++TODO: PERMESSI ???

  try {
    let table = req.body.table;
    let id = req.body.id;
    let idapp = req.body.idapp;

    let mytable = null;
    if (shared_consts.TABLES_ENABLE_GETREC_BYID.includes(table)) {
      mytable = globalTables.getTableByTableName(table);
    }

    if (mytable) {

      return mytable.getMyRecById(idapp, id).
          then((ris) => {

            if (ris) {
              res.send(ris);

            } else {
              res.status(400).send();
            }
          }).catch((e) => {
            console.error('Err', e);
            res.status(400).send(e);
          })

    }
  }catch (e) {
    console.error('/page', e);
  }

  return null;

});

module.exports = router;
