Files
freeplanet_serverside/src/router/products_router.js

103 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-12-21 02:16:42 +01:00
const express = require('express');
const router = express.Router();
const tools = require('../tools/general');
var server_constants = require('../tools/server_constants');
var { Project } = require('../models/project');
2023-12-15 23:36:43 +01:00
const { User } = require('../models/user');
2020-12-25 03:54:16 +01:00
var { authenticate, auth_default } = require('../middleware/authenticate');
2020-12-21 02:16:42 +01:00
const _ = require('lodash');
const Product = require('../models/product');
const OrdersCart = require('../models/orderscart');
2020-12-21 02:16:42 +01:00
const Variant = require('../models/variant');
/*const Department = require('../models/Department')
const Category = require('../models/Category')
const TypedError = require('../modules/ErrorHandler')
const paypal_config = require('../configs/paypal-config')
const paypal = require('paypal-rest-sdk')
*/
const CartClass = require('../modules/Cart')
const Cart = require('../models/cart');
//GET /products
2020-12-25 03:54:16 +01:00
router.post('/', auth_default, async function (req, res, next) {
const idapp = req.body.idapp;
const userId = req.body.userId;
2024-02-15 18:58:58 +01:00
let ismanager = await tools.isManagerByReq(req);
2024-01-13 16:21:07 +01:00
let products = await Product.findAllIdApp(idapp, "", undefined, ismanager);
2023-12-15 23:36:43 +01:00
let orders = null;
if (ismanager) {
2023-12-15 23:36:43 +01:00
// Prende Tutti gli Ordini !
2023-12-20 21:52:17 +01:00
orders = await OrdersCart.getOrdersCartByUserId('ALL', idapp, 0, false);
2023-12-15 23:36:43 +01:00
} else {
2023-12-20 21:52:17 +01:00
orders = await OrdersCart.getOrdersCartByUserId(userId, idapp, 0, false);
2023-12-15 23:36:43 +01:00
}
2020-12-21 02:16:42 +01:00
2024-02-15 18:59:07 +01:00
/*
2024-02-15 18:58:58 +01:00
let ind = 0;
for (const ord of orders) {
let newitems = []
for (myord of ord.items) {
if (!myord.order) {
console.log('NO ORDINE ', myord, 'Ind=', ind);
} else {
if (myord.order && !myord.order.hasOwnProperty('idGasordine')) {
console.log('NO idGasordine', myord);
} else {
newitems.push(myord)
}
}
}
ind++;
ord.items = newitems
}
2024-02-15 18:59:07 +01:00
*/
2024-02-15 18:58:58 +01:00
2020-12-21 02:16:42 +01:00
if (products)
return res.send({ code: server_constants.RIS_CODE_OK, products, orders });
2020-12-21 02:16:42 +01:00
else
return res.status(400).send({ code: server_constants.RIS_CODE_OK, products, orders });
2024-02-15 18:58:58 +01:00
2020-12-21 02:16:42 +01:00
});
2021-01-18 00:48:17 +01:00
router.post('/:code', auth_default, async function (req, res, next) {
const idapp = req.body.idapp;
const code = req.body.code;
var product = await Product.findAllIdApp(idapp, code);
if (product.length > 0) {
return res.send({ code: server_constants.RIS_CODE_OK, product: product[0] });
}
});
2020-12-21 02:16:42 +01:00
2024-01-13 00:28:53 +01:00
router.get('/id/:id', async function (req, res) {
const id = req.params.id;
var product = await Product.getProductById(id);
// console.log('Product ID', id, product);
2025-04-11 18:49:59 +02:00
2024-01-13 00:28:53 +01:00
if (product) {
return res.send({ code: server_constants.RIS_CODE_OK, product: product });
} else {
return res.send({ code: server_constants.RIS_CODE_OK, product: null });
}
});
2020-12-21 02:16:42 +01:00
module.exports = router;