Spaces:
Running
Running
feat: implement business date utility and financial reporting system including expense tracking, platform sales, and profit-loss analysis endpoints
ec62259 Download src/controllers/platformSaleController.js from aliroohan179/face: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/spaces/aliroohan179/face/resolve/main/src/controllers/platformSaleController.js
- Command line
-
hf download hf://spaces/aliroohan179/face/src/controllers/platformSaleController.js
-
curl -L -o platformSaleController.js https://huggingface.co/spaces/aliroohan179/face/resolve/main/src/controllers/platformSaleController.js
2.66 kB
| const PlatformSale = require('../models/PlatformSale'); | |
| const { log } = require('../utils/activityLogger'); | |
| // GET /api/platform-sales?month=YYYY-MM or ?from=...&to=... | |
| exports.getPlatformSales = async (req, res) => { | |
| const { month, from, to, platform } = req.query; | |
| const filter = {}; | |
| if (platform) filter.platform = platform; | |
| if (month) { | |
| const [y, m] = month.split('-').map(Number); | |
| const start = new Date(y, m - 1, 1, 0, 0, 0, 0); | |
| const end = new Date(y, m, 0, 23, 59, 59, 999); | |
| filter.date = { $gte: start, $lte: end }; | |
| } else if (from || to) { | |
| filter.date = {}; | |
| if (from) filter.date.$gte = new Date(from); | |
| if (to) filter.date.$lte = new Date(new Date(to).setHours(23, 59, 59, 999)); | |
| } | |
| const sales = await PlatformSale.find(filter) | |
| .populate('createdBy', 'name username') | |
| .sort({ date: -1 }); | |
| const total = sales.reduce((s, item) => s + item.amount, 0); | |
| res.json({ sales, total }); | |
| }; | |
| // POST /api/platform-sales | |
| exports.createPlatformSale = async (req, res) => { | |
| const { platform = 'Foodpanda', date, amount, note } = req.body; | |
| if (!date || amount === undefined || isNaN(Number(amount))) { | |
| return res.status(400).json({ message: 'Date and valid amount are required' }); | |
| } | |
| const sale = await PlatformSale.create({ | |
| platform, | |
| date: new Date(date), | |
| amount: Number(amount), | |
| note: note || '', | |
| createdBy: req.user._id, | |
| }); | |
| log(req.user, 'ADD_PLATFORM_SALE', { | |
| entity: 'PlatformSale', | |
| entityId: sale._id, | |
| detail: `${platform} Rs.${sale.amount} (${date})`, | |
| }); | |
| res.status(201).json(sale); | |
| }; | |
| // PUT /api/platform-sales/:id | |
| exports.updatePlatformSale = async (req, res) => { | |
| const sale = await PlatformSale.findById(req.params.id); | |
| if (!sale) return res.status(404).json({ message: 'Platform sale entry not found' }); | |
| if (req.body.date) req.body.date = new Date(req.body.date); | |
| if (req.body.amount !== undefined) req.body.amount = Number(req.body.amount); | |
| Object.assign(sale, req.body); | |
| await sale.save(); | |
| log(req.user, 'EDIT_PLATFORM_SALE', { | |
| entity: 'PlatformSale', | |
| entityId: sale._id, | |
| detail: `${sale.platform} Rs.${sale.amount}`, | |
| }); | |
| res.json(sale); | |
| }; | |
| // DELETE /api/platform-sales/:id | |
| exports.deletePlatformSale = async (req, res) => { | |
| const sale = await PlatformSale.findByIdAndDelete(req.params.id); | |
| if (!sale) return res.status(404).json({ message: 'Platform sale entry not found' }); | |
| log(req.user, 'DELETE_PLATFORM_SALE', { | |
| entity: 'PlatformSale', | |
| entityId: sale._id, | |
| detail: `${sale.platform} Rs.${sale.amount}`, | |
| }); | |
| res.json({ message: 'Platform sale entry deleted' }); | |
| }; | |