| Current Path : /home/deltalab/PMS/partner-manager-backend/rest/routes/ |
| Current File : //home/deltalab/PMS/partner-manager-backend/rest/routes/channels.js |
/* eslint-disable max-len */
const express = require('express');
const mongoose = require('mongoose');
const { channelQuery } = require('../queries');
const { warehouseModel } = require('../../models/mongoose/warehouse');
const { partnerModel } = require('../../models/mongoose/partner');
const router = express.Router();
// TODO: check su warehouse
router.post('/', async (req, res) => {
try {
const newChannel = { ...req.body };
const channel = await channelQuery.createOne(newChannel);
return res.status(200).json({ success: true, data: channel });
} catch (error) {
console.log(error);
return res.status(400).send({ success: false, message: 'Error creating the channel', error: error.message });
}
});
router.get('/single/:id', async (req, res) => {
try {
const channel = await channelQuery.readOne(req.params.id);
return res.status(200).json({ success: true, data: channel });
} catch (error) {
console.log(error);
return res.status(400).send({ success: false, message: 'Error reading the channel', error: error.message });
}
});
router.get('/channelDetails/partners/:channelId', async (req, res) => {
try {
const partners = await channelQuery.readChannelPartners(req.params.channelId);
return res.status(200).json({ success: true, data: partners });
} catch (error) {
return res.status(400).send({ success: false, message: 'Error reading the channel detail partners', error: error.message });
}
});
router.get('/partners/:partnerId', async (req, res) => {
try {
let channels = await channelQuery.readMany(req.params.partnerId);
channels = channels.concat(await channelQuery.readOwned(req.params.partnerId));
//TODO: rimuovere duplicati
return res.status(200).json({ success: true, data: channels });
} catch (error) {
return res.status(400).send({ success: false, message: 'Error reading the partner-s channels', error: error.message });
}
});
router.get('/partners/owned/:partnerId', async (req, res) => {
try {
const channels = await channelQuery.readOwned(req.params.partnerId);
return res.status(200).json({ success: true, data: channels });
} catch (error) {
return res.status(400).send({ success: false, message: 'Error reading the partner-s channels', error: error.message });
}
});
router.get('/warehouses/:warehouseId', async (req, res) => {
try {
const channels = await channelQuery.readManyWarehouse(req.params.warehouseId);
return res.status(200).json({ success: true, data: channels });
} catch (error) {
return res.status(400).send({ success: false, message: 'Error reading the warehouse-s channels', error: error.message });
}
});
router.post('/add/', async (req, res) => {
try {
const data = req.body;
if (data.entityType === 'WAREHOUSE') {
const warehouse = await warehouseModel.findById({ _id: data.entityId });
warehouse.channelAssignments.push({ channelId: mongoose.Types.ObjectId(data.channelId), storageWarehouse: data.isStorageWarehouse, shipmentWarehouse: data.isShipmentWarehouse });
await warehouseModel.findByIdAndUpdate(data.entityId, warehouse, { returnDocument: 'after' });
}
if (data.entityType === 'PARTNER') {
const partner = await partnerModel.findById({ _id: data.entityId });
partner.channels.push({ _id: mongoose.Types.ObjectId(data.channelId) });
await partnerModel.findByIdAndUpdate(data.entityId, partner, { returnDocument: 'after' });
}
return res.status(200).json({ success: true });
} catch (error) {
return res.status(400).send({ success: false, message: 'Error adding the channel to the specified entity', error: error.message });
}
});
module.exports = router;