|
| 1 | +const KIInventoryItem = require('../../models/kitchenandinventory/KIInventoryItems'); |
| 2 | + |
| 3 | +const KIInventoryController = () => { |
| 4 | + const addItem = async (req, res) => { |
| 5 | + const { |
| 6 | + name, |
| 7 | + storedQuantity, |
| 8 | + unit, |
| 9 | + type, |
| 10 | + monthlyUsage, |
| 11 | + category, |
| 12 | + expiryDate, |
| 13 | + location, |
| 14 | + onsite, |
| 15 | + reorderAt, |
| 16 | + lastHarvestDate, |
| 17 | + nextHarvestDate, |
| 18 | + nextHarvestQuantity, |
| 19 | + } = req.body; |
| 20 | + const newItem = new KIInventoryItem({ |
| 21 | + name, |
| 22 | + storedQuantity, |
| 23 | + presentQuantity: storedQuantity, |
| 24 | + unit, |
| 25 | + type, |
| 26 | + monthlyUsage, |
| 27 | + category, |
| 28 | + expiryDate, |
| 29 | + location, |
| 30 | + onsite, |
| 31 | + reorderAt, |
| 32 | + lastHarvestDate, |
| 33 | + nextHarvestDate, |
| 34 | + nextHarvestQuantity, |
| 35 | + }); |
| 36 | + try { |
| 37 | + const savedItem = await newItem.save(); |
| 38 | + res.status(201).json({ |
| 39 | + message: 'Inventory item added successfully', |
| 40 | + data: savedItem, |
| 41 | + }); |
| 42 | + } catch (error) { |
| 43 | + res.status(400).json({ message: error.message }); |
| 44 | + } |
| 45 | + }; |
| 46 | + const getItems = async (req, res) => { |
| 47 | + try { |
| 48 | + const items = await KIInventoryItem.find(null, { __v: 0 }).lean().sort({ createdAt: -1 }); |
| 49 | + res.status(200).json({ message: 'All Items fetched successfully.', data: items }); |
| 50 | + } catch (err) { |
| 51 | + res.status(400).json({ message: 'Something went wrong while fetching items.' }); |
| 52 | + } |
| 53 | + }; |
| 54 | + const getItemsByCategory = async (req, res) => { |
| 55 | + const { category } = req.params; |
| 56 | + try { |
| 57 | + const items = await KIInventoryItem.find({ category }, { __v: 0 }) |
| 58 | + .lean() |
| 59 | + .sort({ createdAt: -1 }); |
| 60 | + res.status(200).json({ message: 'Items fetched successfully.', data: items }); |
| 61 | + } catch (err) { |
| 62 | + res.status(400).json({ message: 'Something went wrong while fetching items.' }); |
| 63 | + } |
| 64 | + }; |
| 65 | + const getPreservedStock = async (req, res) => { |
| 66 | + const oneyearFromNow = new Date(); |
| 67 | + oneyearFromNow.setFullYear(oneyearFromNow.getFullYear() + 1); |
| 68 | + try { |
| 69 | + const items = await KIInventoryItem.find( |
| 70 | + { category: 'INGREDIENT', expiryDate: { $gte: oneyearFromNow } }, |
| 71 | + { __v: 0 }, |
| 72 | + ) |
| 73 | + .lean() |
| 74 | + .sort({ presentQuantity: -1 }); |
| 75 | + res.status(200).json({ message: 'Preserved stock items fetched successfully.', data: items }); |
| 76 | + } catch (err) { |
| 77 | + res |
| 78 | + .status(400) |
| 79 | + .json({ message: 'Something went wrong while fetching preserved stock items.' }); |
| 80 | + } |
| 81 | + }; |
| 82 | + const updateOnUsage = async (req, res) => { |
| 83 | + const { itemId, usedQuantity } = req.body; |
| 84 | + if (usedQuantity <= 0) { |
| 85 | + return res.status(400).json({ message: 'Used quantity must be greater than zero.' }); |
| 86 | + } |
| 87 | + try { |
| 88 | + const item = await KIInventoryItem.findById(itemId); |
| 89 | + if (!item) { |
| 90 | + return res.status(404).json({ message: 'Item not found.' }); |
| 91 | + } |
| 92 | + if (item.expiryDate < new Date()) { |
| 93 | + return res.status(400).json({ message: `This item was expired on ${item.expiryDate}` }); |
| 94 | + } |
| 95 | + let present = item.presentQuantity; |
| 96 | + present -= usedQuantity; |
| 97 | + if (present < 0) { |
| 98 | + present = 0; |
| 99 | + } |
| 100 | + item.presentQuantity = present; |
| 101 | + item.updatedAt = new Date(); |
| 102 | + await item.save(); |
| 103 | + res.status(200).json({ message: 'Item usage updated successfully.', data: item }); |
| 104 | + } catch (err) { |
| 105 | + res.status(400).json({ message: err.message }); |
| 106 | + } |
| 107 | + }; |
| 108 | + const updateStoredQuantity = async (req, res) => { |
| 109 | + const { itemId, addedQuantity, newExpiry } = req.body; |
| 110 | + if (addedQuantity <= 0) { |
| 111 | + return res.status(400).json({ message: 'Added quantity must be greater than zero.' }); |
| 112 | + } |
| 113 | + if (newExpiry && new Date(newExpiry) < new Date()) { |
| 114 | + return res.status(400).json({ message: 'New expiry date must be a future date.' }); |
| 115 | + } |
| 116 | + try { |
| 117 | + const item = await KIInventoryItem.findById(itemId); |
| 118 | + if (!item) { |
| 119 | + return res.status(404).json({ message: 'Item not found.' }); |
| 120 | + } |
| 121 | + if (item.presentQuantity === 0 || item.expiryDate < new Date()) { |
| 122 | + item.storedQuantity = addedQuantity; |
| 123 | + item.presentQuantity = 0; |
| 124 | + } else { |
| 125 | + item.storedQuantity += addedQuantity; |
| 126 | + } |
| 127 | + item.presentQuantity += addedQuantity; |
| 128 | + if (newExpiry) { |
| 129 | + item.expiryDate = newExpiry; |
| 130 | + } |
| 131 | + item.updatedAt = new Date(); |
| 132 | + await item.save(); |
| 133 | + res.status(200).json({ message: 'Stored quantity updated successfully.', data: item }); |
| 134 | + } catch (err) { |
| 135 | + res.status(400).json({ message: err.message }); |
| 136 | + } |
| 137 | + }; |
| 138 | + const updateNextHarvest = async (req, res) => { |
| 139 | + const { itemId, lastHarvestSuccess, nextHarvestDate, nextHarvestQuantity } = req.body; |
| 140 | + try { |
| 141 | + const item = await KIInventoryItem.findById(itemId); |
| 142 | + if (!item) { |
| 143 | + return res.status(404).json({ message: 'Item not found.' }); |
| 144 | + } |
| 145 | + if (lastHarvestSuccess) { |
| 146 | + item.lastHarvestDate = item.nextHarvestDate; |
| 147 | + } |
| 148 | + item.nextHarvestDate = nextHarvestDate; |
| 149 | + item.nextHarvestQuantity = nextHarvestQuantity; |
| 150 | + item.updatedAt = new Date(); |
| 151 | + await item.save(); |
| 152 | + res.status(200).json({ message: 'Next harvest details updated successfully.', data: item }); |
| 153 | + } catch (err) { |
| 154 | + res.status(400).json({ message: err.message }); |
| 155 | + } |
| 156 | + }; |
| 157 | + return { |
| 158 | + addItem, |
| 159 | + getItems, |
| 160 | + getItemsByCategory, |
| 161 | + getPreservedStock, |
| 162 | + updateOnUsage, |
| 163 | + updateStoredQuantity, |
| 164 | + updateNextHarvest, |
| 165 | + }; |
| 166 | +}; |
| 167 | +module.exports = KIInventoryController; |
0 commit comments