Your IP : 216.73.217.95


Current Path : /proc/thread-self/root/proc/thread-self/root/home/deltalab/PMS/ims-connector/rest/routes/
Upload File :
Current File : //proc/thread-self/root/proc/thread-self/root/home/deltalab/PMS/ims-connector/rest/routes/orders.js

/* eslint-disable no-await-in-loop */
const express = require('express');
const { getIntrospectionQuery } = require('graphql');
const { dotenv } = require('dotenv').config();

const { orderQuery, storeQuery } = require('../queries');

const router = express.Router({ mergeParams: true });

router.get('/', async (req, res) => {
  try {
    const orders = await orderQuery.readMany();
    const populatedOrders = [];
    for (let i = 0; i < orders.items.length; i++) {
      populatedOrders.push(await orderQuery.populateOrder(orders.items[i]));
    }
    return res.status(200).send({ success: true, data: populatedOrders });
  } catch (error) {
    return res.status(400).send({ success: false, message: 'Error reading the orders' });
  }
});

router.get('/:orderId', async (req, res) => {
  try {
    const order = await orderQuery.readOne('all', req.params.orderId);
    const populatedOrders = [];

    populatedOrders.push(await orderQuery.populateOrder(order.items[0]));
    return res.status(200).send({ success: true, data: populatedOrders });
  } catch (error) {
    return res.status(400).send({ success: false, message: `Error reading the ${req.params.orderId} order` });
  }
});

router.get('/store/:storeName', async (req, res) => {
  try {
    const orders = await orderQuery.readMany(req.params.storeName);
    const populatedOrders = [];
    for (let i = 0; i < orders.items.length; i++) {
      populatedOrders.push(await orderQuery.populateOrder(orders.items[i]));
    }
    return res.status(200).send({ success: true, data: populatedOrders });
  } catch (error) {
    return res.status(400).send({ success: false, message: 'Error reading the orders' });
  }
});

router.get('/stores/:id', async (req, res) => {
  try {
    const stores = await storeQuery.readAllStores();
    for (let i = 0; i < stores.length; i++) {
      if (req.params.id.toString() === stores[i].id.toString()) {
        return res.status(200).send({ success: true, data: stores[i] });
      }
    }
    return res.status(403).send({ success: false, data: undefined });
  } catch (error) {
    console.log(error);
    return res.status(400).send({ success: false, message: `Error reading the ${req.params.orderId} order` });
  }
});
module.exports = router;